Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: no deterministic account for integration test #420

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 9 additions & 39 deletions testutil/base_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
sdk "github.com/cosmos/cosmos-sdk/types"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -73,6 +72,7 @@
type BaseTestSuiteWithAccount struct {
BaseTestSuite
Address common.Address
PrivKey *ethsecp256k1.PrivKey
Signer keyring.Signer
ConsAddress sdk.ConsAddress
ConsPubKey cryptotypes.PubKey
Expand All @@ -94,29 +94,29 @@
patch func(*app.EthermintApp, app.GenesisState) app.GenesisState,
appOptions simtestutil.AppOptionsMap,
) {
suite.setupAccount(t)
suite.SetupAccount(t)
suite.BaseTestSuite.SetupTestWithCbAndOpts(t, patch, appOptions)
suite.postSetupValidator(t)
suite.PostSetupValidator(t)
}

func (suite *BaseTestSuiteWithAccount) setupAccount(t require.TestingT) {
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)
priv := &ethsecp256k1.PrivKey{
suite.PrivKey = &ethsecp256k1.PrivKey{
Key: crypto.FromECDSA(ecdsaPriv),
}
pubKey := priv.PubKey()
pubKey := suite.PrivKey.PubKey()
suite.Address = common.BytesToAddress(pubKey.Address().Bytes())
suite.Signer = tests.NewSigner(priv)
suite.Signer = tests.NewSigner(suite.PrivKey)
// consensus key
priv, err = ethsecp256k1.GenerateKey()
priv, err := ethsecp256k1.GenerateKey()
suite.ConsPubKey = priv.PubKey()

Check warning

Code scanning / CodeQL

Missing error check Warning test

priv
may be nil at this dereference because
err
may not have been checked.
require.NoError(t, err)
suite.ConsAddress = sdk.ConsAddress(suite.ConsPubKey.Address())
}

func (suite *BaseTestSuiteWithAccount) postSetupValidator(t require.TestingT) stakingtypes.Validator {
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 All @@ -132,11 +132,6 @@
return validator
}

func (suite *BaseTestSuiteWithAccount) GenerateKey() (*ethsecp256k1.PrivKey, sdk.AccAddress) {
address, priv := tests.NewAddrKey()
return priv.(*ethsecp256k1.PrivKey), sdk.AccAddress(address.Bytes())
}

func (suite *BaseTestSuiteWithAccount) getNonce(addressBytes []byte) uint64 {
return suite.App.EvmKeeper.GetNonce(
suite.Ctx,
Expand Down Expand Up @@ -353,28 +348,3 @@
rsp, _ := suite.EvmQueryClient.Params(ctx, &types.QueryParamsRequest{})
return rsp.Params.EvmDenom
}

type FeeMarketTestSuiteWithAccountAndQueryClient struct {
BaseTestSuiteWithAccount
feemarketQueryClientTrait
}

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(t, 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())
require.NoError(t, err)
err = suite.App.StakingKeeper.SetValidatorByConsAddr(suite.Ctx, validator)
require.NoError(t, err)
suite.App.StakingKeeper.SetValidator(suite.Ctx, validator)
suite.Setup(&suite.BaseTestSuite)
}
32 changes: 14 additions & 18 deletions x/evm/keeper/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/evmos/ethermint/app"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
"github.com/evmos/ethermint/tests"
"github.com/evmos/ethermint/testutil"
evmtypes "github.com/evmos/ethermint/x/evm/types"
Expand Down Expand Up @@ -57,14 +56,14 @@ var _ = Describe("Evm", 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`
s.SetupTest(sdk.NewDec(minGasPrices), big.NewInt(baseFee))
setupTest(sdk.NewDec(minGasPrices), big.NewInt(baseFee))
})

Context("during CheckTx", func() {
DescribeTable("should accept transactions with gas Limit > 0",
func(malleate getprices) {
p := malleate()
res := s.CheckTx(s.prepareEthTx(p))
res := s.CheckTx(prepareEthTx(p))
Expect(res.IsOK()).To(Equal(true), "transaction should have succeeded", res.GetLog())
},
Entry("legacy tx", func() txParams {
Expand All @@ -77,7 +76,7 @@ var _ = Describe("Evm", func() {
DescribeTable("should not accept transactions with gas Limit > 0",
func(malleate getprices) {
p := malleate()
res := s.CheckTx(s.prepareEthTx(p))
res := s.CheckTx(prepareEthTx(p))
Expect(res.IsOK()).To(Equal(false), "transaction should have failed", res.GetLog())
},
Entry("legacy tx", func() txParams {
Expand All @@ -93,7 +92,7 @@ var _ = Describe("Evm", func() {
DescribeTable("should accept transactions with gas Limit > 0",
func(malleate getprices) {
p := malleate()
res := s.DeliverTx(s.prepareEthTx(p))
res := s.DeliverTx(prepareEthTx(p))
Expect(res.IsOK()).To(Equal(true), "transaction should have succeeded", res.GetLog())
},
Entry("legacy tx", func() txParams {
Expand All @@ -106,7 +105,7 @@ var _ = Describe("Evm", func() {
DescribeTable("should not accept transactions with gas Limit > 0",
func(malleate getprices) {
p := malleate()
res := s.DeliverTx(s.prepareEthTx(p))
res := s.DeliverTx(prepareEthTx(p))
Expect(res.IsOK()).To(Equal(false), "transaction should have failed", res.GetLog())
},
Entry("legacy tx", func() txParams {
Expand All @@ -123,11 +122,10 @@ var _ = Describe("Evm", func() {

type IntegrationTestSuite struct {
testutil.BaseTestSuiteWithAccount
privKey *ethsecp256k1.PrivKey
}

func (suite *IntegrationTestSuite) SetupTest(minGasPrice sdk.Dec, baseFee *big.Int) {
suite.BaseTestSuiteWithAccount.SetupTestWithCbAndOpts(
func setupTest(minGasPrice sdk.Dec, baseFee *big.Int) {
s.SetupTestWithCbAndOpts(
s.T(),
func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState {
feemarketGenesis := feemarkettypes.DefaultGenesisState()
Expand All @@ -138,24 +136,22 @@ func (suite *IntegrationTestSuite) SetupTest(minGasPrice sdk.Dec, baseFee *big.I
simtestutil.AppOptionsMap{server.FlagMinGasPrices: "1" + evmtypes.DefaultEVMDenom},
)
amount, ok := sdk.NewIntFromString("10000000000000000000")
suite.Require().True(ok)
s.Require().True(ok)
initBalance := sdk.Coins{sdk.Coin{
Denom: evmtypes.DefaultEVMDenom,
Amount: amount,
}}
privKey, address := suite.GenerateKey()
testutil.FundAccount(s.App.BankKeeper, s.Ctx, address, initBalance)
testutil.FundAccount(s.App.BankKeeper, s.Ctx, sdk.AccAddress(s.Address.Bytes()), initBalance)
s.Commit()
params := feemarkettypes.DefaultParams()
params.MinGasPrice = minGasPrice
suite.App.FeeMarketKeeper.SetParams(suite.Ctx, params)
suite.App.FeeMarketKeeper.SetBaseFee(suite.Ctx, baseFee)
s.App.FeeMarketKeeper.SetParams(s.Ctx, params)
s.App.FeeMarketKeeper.SetBaseFee(s.Ctx, baseFee)
s.Commit()
s.privKey = privKey
}

func (suite *IntegrationTestSuite) prepareEthTx(p txParams) []byte {
func prepareEthTx(p txParams) []byte {
to := tests.GenerateAddress()
msg := s.BuildEthTx(&to, p.gasLimit, p.gasPrice, p.gasFeeCap, p.gasTipCap, p.accesses, s.privKey)
return s.PrepareEthTx(msg, suite.privKey)
msg := s.BuildEthTx(&to, p.gasLimit, p.gasPrice, p.gasFeeCap, p.gasTipCap, p.accesses, s.PrivKey)
return s.PrepareEthTx(msg, s.PrivKey)
}
Loading
Loading