Skip to content

Commit

Permalink
cleanup test
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Aug 15, 2024
1 parent 99d3dcd commit ae7cc27
Showing 1 changed file with 22 additions and 59 deletions.
81 changes: 22 additions & 59 deletions app/ante/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -53,7 +51,6 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() {
vmdb.SetCode(addr, []byte("1"))
},
true,
true,
false,
},
{
Expand All @@ -64,7 +61,6 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() {
vmdb.SetCode(addr, nil)
},
true,
true,
false,
},
{
Expand All @@ -75,7 +71,6 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() {
},
true,
true,
true,
},
{
"success existing account",
Expand All @@ -88,7 +83,6 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() {
},
true,
true,
true,
},
}

Expand All @@ -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 {
Expand All @@ -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)
},
Expand All @@ -142,42 +135,21 @@ 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)
},
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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ae7cc27

Please sign in to comment.