Skip to content

Commit

Permalink
refactor(evm): move evmante handlers into separate package (#1970)
Browse files Browse the repository at this point in the history
* Reapply "refactor(evm): move evmante handlers into separate package"

This reverts commit f898bae.

* Update CHANGELOG.md

* fix: add account keeper to ante handler options

* refactor: combine evm testutil with the others

* test(gosdk): remove sequence_test, which causes race condition in CI

---------

Co-authored-by: Unique-Divine <[email protected]>
Co-authored-by: Unique Divine <[email protected]>
  • Loading branch information
3 people authored Jul 24, 2024
1 parent e1febe7 commit abec9bd
Show file tree
Hide file tree
Showing 36 changed files with 375 additions and 600 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1963](https://github.com/NibiruChain/nibiru/pull/1963) - feat(evm): Deduct a fee during the creation of a FunToken mapping. Implemented by `deductCreateFunTokenFee` inside of the `eth.evm.v1.MsgCreateFunToken` transaction.
- [#1965](https://github.com/NibiruChain/nibiru/pull/1965) - refactor(evm): remove evm post-processing hooks
- [#1968](https://github.com/NibiruChain/nibiru/pull/1968) - refactor(evm): funtoken events, cli commands and queries
- [#1970](https://github.com/NibiruChain/nibiru/pull/1970) - refactor(evm): move evm antehandlers to separate package. Remove "gosdk/sequence_test.go", which causes a race condition in CI.
- [#1971](https://github.com/NibiruChain/nibiru/pull/1971) - feat(evm): typed events for contract creation, contract execution and transfer

#### Dapp modules: perp, spot, oracle, etc
Expand Down
7 changes: 4 additions & 3 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"

"github.com/NibiruChain/nibiru/app/ante"
"github.com/NibiruChain/nibiru/app/evmante"
"github.com/NibiruChain/nibiru/eth"
devgasante "github.com/NibiruChain/nibiru/x/devgas/v1/ante"
"github.com/NibiruChain/nibiru/x/evm"
Expand Down Expand Up @@ -53,7 +54,7 @@ func AnteHandlerExtendedTx(
) (anteHandler sdk.AnteHandler) {
switch typeUrl {
case evm.TYPE_URL_ETHEREUM_TX:
anteHandler = NewAnteHandlerEVM(keepers, opts)
anteHandler = evmante.NewAnteHandlerEVM(opts)
case eth.TYPE_URL_DYNAMIC_FEE_TX:
anteHandler = NewAnteHandlerNonEVM(opts)
default:
Expand All @@ -73,7 +74,7 @@ func NewAnteHandlerNonEVM(
opts ante.AnteHandlerOptions,
) sdk.AnteHandler {
return sdk.ChainAnteDecorators(
AnteDecoratorPreventEtheruemTxMsgs{}, // reject MsgEthereumTxs
ante.AnteDecoratorPreventEtheruemTxMsgs{}, // reject MsgEthereumTxs
authante.NewSetUpContextDecorator(),
wasmkeeper.NewLimitSimulationGasDecorator(opts.WasmConfig.SimulationGasLimit),
wasmkeeper.NewCountTXDecorator(opts.TxCounterStoreKey),
Expand Down Expand Up @@ -103,7 +104,7 @@ func NewAnteHandlerNonEVM(
authante.NewSigVerificationDecorator(opts.AccountKeeper, opts.SignModeHandler),
authante.NewIncrementSequenceDecorator(opts.AccountKeeper),
ibcante.NewRedundantRelayDecorator(opts.IBCKeeper),
AnteDecoratorGasWanted{},
ante.AnteDecoratorGasWanted{},
)
}

Expand Down
2 changes: 1 addition & 1 deletion app/evmante_gas_wanted.go → app/ante/gas_wanted.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2023-2024 Nibi, Inc.
package app
package ante

import (
"cosmossdk.io/errors"
Expand Down
18 changes: 9 additions & 9 deletions app/evmante_gas_wanted_test.go → app/ante/gas_wanted_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package app_test
package ante_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/app/ante"
"github.com/NibiruChain/nibiru/x/evm/evmtest"
)

func (s *TestSuite) TestGasWantedDecorator() {
func (s *AnteTestSuite) TestGasWantedDecorator() {
testCases := []struct {
name string
ctxSetup func(deps *evmtest.TestDeps)
Expand All @@ -19,7 +19,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
{
name: "happy: non fee tx type",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return happyCreateContractTx(deps)
return evmtest.HappyCreateContractTx(deps)
},
wantErr: "",
},
Expand All @@ -34,7 +34,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return legacytx.StdTx{
Msgs: []sdk.Msg{
happyCreateContractTx(deps),
evmtest.HappyCreateContractTx(deps),
},
}
},
Expand All @@ -51,7 +51,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return legacytx.StdTx{
Msgs: []sdk.Msg{
happyCreateContractTx(deps),
evmtest.HappyCreateContractTx(deps),
},
Fee: legacytx.StdFee{Gas: 500},
}
Expand All @@ -71,7 +71,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return legacytx.StdTx{
Msgs: []sdk.Msg{
happyCreateContractTx(deps),
evmtest.HappyCreateContractTx(deps),
},
Fee: legacytx.StdFee{Gas: 1000},
}
Expand All @@ -84,7 +84,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
s.Run(tc.name, func() {
deps := evmtest.NewTestDeps()
stateDB := deps.StateDB()
anteDec := app.AnteDecoratorGasWanted{}
anteDec := ante.AnteDecoratorGasWanted{}

tx := tc.txSetup(&deps)
s.Require().NoError(stateDB.Commit())
Expand All @@ -94,7 +94,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
tc.ctxSetup(&deps)
}
_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, NextNoOpAnteHandler,
deps.Ctx, tx, false, evmtest.NextNoOpAnteHandler,
)
if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr)
Expand Down
7 changes: 4 additions & 3 deletions app/ante/handler_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
sdkante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"

devgasante "github.com/NibiruChain/nibiru/x/devgas/v1/ante"
devgaskeeper "github.com/NibiruChain/nibiru/x/devgas/v1/keeper"
evmkeeper "github.com/NibiruChain/nibiru/x/evm/keeper"
)

type AnteHandlerOptions struct {
sdkante.HandlerOptions
IBCKeeper *ibckeeper.Keeper
DevGasKeeper *devgaskeeper.Keeper
DevGasBankKeeper devgasante.BankKeeper
EvmKeeper evmkeeper.Keeper
AccountKeeper authkeeper.AccountKeeper

TxCounterStoreKey types.StoreKey
WasmConfig *wasmtypes.WasmConfig
MaxTxGasWanted uint64
}

func (opts *AnteHandlerOptions) ValidateAndClean() error {
if opts.AccountKeeper == nil {
return AnteHandlerError("account keeper")
}
if opts.BankKeeper == nil {
return AnteHandlerError("bank keeper")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2023-2024 Nibi, Inc.
package app
package ante

import (
"cosmossdk.io/errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package app_test
package ante_test

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

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

func (s *TestSuite) TestAnteDecoratorPreventEtheruemTxMsgs() {
func (s *AnteTestSuite) TestAnteDecoratorPreventEtheruemTxMsgs() {
testCases := []struct {
name string
txSetup func(deps *evmtest.TestDeps) sdk.Tx
Expand All @@ -16,25 +16,25 @@ func (s *TestSuite) TestAnteDecoratorPreventEtheruemTxMsgs() {
{
name: "sad: evm message",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return happyTransfertTx(deps, 0)
return evmtest.HappyTransferTx(deps, 0)
},
wantErr: "invalid type",
},
{
name: "happy: non evm message",
txSetup: nonEvmMsgTx,
txSetup: evmtest.NonEvmMsgTx,
wantErr: "",
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
deps := evmtest.NewTestDeps()
anteDec := app.AnteDecoratorPreventEtheruemTxMsgs{}
anteDec := ante.AnteDecoratorPreventEtheruemTxMsgs{}
tx := tc.txSetup(&deps)

_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, NextNoOpAnteHandler,
deps.Ctx, tx, false, evmtest.NextNoOpAnteHandler,
)
if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr)
Expand Down
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ func NewNibiruApp(
DevGasBankKeeper: app.BankKeeper,
// TODO: feat(evm): enable app/server/config flag for Evm MaxTxGasWanted.
MaxTxGasWanted: DefaultMaxTxGasWanted,
EvmKeeper: app.EvmKeeper,
AccountKeeper: app.AccountKeeper,
})

app.SetAnteHandler(anteHandler)
Expand Down
23 changes: 11 additions & 12 deletions app/evmante_can_transfer.go → app/evmante/evmante_can_transfer.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
// Copyright (c) 2023-2024 Nibi, Inc.
package app
package evmante

import (
"math/big"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
gethcommon "github.com/ethereum/go-ethereum/common"
gethcore "github.com/ethereum/go-ethereum/core/types"

"github.com/NibiruChain/nibiru/x/evm"
"github.com/NibiruChain/nibiru/x/evm/statedb"

gethcommon "github.com/ethereum/go-ethereum/common"
gethcore "github.com/ethereum/go-ethereum/core/types"
)

// CanTransferDecorator checks if the sender is allowed to transfer funds according to the EVM block
// context rules.
type CanTransferDecorator struct {
AppKeepers
evmKeeper EVMKeeper
}

// NewCanTransferDecorator creates a new CanTransferDecorator instance.
func NewCanTransferDecorator(k AppKeepers) CanTransferDecorator {
func NewCanTransferDecorator(k EVMKeeper) CanTransferDecorator {
return CanTransferDecorator{
AppKeepers: k,
evmKeeper: k,
}
}

Expand All @@ -33,8 +32,8 @@ func NewCanTransferDecorator(k AppKeepers) CanTransferDecorator {
func (ctd CanTransferDecorator) AnteHandle(
ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler,
) (sdk.Context, error) {
params := ctd.EvmKeeper.GetParams(ctx)
ethCfg := evm.EthereumConfig(ctd.EvmKeeper.EthChainID(ctx))
params := ctd.evmKeeper.GetParams(ctx)
ethCfg := evm.EthereumConfig(ctd.evmKeeper.EthChainID(ctx))
signer := gethcore.MakeSigner(ethCfg, big.NewInt(ctx.BlockHeight()))

for _, msg := range tx.GetMsgs() {
Expand All @@ -45,7 +44,7 @@ func (ctd CanTransferDecorator) AnteHandle(
"invalid message type %T, expected %T", msg, (*evm.MsgEthereumTx)(nil),
)
}
baseFee := ctd.EvmKeeper.GetBaseFee(ctx)
baseFee := ctd.evmKeeper.GetBaseFee(ctx)

coreMsg, err := msgEthTx.AsMessage(signer, baseFee)
if err != nil {
Expand Down Expand Up @@ -79,10 +78,10 @@ func (ctd CanTransferDecorator) AnteHandle(

stateDB := statedb.New(
ctx,
&ctd.EvmKeeper,
ctd.evmKeeper,
statedb.NewEmptyTxConfig(gethcommon.BytesToHash(ctx.HeaderHash().Bytes())),
)
evmInstance := ctd.EvmKeeper.NewEVM(ctx, coreMsg, cfg, evm.NewNoOpTracer(), stateDB)
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package app_test
package evmante_test

import (
"math/big"

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

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/app/evmante"
"github.com/NibiruChain/nibiru/eth"
"github.com/NibiruChain/nibiru/x/evm/evmtest"
"github.com/NibiruChain/nibiru/x/evm/statedb"
Expand All @@ -25,7 +25,7 @@ func (s *TestSuite) TestCanTransferDecorator() {
sdb.AddBalance(deps.Sender.EthAddr, big.NewInt(100))
},
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyTransfertTx(deps, 0)
txMsg := evmtest.HappyTransferTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

gethSigner := deps.Sender.GethSigner(deps.Chain.EvmKeeper.EthChainID(deps.Ctx))
Expand All @@ -43,7 +43,7 @@ func (s *TestSuite) TestCanTransferDecorator() {
{
name: "sad: signed tx, insufficient funds",
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyTransfertTx(deps, 0)
txMsg := evmtest.HappyTransferTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

gethSigner := deps.Sender.GethSigner(deps.Chain.EvmKeeper.EthChainID(deps.Ctx))
Expand All @@ -61,7 +61,7 @@ func (s *TestSuite) TestCanTransferDecorator() {
{
name: "sad: unsigned tx",
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := happyTransfertTx(deps, 0)
txMsg := evmtest.HappyTransferTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
Expand All @@ -74,7 +74,7 @@ func (s *TestSuite) TestCanTransferDecorator() {
{
name: "sad: tx with non evm message",
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
return nonEvmMsgTx(deps).(sdk.FeeTx)
return evmtest.NonEvmMsgTx(deps).(sdk.FeeTx)
},
wantErr: "invalid message",
},
Expand All @@ -84,7 +84,7 @@ func (s *TestSuite) TestCanTransferDecorator() {
s.Run(tc.name, func() {
deps := evmtest.NewTestDeps()
stateDB := deps.StateDB()
anteDec := app.NewCanTransferDecorator(deps.Chain.AppKeepers)
anteDec := evmante.NewCanTransferDecorator(&deps.Chain.AppKeepers.EvmKeeper)
tx := tc.txSetup(&deps)

if tc.ctxSetup != nil {
Expand All @@ -97,7 +97,7 @@ func (s *TestSuite) TestCanTransferDecorator() {
}

_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, NextNoOpAnteHandler,
deps.Ctx, tx, false, evmtest.NextNoOpAnteHandler,
)
if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr)
Expand Down
12 changes: 7 additions & 5 deletions app/evmante_emit_event.go → app/evmante/evmante_emit_event.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2023-2024 Nibi, Inc.
package app
package evmante

import (
"strconv"
Expand All @@ -13,12 +13,14 @@ import (

// EthEmitEventDecorator emit events in ante handler in case of tx execution failed (out of block gas limit).
type EthEmitEventDecorator struct {
AppKeepers
evmKeeper EVMKeeper
}

// NewEthEmitEventDecorator creates a new EthEmitEventDecorator
func NewEthEmitEventDecorator(k AppKeepers) EthEmitEventDecorator {
return EthEmitEventDecorator{AppKeepers: k}
func NewEthEmitEventDecorator(k EVMKeeper) EthEmitEventDecorator {
return EthEmitEventDecorator{
evmKeeper: k,
}
}

// AnteHandle emits some basic events for the eth messages
Expand All @@ -28,7 +30,7 @@ func (eeed EthEmitEventDecorator) AnteHandle(
// After eth tx passed ante handler, the fee is deducted and nonce increased,
// it shouldn't be ignored by json-rpc. We need to emit some events at the
// very end of ante handler to be indexed by the consensus engine.
txIndex := eeed.EvmKeeper.EVMState().BlockTxIndex.GetOr(ctx, 0)
txIndex := eeed.evmKeeper.EVMState().BlockTxIndex.GetOr(ctx, 0)

for i, msg := range tx.GetMsgs() {
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
Expand Down
Loading

0 comments on commit abec9bd

Please sign in to comment.