Skip to content

Commit

Permalink
refactor(evm): move evmante handlers into separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Jul 22, 2024
1 parent a666e08 commit 0b901e6
Show file tree
Hide file tree
Showing 33 changed files with 385 additions and 521 deletions.
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
19 changes: 10 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,16 @@
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"
evmtestutil "github.com/NibiruChain/nibiru/x/common/testutil/evm"
"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 +20,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
{
name: "happy: non fee tx type",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return happyCreateContractTx(deps)
return evmtestutil.HappyCreateContractTx(deps)
},
wantErr: "",
},
Expand All @@ -34,7 +35,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return legacytx.StdTx{
Msgs: []sdk.Msg{
happyCreateContractTx(deps),
evmtestutil.HappyCreateContractTx(deps),
},
}
},
Expand All @@ -51,7 +52,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return legacytx.StdTx{
Msgs: []sdk.Msg{
happyCreateContractTx(deps),
evmtestutil.HappyCreateContractTx(deps),
},
Fee: legacytx.StdFee{Gas: 500},
}
Expand All @@ -71,7 +72,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return legacytx.StdTx{
Msgs: []sdk.Msg{
happyCreateContractTx(deps),
evmtestutil.HappyCreateContractTx(deps),
},
Fee: legacytx.StdFee{Gas: 1000},
}
Expand All @@ -84,7 +85,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 +95,7 @@ func (s *TestSuite) TestGasWantedDecorator() {
tc.ctxSetup(&deps)
}
_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, NextNoOpAnteHandler,
deps.Ctx, tx, false, evmtestutil.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,14 @@
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"
evmtestutil "github.com/NibiruChain/nibiru/x/common/testutil/evm"
"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 +17,25 @@ func (s *TestSuite) TestAnteDecoratorPreventEtheruemTxMsgs() {
{
name: "sad: evm message",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
return happyTransfertTx(deps, 0)
return evmtestutil.HappyTransferTx(deps, 0)
},
wantErr: "invalid type",
},
{
name: "happy: non evm message",
txSetup: nonEvmMsgTx,
txSetup: evmtestutil.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, evmtestutil.NextNoOpAnteHandler,
)
if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr)
Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func NewNibiruApp(
DevGasBankKeeper: app.BankKeeper,
// TODO: feat(evm): enable app/server/config flag for Evm MaxTxGasWanted.
MaxTxGasWanted: DefaultMaxTxGasWanted,
EvmKeeper: app.EvmKeeper,
})

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,12 +1,13 @@
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"
evmtestutil "github.com/NibiruChain/nibiru/x/common/testutil/evm"
"github.com/NibiruChain/nibiru/x/evm/evmtest"
"github.com/NibiruChain/nibiru/x/evm/statedb"
)
Expand All @@ -25,7 +26,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 := evmtestutil.HappyTransferTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

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

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

tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
Expand All @@ -74,7 +75,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 evmtestutil.NonEvmMsgTx(deps).(sdk.FeeTx)
},
wantErr: "invalid message",
},
Expand All @@ -84,7 +85,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 +98,7 @@ func (s *TestSuite) TestCanTransferDecorator() {
}

_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, NextNoOpAnteHandler,
deps.Ctx, tx, false, evmtestutil.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 0b901e6

Please sign in to comment.