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 2 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 @@ -67,6 +67,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1895](https://github.com/NibiruChain/nibiru/pull/1895) - refactor(geth): Reference go-ethereum as a submodule for easier change tracking with upstream
- [#1901](https://github.com/NibiruChain/nibiru/pull/1901) - test(evm): more e2e test contracts for edge cases
- [#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
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