From 853a1bde978170d5d127ad24c6f6bf1b883129a8 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 11 Mar 2024 10:00:01 +0800 Subject: [PATCH] Problem: integration and benchmark test is not cleanup (#415) * Problem: integration test is not cleanup * merge * keeper benchmark * params * state transition * statedb * no ginkgo * fix lint --- testutil/base_test_suite.go | 85 +++++--- x/evm/handler_test.go | 7 +- x/evm/keeper/benchmark_test.go | 77 ++++++-- x/evm/keeper/grpc_query_test.go | 20 +- x/evm/keeper/hooks_test.go | 2 +- x/evm/keeper/integration_test.go | 182 +++++++----------- x/evm/keeper/keeper_test.go | 126 +----------- x/evm/keeper/msg_server_test.go | 8 +- x/evm/keeper/params_benchmark_test.go | 13 +- .../keeper/state_transition_benchmark_test.go | 59 ++++-- x/evm/keeper/state_transition_test.go | 4 +- x/evm/keeper/statedb_benchmark_test.go | 49 ++--- x/evm/keeper/statedb_test.go | 3 +- x/evm/keeper/utils_test.go | 3 +- x/feemarket/keeper/keeper_test.go | 17 +- 15 files changed, 296 insertions(+), 359 deletions(-) diff --git a/testutil/base_test_suite.go b/testutil/base_test_suite.go index fd471ebade..f90a97830d 100644 --- a/testutil/base_test_suite.go +++ b/testutil/base_test_suite.go @@ -5,6 +5,7 @@ import ( "math/big" "time" + abci "github.com/cometbft/cometbft/abci/types" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -63,20 +64,17 @@ type BaseTestSuiteWithAccount struct { ConsPubKey cryptotypes.PubKey } -func (suite *BaseTestSuiteWithAccount) SetupTest() { - suite.setupAccount() - suite.BaseTestSuite.SetupTest() - suite.postSetupValidator() +func (suite *BaseTestSuiteWithAccount) SetupTest(t require.TestingT) { + suite.SetupTestWithCb(t, nil) } -func (suite *BaseTestSuiteWithAccount) SetupTestWithCb(patch func(*app.EthermintApp, app.GenesisState) app.GenesisState) { - suite.setupAccount() +func (suite *BaseTestSuiteWithAccount) SetupTestWithCb(t require.TestingT, patch func(*app.EthermintApp, app.GenesisState) app.GenesisState) { + suite.setupAccount(t) suite.BaseTestSuite.SetupTestWithCb(patch) - suite.postSetupValidator() + suite.postSetupValidator(t) } -func (suite *BaseTestSuiteWithAccount) setupAccount() { - t := suite.T() +func (suite *BaseTestSuiteWithAccount) setupAccount(t require.TestingT) { // account key, use a constant account to keep unit test deterministic. ecdsaPriv, err := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") require.NoError(t, err) @@ -93,8 +91,7 @@ func (suite *BaseTestSuiteWithAccount) setupAccount() { suite.ConsAddress = sdk.ConsAddress(suite.ConsPubKey.Address()) } -func (suite *BaseTestSuiteWithAccount) postSetupValidator() stakingtypes.Validator { - t := suite.T() +func (suite *BaseTestSuiteWithAccount) postSetupValidator(t require.TestingT) stakingtypes.Validator { suite.Ctx = suite.Ctx.WithProposer(suite.ConsAddress) acc := ðermint.EthAccount{ BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(suite.Address.Bytes()), nil, 0, 0), @@ -136,7 +133,13 @@ type BaseTestSuiteWithFeeMarketQueryClient struct { } func (suite *BaseTestSuiteWithFeeMarketQueryClient) SetupTest() { - suite.BaseTestSuite.SetupTest() + suite.SetupTestWithCb(nil) +} + +func (suite *BaseTestSuiteWithFeeMarketQueryClient) SetupTestWithCb( + patch func(*app.EthermintApp, app.GenesisState) app.GenesisState, +) { + suite.BaseTestSuite.SetupTestWithCb(patch) suite.feemarketQueryClientTrait.Setup(&suite.BaseTestSuite) } @@ -145,17 +148,24 @@ type EVMTestSuiteWithAccountAndQueryClient struct { evmQueryClientTrait } -func (suite *EVMTestSuiteWithAccountAndQueryClient) SetupTestWithCb(patch func(*app.EthermintApp, app.GenesisState) app.GenesisState) { - suite.BaseTestSuiteWithAccount.SetupTestWithCb(patch) +func (suite *EVMTestSuiteWithAccountAndQueryClient) SetupTest(t require.TestingT) { + suite.SetupTestWithCb(t, nil) +} + +func (suite *EVMTestSuiteWithAccountAndQueryClient) SetupTestWithCb( + t require.TestingT, + patch func(*app.EthermintApp, app.GenesisState) app.GenesisState, +) { + suite.BaseTestSuiteWithAccount.SetupTestWithCb(t, patch) suite.evmQueryClientTrait.Setup(&suite.BaseTestSuite) } -// DeployTestContractWithT deploy a test erc20 contract and returns the contract address -func (suite *EVMTestSuiteWithAccountAndQueryClient) DeployTestContractWithT( +// DeployTestContract deploy a test erc20 contract and returns the contract address +func (suite *EVMTestSuiteWithAccountAndQueryClient) DeployTestContract( + t require.TestingT, owner common.Address, supply *big.Int, enableFeemarket bool, - t require.TestingT, ) common.Address { ctx := sdk.WrapSDKContext(suite.Ctx) chainID := suite.App.EvmKeeper.ChainID() @@ -210,12 +220,25 @@ func (suite *EVMTestSuiteWithAccountAndQueryClient) DeployTestContractWithT( return crypto.CreateAddress(suite.Address, nonce) } -func (suite *EVMTestSuiteWithAccountAndQueryClient) DeployTestContract( - owner common.Address, - supply *big.Int, - enableFeemarket bool, -) common.Address { - return suite.DeployTestContractWithT(owner, supply, enableFeemarket, suite.T()) +// Commit and begin new block +func (suite *EVMTestSuiteWithAccountAndQueryClient) Commit() { + _ = suite.App.Commit() + header := suite.Ctx.BlockHeader() + header.Height++ + suite.App.BeginBlock(abci.RequestBeginBlock{ + Header: header, + }) + // update ctx + suite.Ctx = suite.App.BaseApp.NewContext(false, header) + queryHelper := baseapp.NewQueryServerTestHelper(suite.Ctx, suite.App.InterfaceRegistry()) + types.RegisterQueryServer(queryHelper, suite.App.EvmKeeper) + suite.EvmQueryClient = types.NewQueryClient(queryHelper) +} + +func (suite *EVMTestSuiteWithAccountAndQueryClient) EvmDenom() string { + ctx := sdk.WrapSDKContext(suite.Ctx) + rsp, _ := suite.EvmQueryClient.Params(ctx, &types.QueryParamsRequest{}) + return rsp.Params.EvmDenom } type FeeMarketTestSuiteWithAccountAndQueryClient struct { @@ -223,13 +246,19 @@ type FeeMarketTestSuiteWithAccountAndQueryClient struct { feemarketQueryClientTrait } -func (suite *FeeMarketTestSuiteWithAccountAndQueryClient) SetupTest() { - suite.setupAccount() - suite.BaseTestSuite.SetupTestWithCb(nil) - validator := suite.postSetupValidator() +func (suite *FeeMarketTestSuiteWithAccountAndQueryClient) SetupTest(t require.TestingT) { + suite.SetupTestWithCb(t, nil) +} + +func (suite *FeeMarketTestSuiteWithAccountAndQueryClient) SetupTestWithCb( + t require.TestingT, + patch func(*app.EthermintApp, app.GenesisState) app.GenesisState, +) { + suite.setupAccount(t) + suite.BaseTestSuite.SetupTestWithCb(patch) + validator := suite.postSetupValidator(t) validator = stakingkeeper.TestingUpdateValidator(suite.App.StakingKeeper, suite.Ctx, validator, true) err := suite.App.StakingKeeper.Hooks().AfterValidatorCreated(suite.Ctx, validator.GetOperator()) - t := suite.T() require.NoError(t, err) err = suite.App.StakingKeeper.SetValidatorByConsAddr(suite.Ctx, validator) require.NoError(t, err) diff --git a/x/evm/handler_test.go b/x/evm/handler_test.go index 7f99335a83..1e9da1b20d 100644 --- a/x/evm/handler_test.go +++ b/x/evm/handler_test.go @@ -21,7 +21,6 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/evmos/ethermint/app" @@ -44,7 +43,7 @@ func TestHandlerTestSuite(t *testing.T) { func (suite *HandlerTestSuite) SetupTest() { t := suite.T() - suite.SetupTestWithCb(func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { + suite.SetupTestWithCb(t, func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { coins := sdk.NewCoins(sdk.NewCoin(types.DefaultEVMDenom, sdkmath.NewInt(100000000000000))) b32address := sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), suite.ConsPubKey.Address().Bytes()) balances := []banktypes.Balance{ @@ -75,10 +74,6 @@ func (suite *HandlerTestSuite) SetupTest() { genesis[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(&authGenesis) return genesis }) - - queryHelper := baseapp.NewQueryServerTestHelper(suite.Ctx, suite.App.InterfaceRegistry()) - types.RegisterQueryServer(queryHelper, suite.App.EvmKeeper) - suite.ethSigner = ethtypes.LatestSignerForChainID(suite.App.EvmKeeper.ChainID()) suite.handler = evm.NewHandler(suite.App.EvmKeeper) } diff --git a/x/evm/keeper/benchmark_test.go b/x/evm/keeper/benchmark_test.go index 73608881dd..a4c5bdd542 100644 --- a/x/evm/keeper/benchmark_test.go +++ b/x/evm/keeper/benchmark_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "encoding/json" "math/big" "testing" @@ -10,15 +11,61 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" authante "github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/evmos/ethermint/server/config" + "github.com/evmos/ethermint/testutil" ethermint "github.com/evmos/ethermint/types" "github.com/evmos/ethermint/x/evm/types" ) -func SetupContract(b *testing.B) (*KeeperTestSuite, common.Address) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) +type KeeperBenchmarkTestSuite struct { + testutil.EVMTestSuiteWithAccountAndQueryClient +} + +// deployTestMessageCall deploy a test erc20 contract and returns the contract address +func (suite *KeeperBenchmarkTestSuite) deployTestMessageCall(b *testing.B) common.Address { + ctx := sdk.WrapSDKContext(suite.Ctx) + chainID := suite.App.EvmKeeper.ChainID() + data := types.TestMessageCall.Bin + args, err := json.Marshal(&types.TransactionArgs{ + From: &suite.Address, + Data: (*hexutil.Bytes)(&data), + }) + require.NoError(b, err) + + res, err := suite.EvmQueryClient.EstimateGas(ctx, &types.EthCallRequest{ + Args: args, + GasCap: uint64(config.DefaultGasCap), + ProposerAddress: suite.Ctx.BlockHeader().ProposerAddress, + }) + require.NoError(b, err) + + nonce := suite.App.EvmKeeper.GetNonce(suite.Ctx, suite.Address) + erc20DeployTx := types.NewTxContract( + chainID, + nonce, + nil, // amount + res.Gas, // gasLimit + nil, // gasPrice + nil, nil, + data, // input + nil, // accesses + ) + erc20DeployTx.From = suite.Address.Bytes() + err = erc20DeployTx.Sign(ethtypes.LatestSignerForChainID(chainID), suite.Signer) + require.NoError(b, err) + rsp, err := suite.App.EvmKeeper.EthereumTx(ctx, erc20DeployTx) + require.NoError(b, err) + require.Empty(b, rsp.VmError) + return crypto.CreateAddress(suite.Address, nonce) +} + +func setupContract(b *testing.B) (*KeeperBenchmarkTestSuite, common.Address) { + suite := KeeperBenchmarkTestSuite{} + suite.SetupTest(b) amt := sdk.Coins{ethermint.NewPhotonCoinInt64(1000000000000000000)} err := suite.App.BankKeeper.MintCoins(suite.Ctx, types.ModuleName, amt) @@ -26,15 +73,15 @@ func SetupContract(b *testing.B) (*KeeperTestSuite, common.Address) { err = suite.App.BankKeeper.SendCoinsFromModuleToAccount(suite.Ctx, types.ModuleName, suite.Address.Bytes(), amt) require.NoError(b, err) - contractAddr := suite.deployTestContract(b, suite.Address, sdkmath.NewIntWithDecimal(1000, 18).BigInt()) + contractAddr := suite.DeployTestContract(b, suite.Address, sdkmath.NewIntWithDecimal(1000, 18).BigInt(), false) suite.Commit() return &suite, contractAddr } -func SetupTestMessageCall(b *testing.B) (*KeeperTestSuite, common.Address) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) +func setupTestMessageCall(b *testing.B) (*KeeperBenchmarkTestSuite, common.Address) { + suite := KeeperBenchmarkTestSuite{} + suite.SetupTest(b) amt := sdk.Coins{ethermint.NewPhotonCoinInt64(1000000000000000000)} err := suite.App.BankKeeper.MintCoins(suite.Ctx, types.ModuleName, amt) @@ -48,10 +95,10 @@ func SetupTestMessageCall(b *testing.B) (*KeeperTestSuite, common.Address) { return &suite, contractAddr } -type TxBuilder func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx +type TxBuilder func(suite *KeeperBenchmarkTestSuite, contract common.Address) *types.MsgEthereumTx -func DoBenchmark(b *testing.B, txBuilder TxBuilder) { - suite, contractAddr := SetupContract(b) +func doBenchmark(b *testing.B, txBuilder TxBuilder) { + suite, contractAddr := setupContract(b) msg := txBuilder(suite, contractAddr) msg.From = suite.Address.Bytes() @@ -78,7 +125,7 @@ func DoBenchmark(b *testing.B, txBuilder TxBuilder) { } func BenchmarkTokenTransfer(b *testing.B) { - DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx { + doBenchmark(b, func(suite *KeeperBenchmarkTestSuite, contract common.Address) *types.MsgEthereumTx { input, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000)) require.NoError(b, err) nonce := suite.App.EvmKeeper.GetNonce(suite.Ctx, suite.Address) @@ -87,7 +134,7 @@ func BenchmarkTokenTransfer(b *testing.B) { } func BenchmarkEmitLogs(b *testing.B) { - DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx { + doBenchmark(b, func(suite *KeeperBenchmarkTestSuite, contract common.Address) *types.MsgEthereumTx { input, err := types.ERC20Contract.ABI.Pack("benchmarkLogs", big.NewInt(1000)) require.NoError(b, err) nonce := suite.App.EvmKeeper.GetNonce(suite.Ctx, suite.Address) @@ -96,7 +143,7 @@ func BenchmarkEmitLogs(b *testing.B) { } func BenchmarkTokenTransferFrom(b *testing.B) { - DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx { + doBenchmark(b, func(suite *KeeperBenchmarkTestSuite, contract common.Address) *types.MsgEthereumTx { input, err := types.ERC20Contract.ABI.Pack("transferFrom", suite.Address, common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(0)) require.NoError(b, err) nonce := suite.App.EvmKeeper.GetNonce(suite.Ctx, suite.Address) @@ -105,7 +152,7 @@ func BenchmarkTokenTransferFrom(b *testing.B) { } func BenchmarkTokenMint(b *testing.B) { - DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx { + doBenchmark(b, func(suite *KeeperBenchmarkTestSuite, contract common.Address) *types.MsgEthereumTx { input, err := types.ERC20Contract.ABI.Pack("mint", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000)) require.NoError(b, err) nonce := suite.App.EvmKeeper.GetNonce(suite.Ctx, suite.Address) @@ -114,7 +161,7 @@ func BenchmarkTokenMint(b *testing.B) { } func BenchmarkMessageCall(b *testing.B) { - suite, contract := SetupTestMessageCall(b) + suite, contract := setupTestMessageCall(b) input, err := types.TestMessageCall.ABI.Pack("benchmarkMessageCall", big.NewInt(10000)) require.NoError(b, err) diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index 1c1e545345..461f3e9b48 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -9,7 +9,6 @@ import ( sdkmath "cosmossdk.io/math" - abci "github.com/cometbft/cometbft/abci/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ethtypes "github.com/ethereum/go-ethereum/core/types" @@ -25,7 +24,6 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/evmos/ethermint/server/config" @@ -43,7 +41,7 @@ type GRPCServerTestSuiteSuite struct { } func (suite *GRPCServerTestSuiteSuite) SetupTest() { - suite.EVMTestSuiteWithAccountAndQueryClient.SetupTestWithCb(func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { + suite.EVMTestSuiteWithAccountAndQueryClient.SetupTestWithCb(suite.T(), func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { feemarketGenesis := feemarkettypes.DefaultGenesisState() if suite.enableFeemarket { feemarketGenesis.Params.EnableHeight = 1 @@ -73,25 +71,11 @@ func TestGRPCServerTestSuite(t *testing.T) { suite.Run(t, s) } -// Commit and begin new block -func (suite *GRPCServerTestSuiteSuite) Commit() { - _ = suite.App.Commit() - header := suite.Ctx.BlockHeader() - header.Height += 1 - suite.App.BeginBlock(abci.RequestBeginBlock{ - Header: header, - }) - // update ctx - suite.Ctx = suite.App.BaseApp.NewContext(false, header) - queryHelper := baseapp.NewQueryServerTestHelper(suite.Ctx, suite.App.InterfaceRegistry()) - types.RegisterQueryServer(queryHelper, suite.App.EvmKeeper) - suite.EvmQueryClient = types.NewQueryClient(queryHelper) -} - // deployTestContract deploy a test erc20 contract and returns the contract address func (suite *GRPCServerTestSuiteSuite) deployTestContract(owner common.Address) common.Address { supply := sdkmath.NewIntWithDecimal(1000, 18).BigInt() return suite.EVMTestSuiteWithAccountAndQueryClient.DeployTestContract( + suite.T(), owner, supply, suite.enableFeemarket, diff --git a/x/evm/keeper/hooks_test.go b/x/evm/keeper/hooks_test.go index 62c52f863f..956ab1b713 100644 --- a/x/evm/keeper/hooks_test.go +++ b/x/evm/keeper/hooks_test.go @@ -70,7 +70,7 @@ func (suite *HookTestSuite) TestEvmHooks() { } for _, tc := range testCases { - suite.BaseTestSuiteWithAccount.SetupTest() + suite.SetupTest(suite.T()) hook := tc.setupHook() suite.App.EvmKeeper.SetHooks(keeper.NewMultiEvmHooks(hook)) diff --git a/x/evm/keeper/integration_test.go b/x/evm/keeper/integration_test.go index e5bf60197b..8d916da8bb 100644 --- a/x/evm/keeper/integration_test.go +++ b/x/evm/keeper/integration_test.go @@ -1,16 +1,15 @@ package keeper_test import ( - "encoding/json" "math/big" + "testing" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/stretchr/testify/suite" - "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/client/flags" + abci "github.com/cometbft/cometbft/abci/types" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" "github.com/ethereum/go-ethereum/common" @@ -20,19 +19,22 @@ import ( "github.com/evmos/ethermint/encoding" "github.com/evmos/ethermint/tests" "github.com/evmos/ethermint/testutil" - "github.com/evmos/ethermint/x/feemarket/types" - - dbm "github.com/cometbft/cometbft-db" - abci "github.com/cometbft/cometbft/abci/types" - "github.com/cometbft/cometbft/libs/log" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" evmtypes "github.com/evmos/ethermint/x/evm/types" + feemarkettypes "github.com/evmos/ethermint/x/feemarket/types" ) -var _ = Describe("Feemarket", func() { - var privKey *ethsecp256k1.PrivKey +var s *IntegrationTestSuite + +func TestEvm(t *testing.T) { + // Run Ginkgo integration tests + s = new(IntegrationTestSuite) + suite.Run(t, s) + + RegisterFailHandler(Fail) + RunSpecs(t, "IntegrationTestSuite") +} +var _ = Describe("Evm", func() { Describe("Performing EVM transactions", func() { type txParams struct { gasLimit uint64 @@ -57,7 +59,7 @@ var _ = Describe("Feemarket", func() { // 100_000`. With the fee calculation `Fee = (baseFee + tip) * gasLimit`, // a `minGasPrices = 5_000_000_000` results in `minGlobalFee = // 500_000_000_000_000` - privKey, _ = setupTestWithContext("1", sdk.NewDec(minGasPrices), sdk.NewInt(baseFee)) + s.SetupTest(sdk.NewDec(minGasPrices), big.NewInt(baseFee)) }) Context("during CheckTx", func() { @@ -65,8 +67,8 @@ var _ = Describe("Feemarket", func() { func(malleate getprices) { p := malleate() to := tests.GenerateAddress() - msgEthereumTx := buildEthTx(privKey, &to, p.gasLimit, p.gasPrice, p.gasFeeCap, p.gasTipCap, p.accesses) - res := checkEthTx(privKey, msgEthereumTx) + msgEthereumTx := s.buildEthTx(&to, p.gasLimit, p.gasPrice, p.gasFeeCap, p.gasTipCap, p.accesses) + res := s.checkEthTx(msgEthereumTx) Expect(res.IsOK()).To(Equal(true), "transaction should have succeeded", res.GetLog()) }, Entry("legacy tx", func() txParams { @@ -80,9 +82,9 @@ var _ = Describe("Feemarket", func() { func(malleate getprices) { p := malleate() to := tests.GenerateAddress() - msgEthereumTx := buildEthTx(privKey, &to, p.gasLimit, p.gasPrice, p.gasFeeCap, p.gasTipCap, p.accesses) - res := checkEthTx(privKey, msgEthereumTx) - Expect(res.IsOK()).To(Equal(false), "transaction should have succeeded", res.GetLog()) + msgEthereumTx := s.buildEthTx(&to, p.gasLimit, p.gasPrice, p.gasFeeCap, p.gasTipCap, p.accesses) + res := s.checkEthTx(msgEthereumTx) + Expect(res.IsOK()).To(Equal(false), "transaction should have failed", res.GetLog()) }, Entry("legacy tx", func() txParams { return txParams{0, big.NewInt(baseFee), nil, nil, nil} @@ -98,8 +100,8 @@ var _ = Describe("Feemarket", func() { func(malleate getprices) { p := malleate() to := tests.GenerateAddress() - msgEthereumTx := buildEthTx(privKey, &to, p.gasLimit, p.gasPrice, p.gasFeeCap, p.gasTipCap, p.accesses) - res := deliverEthTx(privKey, msgEthereumTx) + msgEthereumTx := s.buildEthTx(&to, p.gasLimit, p.gasPrice, p.gasFeeCap, p.gasTipCap, p.accesses) + res := s.deliverEthTx(msgEthereumTx) Expect(res.IsOK()).To(Equal(true), "transaction should have succeeded", res.GetLog()) }, Entry("legacy tx", func() txParams { @@ -113,9 +115,9 @@ var _ = Describe("Feemarket", func() { func(malleate getprices) { p := malleate() to := tests.GenerateAddress() - msgEthereumTx := buildEthTx(privKey, &to, p.gasLimit, p.gasPrice, p.gasFeeCap, p.gasTipCap, p.accesses) - res := checkEthTx(privKey, msgEthereumTx) - Expect(res.IsOK()).To(Equal(false), "transaction should have succeeded", res.GetLog()) + msgEthereumTx := s.buildEthTx(&to, p.gasLimit, p.gasPrice, p.gasFeeCap, p.gasTipCap, p.accesses) + res := s.checkEthTx(msgEthereumTx) + Expect(res.IsOK()).To(Equal(false), "transaction should have failed", res.GetLog()) }, Entry("legacy tx", func() txParams { return txParams{0, big.NewInt(baseFee), nil, nil, nil} @@ -129,94 +131,51 @@ var _ = Describe("Feemarket", func() { }) }) -// setupTestWithContext sets up a test chain with an example Cosmos send msg, -// given a local (validator config) and a gloabl (feemarket param) minGasPrice -func setupTestWithContext(valMinGasPrice string, minGasPrice sdk.Dec, baseFee sdk.Int) (*ethsecp256k1.PrivKey, banktypes.MsgSend) { - privKey, msg := setupTest(valMinGasPrice + s.denom) - params := types.DefaultParams() - params.MinGasPrice = minGasPrice - s.App.FeeMarketKeeper.SetParams(s.Ctx, params) - s.App.FeeMarketKeeper.SetBaseFee(s.Ctx, baseFee.BigInt()) - s.Commit() - - return privKey, msg +type IntegrationTestSuite struct { + testutil.EVMTestSuiteWithAccountAndQueryClient + ethSigner ethtypes.Signer + privKey *ethsecp256k1.PrivKey } -func setupTest(localMinGasPrices string) (*ethsecp256k1.PrivKey, banktypes.MsgSend) { - setupChain(localMinGasPrices) - - privKey, address := generateKey() +func (suite *IntegrationTestSuite) SetupTest(minGasPrice sdk.Dec, baseFee *big.Int) { + t := s.T() + suite.EVMTestSuiteWithAccountAndQueryClient.SetupTestWithCb(t, func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { + feemarketGenesis := feemarkettypes.DefaultGenesisState() + feemarketGenesis.Params.NoBaseFee = true + genesis[feemarkettypes.ModuleName] = app.AppCodec().MustMarshalJSON(feemarketGenesis) + return genesis + }) amount, ok := sdk.NewIntFromString("10000000000000000000") - s.Require().True(ok) + suite.Require().True(ok) initBalance := sdk.Coins{sdk.Coin{ - Denom: s.denom, + Denom: evmtypes.DefaultEVMDenom, Amount: amount, }} + privKey, address := suite.generateKey() testutil.FundAccount(s.App.BankKeeper, s.Ctx, address, initBalance) - - msg := banktypes.MsgSend{ - FromAddress: address.String(), - ToAddress: address.String(), - Amount: sdk.Coins{sdk.Coin{ - Denom: s.denom, - Amount: sdk.NewInt(10000), - }}, - } s.Commit() - return privKey, msg -} - -func setupChain(localMinGasPricesStr string) { - // Initialize the app, so we can use SetMinGasPrices to set the - // validator-specific min-gas-prices setting - db := dbm.NewMemDB() - appOptions := make(simtestutil.AppOptionsMap, 0) - appOptions[server.FlagInvCheckPeriod] = 5 - appOptions[flags.FlagHome] = app.DefaultNodeHome - newapp := app.NewEthermintApp( - log.NewNopLogger(), - db, - nil, - true, - appOptions, - baseapp.SetMinGasPrices(localMinGasPricesStr), - baseapp.SetChainID(app.ChainID), - ) - - genesisState := app.NewTestGenesisState(newapp.AppCodec()) - genesisState[types.ModuleName] = newapp.AppCodec().MustMarshalJSON(types.DefaultGenesisState()) - - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - s.Require().NoError(err) - - // Initialize the chain - newapp.InitChain( - abci.RequestInitChain{ - ChainId: app.ChainID, - Validators: []abci.ValidatorUpdate{}, - AppStateBytes: stateBytes, - ConsensusParams: app.DefaultConsensusParams, - }, - ) - - s.App = newapp - s.SetupTestWithT(s.T()) + params := feemarkettypes.DefaultParams() + params.MinGasPrice = minGasPrice + suite.App.FeeMarketKeeper.SetParams(suite.Ctx, params) + suite.App.FeeMarketKeeper.SetBaseFee(suite.Ctx, baseFee) + s.Commit() + suite.ethSigner = ethtypes.LatestSignerForChainID(suite.App.EvmKeeper.ChainID()) + suite.privKey = privKey } -func generateKey() (*ethsecp256k1.PrivKey, sdk.AccAddress) { +func (suite *IntegrationTestSuite) generateKey() (*ethsecp256k1.PrivKey, sdk.AccAddress) { address, priv := tests.NewAddrKey() return priv.(*ethsecp256k1.PrivKey), sdk.AccAddress(address.Bytes()) } -func getNonce(addressBytes []byte) uint64 { - return s.App.EvmKeeper.GetNonce( - s.Ctx, +func (suite *IntegrationTestSuite) getNonce(addressBytes []byte) uint64 { + return suite.App.EvmKeeper.GetNonce( + suite.Ctx, common.BytesToAddress(addressBytes), ) } -func buildEthTx( - priv *ethsecp256k1.PrivKey, +func (suite *IntegrationTestSuite) buildEthTx( to *common.Address, gasLimit uint64, gasPrice *big.Int, @@ -224,9 +183,10 @@ func buildEthTx( gasTipCap *big.Int, accesses *ethtypes.AccessList, ) *evmtypes.MsgEthereumTx { - chainID := s.App.EvmKeeper.ChainID() - from := common.BytesToAddress(priv.PubKey().Address().Bytes()) - nonce := getNonce(from.Bytes()) + chainID := suite.App.EvmKeeper.ChainID() + adr := suite.privKey.PubKey().Address() + from := common.BytesToAddress(adr.Bytes()) + nonce := suite.getNonce(from.Bytes()) data := make([]byte, 0) msgEthereumTx := evmtypes.NewTx( chainID, @@ -244,46 +204,46 @@ func buildEthTx( return msgEthereumTx } -func prepareEthTx(priv *ethsecp256k1.PrivKey, msgEthereumTx *evmtypes.MsgEthereumTx) []byte { +func (suite *IntegrationTestSuite) prepareEthTx(msgEthereumTx *evmtypes.MsgEthereumTx) []byte { encodingConfig := encoding.MakeConfig(app.ModuleBasics) option, err := codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{}) - s.Require().NoError(err) + suite.Require().NoError(err) txBuilder := encodingConfig.TxConfig.NewTxBuilder() builder, ok := txBuilder.(authtx.ExtensionOptionsTxBuilder) - s.Require().True(ok) + suite.Require().True(ok) builder.SetExtensionOptions(option) - err = msgEthereumTx.Sign(s.ethSigner, tests.NewSigner(priv)) - s.Require().NoError(err) + err = msgEthereumTx.Sign(suite.ethSigner, tests.NewSigner(suite.privKey)) + suite.Require().NoError(err) err = txBuilder.SetMsgs(msgEthereumTx) - s.Require().NoError(err) + suite.Require().NoError(err) txData, err := evmtypes.UnpackTxData(msgEthereumTx.Data) - s.Require().NoError(err) + suite.Require().NoError(err) - evmDenom := s.App.EvmKeeper.GetParams(s.Ctx).EvmDenom + evmDenom := suite.App.EvmKeeper.GetParams(suite.Ctx).EvmDenom fees := sdk.Coins{{Denom: evmDenom, Amount: sdk.NewIntFromBigInt(txData.Fee())}} builder.SetFeeAmount(fees) builder.SetGasLimit(msgEthereumTx.GetGas()) // bz are bytes to be broadcasted over the network bz, err := encodingConfig.TxConfig.TxEncoder()(txBuilder.GetTx()) - s.Require().NoError(err) + suite.Require().NoError(err) return bz } -func checkEthTx(priv *ethsecp256k1.PrivKey, msgEthereumTx *evmtypes.MsgEthereumTx) abci.ResponseCheckTx { - bz := prepareEthTx(priv, msgEthereumTx) +func (suite *IntegrationTestSuite) checkEthTx(msgEthereumTx *evmtypes.MsgEthereumTx) abci.ResponseCheckTx { + bz := suite.prepareEthTx(msgEthereumTx) req := abci.RequestCheckTx{Tx: bz} - res := s.App.BaseApp.CheckTx(req) + res := suite.App.BaseApp.CheckTx(req) return res } -func deliverEthTx(priv *ethsecp256k1.PrivKey, msgEthereumTx *evmtypes.MsgEthereumTx) abci.ResponseDeliverTx { - bz := prepareEthTx(priv, msgEthereumTx) +func (suite *IntegrationTestSuite) deliverEthTx(msgEthereumTx *evmtypes.MsgEthereumTx) abci.ResponseDeliverTx { + bz := suite.prepareEthTx(msgEthereumTx) req := abci.RequestDeliverTx{Tx: bz} res := s.App.BaseApp.DeliverTx(req) return res diff --git a/x/evm/keeper/keeper_test.go b/x/evm/keeper/keeper_test.go index 1b4658ac62..79bbe58dcb 100644 --- a/x/evm/keeper/keeper_test.go +++ b/x/evm/keeper/keeper_test.go @@ -2,37 +2,26 @@ package keeper_test import ( _ "embed" - "encoding/json" "math" "math/big" - "os" "testing" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - sdkmath "cosmossdk.io/math" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" - sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/evmos/ethermint/testutil" feemarkettypes "github.com/evmos/ethermint/x/feemarket/types" "github.com/evmos/ethermint/app" "github.com/evmos/ethermint/encoding" - "github.com/evmos/ethermint/server/config" ethermint "github.com/evmos/ethermint/types" "github.com/evmos/ethermint/x/evm/statedb" "github.com/evmos/ethermint/x/evm/types" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" ethtypes "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" abci "github.com/cometbft/cometbft/abci/types" ) @@ -48,25 +37,15 @@ type KeeperTestSuite struct { denom string } -var s *KeeperTestSuite - func TestKeeperTestSuite(t *testing.T) { - if os.Getenv("benchmark") != "" { - t.Skip("Skipping Gingko Test") - } - s = new(KeeperTestSuite) + s := new(KeeperTestSuite) s.enableFeemarket = false s.enableLondonHF = true suite.Run(t, s) - - // Run Ginkgo integration tests - RegisterFailHandler(Fail) - RunSpecs(t, "Keeper Suite") } -// SetupApp setup test environment, it uses`require.TestingT` to support both `testing.T` and `testing.B`. -func (suite *KeeperTestSuite) SetupTestWithT(t require.TestingT) { - suite.EVMTestSuiteWithAccountAndQueryClient.SetupTestWithCb(func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { +func (suite *KeeperTestSuite) SetupTest() { + suite.EVMTestSuiteWithAccountAndQueryClient.SetupTestWithCb(suite.T(), func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { feemarketGenesis := feemarkettypes.DefaultGenesisState() if suite.enableFeemarket { feemarketGenesis.Params.EnableHeight = 1 @@ -94,95 +73,6 @@ func (suite *KeeperTestSuite) SetupTestWithT(t require.TestingT) { suite.denom = types.DefaultEVMDenom } -func (suite *KeeperTestSuite) EvmDenom() string { - ctx := sdk.WrapSDKContext(suite.Ctx) - rsp, _ := suite.EvmQueryClient.Params(ctx, &types.QueryParamsRequest{}) - return rsp.Params.EvmDenom -} - -// Commit and begin new block -func (suite *KeeperTestSuite) Commit() { - _ = suite.App.Commit() - header := suite.Ctx.BlockHeader() - header.Height += 1 - suite.App.BeginBlock(abci.RequestBeginBlock{ - Header: header, - }) - - // update ctx - suite.Ctx = suite.App.BaseApp.NewContext(false, header) - - queryHelper := baseapp.NewQueryServerTestHelper(suite.Ctx, suite.App.InterfaceRegistry()) - types.RegisterQueryServer(queryHelper, suite.App.EvmKeeper) - suite.EvmQueryClient = types.NewQueryClient(queryHelper) -} - -// deployTestContract deploy a test erc20 contract and returns the contract address -func (suite *KeeperTestSuite) deployTestContract(t require.TestingT, owner common.Address, supply *big.Int) common.Address { - return suite.EVMTestSuiteWithAccountAndQueryClient.DeployTestContractWithT( - owner, - supply, - suite.enableFeemarket, - t, - ) -} - -// deployTestMessageCall deploy a test erc20 contract and returns the contract address -func (suite *KeeperTestSuite) deployTestMessageCall(t require.TestingT) common.Address { - ctx := sdk.WrapSDKContext(suite.Ctx) - chainID := suite.App.EvmKeeper.ChainID() - - data := types.TestMessageCall.Bin - args, err := json.Marshal(&types.TransactionArgs{ - From: &suite.Address, - Data: (*hexutil.Bytes)(&data), - }) - require.NoError(t, err) - - res, err := suite.EvmQueryClient.EstimateGas(ctx, &types.EthCallRequest{ - Args: args, - GasCap: uint64(config.DefaultGasCap), - ProposerAddress: suite.Ctx.BlockHeader().ProposerAddress, - }) - require.NoError(t, err) - - nonce := suite.App.EvmKeeper.GetNonce(suite.Ctx, suite.Address) - - var erc20DeployTx *types.MsgEthereumTx - if suite.enableFeemarket { - erc20DeployTx = types.NewTxContract( - chainID, - nonce, - nil, // amount - res.Gas, // gasLimit - nil, // gasPrice - suite.App.FeeMarketKeeper.GetBaseFee(suite.Ctx), - big.NewInt(1), - data, // input - ðtypes.AccessList{}, // accesses - ) - } else { - erc20DeployTx = types.NewTxContract( - chainID, - nonce, - nil, // amount - res.Gas, // gasLimit - nil, // gasPrice - nil, nil, - data, // input - nil, // accesses - ) - } - - erc20DeployTx.From = suite.Address.Bytes() - err = erc20DeployTx.Sign(ethtypes.LatestSignerForChainID(chainID), suite.Signer) - require.NoError(t, err) - rsp, err := suite.App.EvmKeeper.EthereumTx(ctx, erc20DeployTx) - require.NoError(t, err) - require.Empty(t, rsp.VmError) - return crypto.CreateAddress(suite.Address, nonce) -} - func (suite *KeeperTestSuite) TestBaseFee() { testCases := []struct { name string @@ -200,7 +90,7 @@ func (suite *KeeperTestSuite) TestBaseFee() { suite.Run(tc.name, func() { suite.enableFeemarket = tc.enableFeemarket suite.enableLondonHF = tc.enableLondonHF - suite.SetupTestWithT(suite.T()) + suite.SetupTest() suite.App.EvmKeeper.BeginBlock(suite.Ctx, abci.RequestBeginBlock{}) params := suite.App.EvmKeeper.GetParams(suite.Ctx) ethCfg := params.ChainConfig.EthereumConfig(suite.App.EvmKeeper.ChainID()) @@ -227,7 +117,7 @@ func (suite *KeeperTestSuite) TestGetAccountStorage() { "Two accounts - one contract (with storage), one wallet", func() { supply := big.NewInt(100) - suite.deployTestContract(suite.T(), suite.Address, supply) + suite.DeployTestContract(suite.T(), suite.Address, supply, suite.enableFeemarket) }, []int{2, 0}, }, @@ -235,7 +125,7 @@ func (suite *KeeperTestSuite) TestGetAccountStorage() { for _, tc := range testCases { suite.Run(tc.name, func() { - suite.SetupTestWithT(suite.T()) + suite.SetupTest() tc.malleate() i := 0 suite.App.AccountKeeper.IterateAccounts(suite.Ctx, func(account authtypes.AccountI) bool { @@ -278,7 +168,7 @@ func (suite *KeeperTestSuite) TestGetAccountOrEmpty() { { "existing contract account", func() common.Address { - return suite.deployTestContract(suite.T(), suite.Address, supply) + return suite.DeployTestContract(suite.T(), suite.Address, supply, suite.enableFeemarket) }, false, }, @@ -286,7 +176,7 @@ func (suite *KeeperTestSuite) TestGetAccountOrEmpty() { for _, tc := range testCases { suite.Run(tc.name, func() { - suite.SetupTestWithT(suite.T()) + suite.SetupTest() res := suite.App.EvmKeeper.GetAccountOrEmpty(suite.Ctx, tc.addr()) if tc.expEmpty { suite.Require().Equal(empty, res) diff --git a/x/evm/keeper/msg_server_test.go b/x/evm/keeper/msg_server_test.go index 8c7ef00091..4834faad2c 100644 --- a/x/evm/keeper/msg_server_test.go +++ b/x/evm/keeper/msg_server_test.go @@ -20,10 +20,6 @@ type MsgServerTestSuite struct { testutil.BaseTestSuiteWithAccount } -func (suite *MsgServerTestSuite) SetupTest() { - suite.BaseTestSuiteWithAccount.SetupTest() -} - func TestMsgServerTestSuite(t *testing.T) { suite.Run(t, new(MsgServerTestSuite)) } @@ -80,7 +76,7 @@ func (suite *MsgServerTestSuite) TestEthereumTx() { for _, tc := range testCases { suite.Run(tc.name, func() { - suite.SetupTest() + suite.SetupTest(suite.T()) keeperParams := suite.App.EvmKeeper.GetParams(suite.Ctx) chainCfg = keeperParams.ChainConfig.EthereumConfig(suite.App.EvmKeeper.ChainID()) signer = ethtypes.LatestSignerForChainID(suite.App.EvmKeeper.ChainID()) @@ -122,7 +118,7 @@ func (suite *MsgServerTestSuite) TestUpdateParams() { for _, tc := range testCases { suite.Run("MsgUpdateParams", func() { - suite.SetupTest() + suite.SetupTest(suite.T()) _, err := suite.App.EvmKeeper.UpdateParams(suite.Ctx, tc.request) if tc.expectErr { suite.Require().Error(err) diff --git a/x/evm/keeper/params_benchmark_test.go b/x/evm/keeper/params_benchmark_test.go index 8100eb9669..4257236129 100644 --- a/x/evm/keeper/params_benchmark_test.go +++ b/x/evm/keeper/params_benchmark_test.go @@ -3,12 +3,17 @@ package keeper_test import ( "testing" + "github.com/evmos/ethermint/testutil" "github.com/evmos/ethermint/x/evm/types" ) +type ParamsBenchmarkTestSuite struct { + testutil.BaseTestSuite +} + func BenchmarkSetParams(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := ParamsBenchmarkTestSuite{} + suite.SetupTest() params := types.DefaultParams() b.ReportAllocs() @@ -19,8 +24,8 @@ func BenchmarkSetParams(b *testing.B) { } func BenchmarkGetParams(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := ParamsBenchmarkTestSuite{} + suite.SetupTest() b.ReportAllocs() b.ResetTimer() diff --git a/x/evm/keeper/state_transition_benchmark_test.go b/x/evm/keeper/state_transition_benchmark_test.go index de4bd646f1..7c586318ee 100644 --- a/x/evm/keeper/state_transition_benchmark_test.go +++ b/x/evm/keeper/state_transition_benchmark_test.go @@ -2,19 +2,54 @@ package keeper_test import ( "errors" + "math" "math/big" "testing" + sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/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/app" + "github.com/evmos/ethermint/testutil" evmtypes "github.com/evmos/ethermint/x/evm/types" + feemarkettypes "github.com/evmos/ethermint/x/feemarket/types" "github.com/stretchr/testify/require" ) +type StateTransitionBenchmarkTestSuite struct { + testutil.BaseTestSuiteWithAccount + enableFeemarket bool + enableLondonHF bool +} + +func (suite *StateTransitionBenchmarkTestSuite) SetupTest(b *testing.B) { + suite.BaseTestSuiteWithAccount.SetupTestWithCb(b, func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { + feemarketGenesis := feemarkettypes.DefaultGenesisState() + if suite.enableFeemarket { + feemarketGenesis.Params.EnableHeight = 1 + feemarketGenesis.Params.NoBaseFee = false + } else { + feemarketGenesis.Params.NoBaseFee = true + } + genesis[feemarkettypes.ModuleName] = app.AppCodec().MustMarshalJSON(feemarketGenesis) + if !suite.enableLondonHF { + evmGenesis := evmtypes.DefaultGenesisState() + maxInt := sdkmath.NewInt(math.MaxInt64) + evmGenesis.Params.ChainConfig.LondonBlock = &maxInt + evmGenesis.Params.ChainConfig.ArrowGlacierBlock = &maxInt + evmGenesis.Params.ChainConfig.GrayGlacierBlock = &maxInt + evmGenesis.Params.ChainConfig.MergeNetsplitBlock = &maxInt + evmGenesis.Params.ChainConfig.ShanghaiTime = &maxInt + genesis[evmtypes.ModuleName] = app.AppCodec().MustMarshalJSON(evmGenesis) + } + return genesis + }) +} + var templateAccessListTx = ðtypes.AccessListTx{ GasPrice: big.NewInt(1), Gas: 21000, @@ -158,8 +193,8 @@ func newNativeMessage( } func BenchmarkApplyTransaction(b *testing.B) { - suite := KeeperTestSuite{enableLondonHF: true} - suite.SetupTestWithT(b) + suite := StateTransitionBenchmarkTestSuite{enableLondonHF: true} + suite.SetupTest(b) ethSigner := ethtypes.LatestSignerForChainID(suite.App.EvmKeeper.ChainID()) @@ -185,8 +220,8 @@ func BenchmarkApplyTransaction(b *testing.B) { } func BenchmarkApplyTransactionWithLegacyTx(b *testing.B) { - suite := KeeperTestSuite{enableLondonHF: true} - suite.SetupTestWithT(b) + suite := StateTransitionBenchmarkTestSuite{enableLondonHF: true} + suite.SetupTest(b) ethSigner := ethtypes.LatestSignerForChainID(suite.App.EvmKeeper.ChainID()) @@ -212,8 +247,8 @@ func BenchmarkApplyTransactionWithLegacyTx(b *testing.B) { } func BenchmarkApplyTransactionWithDynamicFeeTx(b *testing.B) { - suite := KeeperTestSuite{enableFeemarket: true, enableLondonHF: true} - suite.SetupTestWithT(b) + suite := StateTransitionBenchmarkTestSuite{enableFeemarket: true, enableLondonHF: true} + suite.SetupTest(b) ethSigner := ethtypes.LatestSignerForChainID(suite.App.EvmKeeper.ChainID()) @@ -239,8 +274,8 @@ func BenchmarkApplyTransactionWithDynamicFeeTx(b *testing.B) { } func BenchmarkApplyMessage(b *testing.B) { - suite := KeeperTestSuite{enableLondonHF: true} - suite.SetupTestWithT(b) + suite := StateTransitionBenchmarkTestSuite{enableLondonHF: true} + suite.SetupTest(b) params := suite.App.EvmKeeper.GetParams(suite.Ctx) ethCfg := params.ChainConfig.EthereumConfig(suite.App.EvmKeeper.ChainID()) @@ -274,8 +309,8 @@ func BenchmarkApplyMessage(b *testing.B) { } func BenchmarkApplyMessageWithLegacyTx(b *testing.B) { - suite := KeeperTestSuite{enableLondonHF: true} - suite.SetupTestWithT(b) + suite := StateTransitionBenchmarkTestSuite{enableLondonHF: true} + suite.SetupTest(b) params := suite.App.EvmKeeper.GetParams(suite.Ctx) ethCfg := params.ChainConfig.EthereumConfig(suite.App.EvmKeeper.ChainID()) @@ -309,8 +344,8 @@ func BenchmarkApplyMessageWithLegacyTx(b *testing.B) { } func BenchmarkApplyMessageWithDynamicFeeTx(b *testing.B) { - suite := KeeperTestSuite{enableFeemarket: true, enableLondonHF: true} - suite.SetupTestWithT(b) + suite := StateTransitionBenchmarkTestSuite{enableFeemarket: true, enableLondonHF: true} + suite.SetupTest(b) params := suite.App.EvmKeeper.GetParams(suite.Ctx) ethCfg := params.ChainConfig.EthereumConfig(suite.App.EvmKeeper.ChainID()) diff --git a/x/evm/keeper/state_transition_test.go b/x/evm/keeper/state_transition_test.go index 673834f445..b9818f243e 100644 --- a/x/evm/keeper/state_transition_test.go +++ b/x/evm/keeper/state_transition_test.go @@ -40,7 +40,7 @@ type StateTransitionTestSuite struct { func (suite *StateTransitionTestSuite) SetupTest() { t := suite.T() - suite.EVMTestSuiteWithAccountAndQueryClient.SetupTestWithCb(func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { + suite.EVMTestSuiteWithAccountAndQueryClient.SetupTestWithCb(t, func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { feemarketGenesis := feemarkettypes.DefaultGenesisState() feemarketGenesis.Params.NoBaseFee = true genesis[feemarkettypes.ModuleName] = app.AppCodec().MustMarshalJSON(feemarketGenesis) @@ -214,7 +214,6 @@ func (suite *StateTransitionTestSuite) TestGetCoinbaseAddress() { for _, tc := range testCases { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset - tc.malleate() proposerAddress := suite.Ctx.BlockHeader().ProposerAddress coinbase, err := suite.App.EvmKeeper.GetCoinbaseAddress(suite.Ctx, sdk.ConsAddress(proposerAddress)) @@ -593,6 +592,7 @@ func (suite *StateTransitionTestSuite) TestEVMConfig() { func (suite *StateTransitionTestSuite) TestContractDeployment() { contractAddress := suite.EVMTestSuiteWithAccountAndQueryClient.DeployTestContract( + suite.T(), suite.Address, big.NewInt(10000000000000), false, diff --git a/x/evm/keeper/statedb_benchmark_test.go b/x/evm/keeper/statedb_benchmark_test.go index 44c707bf30..3213271b89 100644 --- a/x/evm/keeper/statedb_benchmark_test.go +++ b/x/evm/keeper/statedb_benchmark_test.go @@ -11,11 +11,16 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/evmos/ethermint/tests" + "github.com/evmos/ethermint/testutil" ) +type StatedbBenchmarkTestSuite struct { + testutil.BaseTestSuiteWithAccount +} + func BenchmarkCreateAccountNew(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() b.ResetTimer() @@ -30,8 +35,8 @@ func BenchmarkCreateAccountNew(b *testing.B) { } func BenchmarkCreateAccountExisting(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() b.ResetTimer() @@ -43,8 +48,8 @@ func BenchmarkCreateAccountExisting(b *testing.B) { } func BenchmarkAddBalance(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() amt := big.NewInt(10) @@ -58,8 +63,8 @@ func BenchmarkAddBalance(b *testing.B) { } func BenchmarkSetCode(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() hash := crypto.Keccak256Hash([]byte("code")).Bytes() @@ -73,8 +78,8 @@ func BenchmarkSetCode(b *testing.B) { } func BenchmarkSetState(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() hash := crypto.Keccak256Hash([]byte("topic")).Bytes() @@ -88,8 +93,8 @@ func BenchmarkSetState(b *testing.B) { } func BenchmarkAddLog(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() topic := crypto.Keccak256Hash([]byte("topic")) @@ -115,8 +120,8 @@ func BenchmarkAddLog(b *testing.B) { } func BenchmarkSnapshot(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() b.ResetTimer() @@ -135,8 +140,8 @@ func BenchmarkSnapshot(b *testing.B) { } func BenchmarkSubBalance(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() amt := big.NewInt(10) @@ -150,8 +155,8 @@ func BenchmarkSubBalance(b *testing.B) { } func BenchmarkSetNonce(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() b.ResetTimer() @@ -163,8 +168,8 @@ func BenchmarkSetNonce(b *testing.B) { } func BenchmarkAddRefund(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() b.ResetTimer() @@ -176,8 +181,8 @@ func BenchmarkAddRefund(b *testing.B) { } func BenchmarkSuicide(b *testing.B) { - suite := KeeperTestSuite{} - suite.SetupTestWithT(b) + suite := StatedbBenchmarkTestSuite{} + suite.SetupTest(b) vmdb := suite.StateDB() b.ResetTimer() diff --git a/x/evm/keeper/statedb_test.go b/x/evm/keeper/statedb_test.go index fcee73af62..4312de6443 100644 --- a/x/evm/keeper/statedb_test.go +++ b/x/evm/keeper/statedb_test.go @@ -40,7 +40,7 @@ func TestStateDBTestSuite(t *testing.T) { } func (suite *StateDBTestSuite) SetupTest() { - suite.EVMTestSuiteWithAccountAndQueryClient.SetupTestWithCb(nil) + suite.EVMTestSuiteWithAccountAndQueryClient.SetupTest(suite.T()) encodingConfig := encoding.MakeConfig(app.ModuleBasics) suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig) suite.ethSigner = ethtypes.LatestSignerForChainID(suite.App.EvmKeeper.ChainID()) @@ -899,6 +899,7 @@ func (suite *StateDBTestSuite) TestDeleteAccount() { "remove deployed contract", func() common.Address { return suite.EVMTestSuiteWithAccountAndQueryClient.DeployTestContract( + suite.T(), suite.Address, supply, false, diff --git a/x/evm/keeper/utils_test.go b/x/evm/keeper/utils_test.go index 6011178e2a..677a073af2 100644 --- a/x/evm/keeper/utils_test.go +++ b/x/evm/keeper/utils_test.go @@ -29,7 +29,8 @@ func TestUtilsTestSuite(t *testing.T) { } func (suite *UtilsTestSuite) SetupTest() { - suite.BaseTestSuiteWithAccount.SetupTestWithCb(func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { + t := suite.T() + suite.BaseTestSuiteWithAccount.SetupTestWithCb(t, func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState { feemarketGenesis := feemarkettypes.DefaultGenesisState() if suite.enableFeemarket { feemarketGenesis.Params.EnableHeight = 1 diff --git a/x/feemarket/keeper/keeper_test.go b/x/feemarket/keeper/keeper_test.go index 389b7dfc66..3dca154f50 100644 --- a/x/feemarket/keeper/keeper_test.go +++ b/x/feemarket/keeper/keeper_test.go @@ -6,9 +6,6 @@ import ( "testing" "time" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "github.com/stretchr/testify/suite" "github.com/cosmos/cosmos-sdk/baseapp" @@ -40,15 +37,11 @@ var s *KeeperTestSuite func TestKeeperTestSuite(t *testing.T) { s = new(KeeperTestSuite) suite.Run(t, s) - - // Run Ginkgo integration tests - RegisterFailHandler(Fail) - RunSpecs(t, "Keeper Suite") } // SetupTest setup test environment, it uses`require.TestingT` to support both `testing.T` and `testing.B`. func (suite *KeeperTestSuite) SetupTest() { - suite.FeeMarketTestSuiteWithAccountAndQueryClient.SetupTest() + suite.FeeMarketTestSuiteWithAccountAndQueryClient.SetupTest(suite.T()) encodingConfig := encoding.MakeConfig(app.ModuleBasics) suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig) suite.ethSigner = ethtypes.LatestSignerForChainID(suite.App.EvmKeeper.ChainID()) @@ -57,17 +50,13 @@ func (suite *KeeperTestSuite) SetupTest() { // Commit commits and starts a new block with an updated context. func (suite *KeeperTestSuite) Commit() { - suite.CommitAfter(time.Second * 0) -} - -// Commit commits a block at a given time. -func (suite *KeeperTestSuite) CommitAfter(t time.Duration) { + jumpTime := time.Second * 0 header := suite.Ctx.BlockHeader() suite.App.EndBlock(abci.RequestEndBlock{Height: header.Height}) _ = suite.App.Commit() header.Height += 1 - header.Time = header.Time.Add(t) + header.Time = header.Time.Add(jumpTime) suite.App.BeginBlock(abci.RequestBeginBlock{ Header: header, })