fix: dasigners module

This commit is contained in:
MiniFrenchBread 2024-05-11 02:41:14 +08:00 committed by 0g-wh
parent bb5d5130cf
commit 422e940c28
9 changed files with 74 additions and 42 deletions

4
go.sum
View File

@ -212,8 +212,8 @@ filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw= git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw=
git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9/go.mod h1:BVJwbDfVjCjoFiKrhkei6NdGcZYpkDkdyCdg1ukytRA= git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9/go.mod h1:BVJwbDfVjCjoFiKrhkei6NdGcZYpkDkdyCdg1ukytRA=
github.com/0glabs/ethermint v0.21.0-0g.v2.0.0 h1:3sfsRkaPaG7v2smfxEJ2TvwPcVMIkG8yRRVR8+tbYkc= github.com/0glabs/ethermint v0.21.0-0g.v2.0.1 h1:loFnZAEZ8tboo3JO3+AE+1gJcUm6hkYuwcn+ZHBhjxE=
github.com/0glabs/ethermint v0.21.0-0g.v2.0.0/go.mod h1:peUmQT71k9BOBgoWoIRWRrM/O01mffVjIH0RLnoaFuI= github.com/0glabs/ethermint v0.21.0-0g.v2.0.1/go.mod h1:peUmQT71k9BOBgoWoIRWRrM/O01mffVjIH0RLnoaFuI=
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs=
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4=
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.21.1/go.mod h1:fBF9PQNqB8scdgpZ3ufzaLntG0AG7C1WjPMsiFOmfHM= github.com/Azure/azure-sdk-for-go/sdk/azcore v0.21.1/go.mod h1:fBF9PQNqB8scdgpZ3ufzaLntG0AG7C1WjPMsiFOmfHM=

File diff suppressed because one or more lines are too long

View File

@ -6,6 +6,7 @@ import (
precopmiles_common "github.com/0glabs/0g-chain/precompiles/common" precopmiles_common "github.com/0glabs/0g-chain/precompiles/common"
dasignerskeeper "github.com/0glabs/0g-chain/x/dasigners/v1/keeper" dasignerskeeper "github.com/0glabs/0g-chain/x/dasigners/v1/keeper"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
@ -13,8 +14,9 @@ import (
) )
const ( const (
PrecompileAddress = "0x0000000000000000000000000000000000001000" PrecompileAddress = "0x0000000000000000000000000000000000001000"
RequiredGasBasic uint64 = 100
RequiredGasMax uint64 = 1000_000_000
DASignersFunctionEpochNumber = "epochNumber" DASignersFunctionEpochNumber = "epochNumber"
DASignersFunctionGetSigner = "getSigner" DASignersFunctionGetSigner = "getSigner"
@ -25,6 +27,26 @@ const (
DASignersFunctionGetAggPkG1 = "getAggPkG1" DASignersFunctionGetAggPkG1 = "getAggPkG1"
) )
var RequiredGasBasic = map[string]uint64{
"epochNumber": 1000,
"getSigner": 10000,
"getSigners": 1000000,
"updateSocket": 50000,
"registerNextEpoch": 100000,
"registerSigner": 100000,
"getAggPkG1": 1000000,
}
var KVGasConfig storetypes.GasConfig = storetypes.GasConfig{
HasCost: 0,
DeleteCost: 0,
ReadCostFlat: 0,
ReadCostPerByte: 0,
WriteCostFlat: 0,
WriteCostPerByte: 0,
IterNextCostFlat: 0,
}
var _ vm.PrecompiledContract = &DASignersPrecompile{} var _ vm.PrecompiledContract = &DASignersPrecompile{}
type DASignersPrecompile struct { type DASignersPrecompile struct {
@ -50,7 +72,14 @@ func (d *DASignersPrecompile) Address() common.Address {
// RequiredGas implements vm.PrecompiledContract. // RequiredGas implements vm.PrecompiledContract.
func (d *DASignersPrecompile) RequiredGas(input []byte) uint64 { func (d *DASignersPrecompile) RequiredGas(input []byte) uint64 {
return RequiredGasBasic method, err := d.abi.MethodById(input[:4])
if err != nil {
return RequiredGasMax
}
if gas, ok := RequiredGasBasic[method.Name]; ok {
return gas
}
return RequiredGasMax
} }
// Run implements vm.PrecompiledContract. // Run implements vm.PrecompiledContract.
@ -73,6 +102,8 @@ func (d *DASignersPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly b
return nil, fmt.Errorf(precopmiles_common.ErrGetStateDB) return nil, fmt.Errorf(precopmiles_common.ErrGetStateDB)
} }
ctx := stateDB.GetContext() ctx := stateDB.GetContext()
// reset gas config
ctx = ctx.WithKVGasConfig(KVGasConfig)
initialGas := ctx.GasMeter().GasConsumed() initialGas := ctx.GasMeter().GasConsumed()
var bz []byte var bz []byte

View File

@ -22,7 +22,8 @@ func (d *DASignersPrecompile) EmitNewSignerEvent(ctx sdk.Context, stateDB *state
if err != nil { if err != nil {
return err return err
} }
b, err := event.Inputs.Pack(signer.Signer, signer.PkG1, signer.PkG2) arguments := abi.Arguments{event.Inputs[1], event.Inputs[2]}
b, err := arguments.Pack(signer.PkG1, signer.PkG2)
if err != nil { if err != nil {
return err return err
} }
@ -44,7 +45,8 @@ func (d *DASignersPrecompile) EmitSocketUpdatedEvent(ctx sdk.Context, stateDB *s
if err != nil { if err != nil {
return err return err
} }
b, err := event.Inputs.Pack(signer, socket) arguments := abi.Arguments{event.Inputs[1]}
b, err := arguments.Pack(socket)
if err != nil { if err != nil {
return err return err
} }

View File

@ -10,6 +10,23 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
type BN254G1Point = struct {
X *big.Int "json:\"X\""
Y *big.Int "json:\"Y\""
}
type BN254G2Point = struct {
X [2]*big.Int "json:\"X\""
Y [2]*big.Int "json:\"Y\""
}
type IDASignersSignerDetail = struct {
Signer common.Address "json:\"signer\""
Socket string "json:\"socket\""
PkG1 BN254G1Point "json:\"pkG1\""
PkG2 BN254G2Point "json:\"pkG2\""
}
func NewBN254G1Point(b []byte) BN254G1Point { func NewBN254G1Point(b []byte) BN254G1Point {
return BN254G1Point{ return BN254G1Point{
X: new(big.Int).SetBytes(b[:32]), X: new(big.Int).SetBytes(b[:32]),
@ -17,7 +34,7 @@ func NewBN254G1Point(b []byte) BN254G1Point {
} }
} }
func (p BN254G1Point) Serialize() []byte { func SerializeG1(p BN254G1Point) []byte {
b := make([]byte, 0) b := make([]byte, 0)
b = append(b, common.LeftPadBytes(p.X.Bytes(), 32)...) b = append(b, common.LeftPadBytes(p.X.Bytes(), 32)...)
b = append(b, common.LeftPadBytes(p.Y.Bytes(), 32)...) b = append(b, common.LeftPadBytes(p.Y.Bytes(), 32)...)
@ -37,7 +54,7 @@ func NewBN254G2Point(b []byte) BN254G2Point {
} }
} }
func (p BN254G2Point) Serialize() []byte { func SerializeG2(p BN254G2Point) []byte {
b := make([]byte, 0) b := make([]byte, 0)
b = append(b, common.LeftPadBytes(p.X[0].Bytes(), 32)...) b = append(b, common.LeftPadBytes(p.X[0].Bytes(), 32)...)
b = append(b, common.LeftPadBytes(p.X[1].Bytes(), 32)...) b = append(b, common.LeftPadBytes(p.X[1].Bytes(), 32)...)
@ -52,7 +69,7 @@ func NewQuerySignerRequest(args []interface{}) (*dasignerstypes.QuerySignerReque
} }
return &dasignerstypes.QuerySignerRequest{ return &dasignerstypes.QuerySignerRequest{
Account: args[0].(string), Account: ToLowerHexWithoutPrefix(args[0].(common.Address)),
}, nil }, nil
} }
@ -100,10 +117,10 @@ func NewMsgRegisterSigner(args []interface{}) (*dasignerstypes.MsgRegisterSigner
Signer: &dasignerstypes.Signer{ Signer: &dasignerstypes.Signer{
Account: ToLowerHexWithoutPrefix(signer.Signer), Account: ToLowerHexWithoutPrefix(signer.Signer),
Socket: signer.Socket, Socket: signer.Socket,
PubkeyG1: signer.PkG1.Serialize(), PubkeyG1: SerializeG1(signer.PkG1),
PubkeyG2: signer.PkG2.Serialize(), PubkeyG2: SerializeG2(signer.PkG2),
}, },
Signature: args[1].(BN254G1Point).Serialize(), Signature: SerializeG1(args[1].(BN254G1Point)),
}, nil }, nil
} }
@ -114,7 +131,7 @@ func NewMsgRegisterNextEpoch(args []interface{}, account string) (*dasignerstype
return &dasignerstypes.MsgRegisterNextEpoch{ return &dasignerstypes.MsgRegisterNextEpoch{
Account: account, Account: account,
Signature: args[0].(BN254G1Point).Serialize(), Signature: SerializeG1(args[0].(BN254G1Point)),
}, nil }, nil
} }

View File

@ -43,7 +43,7 @@ func (k Keeper) EpochSignerSet(c context.Context, request *types.QueryEpochSigne
epochSignerSet := make([]*types.Signer, 0) epochSignerSet := make([]*types.Signer, 0)
signers, found := k.GetEpochSignerSet(ctx, request.EpochNumber) signers, found := k.GetEpochSignerSet(ctx, request.EpochNumber)
if !found { if !found {
return &types.QueryEpochSignerSetResponse{Signers: epochSignerSet}, nil return &types.QueryEpochSignerSetResponse{Signers: epochSignerSet}, types.ErrEpochSignerSetNotFound
} }
for _, account := range signers.Signers { for _, account := range signers.Signers {
signer, found, err := k.GetSigner(ctx, account) signer, found, err := k.GetSigner(ctx, account)

View File

@ -104,7 +104,8 @@ func (k Keeper) SetSigner(ctx sdk.Context, signer types.Signer) error {
func (k Keeper) IterateSigners(ctx sdk.Context, fn func(index int64, signer types.Signer) (stop bool)) { func (k Keeper) IterateSigners(ctx sdk.Context, fn func(index int64, signer types.Signer) (stop bool)) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SignerKeyPrefix) prefix := types.SignerKeyPrefix
iterator := sdk.KVStorePrefixIterator(store, prefix)
defer iterator.Close() defer iterator.Close()
i := int64(0) i := int64(0)
@ -122,7 +123,7 @@ func (k Keeper) IterateSigners(ctx sdk.Context, fn func(index int64, signer type
} }
func (k Keeper) GetEpochSignerSet(ctx sdk.Context, epoch uint64) (types.EpochSignerSet, bool) { func (k Keeper) GetEpochSignerSet(ctx sdk.Context, epoch uint64) (types.EpochSignerSet, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SignerKeyPrefix) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.EpochSignerSetKeyPrefix)
bz := store.Get(types.GetEpochSignerSetKeyFromEpoch(epoch)) bz := store.Get(types.GetEpochSignerSetKeyFromEpoch(epoch))
if bz == nil { if bz == nil {
return types.EpochSignerSet{}, false return types.EpochSignerSet{}, false
@ -155,13 +156,14 @@ func (k Keeper) GetRegistration(ctx sdk.Context, epoch uint64, account string) (
func (k Keeper) IterateRegistrations(ctx sdk.Context, epoch uint64, fn func(account string, signature []byte) (stop bool)) { func (k Keeper) IterateRegistrations(ctx sdk.Context, epoch uint64, fn func(account string, signature []byte) (stop bool)) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetEpochRegistrationKeyPrefix(epoch)) prefix := types.GetEpochRegistrationKeyPrefix(epoch)
iterator := sdk.KVStorePrefixIterator(store, prefix)
defer iterator.Close() defer iterator.Close()
i := int64(0) i := int64(0)
for ; iterator.Valid(); iterator.Next() { for ; iterator.Valid(); iterator.Next() {
stop := fn(hex.EncodeToString(iterator.Key()), iterator.Value()) stop := fn(hex.EncodeToString((iterator.Key())[len(prefix):]), iterator.Value())
if stop { if stop {
break break

View File

@ -16,9 +16,9 @@ func NewGenesisState(params Params, epoch uint64, signers []*Signer, signersByEp
func DefaultGenesisState() *GenesisState { func DefaultGenesisState() *GenesisState {
return NewGenesisState(Params{ return NewGenesisState(Params{
QuorumSize: 1024, QuorumSize: 1024,
TokensPerVote: "1000", TokensPerVote: "100",
MaxVotes: 100, MaxVotes: 100,
EpochBlocks: 5, EpochBlocks: 1000,
}, 0, make([]*Signer, 0), make([]*EpochSignerSet, 0)) }, 0, make([]*Signer, 0), make([]*EpochSignerSet, 0))
} }

View File

@ -14,7 +14,7 @@ const (
StoreKey = ModuleName StoreKey = ModuleName
// QuerierRoute Top level query string // QuerierRoute Top level query string
QuerierRoute = ModuleName QuerierRoute = "dasigners"
) )
var ( var (