From 42e191c9927115be5f1e917dc6e1ff3e690722dd Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 18 Oct 2022 19:04:27 -0300 Subject: [PATCH 01/12] [issue-1018] add new tests on evm module keeper --- x/evm/keeper/abci_test.go | 24 ++++ x/evm/keeper/grpc_query_test.go | 241 +++++++++++++++++++++++++++++++- x/evm/keeper/migrations_test.go | 19 +++ x/evm/keeper/utils_test.go | 22 ++- 4 files changed, 298 insertions(+), 8 deletions(-) create mode 100644 x/evm/keeper/abci_test.go create mode 100644 x/evm/keeper/migrations_test.go diff --git a/x/evm/keeper/abci_test.go b/x/evm/keeper/abci_test.go new file mode 100644 index 0000000000..0fd4b7ca02 --- /dev/null +++ b/x/evm/keeper/abci_test.go @@ -0,0 +1,24 @@ +package keeper_test + +import ( + "github.com/tendermint/tendermint/abci/types" + evmtypes "github.com/evmos/ethermint/x/evm/types" +) + + +func (suite *KeeperTestSuite) TestEndBlock() { + + suite.Run("EndBlock test", func() { + suite.SetupTest() // reset + + em := suite.ctx.EventManager() + suite.Require().Equal(0, len(em.Events())) + + res := suite.app.EvmKeeper.EndBlock(suite.ctx, types.RequestEndBlock{}) + suite.Require().Equal([]types.ValidatorUpdate{}, res) + + // should emit 1 EventTypeBlockBloom event on EndBlock + suite.Require().Equal(1, len(em.Events())) + suite.Require().Equal(evmtypes.EventTypeBlockBloom, em.Events()[0].Type) + }) +} diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index 1152c2e4fd..0dd5887c6c 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -491,9 +491,11 @@ func (suite *KeeperTestSuite) TestQueryValidatorAccount() { func (suite *KeeperTestSuite) TestEstimateGas() { gasHelper := hexutil.Uint64(20000) + higherGas := hexutil.Uint64(25000) + hexBigInt := hexutil.Big(*big.NewInt(1)) var ( - args types.TransactionArgs + args interface{} gasCap uint64 ) testCases := []struct { @@ -504,9 +506,13 @@ func (suite *KeeperTestSuite) TestEstimateGas() { enableFeemarket bool }{ // should success, because transfer value is zero - {"default args", func() { + {"default args - special case for ErrIntrinsicGas on contract creation, raise gas limit", func() { + args = types.TransactionArgs{} + }, true, ethparams.TxGasContractCreation, false}, + // should success, because transfer value is zero + {"default args with 'to' address", func() { args = types.TransactionArgs{To: &common.Address{}} - }, true, 21000, false}, + }, true, ethparams.TxGas, false}, // should fail, because the default From address(zero address) don't have fund {"not enough balance", func() { args = types.TransactionArgs{To: &common.Address{}, Value: (*hexutil.Big)(big.NewInt(100))} @@ -518,7 +524,7 @@ func (suite *KeeperTestSuite) TestEstimateGas() { // should success, because gas limit lower than 21000 is ignored {"gas exceed allowance", func() { args = types.TransactionArgs{To: &common.Address{}, Gas: &gasHelper} - }, true, 21000, false}, + }, true, ethparams.TxGas, false}, // should fail, invalid gas cap {"gas exceed global allowance", func() { args = types.TransactionArgs{To: &common.Address{}} @@ -546,7 +552,7 @@ func (suite *KeeperTestSuite) TestEstimateGas() { // repeated tests with enableFeemarket {"default args w/ enableFeemarket", func() { args = types.TransactionArgs{To: &common.Address{}} - }, true, 21000, true}, + }, true, ethparams.TxGas, true}, {"not enough balance w/ enableFeemarket", func() { args = types.TransactionArgs{To: &common.Address{}, Value: (*hexutil.Big)(big.NewInt(100))} }, false, 0, true}, @@ -555,7 +561,7 @@ func (suite *KeeperTestSuite) TestEstimateGas() { }, false, 0, true}, {"gas exceed allowance w/ enableFeemarket", func() { args = types.TransactionArgs{To: &common.Address{}, Gas: &gasHelper} - }, true, 21000, true}, + }, true, ethparams.TxGas, true}, {"gas exceed global allowance w/ enableFeemarket", func() { args = types.TransactionArgs{To: &common.Address{}} gasCap = 20000 @@ -576,6 +582,38 @@ func (suite *KeeperTestSuite) TestEstimateGas() { suite.Require().NoError(err) args = types.TransactionArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)} }, true, 51880, true}, + {"contract creation but 'create' param disabled", func() { + ctorArgs, err := types.ERC20Contract.ABI.Pack("", &suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) + suite.Require().NoError(err) + data := append(types.ERC20Contract.Bin, ctorArgs...) + args = types.TransactionArgs{ + From: &suite.address, + Data: (*hexutil.Bytes)(&data), + } + params := suite.app.EvmKeeper.GetParams(suite.ctx) + params.EnableCreate = false + suite.app.EvmKeeper.SetParams(suite.ctx, params) + }, false, 0, false}, + {"specified gas in args higher than ethparams.TxGas (21,000)", func() { + args = types.TransactionArgs{ + To: &common.Address{}, + Gas: &higherGas, + } + }, true, ethparams.TxGas, false}, + {"specified gas in args higher than request gasCap", func() { + gasCap = 22_000 + args = types.TransactionArgs{ + To: &common.Address{}, + Gas: &higherGas, + } + }, true, ethparams.TxGas, false}, + {"invalid args - specified both gasPrice and maxFeePerGas", func() { + args = types.TransactionArgs{ + To: &common.Address{}, + GasPrice: &hexBigInt, + MaxFeePerGas: &hexBigInt, + } + }, false, 0, false}, } for _, tc := range testCases { @@ -702,6 +740,88 @@ func (suite *KeeperTestSuite) TestTraceTx() { traceResponse: "{\"gas\":34828,\"failed\":false,\"returnValue\":\"0000000000000000000000000000000000000000000000000000000000000001\",\"structLogs\":[{\"pc\":0,\"op\":\"PUSH1\",\"gas\":", enableFeemarket: false, }, + { + msg: "invalid trace config - Negative Limit", + malleate: func() { + traceConfig = &types.TraceConfig{ + DisableStack: true, + DisableStorage: true, + EnableMemory: false, + Limit: -1, + } + }, + expPass: false, + }, + { + msg: "invalid trace config - Invalid Tracer", + malleate: func() { + traceConfig = &types.TraceConfig{ + DisableStack: true, + DisableStorage: true, + EnableMemory: false, + Tracer: "invalid_tracer", + } + }, + expPass: false, + }, + { + msg: "invalid trace config - Invalid Timeout", + malleate: func() { + traceConfig = &types.TraceConfig{ + DisableStack: true, + DisableStorage: true, + EnableMemory: false, + Timeout: "wrong_time", + } + }, + expPass: false, + }, + { + msg: "trace config - Execution Timeout", + malleate: func() { + traceConfig = &types.TraceConfig{ + DisableStack: true, + DisableStorage: true, + EnableMemory: false, + Timeout: "0s", + } + }, + expPass: false, + }, + { + msg: "default tracer with contract creation tx as predecessor but 'create' param disabled", + malleate: func() { + traceConfig = nil + + // increase nonce to avoid address collision + vmdb := suite.StateDB() + vmdb.SetNonce(suite.address, vmdb.GetNonce(suite.address)+1) + suite.Require().NoError(vmdb.Commit()) + + chainID := suite.app.EvmKeeper.ChainID() + nonce := suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address) + data := types.ERC20Contract.Bin + contractTx := types.NewTxContract( + chainID, + nonce, + nil, // amount + ethparams.TxGasContractCreation, // gasLimit + nil, // gasPrice + nil, nil, + data, // input + nil, // accesses + ) + + predecessors = append(predecessors, contractTx) + suite.Commit() + + params := suite.app.EvmKeeper.GetParams(suite.ctx) + params.EnableCreate = false + suite.app.EvmKeeper.SetParams(suite.ctx, params) + }, + expPass: true, + traceResponse: "{\"gas\":34828,\"failed\":false,\"returnValue\":\"0000000000000000000000000000000000000000000000000000000000000001\",\"structLogs\":[{\"pc\":0,\"op\":\"PUSH1\",\"gas\":", + }, } for _, tc := range testCases { @@ -722,7 +842,6 @@ func (suite *KeeperTestSuite) TestTraceTx() { Predecessors: predecessors, } res, err := suite.queryClient.TraceTx(sdk.WrapSDKContext(suite.ctx), &traceReq) - suite.Require().NoError(err) if tc.expPass { suite.Require().NoError(err) @@ -836,6 +955,31 @@ func (suite *KeeperTestSuite) TestTraceBlock() { traceResponse: "[{\"result\":{\"gas\":34828,\"failed\":false,\"returnValue\":\"0000000000000000000000000000000000000000000000000000000000000001\",\"structLogs\":[{\"pc\":0,\"op\":\"PU", enableFeemarket: false, }, + { + msg: "invalid trace config - Negative Limit", + malleate: func() { + traceConfig = &types.TraceConfig{ + DisableStack: true, + DisableStorage: true, + EnableMemory: false, + Limit: -1, + } + }, + expPass: false, + }, + { + msg: "invalid trace config - Invalid Tracer", + malleate: func() { + traceConfig = &types.TraceConfig{ + DisableStack: true, + DisableStorage: true, + EnableMemory: false, + Tracer: "invalid_tracer", + } + }, + expPass: true, + traceResponse: "[]", + }, } for _, tc := range testCases { @@ -981,3 +1125,86 @@ func (suite *KeeperTestSuite) TestQueryBaseFee() { suite.enableFeemarket = false suite.enableLondonHF = true } + +func (suite *KeeperTestSuite) TestEthCall() { + var ( + req *types.EthCallRequest + ) + + suite.SetupTest() + + priv, err := ethsecp256k1.GenerateKey() + suite.Require().NoError(err) + address := common.BytesToAddress(priv.PubKey().Address().Bytes()) + suite.Require().Equal(uint64(0), suite.app.EvmKeeper.GetNonce(suite.ctx, address)) + supply := sdkmath.NewIntWithDecimal(1000, 18).BigInt() + + hexBigInt := hexutil.Big(*big.NewInt(1)) + ctorArgs, err := types.ERC20Contract.ABI.Pack("", address, supply) + suite.Require().NoError(err) + + data := append(types.ERC20Contract.Bin, ctorArgs...) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "invalid args", + func() { + req = &types.EthCallRequest{Args: []byte("invalid args"), GasCap: uint64(config.DefaultGasCap)} + }, + false, + }, + { + "invalid args - specified both gasPrice and maxFeePerGas", + func() { + + args, err := json.Marshal(&types.TransactionArgs{ + From: &address, + Data: (*hexutil.Bytes)(&data), + GasPrice: &hexBigInt, + MaxFeePerGas: &hexBigInt, + }) + + suite.Require().NoError(err) + req = &types.EthCallRequest{Args: args, GasCap: uint64(config.DefaultGasCap)} + }, + false, + }, + { + "set param EnableCreate = false", + func() { + + args, err := json.Marshal(&types.TransactionArgs{ + From: &address, + Data: (*hexutil.Bytes)(&data), + }) + + suite.Require().NoError(err) + req = &types.EthCallRequest{Args: args, GasCap: uint64(config.DefaultGasCap)} + + params := suite.app.EvmKeeper.GetParams(suite.ctx) + params.EnableCreate = false + suite.app.EvmKeeper.SetParams(suite.ctx, params) + }, + false, + }, + } + for _, tc := range testCases { + suite.Run(tc.name, func() { + + tc.malleate() + + res, err := suite.queryClient.EthCall(suite.ctx, req) + if tc.expPass { + suite.Require().NotNil(res) + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } + +} diff --git a/x/evm/keeper/migrations_test.go b/x/evm/keeper/migrations_test.go new file mode 100644 index 0000000000..0e43976341 --- /dev/null +++ b/x/evm/keeper/migrations_test.go @@ -0,0 +1,19 @@ +package keeper_test + +import ( + evmkeeper "github.com/evmos/ethermint/x/evm/keeper" +) + +func (suite *KeeperTestSuite) TestMigrations() { + migrator := evmkeeper.NewMigrator(*suite.app.EvmKeeper) + + suite.Run("Run Migrate1to2", func() { + err := migrator.Migrate1to2(suite.ctx) + suite.Require().NoError(err) + }) + + suite.Run("Run Migrate2to3", func() { + err := migrator.Migrate2to3(suite.ctx) + suite.Require().NoError(err) + }) +} diff --git a/x/evm/keeper/utils_test.go b/x/evm/keeper/utils_test.go index 6f4074c76c..1aadcb0b77 100644 --- a/x/evm/keeper/utils_test.go +++ b/x/evm/keeper/utils_test.go @@ -271,6 +271,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { accessList *ethtypes.AccessList expectPass bool enableFeemarket bool + from string }{ { name: "Enough balance", @@ -279,6 +280,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { cost: &oneInt, accessList: ðtypes.AccessList{}, expectPass: true, + from: suite.address.String(), }, { name: "Equal balance", @@ -287,6 +289,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { cost: &oneInt, accessList: ðtypes.AccessList{}, expectPass: true, + from: suite.address.String(), }, { name: "Higher gas limit, not enough balance", @@ -295,6 +298,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { cost: &oneInt, accessList: ðtypes.AccessList{}, expectPass: false, + from: suite.address.String(), }, { name: "Higher gas price, enough balance", @@ -303,6 +307,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { cost: &oneInt, accessList: ðtypes.AccessList{}, expectPass: true, + from: suite.address.String(), }, { name: "Higher gas price, not enough balance", @@ -311,6 +316,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { cost: &oneInt, accessList: ðtypes.AccessList{}, expectPass: false, + from: suite.address.String(), }, // This case is expected to be true because the fees can be deducted, but the tx // execution is going to fail because there is no more balance to pay the cost @@ -321,6 +327,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { cost: &fiftyInt, accessList: ðtypes.AccessList{}, expectPass: true, + from: suite.address.String(), }, // testcases with enableFeemarket enabled. { @@ -332,6 +339,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { accessList: ðtypes.AccessList{}, expectPass: false, enableFeemarket: true, + from: suite.address.String(), }, { name: "empty tip fee is valid to deduct", @@ -342,6 +350,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { accessList: ðtypes.AccessList{}, expectPass: true, enableFeemarket: true, + from: suite.address.String(), }, { name: "effectiveTip equal to gasTipCap", @@ -351,6 +360,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { accessList: ðtypes.AccessList{}, expectPass: true, enableFeemarket: true, + from: suite.address.String(), }, { name: "effectiveTip equal to (gasFeeCap - baseFee)", @@ -361,6 +371,16 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { accessList: ðtypes.AccessList{}, expectPass: true, enableFeemarket: true, + from: suite.address.String(), + }, + { + name: "Invalid from address", + gasLimit: 10, + gasPrice: &oneInt, + cost: &oneInt, + accessList: ðtypes.AccessList{}, + expectPass: false, + from: "", }, } @@ -399,7 +419,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { vmdb.Commit() tx := evmtypes.NewTx(zeroInt.BigInt(), 1, &suite.address, amount, tc.gasLimit, gasPrice, gasFeeCap, gasTipCap, nil, tc.accessList) - tx.From = suite.address.String() + tx.From = tc.from txData, _ := evmtypes.UnpackTxData(tx.Data) From 3c78b7f79e15b9d5d540a5ff2cacca8e2de3d6e0 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 19 Oct 2022 19:33:36 -0300 Subject: [PATCH 02/12] [issue-1018] add more new tests on evm module keeper --- x/evm/keeper/keeper_test.go | 89 ++++++++++++++++++++++++ x/evm/keeper/migrations_test.go | 28 +++++--- x/evm/keeper/state_transition_test.go | 86 +++++++++++++++++++++++ x/evm/keeper/statedb_test.go | 99 +++++++++++++++++++++++++++ 4 files changed, 294 insertions(+), 8 deletions(-) diff --git a/x/evm/keeper/keeper_test.go b/x/evm/keeper/keeper_test.go index a0087a932d..22e93acfb8 100644 --- a/x/evm/keeper/keeper_test.go +++ b/x/evm/keeper/keeper_test.go @@ -200,6 +200,7 @@ func (suite *KeeperTestSuite) SetupApp(checkTx bool) { valAddr := sdk.ValAddress(suite.address.Bytes()) validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{}) + require.NoError(t, err) err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) require.NoError(t, err) err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) @@ -434,3 +435,91 @@ func (suite *KeeperTestSuite) TestBaseFee() { suite.enableFeemarket = false suite.enableLondonHF = true } + +func (suite *KeeperTestSuite) TestGetAccountStorage() { + suite.SetupTest() + + testCases := []struct { + name string + malleate func() + expRes []int + }{ + { + "Only one account that's not a contract (no storage)", + func() {}, + []int{0}, + }, + { + "Two accounts - one contract (with storage), one wallet", + func() { + supply := big.NewInt(100) + suite.DeployTestContract(suite.T(), suite.address, supply) + }, + []int{2, 0}, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + tc.malleate() + i := 0 + suite.app.AccountKeeper.IterateAccounts(suite.ctx, func(account authtypes.AccountI) bool { + ethAccount, ok := account.(ethermint.EthAccountI) + if !ok { + // ignore non EthAccounts + return false + } + + addr := ethAccount.EthAddress() + storage := suite.app.EvmKeeper.GetAccountStorage(suite.ctx, addr) + + suite.Require().Equal(tc.expRes[i], len(storage)) + i++ + return false + }) + + }) + } + +} + +func (suite *KeeperTestSuite) TestGetAccountOrEmpty() { + suite.SetupTest() + empty := statedb.Account{ + Balance: new(big.Int), + CodeHash: types.EmptyCodeHash, + } + + supply := big.NewInt(100) + contractAddr := suite.DeployTestContract(suite.T(), suite.address, supply) + + testCases := []struct { + name string + addr common.Address + expEmpty bool + }{ + { + "unexisting account - get empty", + common.Address{}, + true, + }, + { + "existing contract account", + contractAddr, + false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + res := suite.app.EvmKeeper.GetAccountOrEmpty(suite.ctx, tc.addr) + if tc.expEmpty { + suite.Require().Equal(empty, res) + } else { + suite.Require().NotEqual(empty, res) + } + + }) + } +} + diff --git a/x/evm/keeper/migrations_test.go b/x/evm/keeper/migrations_test.go index 0e43976341..3d680cdac6 100644 --- a/x/evm/keeper/migrations_test.go +++ b/x/evm/keeper/migrations_test.go @@ -1,19 +1,31 @@ package keeper_test import ( + sdk "github.com/cosmos/cosmos-sdk/types" evmkeeper "github.com/evmos/ethermint/x/evm/keeper" ) func (suite *KeeperTestSuite) TestMigrations() { migrator := evmkeeper.NewMigrator(*suite.app.EvmKeeper) - suite.Run("Run Migrate1to2", func() { - err := migrator.Migrate1to2(suite.ctx) - suite.Require().NoError(err) - }) + testCases := []struct { + name string + migrateFunc func(ctx sdk.Context) error + }{ + { + "Run Migrate1to2", + migrator.Migrate1to2, + }, + { + "Run Migrate2to3", + migrator.Migrate2to3, + }, + } - suite.Run("Run Migrate2to3", func() { - err := migrator.Migrate2to3(suite.ctx) - suite.Require().NoError(err) - }) + for _, tc := range testCases { + suite.Run(tc.name, func() { + err := tc.migrateFunc(suite.ctx) + suite.Require().NoError(err) + }) + } } diff --git a/x/evm/keeper/state_transition_test.go b/x/evm/keeper/state_transition_test.go index 914db7d9b1..2d3913eedf 100644 --- a/x/evm/keeper/state_transition_test.go +++ b/x/evm/keeper/state_transition_test.go @@ -502,3 +502,89 @@ func (suite *KeeperTestSuite) TestContractDeployment() { db := suite.StateDB() suite.Require().Greater(db.GetCodeSize(contractAddress), 0) } + +// define an ethMessage struct that implements core.Message interface to test ApplyMessage +type ethMessage struct { + from common.Address + to *common.Address + gasPrice *big.Int + gasFeeCap *big.Int + gasTipCap *big.Int + gas uint64 + value *big.Int + nonce uint64 + isFake bool + data []byte + accessList ethtypes.AccessList +} + +func (msg ethMessage) From() common.Address { + return msg.from +} + +func (msg ethMessage) To() *common.Address { + return msg.to +} + +func (msg ethMessage) GasPrice() *big.Int { + return msg.gasPrice +} + +func (msg ethMessage) GasFeeCap() *big.Int { + return msg.gasFeeCap +} + +func (msg ethMessage) GasTipCap() *big.Int { + return msg.gasTipCap +} + +func (msg ethMessage) Gas() uint64 { + return msg.gas +} + +func (msg ethMessage) Value() *big.Int { + return msg.value +} + +func (msg ethMessage) Nonce() uint64 { + return msg.nonce +} + +func (msg ethMessage) IsFake() bool { + return msg.isFake +} + +func (msg ethMessage) Data() []byte { + return msg.data +} + +func (msg ethMessage) AccessList() ethtypes.AccessList { + return msg.accessList +} + +func (suite *KeeperTestSuite) TestApplyMessage() { + suite.SetupTest() + amount := big.NewInt(1000) + gasPrice := big.NewInt(100_000) + var gas uint64 = 100_000 + contractAddr := suite.DeployTestContract(suite.T(), suite.address, amount) + + msg := ethMessage{ + from: suite.address, + to: &contractAddr, + gasPrice: gasPrice, + gas: gas, + value: amount, + } + + expectedGasUsed := uint64(50_000) + + config, err := suite.app.EvmKeeper.EVMConfig(suite.ctx) + suite.Require().NoError(err) + tracer := suite.app.EvmKeeper.Tracer(suite.ctx, msg, config.ChainConfig) + + res, err := suite.app.EvmKeeper.ApplyMessage(suite.ctx, msg, tracer, true) + + suite.Require().NoError(err) + suite.Require().Equal(expectedGasUsed, res.GasUsed) +} diff --git a/x/evm/keeper/statedb_test.go b/x/evm/keeper/statedb_test.go index e2c82a6cbe..b846a42514 100644 --- a/x/evm/keeper/statedb_test.go +++ b/x/evm/keeper/statedb_test.go @@ -811,3 +811,102 @@ func (suite *KeeperTestSuite) _TestForEachStorage() { storage = types.Storage{} } } + +func (suite *KeeperTestSuite) TestSetBalance() { + suite.SetupTest() + amount := big.NewInt(-10) + + testCases := []struct { + name string + addr common.Address + malleate func() + expErr bool + }{ + { + "address without funds - invalid amount", + suite.address, + func() {}, + true, + }, + { + "mint to address", + suite.address, + func() { + amount = big.NewInt(100) + }, + false, + }, + { + "burn from address", + suite.address, + func() { + amount = big.NewInt(60) + }, + false, + }, + { + "address with funds - invalid amount", + suite.address, + func() { + amount = big.NewInt(-10) + }, + true, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + tc.malleate() + err := suite.app.EvmKeeper.SetBalance(suite.ctx, tc.addr, amount) + if tc.expErr { + suite.Require().Error(err) + } else { + balance := suite.app.EvmKeeper.GetBalance(suite.ctx, tc.addr) + suite.Require().NoError(err) + suite.Require().Equal(amount, balance) + } + + }) + } +} + +func (suite *KeeperTestSuite) TestDeleteAccount() { + suite.SetupTest() + supply := big.NewInt(100) + contractAddr := suite.DeployTestContract(suite.T(), suite.address, supply) + + testCases := []struct { + name string + addr common.Address + expErr bool + }{ + { + "remove address", + suite.address, + false, + }, + { + "remove unexistent address - returns nil error", + common.HexToAddress("unexistent_address"), + false, + }, + { + "remove deployed contract", + contractAddr, + false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + err := suite.app.EvmKeeper.DeleteAccount(suite.ctx, tc.addr) + if tc.expErr { + suite.Require().Error(err) + } else { + suite.Require().NoError(err) + balance := suite.app.EvmKeeper.GetBalance(suite.ctx, tc.addr) + suite.Require().Equal(new(big.Int), balance) + } + }) + } +} From d19b041a593c98f60a30bc1c79297570643435a7 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 20 Oct 2022 16:51:25 -0300 Subject: [PATCH 03/12] [issue-1018] add more new tests on evm module keeper (state_transition, statedb, utils) --- x/evm/keeper/state_transition_test.go | 268 +++++++++++++++++--------- x/evm/keeper/statedb_test.go | 36 ++++ x/evm/keeper/utils_test.go | 30 +++ 3 files changed, 246 insertions(+), 88 deletions(-) diff --git a/x/evm/keeper/state_transition_test.go b/x/evm/keeper/state_transition_test.go index 2d3913eedf..28916df2a1 100644 --- a/x/evm/keeper/state_transition_test.go +++ b/x/evm/keeper/state_transition_test.go @@ -9,10 +9,12 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" "github.com/evmos/ethermint/tests" "github.com/evmos/ethermint/x/evm/keeper" + "github.com/evmos/ethermint/x/evm/statedb" "github.com/evmos/ethermint/x/evm/types" "github.com/tendermint/tendermint/crypto/tmhash" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -346,40 +348,60 @@ func (suite *KeeperTestSuite) TestGasToRefund() { } func (suite *KeeperTestSuite) TestRefundGas() { + var m core.Message + var err error testCases := []struct { name string leftoverGas uint64 refundQuotient uint64 noError bool expGasRefund uint64 + malleate func() }{ { - "leftoverGas more than tx gas limit", - params.TxGas + 1, - params.RefundQuotient, - false, - params.TxGas + 1, + name: "leftoverGas more than tx gas limit", + leftoverGas: params.TxGas + 1, + refundQuotient: params.RefundQuotient, + noError: false, + expGasRefund: params.TxGas + 1, }, { - "leftoverGas equal to tx gas limit, insufficient fee collector account", - params.TxGas, - params.RefundQuotient, - true, - 0, + name: "leftoverGas equal to tx gas limit, insufficient fee collector account", + leftoverGas: params.TxGas, + refundQuotient: params.RefundQuotient, + noError: true, + expGasRefund: 0, }, { - "leftoverGas less than to tx gas limit", - params.TxGas - 1, - params.RefundQuotient, - true, - 0, + name: "leftoverGas less than to tx gas limit", + leftoverGas: params.TxGas - 1, + refundQuotient: params.RefundQuotient, + noError: true, + expGasRefund: 0, }, { - "no leftoverGas, refund half used gas ", - 0, - params.RefundQuotient, - true, - params.TxGas / params.RefundQuotient, + name: "no leftoverGas, refund half used gas ", + leftoverGas: 0, + refundQuotient: params.RefundQuotient, + noError: true, + expGasRefund: params.TxGas / params.RefundQuotient, + }, + { + name: "invalid Gas value in msg", + leftoverGas: 0, + refundQuotient: params.RefundQuotient, + noError: false, + expGasRefund: params.TxGas, + malleate: func() { + keeperParams := suite.app.EvmKeeper.GetParams(suite.ctx) + m, err = suite.createContractMsg( + suite.StateDB().GetNonce(suite.address), + ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()), + keeperParams.ChainConfig.EthereumConfig(suite.app.EvmKeeper.ChainID()), + big.NewInt(-100), + ) + suite.Require().NoError(err) + }, }, } @@ -393,7 +415,7 @@ func (suite *KeeperTestSuite) TestRefundGas() { signer := ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()) vmdb := suite.StateDB() - m, err := newNativeMessage( + m, err = newNativeMessage( vmdb.GetNonce(suite.address), suite.ctx.BlockHeight(), suite.address, @@ -411,6 +433,11 @@ func (suite *KeeperTestSuite) TestRefundGas() { if tc.leftoverGas > m.Gas() { return } + + if tc.malleate != nil { + tc.malleate() + } + gasUsed := m.Gas() - tc.leftoverGas refund := keeper.GasToRefund(vmdb.GetRefund(), gasUsed, tc.refundQuotient) suite.Require().Equal(tc.expGasRefund, refund) @@ -503,88 +530,153 @@ func (suite *KeeperTestSuite) TestContractDeployment() { suite.Require().Greater(db.GetCodeSize(contractAddress), 0) } -// define an ethMessage struct that implements core.Message interface to test ApplyMessage -type ethMessage struct { - from common.Address - to *common.Address - gasPrice *big.Int - gasFeeCap *big.Int - gasTipCap *big.Int - gas uint64 - value *big.Int - nonce uint64 - isFake bool - data []byte - accessList ethtypes.AccessList -} +func (suite *KeeperTestSuite) TestApplyMessage() { + suite.SetupTest() + expectedGasUsed := params.TxGas + var msg core.Message -func (msg ethMessage) From() common.Address { - return msg.from -} + config, err := suite.app.EvmKeeper.EVMConfig(suite.ctx) + suite.Require().NoError(err) -func (msg ethMessage) To() *common.Address { - return msg.to -} + keeperParams := suite.app.EvmKeeper.GetParams(suite.ctx) + chainCfg := keeperParams.ChainConfig.EthereumConfig(suite.app.EvmKeeper.ChainID()) + signer := ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()) + tracer := suite.app.EvmKeeper.Tracer(suite.ctx, msg, config.ChainConfig) + vmdb := suite.StateDB() + + msg, err = newNativeMessage( + vmdb.GetNonce(suite.address), + suite.ctx.BlockHeight(), + suite.address, + chainCfg, + suite.signer, + signer, + ethtypes.AccessListTxType, + nil, + nil, + ) + suite.Require().NoError(err) -func (msg ethMessage) GasPrice() *big.Int { - return msg.gasPrice -} + res, err := suite.app.EvmKeeper.ApplyMessage(suite.ctx, msg, tracer, true) -func (msg ethMessage) GasFeeCap() *big.Int { - return msg.gasFeeCap + suite.Require().NoError(err) + suite.Require().Equal(expectedGasUsed, res.GasUsed) + suite.Require().False(res.Failed()) } -func (msg ethMessage) GasTipCap() *big.Int { - return msg.gasTipCap -} +func (suite *KeeperTestSuite) TestApplyMessageWithConfig() { + suite.SetupTest() + var msg core.Message + var err error + var expectedGasUsed uint64 + var config *types.EVMConfig + var keeperParams types.Params + var signer ethtypes.Signer + var vmdb *statedb.StateDB + var txConfig statedb.TxConfig + var chainCfg *params.ChainConfig -func (msg ethMessage) Gas() uint64 { - return msg.gas -} + testCases := []struct { + name string + malleate func() + expErr bool + }{ + { + "messsage applied ok", + func() { + msg, err = newNativeMessage( + vmdb.GetNonce(suite.address), + suite.ctx.BlockHeight(), + suite.address, + chainCfg, + suite.signer, + signer, + ethtypes.AccessListTxType, + nil, + nil, + ) + suite.Require().NoError(err) + }, + false, + }, + { + "call contract tx with config param EnableCall = false", + func() { + config.Params.EnableCall = false + msg, err = newNativeMessage( + vmdb.GetNonce(suite.address), + suite.ctx.BlockHeight(), + suite.address, + chainCfg, + suite.signer, + signer, + ethtypes.AccessListTxType, + nil, + nil, + ) + suite.Require().NoError(err) + }, + true, + }, + { + "create contract tx with config param EnableCreate = false", + func() { + msg, err = suite.createContractMsg(vmdb.GetNonce(suite.address), signer, chainCfg, big.NewInt(1)) + suite.Require().NoError(err) + config.Params.EnableCreate = false + }, + true, + }, + } -func (msg ethMessage) Value() *big.Int { - return msg.value -} + for _, tc := range testCases { + suite.Run(fmt.Sprintf("Case %s", tc.name), func() { + suite.SetupTest() + expectedGasUsed = params.TxGas -func (msg ethMessage) Nonce() uint64 { - return msg.nonce -} + config, err = suite.app.EvmKeeper.EVMConfig(suite.ctx) + suite.Require().NoError(err) -func (msg ethMessage) IsFake() bool { - return msg.isFake -} + keeperParams = suite.app.EvmKeeper.GetParams(suite.ctx) + chainCfg = keeperParams.ChainConfig.EthereumConfig(suite.app.EvmKeeper.ChainID()) + signer = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()) + vmdb = suite.StateDB() + txConfig = suite.app.EvmKeeper.TxConfig(suite.ctx, common.Hash{}) -func (msg ethMessage) Data() []byte { - return msg.data -} + tc.malleate() + res, err := suite.app.EvmKeeper.ApplyMessageWithConfig(suite.ctx, msg, nil, true, config, txConfig) -func (msg ethMessage) AccessList() ethtypes.AccessList { - return msg.accessList -} + if tc.expErr { + suite.Require().Error(err) + return + } -func (suite *KeeperTestSuite) TestApplyMessage() { - suite.SetupTest() - amount := big.NewInt(1000) - gasPrice := big.NewInt(100_000) - var gas uint64 = 100_000 - contractAddr := suite.DeployTestContract(suite.T(), suite.address, amount) - - msg := ethMessage{ - from: suite.address, - to: &contractAddr, - gasPrice: gasPrice, - gas: gas, - value: amount, + suite.Require().NoError(err) + suite.Require().False(res.Failed()) + suite.Require().Equal(expectedGasUsed, res.GasUsed) + }) } - expectedGasUsed := uint64(50_000) +} + +func (suite *KeeperTestSuite) createContractMsg(nonce uint64, signer ethtypes.Signer, cfg *params.ChainConfig, gasPrice *big.Int) (core.Message, error) { - config, err := suite.app.EvmKeeper.EVMConfig(suite.ctx) - suite.Require().NoError(err) - tracer := suite.app.EvmKeeper.Tracer(suite.ctx, msg, config.ChainConfig) - - res, err := suite.app.EvmKeeper.ApplyMessage(suite.ctx, msg, tracer, true) + contractCreateTx := ðtypes.AccessListTx{ + GasPrice: gasPrice, + Gas: params.TxGasContractCreation, + To: nil, + Data: []byte("contract_data"), + Nonce: nonce, + } + ethTx := ethtypes.NewTx(contractCreateTx) + ethMsg := &types.MsgEthereumTx{} + ethMsg.FromEthereumTx(ethTx) + ethMsg.From = suite.address.Hex() - suite.Require().NoError(err) - suite.Require().Equal(expectedGasUsed, res.GasUsed) + if err := ethMsg.Sign(signer, suite.signer); err != nil { + return nil, err + } + + msgSigner := ethtypes.MakeSigner(cfg, big.NewInt(suite.ctx.BlockHeight())) + return ethMsg.AsMessage(msgSigner, nil) } diff --git a/x/evm/keeper/statedb_test.go b/x/evm/keeper/statedb_test.go index b846a42514..694ca4058c 100644 --- a/x/evm/keeper/statedb_test.go +++ b/x/evm/keeper/statedb_test.go @@ -6,6 +6,7 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" @@ -317,6 +318,41 @@ func (suite *KeeperTestSuite) TestSetCode() { } } + +func (suite *KeeperTestSuite) TestKeeperSetCode() { + addr := tests.GenerateAddress() + baseAcc := &authtypes.BaseAccount{Address: sdk.AccAddress(addr.Bytes()).String()} + suite.app.AccountKeeper.SetAccount(suite.ctx, baseAcc) + + testCases := []struct { + name string + codeHash []byte + code []byte + }{ + { + "set code", + []byte("codeHash"), + []byte("this is the code"), + }, + { + "delete code", + []byte("codeHash"), + nil, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + suite.app.EvmKeeper.SetCode(suite.ctx, tc.codeHash, tc.code) + key := suite.app.GetKey(types.StoreKey) + store := prefix.NewStore(suite.ctx.KVStore(key), types.KeyPrefixCode) + code := store.Get(tc.codeHash) + + suite.Require().Equal(tc.code, code) + }) + } +} + func (suite *KeeperTestSuite) TestRefund() { testCases := []struct { name string diff --git a/x/evm/keeper/utils_test.go b/x/evm/keeper/utils_test.go index 1aadcb0b77..f24cdeea88 100644 --- a/x/evm/keeper/utils_test.go +++ b/x/evm/keeper/utils_test.go @@ -272,6 +272,7 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { expectPass bool enableFeemarket bool from string + malleate func() }{ { name: "Enough balance", @@ -382,6 +383,32 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { expectPass: false, from: "", }, + { + name: "Enough balance - with access list", + gasLimit: 10, + gasPrice: &oneInt, + cost: &oneInt, + accessList: ðtypes.AccessList{ + ethtypes.AccessTuple{ + Address: suite.address, + StorageKeys: []common.Hash{}, + }, + }, + expectPass: true, + from: suite.address.String(), + }, + { + name: "gasLimit < intrinsicGas during IsCheckTx", + gasLimit: 1, + gasPrice: &oneInt, + cost: &oneInt, + accessList: ðtypes.AccessList{}, + expectPass: false, + from: suite.address.String(), + malleate: func() { + suite.ctx = suite.ctx.WithIsCheckTx(true) + }, + }, } for i, tc := range testCases { @@ -390,6 +417,9 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() { suite.SetupTest() vmdb := suite.StateDB() + if tc.malleate != nil { + tc.malleate() + } var amount, gasPrice, gasFeeCap, gasTipCap *big.Int if tc.cost != nil { amount = tc.cost.BigInt() From 02f141838d3f5d3024e68db39bc061ff576803d5 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 20 Oct 2022 18:42:51 -0300 Subject: [PATCH 04/12] [issue-1018] add more new tests on evm module keeper (msg_server) --- x/evm/keeper/grpc_query_test.go | 78 ++++++++++++++++++ x/evm/keeper/msg_server_test.go | 80 +++++++++++++++++++ .../keeper/state_transition_benchmark_test.go | 29 +++++-- x/evm/keeper/state_transition_test.go | 54 ++++++++----- 4 files changed, 213 insertions(+), 28 deletions(-) create mode 100644 x/evm/keeper/msg_server_test.go diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index 0dd5887c6c..eb3219d8b0 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -1208,3 +1208,81 @@ func (suite *KeeperTestSuite) TestEthCall() { } } + +func (suite *KeeperTestSuite) TestEmptyRequest() { + suite.SetupTest() + k := suite.app.EvmKeeper + + testCases := []struct { + name string + queryFunc func() (interface{}, error) + }{ + { + "Account method", + func() (interface{}, error) { + return k.Account(suite.ctx, nil) + }, + }, + { + "CosmosAccount method", + func() (interface{}, error) { + return k.CosmosAccount(suite.ctx, nil) + }, + }, + { + "ValidatorAccount method", + func() (interface{}, error) { + return k.ValidatorAccount(suite.ctx, nil) + }, + }, + { + "Balance method", + func() (interface{}, error) { + return k.Balance(suite.ctx, nil) + }, + }, + { + "Storage method", + func() (interface{}, error) { + return k.Storage(suite.ctx, nil) + }, + }, + { + "Code method", + func() (interface{}, error) { + return k.Code(suite.ctx, nil) + }, + }, + { + "EthCall method", + func() (interface{}, error) { + return k.EthCall(suite.ctx, nil) + }, + }, + { + "EstimateGas method", + func() (interface{}, error) { + return k.EstimateGas(suite.ctx, nil) + }, + }, + { + "TraceTx method", + func() (interface{}, error) { + return k.TraceTx(suite.ctx, nil) + }, + }, + { + "TraceBlock method", + func() (interface{}, error) { + return k.TraceBlock(suite.ctx, nil) + }, + }, + } + + for _, tc := range testCases { + suite.Run(fmt.Sprintf("Case %s", tc.name), func() { + _, err := tc.queryFunc() + suite.Require().Error(err) + }) + } +} diff --git a/x/evm/keeper/msg_server_test.go b/x/evm/keeper/msg_server_test.go new file mode 100644 index 0000000000..335a349c7d --- /dev/null +++ b/x/evm/keeper/msg_server_test.go @@ -0,0 +1,80 @@ +package keeper_test + +import ( + "math/big" + + ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/params" + "github.com/evmos/ethermint/x/evm/statedb" + "github.com/evmos/ethermint/x/evm/types" +) + +func (suite *KeeperTestSuite) TestEthereumTx() { + var ( + err error + msg *types.MsgEthereumTx + signer ethtypes.Signer + vmdb *statedb.StateDB + chainCfg *params.ChainConfig + expectedGasUsed uint64 + ) + + testCases := []struct { + name string + malleate func() + expErr bool + }{ + { + "Deploy contract tx - insufficient gas", + func() { + msg, err = suite.createContractMsgTx( + vmdb.GetNonce(suite.address), + signer, + chainCfg, + big.NewInt(1), + ) + suite.Require().NoError(err) + }, + true, + }, + { + "Transfer funds tx", + func() { + msg, _, err = newEthMsgTx( + vmdb.GetNonce(suite.address), + suite.ctx.BlockHeight(), + suite.address, + chainCfg, + suite.signer, + signer, + ethtypes.AccessListTxType, + nil, + nil, + ) + suite.Require().NoError(err) + expectedGasUsed = params.TxGas + }, + false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + suite.SetupTest() + keeperParams := suite.app.EvmKeeper.GetParams(suite.ctx) + chainCfg = keeperParams.ChainConfig.EthereumConfig(suite.app.EvmKeeper.ChainID()) + signer = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()) + vmdb = suite.StateDB() + + tc.malleate() + res, err := suite.app.EvmKeeper.EthereumTx(suite.ctx, msg) + if tc.expErr { + suite.Require().Error(err) + return + } + suite.Require().NoError(err) + suite.Require().Equal(expectedGasUsed, res.GasUsed) + suite.Require().False(res.Failed()) + }) + } +} diff --git a/x/evm/keeper/state_transition_benchmark_test.go b/x/evm/keeper/state_transition_benchmark_test.go index c9b3c09234..795f6ff548 100644 --- a/x/evm/keeper/state_transition_benchmark_test.go +++ b/x/evm/keeper/state_transition_benchmark_test.go @@ -75,7 +75,7 @@ func newSignedEthTx( return ethTx, nil } -func newNativeMessage( +func newEthMsgTx( nonce uint64, blockHeight int64, address common.Address, @@ -85,14 +85,11 @@ func newNativeMessage( txType byte, data []byte, accessList ethtypes.AccessList, -) (core.Message, error) { - msgSigner := ethtypes.MakeSigner(cfg, big.NewInt(blockHeight)) - +) (*evmtypes.MsgEthereumTx, *big.Int, error) { var ( ethTx *ethtypes.Transaction baseFee *big.Int ) - switch txType { case ethtypes.LegacyTxType: templateLegacyTx.Nonce = nonce @@ -122,14 +119,32 @@ func newNativeMessage( ethTx = ethtypes.NewTx(templateDynamicFeeTx) baseFee = big.NewInt(3) default: - return nil, errors.New("unsupport tx type") + return nil, baseFee, errors.New("unsupport tx type") } msg := &evmtypes.MsgEthereumTx{} msg.FromEthereumTx(ethTx) msg.From = address.Hex() - if err := msg.Sign(ethSigner, krSigner); err != nil { + return msg, baseFee, msg.Sign(ethSigner, krSigner) +} + +func newNativeMessage( + nonce uint64, + blockHeight int64, + address common.Address, + cfg *params.ChainConfig, + krSigner keyring.Signer, + ethSigner ethtypes.Signer, + txType byte, + data []byte, + accessList ethtypes.AccessList, +) (core.Message, error) { + msgSigner := ethtypes.MakeSigner(cfg, big.NewInt(blockHeight)) + + msg, baseFee, err := newEthMsgTx(nonce, blockHeight, address, cfg, krSigner, ethSigner, txType, data, accessList) + + if err != nil { return nil, err } diff --git a/x/evm/keeper/state_transition_test.go b/x/evm/keeper/state_transition_test.go index 28916df2a1..6a5159ab32 100644 --- a/x/evm/keeper/state_transition_test.go +++ b/x/evm/keeper/state_transition_test.go @@ -348,8 +348,11 @@ func (suite *KeeperTestSuite) TestGasToRefund() { } func (suite *KeeperTestSuite) TestRefundGas() { - var m core.Message - var err error + var ( + m core.Message + err error + ) + testCases := []struct { name string leftoverGas uint64 @@ -394,7 +397,7 @@ func (suite *KeeperTestSuite) TestRefundGas() { expGasRefund: params.TxGas, malleate: func() { keeperParams := suite.app.EvmKeeper.GetParams(suite.ctx) - m, err = suite.createContractMsg( + m, err = suite.createContractGethMsg( suite.StateDB().GetNonce(suite.address), ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()), keeperParams.ChainConfig.EthereumConfig(suite.app.EvmKeeper.ChainID()), @@ -437,7 +440,7 @@ func (suite *KeeperTestSuite) TestRefundGas() { if tc.malleate != nil { tc.malleate() } - + gasUsed := m.Gas() - tc.leftoverGas refund := keeper.GasToRefund(vmdb.GetRefund(), gasUsed, tc.refundQuotient) suite.Require().Equal(tc.expGasRefund, refund) @@ -566,15 +569,17 @@ func (suite *KeeperTestSuite) TestApplyMessage() { func (suite *KeeperTestSuite) TestApplyMessageWithConfig() { suite.SetupTest() - var msg core.Message - var err error - var expectedGasUsed uint64 - var config *types.EVMConfig - var keeperParams types.Params - var signer ethtypes.Signer - var vmdb *statedb.StateDB - var txConfig statedb.TxConfig - var chainCfg *params.ChainConfig + var ( + msg core.Message + err error + expectedGasUsed uint64 + config *types.EVMConfig + keeperParams types.Params + signer ethtypes.Signer + vmdb *statedb.StateDB + txConfig statedb.TxConfig + chainCfg *params.ChainConfig + ) testCases := []struct { name string @@ -621,7 +626,7 @@ func (suite *KeeperTestSuite) TestApplyMessageWithConfig() { { "create contract tx with config param EnableCreate = false", func() { - msg, err = suite.createContractMsg(vmdb.GetNonce(suite.address), signer, chainCfg, big.NewInt(1)) + msg, err = suite.createContractGethMsg(vmdb.GetNonce(suite.address), signer, chainCfg, big.NewInt(1)) suite.Require().NoError(err) config.Params.EnableCreate = false }, @@ -659,7 +664,19 @@ func (suite *KeeperTestSuite) TestApplyMessageWithConfig() { } -func (suite *KeeperTestSuite) createContractMsg(nonce uint64, signer ethtypes.Signer, cfg *params.ChainConfig, gasPrice *big.Int) (core.Message, error) { +func (suite *KeeperTestSuite) createContractGethMsg(nonce uint64, signer ethtypes.Signer, cfg *params.ChainConfig, gasPrice *big.Int) (core.Message, error) { + + ethMsg, err := suite.createContractMsgTx(nonce, signer, cfg, gasPrice) + + if err != nil { + return nil, err + } + + msgSigner := ethtypes.MakeSigner(cfg, big.NewInt(suite.ctx.BlockHeight())) + return ethMsg.AsMessage(msgSigner, nil) +} + +func (suite *KeeperTestSuite) createContractMsgTx(nonce uint64, signer ethtypes.Signer, cfg *params.ChainConfig, gasPrice *big.Int) (*types.MsgEthereumTx, error) { contractCreateTx := ðtypes.AccessListTx{ GasPrice: gasPrice, @@ -673,10 +690,5 @@ func (suite *KeeperTestSuite) createContractMsg(nonce uint64, signer ethtypes.Si ethMsg.FromEthereumTx(ethTx) ethMsg.From = suite.address.Hex() - if err := ethMsg.Sign(signer, suite.signer); err != nil { - return nil, err - } - - msgSigner := ethtypes.MakeSigner(cfg, big.NewInt(suite.ctx.BlockHeight())) - return ethMsg.AsMessage(msgSigner, nil) + return ethMsg, ethMsg.Sign(signer, suite.signer) } From ea6c36fc8031b58f12736f9f3edee30908cacc41 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 20 Oct 2022 18:47:51 -0300 Subject: [PATCH 05/12] [issue-1018] fix code style --- x/evm/keeper/msg_server_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/x/evm/keeper/msg_server_test.go b/x/evm/keeper/msg_server_test.go index 335a349c7d..1abf2a19ce 100644 --- a/x/evm/keeper/msg_server_test.go +++ b/x/evm/keeper/msg_server_test.go @@ -11,12 +11,12 @@ import ( func (suite *KeeperTestSuite) TestEthereumTx() { var ( - err error - msg *types.MsgEthereumTx - signer ethtypes.Signer - vmdb *statedb.StateDB - chainCfg *params.ChainConfig - expectedGasUsed uint64 + err error + msg *types.MsgEthereumTx + signer ethtypes.Signer + vmdb *statedb.StateDB + chainCfg *params.ChainConfig + expectedGasUsed uint64 ) testCases := []struct { @@ -37,7 +37,7 @@ func (suite *KeeperTestSuite) TestEthereumTx() { }, true, }, - { + { "Transfer funds tx", func() { msg, _, err = newEthMsgTx( @@ -52,7 +52,7 @@ func (suite *KeeperTestSuite) TestEthereumTx() { nil, ) suite.Require().NoError(err) - expectedGasUsed = params.TxGas + expectedGasUsed = params.TxGas }, false, }, From 97bdc27be7edb84d77975dea12b93957c49f74ed Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 20 Oct 2022 18:55:49 -0300 Subject: [PATCH 06/12] [issue-1018] add changes in CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be40b4d92c..7fbfa9c674 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (cli) [#1360](https://github.com/evmos/ethermint/pull/1360) Introduce a new `grpc-only` flag, such that when enabled, will start the node in a query-only mode. Note, gRPC MUST be enabled with this flag. * (rpc) [#1378](https://github.com/evmos/ethermint/pull/1378) Add support for EVM RPC metrics * (ante) [#1390](https://github.com/evmos/ethermint/pull/1390) Added multisig tx support. +* (test) [#1396](https://github.com/evmos/ethermint/pull/1396) Increase test coverage for the EVM module `keeper` ### Bug Fixes From 29e2588ea054f86c0c538fff586dfa1a9f53d678 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 20 Oct 2022 19:00:49 -0300 Subject: [PATCH 07/12] [issue-1018] add missing error check in grpc_query_test --- x/evm/keeper/grpc_query_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index eb3219d8b0..3980ef8a87 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -842,6 +842,7 @@ func (suite *KeeperTestSuite) TestTraceTx() { Predecessors: predecessors, } res, err := suite.queryClient.TraceTx(sdk.WrapSDKContext(suite.ctx), &traceReq) + suite.Require().NoError(err) if tc.expPass { suite.Require().NoError(err) @@ -1276,7 +1277,7 @@ func (suite *KeeperTestSuite) TestEmptyRequest() { func() (interface{}, error) { return k.TraceBlock(suite.ctx, nil) }, - }, + }, } for _, tc := range testCases { From 2981fe28ed510ceb288fff19b79224a86608da1c Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 20 Oct 2022 19:11:34 -0300 Subject: [PATCH 08/12] [issue-1018] fix failing tests --- x/evm/keeper/grpc_query_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index 3980ef8a87..8823de824e 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -842,7 +842,6 @@ func (suite *KeeperTestSuite) TestTraceTx() { Predecessors: predecessors, } res, err := suite.queryClient.TraceTx(sdk.WrapSDKContext(suite.ctx), &traceReq) - suite.Require().NoError(err) if tc.expPass { suite.Require().NoError(err) From 87037b470d5b8cb610a498c7fbc68b251c3bea00 Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 21 Oct 2022 09:29:04 -0300 Subject: [PATCH 09/12] [issue-1018] add changes based on review comments (grpc_query_test & abci_test) --- x/evm/keeper/abci_test.go | 24 +-- x/evm/keeper/grpc_query_test.go | 322 +++++++++++++++++++++----------- 2 files changed, 222 insertions(+), 124 deletions(-) diff --git a/x/evm/keeper/abci_test.go b/x/evm/keeper/abci_test.go index 0fd4b7ca02..dc7c265cec 100644 --- a/x/evm/keeper/abci_test.go +++ b/x/evm/keeper/abci_test.go @@ -1,24 +1,18 @@ package keeper_test import ( + evmtypes "github.com/evmos/ethermint/x/evm/types" "github.com/tendermint/tendermint/abci/types" - evmtypes "github.com/evmos/ethermint/x/evm/types" ) - func (suite *KeeperTestSuite) TestEndBlock() { + em := suite.ctx.EventManager() + suite.Require().Equal(0, len(em.Events())) + + res := suite.app.EvmKeeper.EndBlock(suite.ctx, types.RequestEndBlock{}) + suite.Require().Equal([]types.ValidatorUpdate{}, res) - suite.Run("EndBlock test", func() { - suite.SetupTest() // reset - - em := suite.ctx.EventManager() - suite.Require().Equal(0, len(em.Events())) - - res := suite.app.EvmKeeper.EndBlock(suite.ctx, types.RequestEndBlock{}) - suite.Require().Equal([]types.ValidatorUpdate{}, res) - - // should emit 1 EventTypeBlockBloom event on EndBlock - suite.Require().Equal(1, len(em.Events())) - suite.Require().Equal(evmtypes.EventTypeBlockBloom, em.Events()[0].Type) - }) + // should emit 1 EventTypeBlockBloom event on EndBlock + suite.Require().Equal(1, len(em.Events())) + suite.Require().Equal(evmtypes.EventTypeBlockBloom, em.Events()[0].Type) } diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index 8823de824e..33cdcff44f 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -13,6 +13,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" ethlogger "github.com/ethereum/go-ethereum/eth/tracers/logger" ethparams "github.com/ethereum/go-ethereum/params" + "github.com/evmos/ethermint/tests" "github.com/evmos/ethermint/x/evm/statedb" sdk "github.com/cosmos/cosmos-sdk/types" @@ -506,114 +507,223 @@ func (suite *KeeperTestSuite) TestEstimateGas() { enableFeemarket bool }{ // should success, because transfer value is zero - {"default args - special case for ErrIntrinsicGas on contract creation, raise gas limit", func() { - args = types.TransactionArgs{} - }, true, ethparams.TxGasContractCreation, false}, + { + "default args - special case for ErrIntrinsicGas on contract creation, raise gas limit", + func() { + args = types.TransactionArgs{} + }, + true, + ethparams.TxGasContractCreation, + false, + }, // should success, because transfer value is zero - {"default args with 'to' address", func() { - args = types.TransactionArgs{To: &common.Address{}} - }, true, ethparams.TxGas, false}, + { + "default args with 'to' address", + func() { + args = types.TransactionArgs{To: &common.Address{}} + }, + true, + ethparams.TxGas, + false, + }, // should fail, because the default From address(zero address) don't have fund - {"not enough balance", func() { - args = types.TransactionArgs{To: &common.Address{}, Value: (*hexutil.Big)(big.NewInt(100))} - }, false, 0, false}, + { + "not enough balance", + func() { + args = types.TransactionArgs{To: &common.Address{}, Value: (*hexutil.Big)(big.NewInt(100))} + }, + false, + 0, + false, + }, // should success, enough balance now - {"enough balance", func() { - args = types.TransactionArgs{To: &common.Address{}, From: &suite.address, Value: (*hexutil.Big)(big.NewInt(100))} - }, false, 0, false}, + { + "enough balance", + func() { + args = types.TransactionArgs{To: &common.Address{}, From: &suite.address, Value: (*hexutil.Big)(big.NewInt(100))} + }, false, 0, false}, // should success, because gas limit lower than 21000 is ignored - {"gas exceed allowance", func() { - args = types.TransactionArgs{To: &common.Address{}, Gas: &gasHelper} - }, true, ethparams.TxGas, false}, + { + "gas exceed allowance", + func() { + args = types.TransactionArgs{To: &common.Address{}, Gas: &gasHelper} + }, + true, + ethparams.TxGas, + false, + }, // should fail, invalid gas cap - {"gas exceed global allowance", func() { - args = types.TransactionArgs{To: &common.Address{}} - gasCap = 20000 - }, false, 0, false}, + { + "gas exceed global allowance", + func() { + args = types.TransactionArgs{To: &common.Address{}} + gasCap = 20000 + }, + false, + 0, + false, + }, // estimate gas of an erc20 contract deployment, the exact gas number is checked with geth - {"contract deployment", func() { - ctorArgs, err := types.ERC20Contract.ABI.Pack("", &suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) - suite.Require().NoError(err) - data := append(types.ERC20Contract.Bin, ctorArgs...) - args = types.TransactionArgs{ - From: &suite.address, - Data: (*hexutil.Bytes)(&data), - } - }, true, 1186778, false}, + { + "contract deployment", + func() { + ctorArgs, err := types.ERC20Contract.ABI.Pack("", &suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) + suite.Require().NoError(err) + data := append(types.ERC20Contract.Bin, ctorArgs...) + args = types.TransactionArgs{ + From: &suite.address, + Data: (*hexutil.Bytes)(&data), + } + }, + true, + 1186778, + false, + }, // estimate gas of an erc20 transfer, the exact gas number is checked with geth - {"erc20 transfer", func() { - contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) - suite.Commit() - transferData, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000)) - suite.Require().NoError(err) - args = types.TransactionArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)} - }, true, 51880, false}, - + { + "erc20 transfer", + func() { + contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) + suite.Commit() + transferData, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000)) + suite.Require().NoError(err) + args = types.TransactionArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)} + }, + true, + 51880, + false, + }, // repeated tests with enableFeemarket - {"default args w/ enableFeemarket", func() { - args = types.TransactionArgs{To: &common.Address{}} - }, true, ethparams.TxGas, true}, - {"not enough balance w/ enableFeemarket", func() { - args = types.TransactionArgs{To: &common.Address{}, Value: (*hexutil.Big)(big.NewInt(100))} - }, false, 0, true}, - {"enough balance w/ enableFeemarket", func() { - args = types.TransactionArgs{To: &common.Address{}, From: &suite.address, Value: (*hexutil.Big)(big.NewInt(100))} - }, false, 0, true}, - {"gas exceed allowance w/ enableFeemarket", func() { - args = types.TransactionArgs{To: &common.Address{}, Gas: &gasHelper} - }, true, ethparams.TxGas, true}, - {"gas exceed global allowance w/ enableFeemarket", func() { - args = types.TransactionArgs{To: &common.Address{}} - gasCap = 20000 - }, false, 0, true}, - {"contract deployment w/ enableFeemarket", func() { - ctorArgs, err := types.ERC20Contract.ABI.Pack("", &suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) - suite.Require().NoError(err) - data := append(types.ERC20Contract.Bin, ctorArgs...) - args = types.TransactionArgs{ - From: &suite.address, - Data: (*hexutil.Bytes)(&data), - } - }, true, 1186778, true}, - {"erc20 transfer w/ enableFeemarket", func() { - contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) - suite.Commit() - transferData, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000)) - suite.Require().NoError(err) - args = types.TransactionArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)} - }, true, 51880, true}, - {"contract creation but 'create' param disabled", func() { - ctorArgs, err := types.ERC20Contract.ABI.Pack("", &suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) - suite.Require().NoError(err) - data := append(types.ERC20Contract.Bin, ctorArgs...) - args = types.TransactionArgs{ - From: &suite.address, - Data: (*hexutil.Bytes)(&data), - } - params := suite.app.EvmKeeper.GetParams(suite.ctx) - params.EnableCreate = false - suite.app.EvmKeeper.SetParams(suite.ctx, params) - }, false, 0, false}, - {"specified gas in args higher than ethparams.TxGas (21,000)", func() { - args = types.TransactionArgs{ - To: &common.Address{}, - Gas: &higherGas, - } - }, true, ethparams.TxGas, false}, - {"specified gas in args higher than request gasCap", func() { - gasCap = 22_000 - args = types.TransactionArgs{ - To: &common.Address{}, - Gas: &higherGas, - } - }, true, ethparams.TxGas, false}, - {"invalid args - specified both gasPrice and maxFeePerGas", func() { - args = types.TransactionArgs{ - To: &common.Address{}, - GasPrice: &hexBigInt, - MaxFeePerGas: &hexBigInt, - } - }, false, 0, false}, + { + "default args w/ enableFeemarket", + func() { + args = types.TransactionArgs{To: &common.Address{}} + }, + true, + ethparams.TxGas, + true, + }, + { + "not enough balance w/ enableFeemarket", + func() { + args = types.TransactionArgs{To: &common.Address{}, Value: (*hexutil.Big)(big.NewInt(100))} + }, + false, + 0, + true, + }, + { + "enough balance w/ enableFeemarket", + func() { + args = types.TransactionArgs{To: &common.Address{}, From: &suite.address, Value: (*hexutil.Big)(big.NewInt(100))} + }, + false, + 0, + true, + }, + { + "gas exceed allowance w/ enableFeemarket", + func() { + args = types.TransactionArgs{To: &common.Address{}, Gas: &gasHelper} + }, + true, + ethparams.TxGas, + true, + }, + { + "gas exceed global allowance w/ enableFeemarket", + func() { + args = types.TransactionArgs{To: &common.Address{}} + gasCap = 20000 + }, + false, + 0, + true, + }, + { + "contract deployment w/ enableFeemarket", + func() { + ctorArgs, err := types.ERC20Contract.ABI.Pack("", &suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) + suite.Require().NoError(err) + data := append(types.ERC20Contract.Bin, ctorArgs...) + args = types.TransactionArgs{ + From: &suite.address, + Data: (*hexutil.Bytes)(&data), + } + }, + true, + 1186778, + true, + }, + { + "erc20 transfer w/ enableFeemarket", + func() { + contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) + suite.Commit() + transferData, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000)) + suite.Require().NoError(err) + args = types.TransactionArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)} + }, + true, + 51880, + true, + }, + { + "contract creation but 'create' param disabled", + func() { + ctorArgs, err := types.ERC20Contract.ABI.Pack("", &suite.address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) + suite.Require().NoError(err) + data := append(types.ERC20Contract.Bin, ctorArgs...) + args = types.TransactionArgs{ + From: &suite.address, + Data: (*hexutil.Bytes)(&data), + } + params := suite.app.EvmKeeper.GetParams(suite.ctx) + params.EnableCreate = false + suite.app.EvmKeeper.SetParams(suite.ctx, params) + }, + false, + 0, + false, + }, + { + "specified gas in args higher than ethparams.TxGas (21,000)", + func() { + args = types.TransactionArgs{ + To: &common.Address{}, + Gas: &higherGas, + } + }, + true, + ethparams.TxGas, + false, + }, + { + "specified gas in args higher than request gasCap", + func() { + gasCap = 22_000 + args = types.TransactionArgs{ + To: &common.Address{}, + Gas: &higherGas, + } + }, + true, + ethparams.TxGas, + false, + }, + { + "invalid args - specified both gasPrice and maxFeePerGas", + func() { + args = types.TransactionArgs{ + To: &common.Address{}, + GasPrice: &hexBigInt, + MaxFeePerGas: &hexBigInt, + } + }, + false, + 0, + false, + }, } for _, tc := range testCases { @@ -1022,9 +1132,7 @@ func (suite *KeeperTestSuite) TestTraceBlock() { func (suite *KeeperTestSuite) TestNonceInQuery() { suite.SetupTest() - priv, err := ethsecp256k1.GenerateKey() - suite.Require().NoError(err) - address := common.BytesToAddress(priv.PubKey().Address().Bytes()) + address := tests.GenerateAddress() suite.Require().Equal(uint64(0), suite.app.EvmKeeper.GetNonce(suite.ctx, address)) supply := sdkmath.NewIntWithDecimal(1000, 18).BigInt() @@ -1131,8 +1239,6 @@ func (suite *KeeperTestSuite) TestEthCall() { req *types.EthCallRequest ) - suite.SetupTest() - priv, err := ethsecp256k1.GenerateKey() suite.Require().NoError(err) address := common.BytesToAddress(priv.PubKey().Address().Bytes()) @@ -1160,7 +1266,6 @@ func (suite *KeeperTestSuite) TestEthCall() { { "invalid args - specified both gasPrice and maxFeePerGas", func() { - args, err := json.Marshal(&types.TransactionArgs{ From: &address, Data: (*hexutil.Bytes)(&data), @@ -1176,7 +1281,6 @@ func (suite *KeeperTestSuite) TestEthCall() { { "set param EnableCreate = false", func() { - args, err := json.Marshal(&types.TransactionArgs{ From: &address, Data: (*hexutil.Bytes)(&data), @@ -1194,7 +1298,7 @@ func (suite *KeeperTestSuite) TestEthCall() { } for _, tc := range testCases { suite.Run(tc.name, func() { - + suite.SetupTest() tc.malleate() res, err := suite.queryClient.EthCall(suite.ctx, req) @@ -1210,7 +1314,6 @@ func (suite *KeeperTestSuite) TestEthCall() { } func (suite *KeeperTestSuite) TestEmptyRequest() { - suite.SetupTest() k := suite.app.EvmKeeper testCases := []struct { @@ -1281,6 +1384,7 @@ func (suite *KeeperTestSuite) TestEmptyRequest() { for _, tc := range testCases { suite.Run(fmt.Sprintf("Case %s", tc.name), func() { + suite.SetupTest() _, err := tc.queryFunc() suite.Require().Error(err) }) From 7652c0d251e889714163eb75dad2451aefcfe10e Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 21 Oct 2022 09:42:04 -0300 Subject: [PATCH 10/12] [issue-1018] add GenerateAddress in TestEthCall (grpc_query_test) --- x/evm/keeper/grpc_query_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index 33cdcff44f..d19479251d 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -18,7 +18,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/evmos/ethermint/crypto/ethsecp256k1" "github.com/evmos/ethermint/server/config" ethermint "github.com/evmos/ethermint/types" "github.com/evmos/ethermint/x/evm/types" @@ -1239,9 +1238,7 @@ func (suite *KeeperTestSuite) TestEthCall() { req *types.EthCallRequest ) - priv, err := ethsecp256k1.GenerateKey() - suite.Require().NoError(err) - address := common.BytesToAddress(priv.PubKey().Address().Bytes()) + address := tests.GenerateAddress() suite.Require().Equal(uint64(0), suite.app.EvmKeeper.GetNonce(suite.ctx, address)) supply := sdkmath.NewIntWithDecimal(1000, 18).BigInt() From c38818633f26b3573479d0de91f1f5f3a2580bbf Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 21 Oct 2022 09:53:28 -0300 Subject: [PATCH 11/12] [issue-1018] remove unnecessary SetupTest calls --- x/evm/keeper/keeper_test.go | 5 +---- x/evm/keeper/state_transition_test.go | 4 ---- x/evm/keeper/statedb_test.go | 2 +- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/x/evm/keeper/keeper_test.go b/x/evm/keeper/keeper_test.go index 22e93acfb8..95cb02b940 100644 --- a/x/evm/keeper/keeper_test.go +++ b/x/evm/keeper/keeper_test.go @@ -437,8 +437,6 @@ func (suite *KeeperTestSuite) TestBaseFee() { } func (suite *KeeperTestSuite) TestGetAccountStorage() { - suite.SetupTest() - testCases := []struct { name string malleate func() @@ -461,6 +459,7 @@ func (suite *KeeperTestSuite) TestGetAccountStorage() { for _, tc := range testCases { suite.Run(tc.name, func() { + suite.SetupTest() tc.malleate() i := 0 suite.app.AccountKeeper.IterateAccounts(suite.ctx, func(account authtypes.AccountI) bool { @@ -484,7 +483,6 @@ func (suite *KeeperTestSuite) TestGetAccountStorage() { } func (suite *KeeperTestSuite) TestGetAccountOrEmpty() { - suite.SetupTest() empty := statedb.Account{ Balance: new(big.Int), CodeHash: types.EmptyCodeHash, @@ -522,4 +520,3 @@ func (suite *KeeperTestSuite) TestGetAccountOrEmpty() { }) } } - diff --git a/x/evm/keeper/state_transition_test.go b/x/evm/keeper/state_transition_test.go index 6a5159ab32..4955e5b379 100644 --- a/x/evm/keeper/state_transition_test.go +++ b/x/evm/keeper/state_transition_test.go @@ -516,7 +516,6 @@ func (suite *KeeperTestSuite) TestResetGasMeterAndConsumeGas() { } func (suite *KeeperTestSuite) TestEVMConfig() { - suite.SetupTest() cfg, err := suite.app.EvmKeeper.EVMConfig(suite.ctx) suite.Require().NoError(err) suite.Require().Equal(types.DefaultParams(), cfg.Params) @@ -527,14 +526,12 @@ func (suite *KeeperTestSuite) TestEVMConfig() { } func (suite *KeeperTestSuite) TestContractDeployment() { - suite.SetupTest() contractAddress := suite.DeployTestContract(suite.T(), suite.address, big.NewInt(10000000000000)) db := suite.StateDB() suite.Require().Greater(db.GetCodeSize(contractAddress), 0) } func (suite *KeeperTestSuite) TestApplyMessage() { - suite.SetupTest() expectedGasUsed := params.TxGas var msg core.Message @@ -568,7 +565,6 @@ func (suite *KeeperTestSuite) TestApplyMessage() { } func (suite *KeeperTestSuite) TestApplyMessageWithConfig() { - suite.SetupTest() var ( msg core.Message err error diff --git a/x/evm/keeper/statedb_test.go b/x/evm/keeper/statedb_test.go index 694ca4058c..bd6aaa39bf 100644 --- a/x/evm/keeper/statedb_test.go +++ b/x/evm/keeper/statedb_test.go @@ -849,7 +849,6 @@ func (suite *KeeperTestSuite) _TestForEachStorage() { } func (suite *KeeperTestSuite) TestSetBalance() { - suite.SetupTest() amount := big.NewInt(-10) testCases := []struct { @@ -892,6 +891,7 @@ func (suite *KeeperTestSuite) TestSetBalance() { for _, tc := range testCases { suite.Run(tc.name, func() { + suite.SetupTest() tc.malleate() err := suite.app.EvmKeeper.SetBalance(suite.ctx, tc.addr, amount) if tc.expErr { From 1a0d7323cd5be654b0beeb0280ff8ccd57e03bbc Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 21 Oct 2022 09:57:41 -0300 Subject: [PATCH 12/12] [issue-1018] refactor SetupTest calls (grpc_query_test & statedb_test) --- x/evm/keeper/grpc_query_test.go | 1 - x/evm/keeper/statedb_test.go | 12 ++++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index d19479251d..56f055a6a3 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -1130,7 +1130,6 @@ func (suite *KeeperTestSuite) TestTraceBlock() { } func (suite *KeeperTestSuite) TestNonceInQuery() { - suite.SetupTest() address := tests.GenerateAddress() suite.Require().Equal(uint64(0), suite.app.EvmKeeper.GetNonce(suite.ctx, address)) supply := sdkmath.NewIntWithDecimal(1000, 18).BigInt() diff --git a/x/evm/keeper/statedb_test.go b/x/evm/keeper/statedb_test.go index bd6aaa39bf..1b89f48223 100644 --- a/x/evm/keeper/statedb_test.go +++ b/x/evm/keeper/statedb_test.go @@ -318,16 +318,15 @@ func (suite *KeeperTestSuite) TestSetCode() { } } - func (suite *KeeperTestSuite) TestKeeperSetCode() { addr := tests.GenerateAddress() baseAcc := &authtypes.BaseAccount{Address: sdk.AccAddress(addr.Bytes()).String()} suite.app.AccountKeeper.SetAccount(suite.ctx, baseAcc) testCases := []struct { - name string + name string codeHash []byte - code []byte + code []byte }{ { "set code", @@ -420,8 +419,6 @@ func (suite *KeeperTestSuite) TestState() { } func (suite *KeeperTestSuite) TestCommittedState() { - suite.SetupTest() - key := common.BytesToHash([]byte("key")) value1 := common.BytesToHash([]byte("value1")) value2 := common.BytesToHash([]byte("value2")) @@ -523,8 +520,6 @@ func (suite *KeeperTestSuite) TestExist() { } func (suite *KeeperTestSuite) TestEmpty() { - suite.SetupTest() - testCases := []struct { name string address common.Address @@ -543,6 +538,7 @@ func (suite *KeeperTestSuite) TestEmpty() { for _, tc := range testCases { suite.Run(tc.name, func() { + suite.SetupTest() vmdb := suite.StateDB() tc.malleate(vmdb) @@ -907,7 +903,6 @@ func (suite *KeeperTestSuite) TestSetBalance() { } func (suite *KeeperTestSuite) TestDeleteAccount() { - suite.SetupTest() supply := big.NewInt(100) contractAddr := suite.DeployTestContract(suite.T(), suite.address, supply) @@ -935,6 +930,7 @@ func (suite *KeeperTestSuite) TestDeleteAccount() { for _, tc := range testCases { suite.Run(tc.name, func() { + suite.SetupTest() err := suite.app.EvmKeeper.DeleteAccount(suite.ctx, tc.addr) if tc.expErr { suite.Require().Error(err)