Skip to content

Commit

Permalink
test(evm): evmante tests for can_transfer, fee_checker, gas_comsume
Browse files Browse the repository at this point in the history
  • Loading branch information
onikonychev committed Jun 11, 2024
1 parent 89cc591 commit ec4dfca
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 0 deletions.
97 changes: 97 additions & 0 deletions app/evmante_can_transfer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package app_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"

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

func (s *TestSuite) TestNewDynamicFeeChecker() {
testCases := []struct {
name string
txSetup func(deps *evmtest.TestDeps) sdk.FeeTx
ctxSetup func(deps *evmtest.TestDeps)
wantErr string
wantFee int64
wantPriority int64
}{
{
name: "happy: genesis tx with sufficient fee",
ctxSetup: func(deps *evmtest.TestDeps) {
gasPrice := sdk.NewInt64Coin("unibi", 1)
deps.Ctx = deps.Ctx.
WithBlockHeight(0).
WithMinGasPrices(
sdk.NewDecCoins(sdk.NewDecCoinFromCoin(gasPrice)),
).
WithIsCheckTx(true)
},
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyCreateContractTx(deps)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()
tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
s.Require().NoError(err)
return tx
},
wantErr: "",
wantFee: gasLimitCreateContract().Int64(),
wantPriority: 0,
},
{
name: "sad: genesis tx insufficient fee",
ctxSetup: func(deps *evmtest.TestDeps) {
gasPrice := sdk.NewInt64Coin("unibi", 2)
deps.Ctx = deps.Ctx.
WithBlockHeight(0).
WithMinGasPrices(
sdk.NewDecCoins(sdk.NewDecCoinFromCoin(gasPrice)),
).
WithIsCheckTx(true)
},
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyCreateContractTx(deps)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()
tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
s.Require().NoError(err)
return tx
},
wantErr: "insufficient fee",
},
{
name: "happy: tx with sufficient fee",
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyCreateContractTx(deps)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()
tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
s.Require().NoError(err)
return tx
},
wantErr: "",
wantFee: gasLimitCreateContract().Int64(),
wantPriority: 0,
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
deps := evmtest.NewTestDeps()
checker := app.NewDynamicFeeChecker(deps.K)

if tc.ctxSetup != nil {
tc.ctxSetup(&deps)
}

fee, priority, err := checker(deps.Ctx, tc.txSetup(&deps))

if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr)
return
}
s.Require().NoError(err)
s.Require().Equal(tc.wantFee, fee.AmountOf("unibi").Int64())
s.Require().Equal(tc.wantPriority, priority)
})
}
}
106 changes: 106 additions & 0 deletions app/evmante_gas_consume_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.AnteDecoratorGasWanted{}

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)
})
}
}

0 comments on commit ec4dfca

Please sign in to comment.