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

test(evm): unit tests for evm_ante #1912

Merged
merged 16 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1901](https://github.com/NibiruChain/nibiru/pull/1901) - test(evm): more e2e test contracts for edge cases
- [#1907](https://github.com/NibiruChain/nibiru/pull/1907) - test(evm): grpc_query full coverage
- [#1909](https://github.com/NibiruChain/nibiru/pull/1909) - chore(evm): set is_london true by default and removed from config
- [#1912](https://github.com/NibiruChain/nibiru/pull/1912) - test(evm): unit tests for evm_ante_sigverify

#### Dapp modules: perp, spot, oracle, etc

Expand Down
File renamed without changes.
106 changes: 106 additions & 0 deletions app/evmante_gas_wanted_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package app_test

import (
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/x/evm/evmtest"
)

func (s *TestSuite) TestGasWantedDecorator() {
testCases := []struct {
name string
ctxSetup func(deps *evmtest.TestDeps)
txSetup func(deps *evmtest.TestDeps) sdk.Tx
wantErr string
}{
{
name: "happy: non fee tx type",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return happyCreateContractTx(deps)
},
wantErr: "",
},
{
name: "happy: tx without gas, block gas limit 1000",
ctxSetup: func(deps *evmtest.TestDeps) {
cp := &tmproto.ConsensusParams{
Block: &tmproto.BlockParams{MaxGas: 1000},
}
deps.Ctx = deps.Ctx.WithConsensusParams(cp)
},
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return legacytx.StdTx{
Msgs: []sdk.Msg{
happyCreateContractTx(deps),
},
}
},
wantErr: "",
},
{
name: "happy: tx with gas wanted 500, block gas limit 1000",
ctxSetup: func(deps *evmtest.TestDeps) {
cp := &tmproto.ConsensusParams{
Block: &tmproto.BlockParams{MaxGas: 1000},
}
deps.Ctx = deps.Ctx.WithConsensusParams(cp)
},
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return legacytx.StdTx{
Msgs: []sdk.Msg{
happyCreateContractTx(deps),
},
Fee: legacytx.StdFee{Gas: 500},
}
},
wantErr: "",
},
{
name: "sad: tx with gas wanted 1000, block gas limit 500",
ctxSetup: func(deps *evmtest.TestDeps) {
cp := &tmproto.ConsensusParams{
Block: &tmproto.BlockParams{
MaxGas: 500,
},
}
deps.Ctx = deps.Ctx.WithConsensusParams(cp)
},
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return legacytx.StdTx{
Msgs: []sdk.Msg{
happyCreateContractTx(deps),
},
Fee: legacytx.StdFee{Gas: 1000},
}
},
wantErr: "exceeds block gas limit",
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
deps := evmtest.NewTestDeps()
stateDB := deps.StateDB()
anteDec := app.NewGasWantedDecorator(deps.Chain.AppKeepers)

tx := tc.txSetup(&deps)
s.Require().NoError(stateDB.Commit())

deps.Ctx = deps.Ctx.WithIsCheckTx(true)
if tc.ctxSetup != nil {
tc.ctxSetup(&deps)
}
_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, NextNoOpAnteHandler,
)
if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr)
return
}
s.Require().NoError(err)
})
}
}
9 changes: 7 additions & 2 deletions app/evmante_sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ func (esvd EthSigVerificationDecorator) AnteHandle(
for _, msg := range tx.GetMsgs() {
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
if !ok {
return ctx, errors.Wrapf(errortypes.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evm.MsgEthereumTx)(nil))
return ctx, errors.Wrapf(
errortypes.ErrUnknownRequest,
"invalid message type %T, expected %T", msg, (*evm.MsgEthereumTx)(nil),
)
}

allowUnprotectedTxs := evmParams.GetAllowUnprotectedTxs()
ethTx := msgEthTx.AsTransaction()
if !allowUnprotectedTxs && !ethTx.Protected() {
return ctx, errors.Wrapf(
errortypes.ErrNotSupported,
"rejected unprotected Ethereum transaction. Please EIP155 sign your transaction to protect it against replay-attacks")
"rejected unprotected Ethereum transaction. "+
"Please EIP155 sign your transaction to protect it against replay-attacks",
)
}

sender, err := signer.Sender(ethTx)
Expand Down
88 changes: 88 additions & 0 deletions app/evmante_sigverify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package app_test

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/x/evm/evmtest"
tf "github.com/NibiruChain/nibiru/x/tokenfactory/types"
)

var InvalidChainID = big.NewInt(987654321)
var RandomAddress = evmtest.NewEthAccInfo().EthAddr.Hex()

func (s *TestSuite) TestEthSigVerificationDecorator() {
testCases := []struct {
name string
txSetup func(deps *evmtest.TestDeps) sdk.Tx
wantErr string
}{
{
name: "sad: unsigned tx",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
tx := happyCreateContractTx(deps)
return tx
},
wantErr: "rejected unprotected Ethereum transaction",
},
{
name: "sad: non ethereum tx",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return legacytx.StdTx{
Msgs: []sdk.Msg{
&tf.MsgMint{},
},
}
},
wantErr: "invalid message",
},
{
name: "sad: ethereum tx invalid chain id",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
tx := happyCreateContractTx(deps)
gethSigner := deps.Sender.GethSigner(InvalidChainID)
keyringSigner := deps.Sender.KeyringSigner
err := tx.Sign(gethSigner, keyringSigner)
s.Require().NoError(err)
return tx
},
wantErr: "invalid chain id for signer",
},
{
name: "happy: signed ethereum tx",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
tx := happyCreateContractTx(deps)
gethSigner := deps.Sender.GethSigner(deps.Chain.EvmKeeper.EthChainID(deps.Ctx))
keyringSigner := deps.Sender.KeyringSigner
err := tx.Sign(gethSigner, keyringSigner)
s.Require().NoError(err)
return tx
},
wantErr: "",
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
deps := evmtest.NewTestDeps()
stateDB := deps.StateDB()
anteDec := app.NewEthSigVerificationDecorator(deps.Chain.AppKeepers)

tx := tc.txSetup(&deps)
s.Require().NoError(stateDB.Commit())

deps.Ctx = deps.Ctx.WithIsCheckTx(true)
_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, NextNoOpAnteHandler,
)
if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr)
return
}
s.Require().NoError(err)
})
}
}
Loading