From 422e940c2814ffea507a727b7810a3306e5b9ec0 Mon Sep 17 00:00:00 2001 From: MiniFrenchBread <103425574+MiniFrenchBread@users.noreply.github.com> Date: Sat, 11 May 2024 02:41:14 +0800 Subject: [PATCH] fix: dasigners module --- go.sum | 4 ++-- precompiles/dasigners/contract.go | 20 ---------------- precompiles/dasigners/dasigners.go | 37 ++++++++++++++++++++++++++--- precompiles/dasigners/events.go | 6 +++-- precompiles/dasigners/types.go | 31 ++++++++++++++++++------ x/dasigners/v1/keeper/grpc_query.go | 2 +- x/dasigners/v1/keeper/keeper.go | 10 ++++---- x/dasigners/v1/types/genesis.go | 4 ++-- x/dasigners/v1/types/keys.go | 2 +- 9 files changed, 74 insertions(+), 42 deletions(-) diff --git a/go.sum b/go.sum index 3b1068b2..81bb24fe 100644 --- a/go.sum +++ b/go.sum @@ -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= 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= -github.com/0glabs/ethermint v0.21.0-0g.v2.0.0 h1:3sfsRkaPaG7v2smfxEJ2TvwPcVMIkG8yRRVR8+tbYkc= -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 h1:loFnZAEZ8tboo3JO3+AE+1gJcUm6hkYuwcn+ZHBhjxE= +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/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4= github.com/Azure/azure-sdk-for-go/sdk/azcore v0.21.1/go.mod h1:fBF9PQNqB8scdgpZ3ufzaLntG0AG7C1WjPMsiFOmfHM= diff --git a/precompiles/dasigners/contract.go b/precompiles/dasigners/contract.go index 2fe9629b..b558627b 100644 --- a/precompiles/dasigners/contract.go +++ b/precompiles/dasigners/contract.go @@ -28,26 +28,6 @@ var ( _ = event.NewSubscription ) -// BN254G1Point is an auto generated low-level Go binding around an user-defined struct. -type BN254G1Point struct { - X *big.Int - Y *big.Int -} - -// BN254G2Point is an auto generated low-level Go binding around an user-defined struct. -type BN254G2Point struct { - X [2]*big.Int - Y [2]*big.Int -} - -// IDASignersSignerDetail is an auto generated low-level Go binding around an user-defined struct. -type IDASignersSignerDetail struct { - Signer common.Address - Socket string - PkG1 BN254G1Point - PkG2 BN254G2Point -} - // DASignersMetaData contains all meta data concerning the DASigners contract. var DASignersMetaData = &bind.MetaData{ ABI: "[{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"signer\",\"type\":\"address\"},{\"components\":[{\"internalType\":\"uint256\",\"name\":\"X\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"Y\",\"type\":\"uint256\"}],\"indexed\":false,\"internalType\":\"structBN254.G1Point\",\"name\":\"pkG1\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"uint256[2]\",\"name\":\"X\",\"type\":\"uint256[2]\"},{\"internalType\":\"uint256[2]\",\"name\":\"Y\",\"type\":\"uint256[2]\"}],\"indexed\":false,\"internalType\":\"structBN254.G2Point\",\"name\":\"pkG2\",\"type\":\"tuple\"}],\"name\":\"NewSigner\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"signer\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"string\",\"name\":\"socket\",\"type\":\"string\"}],\"name\":\"SocketUpdated\",\"type\":\"event\"},{\"inputs\":[],\"name\":\"epochNumber\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"epoch\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"signersBitmap\",\"type\":\"bytes\"}],\"name\":\"getAggPkG1\",\"outputs\":[{\"components\":[{\"internalType\":\"uint256\",\"name\":\"X\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"Y\",\"type\":\"uint256\"}],\"internalType\":\"structBN254.G1Point\",\"name\":\"aggPkG1\",\"type\":\"tuple\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"}],\"name\":\"getSigner\",\"outputs\":[{\"components\":[{\"internalType\":\"address\",\"name\":\"signer\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"socket\",\"type\":\"string\"},{\"components\":[{\"internalType\":\"uint256\",\"name\":\"X\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"Y\",\"type\":\"uint256\"}],\"internalType\":\"structBN254.G1Point\",\"name\":\"pkG1\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"uint256[2]\",\"name\":\"X\",\"type\":\"uint256[2]\"},{\"internalType\":\"uint256[2]\",\"name\":\"Y\",\"type\":\"uint256[2]\"}],\"internalType\":\"structBN254.G2Point\",\"name\":\"pkG2\",\"type\":\"tuple\"}],\"internalType\":\"structIDASigners.SignerDetail\",\"name\":\"\",\"type\":\"tuple\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"epoch\",\"type\":\"uint256\"}],\"name\":\"getSigners\",\"outputs\":[{\"components\":[{\"internalType\":\"address\",\"name\":\"signer\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"socket\",\"type\":\"string\"},{\"components\":[{\"internalType\":\"uint256\",\"name\":\"X\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"Y\",\"type\":\"uint256\"}],\"internalType\":\"structBN254.G1Point\",\"name\":\"pkG1\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"uint256[2]\",\"name\":\"X\",\"type\":\"uint256[2]\"},{\"internalType\":\"uint256[2]\",\"name\":\"Y\",\"type\":\"uint256[2]\"}],\"internalType\":\"structBN254.G2Point\",\"name\":\"pkG2\",\"type\":\"tuple\"}],\"internalType\":\"structIDASigners.SignerDetail[]\",\"name\":\"details\",\"type\":\"tuple[]\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"components\":[{\"internalType\":\"uint256\",\"name\":\"X\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"Y\",\"type\":\"uint256\"}],\"internalType\":\"structBN254.G1Point\",\"name\":\"_signature\",\"type\":\"tuple\"}],\"name\":\"registerNextEpoch\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"components\":[{\"internalType\":\"address\",\"name\":\"signer\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"socket\",\"type\":\"string\"},{\"components\":[{\"internalType\":\"uint256\",\"name\":\"X\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"Y\",\"type\":\"uint256\"}],\"internalType\":\"structBN254.G1Point\",\"name\":\"pkG1\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"uint256[2]\",\"name\":\"X\",\"type\":\"uint256[2]\"},{\"internalType\":\"uint256[2]\",\"name\":\"Y\",\"type\":\"uint256[2]\"}],\"internalType\":\"structBN254.G2Point\",\"name\":\"pkG2\",\"type\":\"tuple\"}],\"internalType\":\"structIDASigners.SignerDetail\",\"name\":\"_signer\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"uint256\",\"name\":\"X\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"Y\",\"type\":\"uint256\"}],\"internalType\":\"structBN254.G1Point\",\"name\":\"_signature\",\"type\":\"tuple\"}],\"name\":\"registerSigner\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"socket\",\"type\":\"string\"}],\"name\":\"updateSocket\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]", diff --git a/precompiles/dasigners/dasigners.go b/precompiles/dasigners/dasigners.go index 601e626c..61ba2a17 100644 --- a/precompiles/dasigners/dasigners.go +++ b/precompiles/dasigners/dasigners.go @@ -6,6 +6,7 @@ import ( precopmiles_common "github.com/0glabs/0g-chain/precompiles/common" 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/common" "github.com/ethereum/go-ethereum/core/vm" @@ -13,8 +14,9 @@ import ( ) const ( - PrecompileAddress = "0x0000000000000000000000000000000000001000" - RequiredGasBasic uint64 = 100 + PrecompileAddress = "0x0000000000000000000000000000000000001000" + + RequiredGasMax uint64 = 1000_000_000 DASignersFunctionEpochNumber = "epochNumber" DASignersFunctionGetSigner = "getSigner" @@ -25,6 +27,26 @@ const ( 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{} type DASignersPrecompile struct { @@ -50,7 +72,14 @@ func (d *DASignersPrecompile) Address() common.Address { // RequiredGas implements vm.PrecompiledContract. 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. @@ -73,6 +102,8 @@ func (d *DASignersPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly b return nil, fmt.Errorf(precopmiles_common.ErrGetStateDB) } ctx := stateDB.GetContext() + // reset gas config + ctx = ctx.WithKVGasConfig(KVGasConfig) initialGas := ctx.GasMeter().GasConsumed() var bz []byte diff --git a/precompiles/dasigners/events.go b/precompiles/dasigners/events.go index 5abd8431..3ac0f4b5 100644 --- a/precompiles/dasigners/events.go +++ b/precompiles/dasigners/events.go @@ -22,7 +22,8 @@ func (d *DASignersPrecompile) EmitNewSignerEvent(ctx sdk.Context, stateDB *state if err != nil { 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 { return err } @@ -44,7 +45,8 @@ func (d *DASignersPrecompile) EmitSocketUpdatedEvent(ctx sdk.Context, stateDB *s if err != nil { return err } - b, err := event.Inputs.Pack(signer, socket) + arguments := abi.Arguments{event.Inputs[1]} + b, err := arguments.Pack(socket) if err != nil { return err } diff --git a/precompiles/dasigners/types.go b/precompiles/dasigners/types.go index a7fe5e5e..16237286 100644 --- a/precompiles/dasigners/types.go +++ b/precompiles/dasigners/types.go @@ -10,6 +10,23 @@ import ( "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 { return BN254G1Point{ 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 = append(b, common.LeftPadBytes(p.X.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 = append(b, common.LeftPadBytes(p.X[0].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{ - Account: args[0].(string), + Account: ToLowerHexWithoutPrefix(args[0].(common.Address)), }, nil } @@ -100,10 +117,10 @@ func NewMsgRegisterSigner(args []interface{}) (*dasignerstypes.MsgRegisterSigner Signer: &dasignerstypes.Signer{ Account: ToLowerHexWithoutPrefix(signer.Signer), Socket: signer.Socket, - PubkeyG1: signer.PkG1.Serialize(), - PubkeyG2: signer.PkG2.Serialize(), + PubkeyG1: SerializeG1(signer.PkG1), + PubkeyG2: SerializeG2(signer.PkG2), }, - Signature: args[1].(BN254G1Point).Serialize(), + Signature: SerializeG1(args[1].(BN254G1Point)), }, nil } @@ -114,7 +131,7 @@ func NewMsgRegisterNextEpoch(args []interface{}, account string) (*dasignerstype return &dasignerstypes.MsgRegisterNextEpoch{ Account: account, - Signature: args[0].(BN254G1Point).Serialize(), + Signature: SerializeG1(args[0].(BN254G1Point)), }, nil } diff --git a/x/dasigners/v1/keeper/grpc_query.go b/x/dasigners/v1/keeper/grpc_query.go index 51ed5c7e..cb0d1dbf 100644 --- a/x/dasigners/v1/keeper/grpc_query.go +++ b/x/dasigners/v1/keeper/grpc_query.go @@ -43,7 +43,7 @@ func (k Keeper) EpochSignerSet(c context.Context, request *types.QueryEpochSigne epochSignerSet := make([]*types.Signer, 0) signers, found := k.GetEpochSignerSet(ctx, request.EpochNumber) if !found { - return &types.QueryEpochSignerSetResponse{Signers: epochSignerSet}, nil + return &types.QueryEpochSignerSetResponse{Signers: epochSignerSet}, types.ErrEpochSignerSetNotFound } for _, account := range signers.Signers { signer, found, err := k.GetSigner(ctx, account) diff --git a/x/dasigners/v1/keeper/keeper.go b/x/dasigners/v1/keeper/keeper.go index dad48038..44d437e5 100644 --- a/x/dasigners/v1/keeper/keeper.go +++ b/x/dasigners/v1/keeper/keeper.go @@ -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)) { store := ctx.KVStore(k.storeKey) - iterator := sdk.KVStorePrefixIterator(store, types.SignerKeyPrefix) + prefix := types.SignerKeyPrefix + iterator := sdk.KVStorePrefixIterator(store, prefix) defer iterator.Close() 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) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SignerKeyPrefix) + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.EpochSignerSetKeyPrefix) bz := store.Get(types.GetEpochSignerSetKeyFromEpoch(epoch)) if bz == nil { 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)) { store := ctx.KVStore(k.storeKey) - iterator := sdk.KVStorePrefixIterator(store, types.GetEpochRegistrationKeyPrefix(epoch)) + prefix := types.GetEpochRegistrationKeyPrefix(epoch) + iterator := sdk.KVStorePrefixIterator(store, prefix) defer iterator.Close() i := int64(0) 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 { break diff --git a/x/dasigners/v1/types/genesis.go b/x/dasigners/v1/types/genesis.go index 423fa102..b3a4c651 100644 --- a/x/dasigners/v1/types/genesis.go +++ b/x/dasigners/v1/types/genesis.go @@ -16,9 +16,9 @@ func NewGenesisState(params Params, epoch uint64, signers []*Signer, signersByEp func DefaultGenesisState() *GenesisState { return NewGenesisState(Params{ QuorumSize: 1024, - TokensPerVote: "1000", + TokensPerVote: "100", MaxVotes: 100, - EpochBlocks: 5, + EpochBlocks: 1000, }, 0, make([]*Signer, 0), make([]*EpochSignerSet, 0)) } diff --git a/x/dasigners/v1/types/keys.go b/x/dasigners/v1/types/keys.go index 47ad3b95..d49436fc 100644 --- a/x/dasigners/v1/types/keys.go +++ b/x/dasigners/v1/types/keys.go @@ -14,7 +14,7 @@ const ( StoreKey = ModuleName // QuerierRoute Top level query string - QuerierRoute = ModuleName + QuerierRoute = "dasigners" ) var (