From 9ce1d87bc932c430832ece3e9c3247c7cb4a815d Mon Sep 17 00:00:00 2001 From: Matthias <97468149+matthiasmatt@users.noreply.github.com> Date: Thu, 24 Oct 2024 17:23:25 +0200 Subject: [PATCH] feat(evm): add simple validation for fun token fee in set params (#2091) * feat: add simple validation for fun token fee in set params * chore: changelog * linting changelog * lint: fix lint issues --------- Co-authored-by: Unique-Divine --- CHANGELOG.md | 5 +++-- app/evmante/evmante_validate_basic_test.go | 24 ++++++++++++++++++---- x/evm/evmmodule/genesis.go | 5 ++++- x/evm/keeper/evm_state.go | 8 +++++++- x/evm/keeper/grpc_query_test.go | 7 +++++-- x/evm/keeper/msg_update_params.go | 5 ++++- 6 files changed, 43 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f89a4add4..68f7f9cc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ consistent setup and dynamic gas calculations, addressing the following tickets. - https://github.com/code-423n4/2024-10-nibiru-zenith/issues/47 - [#2088](https://github.com/NibiruChain/nibiru/pull/2088) - refactor(evm): remove outdated comment and improper error message text - [#2089](https://github.com/NibiruChain/nibiru/pull/2089) - better handling of gas consumption within erc20 contract execution +- [#2091](https://github.com/NibiruChain/nibiru/pull/2091) - feat(evm): add fun token creation fee validation #### Nibiru EVM | Before Audit 1 - 2024-10-18 @@ -127,7 +128,7 @@ consistent setup and dynamic gas calculations, addressing the following tickets. - [#2002](https://github.com/NibiruChain/nibiru/pull/2002) - feat(evm): Add the account query to the EVM command. Cover the CLI with tests. - [#2003](https://github.com/NibiruChain/nibiru/pull/2003) - fix(evm): fix FunToken conversions between Cosmos and EVM - [#2004](https://github.com/NibiruChain/nibiru/pull/2004) - refactor(evm)!: replace `HexAddr` with `EIP55Addr` -- [#2006](https://github.com/NibiruChain/nibiru/pull/2006) - test(evm): e2e tests for eth_* endpoints +- [#2006](https://github.com/NibiruChain/nibiru/pull/2006) - test(evm): e2e tests for eth\_\* endpoints - [#2008](https://github.com/NibiruChain/nibiru/pull/2008) - refactor(evm): clean up precompile setups - [#2013](https://github.com/NibiruChain/nibiru/pull/2013) - chore(evm): Set appropriate gas value for the required gas of the "IFunToken.sol" precompile. - [#2014](https://github.com/NibiruChain/nibiru/pull/2014) - feat(evm): Emit block bloom event in EndBlock hook. @@ -143,7 +144,7 @@ consistent setup and dynamic gas calculations, addressing the following tickets. - [#2044](https://github.com/NibiruChain/nibiru/pull/2044) - feat(evm): evm tx indexer service implemented - [#2045](https://github.com/NibiruChain/nibiru/pull/2045) - test(evm): backend tests with test network and real txs - [#2053](https://github.com/NibiruChain/nibiru/pull/2053) - refactor(evm): converted untyped event to typed and cleaned up -- [#2054](https://github.com/NibiruChain/nibiru/pull/2054) - feat(evm-precompile): Precompile for one-way EVM calls to invoke/execute Wasm contracts. +- [#2054](https://github.com/NibiruChain/nibiru/pull/2054) - feat(evm-precompile): Precompile for one-way EVM calls to invoke/execute Wasm contracts. - [#2060](https://github.com/NibiruChain/nibiru/pull/2060) - fix(evm-precompiles): add assertNumArgs validation - [#2056](https://github.com/NibiruChain/nibiru/pull/2056) - feat(evm): add oracle precompile - [#2065](https://github.com/NibiruChain/nibiru/pull/2065) - refactor(evm)!: Refactor out dead code from the evm.Params diff --git a/app/evmante/evmante_validate_basic_test.go b/app/evmante/evmante_validate_basic_test.go index 4f0e136ff..3f1263dee 100644 --- a/app/evmante/evmante_validate_basic_test.go +++ b/app/evmante/evmante_validate_basic_test.go @@ -36,6 +36,18 @@ func (s *TestSuite) TestEthValidateBasicDecorator() { }, wantErr: "", }, + { + name: "sad: fail to set params", + txSetup: func(deps *evmtest.TestDeps) sdk.Tx { + return evmtest.HappyCreateContractTx(deps) + }, + paramsSetup: func(deps *evmtest.TestDeps) evm.Params { + return evm.Params{ + CreateFuntokenFee: sdk.NewInt(-1), + } + }, + wantErr: "createFuntokenFee cannot be negative: -1", + }, { name: "happy: ctx recheck should ignore validation", ctxSetup: func(deps *evmtest.TestDeps) { @@ -195,12 +207,16 @@ func (s *TestSuite) TestEthValidateBasicDecorator() { if tc.ctxSetup != nil { tc.ctxSetup(&deps) } + var err error if tc.paramsSetup != nil { - deps.EvmKeeper.SetParams(deps.Ctx, tc.paramsSetup(&deps)) + err = deps.EvmKeeper.SetParams(deps.Ctx, tc.paramsSetup(&deps)) + } + + if err == nil { + _, err = anteDec.AnteHandle( + deps.Ctx, tx, false, evmtest.NextNoOpAnteHandler, + ) } - _, err := anteDec.AnteHandle( - deps.Ctx, tx, false, evmtest.NextNoOpAnteHandler, - ) if tc.wantErr != "" { s.Require().ErrorContains(err, tc.wantErr) return diff --git a/x/evm/evmmodule/genesis.go b/x/evm/evmmodule/genesis.go index d67a0c18a..b552b25ea 100644 --- a/x/evm/evmmodule/genesis.go +++ b/x/evm/evmmodule/genesis.go @@ -23,7 +23,10 @@ func InitGenesis( accountKeeper evm.AccountKeeper, genState evm.GenesisState, ) []abci.ValidatorUpdate { - k.SetParams(ctx, genState.Params) + err := k.SetParams(ctx, genState.Params) + if err != nil { + panic(fmt.Errorf("failed to set params: %w", err)) + } // Note that "GetModuleAccount" initializes the module account with permissions // under the hood if it did not already exist. This is important because the diff --git a/x/evm/keeper/evm_state.go b/x/evm/keeper/evm_state.go index 426e78d8e..9e78cc17b 100644 --- a/x/evm/keeper/evm_state.go +++ b/x/evm/keeper/evm_state.go @@ -2,6 +2,7 @@ package keeper import ( + "fmt" "math/big" "github.com/NibiruChain/collections" @@ -116,8 +117,13 @@ func (k Keeper) GetParams(ctx sdk.Context) (params evm.Params) { } // SetParams: Setter for the module parameters. -func (k Keeper) SetParams(ctx sdk.Context, params evm.Params) { +func (k Keeper) SetParams(ctx sdk.Context, params evm.Params) (err error) { + if params.CreateFuntokenFee.IsNegative() { + return fmt.Errorf("createFuntokenFee cannot be negative: %s", params.CreateFuntokenFee) + } + k.EvmState.ModuleParams.Set(ctx, params) + return } // SetState updates contract storage and deletes if the value is empty. diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index fa1d9ca2b..b16bea40c 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -447,7 +447,9 @@ func (s *Suite) TestQueryCode() { func (s *Suite) TestQueryParams() { deps := evmtest.NewTestDeps() want := evm.DefaultParams() - deps.EvmKeeper.SetParams(deps.Ctx, want) + err := deps.EvmKeeper.SetParams(deps.Ctx, want) + s.NoError(err) + gotResp, err := deps.EvmKeeper.Params(sdk.WrapSDKContext(deps.Ctx), nil) s.NoError(err) got := gotResp.Params @@ -458,7 +460,8 @@ func (s *Suite) TestQueryParams() { // Empty params to test the setter want.EVMChannels = []string{"channel-420"} - deps.EvmKeeper.SetParams(deps.Ctx, want) + err = deps.EvmKeeper.SetParams(deps.Ctx, want) + s.NoError(err) gotResp, err = deps.EvmKeeper.Params(sdk.WrapSDKContext(deps.Ctx), nil) s.Require().NoError(err) got = gotResp.Params diff --git a/x/evm/keeper/msg_update_params.go b/x/evm/keeper/msg_update_params.go index 1098138a3..12dd71fe8 100644 --- a/x/evm/keeper/msg_update_params.go +++ b/x/evm/keeper/msg_update_params.go @@ -18,6 +18,9 @@ func (k *Keeper) UpdateParams( return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority, expected %s, got %s", k.authority.String(), req.Authority) } ctx := sdk.UnwrapSDKContext(goCtx) - k.SetParams(ctx, req.Params) + err = k.SetParams(ctx, req.Params) + if err != nil { + return nil, errors.Wrapf(err, "failed to set params") + } return &evm.MsgUpdateParamsResponse{}, nil }