Skip to content

Commit

Permalink
Problem: integration and benchmark test is not cleanup (#415)
Browse files Browse the repository at this point in the history
* Problem: integration test is not cleanup

* merge

* keeper benchmark

* params

* state transition

* statedb

* no ginkgo

* fix lint
  • Loading branch information
mmsqe authored Mar 11, 2024
1 parent 634d1a3 commit 853a1bd
Show file tree
Hide file tree
Showing 15 changed files with 296 additions and 359 deletions.
85 changes: 57 additions & 28 deletions testutil/base_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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 := &ethermint.EthAccount{
BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(suite.Address.Bytes()), nil, 0, 0),
Expand Down Expand Up @@ -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)
}

Expand All @@ -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()
Expand Down Expand Up @@ -210,26 +220,45 @@ 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 {
BaseTestSuiteWithAccount
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)
Expand Down
7 changes: 1 addition & 6 deletions x/evm/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down Expand Up @@ -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)
}
Expand Down
77 changes: 62 additions & 15 deletions x/evm/keeper/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"encoding/json"
"math/big"
"testing"

Expand All @@ -10,31 +11,77 @@ 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)
require.NoError(b, err)
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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
20 changes: 2 additions & 18 deletions x/evm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Loading

0 comments on commit 853a1bd

Please sign in to comment.