From ae7cc272d9739103b8b3aea2d475641b26f816f2 Mon Sep 17 00:00:00 2001 From: huangyi Date: Thu, 15 Aug 2024 11:10:36 +0800 Subject: [PATCH] cleanup test --- app/ante/eth_test.go | 81 ++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 59 deletions(-) diff --git a/app/ante/eth_test.go b/app/ante/eth_test.go index 624e019a04..186cdec8f8 100644 --- a/app/ante/eth_test.go +++ b/app/ante/eth_test.go @@ -28,22 +28,20 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() { var vmdb *statedb.StateDB testCases := []struct { - name string - tx sdk.Tx - malleate func() - checkTx bool - checkAccount bool - expPass bool + name string + tx sdk.Tx + malleate func() + checkTx bool + expPass bool }{ - {"not CheckTx", nil, func() {}, false, false, true}, - {"invalid transaction type", &invalidTx{}, func() {}, true, false, false}, + {"not CheckTx", nil, func() {}, false, true}, + {"invalid transaction type", &invalidTx{}, func() {}, true, false}, { "sender not set to msg", evmtypes.NewTxContract(suite.app.EvmKeeper.ChainID(), 1, big.NewInt(10), 1000, big.NewInt(1), nil, nil, nil, nil), func() {}, true, false, - false, }, { "sender not EOA", @@ -53,7 +51,6 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() { vmdb.SetCode(addr, []byte("1")) }, true, - true, false, }, { @@ -64,7 +61,6 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() { vmdb.SetCode(addr, nil) }, true, - true, false, }, { @@ -75,7 +71,6 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() { }, true, true, - true, }, { "success existing account", @@ -88,7 +83,6 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() { }, true, true, - true, }, } @@ -97,11 +91,10 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() { vmdb = suite.StateDB() tc.malleate() suite.Require().NoError(vmdb.Commit()) - accounts := map[string]sdk.AccountI{} + accountGetter := ante.NewAccountGetter(suite.ctx, suite.app.AccountKeeper) err := ante.VerifyEthAccount(suite.ctx.WithIsCheckTx(tc.checkTx), tc.tx, suite.app.EvmKeeper, evmtypes.DefaultEVMDenom, accountGetter) - _, ok := accounts[sdk.AccAddress(addr.Bytes()).String()] - suite.Require().Equal(tc.checkAccount, ok) + if tc.expPass { suite.Require().NoError(err) } else { @@ -122,17 +115,17 @@ func (suite *AnteTestSuite) TestEthNonceVerificationDecorator() { testCases := []struct { name string tx sdk.Tx - malleate func(accs map[string]sdk.AccountI) + malleate func() reCheckTx bool expPass bool }{ - {"ReCheckTx", &invalidTx{}, func(_ map[string]sdk.AccountI) {}, true, false}, - {"invalid transaction type", &invalidTx{}, func(_ map[string]sdk.AccountI) {}, false, false}, - {"sender account not found", tx, func(_ map[string]sdk.AccountI) {}, false, false}, + {"ReCheckTx", &invalidTx{}, func() {}, true, false}, + {"invalid transaction type", &invalidTx{}, func() {}, false, false}, + {"sender account not found", tx, func() {}, false, false}, { "sender nonce missmatch", tx, - func(_ map[string]sdk.AccountI) { + func() { acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes()) suite.app.AccountKeeper.SetAccount(suite.ctx, acc) }, @@ -142,7 +135,7 @@ func (suite *AnteTestSuite) TestEthNonceVerificationDecorator() { { "success", tx, - func(_ map[string]sdk.AccountI) { + func() { acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes()) suite.Require().NoError(acc.SetSequence(1)) suite.app.AccountKeeper.SetAccount(suite.ctx, acc) @@ -150,34 +143,13 @@ func (suite *AnteTestSuite) TestEthNonceVerificationDecorator() { false, true, }, - { - "success with accounts instead of SetAccount from keeper", - tx, - func(accounts map[string]sdk.AccountI) { - acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes()) - suite.Require().NoError(acc.SetSequence(1)) - accounts[string(addr.Bytes())] = acc - }, - false, - true, - }, } for _, tc := range testCases { suite.Run(tc.name, func() { - accounts := map[string]sdk.AccountI{} - getAccount := func(addr sdk.AccAddress, load func() sdk.AccountI) sdk.AccountI { - acc := accounts[string(addr)] - if acc == nil { - acc = load() - if acc != nil { - accounts[string(addr)] = acc - } - } - return acc - } - tc.malleate(accounts) - err := ante.CheckAndSetEthSenderNonce(suite.ctx.WithIsReCheckTx(tc.reCheckTx), tc.tx, suite.app.AccountKeeper, false, getAccount) + tc.malleate() + accountGetter := ante.NewAccountGetter(suite.ctx, suite.app.AccountKeeper) + err := ante.CheckAndSetEthSenderNonce(suite.ctx.WithIsReCheckTx(tc.reCheckTx), tc.tx, suite.app.AccountKeeper, false, accountGetter) if tc.expPass { suite.Require().NoError(err) @@ -566,25 +538,16 @@ func (suite *AnteTestSuite) TestEthIncrementSenderSequenceDecorator() { for _, tc := range testCases { suite.Run(tc.name, func() { tc.malleate() - accounts := map[string]sdk.AccountI{} - getAccount := func(addr sdk.AccAddress, load func() sdk.AccountI) sdk.AccountI { - acc := accounts[string(addr)] - if acc == nil { - acc = load() - if acc != nil { - accounts[string(addr)] = acc - } - } - return acc - } + accountGetter := ante.NewAccountGetter(suite.ctx, suite.app.AccountKeeper) + if tc.expPanic { suite.Require().Panics(func() { - _ = ante.CheckAndSetEthSenderNonce(suite.ctx, tc.tx, suite.app.AccountKeeper, false, getAccount) + _ = ante.CheckAndSetEthSenderNonce(suite.ctx, tc.tx, suite.app.AccountKeeper, false, accountGetter) }) return } - err := ante.CheckAndSetEthSenderNonce(suite.ctx, tc.tx, suite.app.AccountKeeper, false, getAccount) + err := ante.CheckAndSetEthSenderNonce(suite.ctx, tc.tx, suite.app.AccountKeeper, false, accountGetter) if tc.expPass { suite.Require().NoError(err)