Skip to content

Commit

Permalink
test(evm): evmante increment_sender_seq, validate_basic, gas_consume,…
Browse files Browse the repository at this point in the history
… reject_msgs, can_transfer
  • Loading branch information
onikonychev committed Jun 11, 2024
1 parent ec4dfca commit fde2383
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 191 deletions.
17 changes: 12 additions & 5 deletions app/evmante_can_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ func (ctd CanTransferDecorator) 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),
)
}

baseFee := ctd.EvmKeeper.GetBaseFee(ctx)

coreMsg, err := msgEthTx.AsMessage(signer, baseFee)
Expand Down Expand Up @@ -75,12 +77,17 @@ func (ctd CanTransferDecorator) AnteHandle(
BaseFee: baseFee,
}

stateDB := statedb.New(ctx, &ctd.EvmKeeper, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(ctx.HeaderHash().Bytes())))
evm := ctd.EvmKeeper.NewEVM(ctx, coreMsg, cfg, evm.NewNoOpTracer(), stateDB)
stateDB := statedb.New(
ctx,
&ctd.EvmKeeper,
statedb.NewEmptyTxConfig(gethcommon.BytesToHash(ctx.HeaderHash().Bytes())),
)
evmInstance := ctd.EvmKeeper.NewEVM(ctx, coreMsg, cfg, evm.NewNoOpTracer(), stateDB)

// check that caller has enough balance to cover asset transfer for **topmost** call
// NOTE: here the gas consumed is from the context with the infinite gas meter
if coreMsg.Value().Sign() > 0 && !evm.Context.CanTransfer(stateDB, coreMsg.From(), coreMsg.Value()) {
if coreMsg.Value().Sign() > 0 &&
!evmInstance.Context.CanTransfer(stateDB, coreMsg.From(), coreMsg.Value()) {
return ctx, errors.Wrapf(
errortypes.ErrInsufficientFunds,
"failed to transfer %s from address %s using the EVM block context transfer function",
Expand Down
96 changes: 54 additions & 42 deletions app/evmante_can_transfer_test.go
Original file line number Diff line number Diff line change
@@ -1,97 +1,109 @@
package app_test

import (
"math/big"

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

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

func (s *TestSuite) TestNewDynamicFeeChecker() {
func (s *TestSuite) TestCanTransferDecorator() {
testCases := []struct {
name string
txSetup func(deps *evmtest.TestDeps) sdk.FeeTx
ctxSetup func(deps *evmtest.TestDeps)
wantErr string
wantFee int64
wantPriority int64
name string
txSetup func(deps *evmtest.TestDeps) sdk.FeeTx
ctxSetup func(deps *evmtest.TestDeps)
beforeTxSetup func(deps *evmtest.TestDeps, sdb *statedb.StateDB)
wantErr string
}{
{
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)
name: "happy: signed tx, sufficient funds",
beforeTxSetup: func(deps *evmtest.TestDeps, sdb *statedb.StateDB) {
sdb.AddBalance(deps.Sender.EthAddr, big.NewInt(100))
},
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyCreateContractTx(deps)
txMsg := happyTransfertTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

gethSigner := deps.Sender.GethSigner(deps.Chain.EvmKeeper.EthChainID(deps.Ctx))
keyringSigner := deps.Sender.KeyringSigner
err := txMsg.Sign(gethSigner, keyringSigner)
s.Require().NoError(err)

tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
s.Require().NoError(err)

return tx
},
wantErr: "",
wantFee: gasLimitCreateContract().Int64(),
wantPriority: 0,
wantErr: "",
},
{
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)
},
name: "sad: signed tx, insufficient funds",
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyCreateContractTx(deps)
txMsg := happyTransfertTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

gethSigner := deps.Sender.GethSigner(deps.Chain.EvmKeeper.EthChainID(deps.Ctx))
keyringSigner := deps.Sender.KeyringSigner
err := txMsg.Sign(gethSigner, keyringSigner)
s.Require().NoError(err)

tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
s.Require().NoError(err)

return tx
},
wantErr: "insufficient fee",
wantErr: "insufficient funds",
},
{
name: "happy: tx with sufficient fee",
name: "sad: unsigned tx",
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyCreateContractTx(deps)
txMsg := happyTransfertTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
s.Require().NoError(err)

return tx
},
wantErr: "",
wantFee: gasLimitCreateContract().Int64(),
wantPriority: 0,
wantErr: "invalid transaction",
},
{
name: "sad: tx with non evm message",
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
return nonEvmMsgTx(deps).(sdk.FeeTx)
},
wantErr: "invalid message",
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
deps := evmtest.NewTestDeps()
checker := app.NewDynamicFeeChecker(deps.K)
stateDB := deps.StateDB()
anteDec := app.NewCanTransferDecorator(deps.Chain.AppKeepers)
tx := tc.txSetup(&deps)

if tc.ctxSetup != nil {
tc.ctxSetup(&deps)
}
if tc.beforeTxSetup != nil {
tc.beforeTxSetup(&deps, stateDB)
err := stateDB.Commit()
s.Require().NoError(err)
}

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

_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, NextNoOpAnteHandler,
)
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)
})
}
}
2 changes: 1 addition & 1 deletion app/evmante_fee_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/NibiruChain/nibiru/x/evm/evmtest"
)

func (s *TestSuite) TestNewDynamicFeeChecker() {
func (s *TestSuite) TestDynamicFeeChecker() {
testCases := []struct {
name string
txSetup func(deps *evmtest.TestDeps) sdk.FeeTx
Expand Down
5 changes: 0 additions & 5 deletions app/evmante_gas_consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import (
"github.com/NibiruChain/nibiru/x/evm/keeper"
)

var (
_ sdk.AnteDecorator = (*AnteDecEthGasConsume)(nil)
_ sdk.AnteDecorator = (*AnteDecVerifyEthAcc)(nil)
)

// AnteDecEthGasConsume validates enough intrinsic gas for the transaction and
// gas consumption.
type AnteDecEthGasConsume struct {
Expand Down
105 changes: 41 additions & 64 deletions app/evmante_gas_consume_test.go
Original file line number Diff line number Diff line change
@@ -1,98 +1,75 @@
package app_test

import (
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"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/eth"
"github.com/NibiruChain/nibiru/x/evm"
"github.com/NibiruChain/nibiru/x/evm/evmtest"
"github.com/NibiruChain/nibiru/x/evm/statedb"
)

func (s *TestSuite) TestGasWantedDecorator() {
func (s *TestSuite) TestAnteDecEthGasConsume() {
testCases := []struct {
name string
ctxSetup func(deps *evmtest.TestDeps)
txSetup func(deps *evmtest.TestDeps) sdk.Tx
wantErr string
name string
beforeTxSetup func(deps *evmtest.TestDeps, sdb *statedb.StateDB)
txSetup func(deps *evmtest.TestDeps) *evm.MsgEthereumTx
wantErr string
maxGasWanted uint64
gasMeter sdk.GasMeter
}{
{
name: "happy: non fee tx type",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return happyCreateContractTx(deps)
name: "happy: sender with funds",
beforeTxSetup: func(deps *evmtest.TestDeps, sdb *statedb.StateDB) {
gasLimit := happyGasLimit()
balance := new(big.Int).Add(gasLimit, big.NewInt(100))
sdb.AddBalance(deps.Sender.EthAddr, balance)
},
wantErr: "",
txSetup: happyCreateContractTx,
wantErr: "",
gasMeter: eth.NewInfiniteGasMeterWithLimit(happyGasLimit().Uint64()),
maxGasWanted: 0,
},
{
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),
},
}
name: "happy: is recheck tx",
beforeTxSetup: func(deps *evmtest.TestDeps, sdb *statedb.StateDB) {
deps.Ctx = deps.Ctx.WithIsReCheckTx(true)
},
wantErr: "",
txSetup: happyCreateContractTx,
gasMeter: eth.NewInfiniteGasMeterWithLimit(0),
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},
}
name: "sad: out of gas",
beforeTxSetup: func(deps *evmtest.TestDeps, sdb *statedb.StateDB) {
gasLimit := happyGasLimit()
balance := new(big.Int).Add(gasLimit, big.NewInt(100))
sdb.AddBalance(deps.Sender.EthAddr, balance)
},
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",
txSetup: happyCreateContractTx,
wantErr: "exceeds block gas limit (0)",
gasMeter: eth.NewInfiniteGasMeterWithLimit(0),
maxGasWanted: 0,
},
}

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

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

deps.Ctx = deps.Ctx.WithIsCheckTx(true)
if tc.ctxSetup != nil {
tc.ctxSetup(&deps)
}
deps.Ctx = deps.Ctx.WithBlockGasMeter(tc.gasMeter)
_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, NextNoOpAnteHandler,
)
Expand Down
Loading

0 comments on commit fde2383

Please sign in to comment.