Skip to content

Commit

Permalink
Revert "refactor(evm): move evmante handlers into separate package"
Browse files Browse the repository at this point in the history
This reverts commit 0b901e6.
  • Loading branch information
k-yang committed Jul 22, 2024
1 parent 0b901e6 commit f898bae
Show file tree
Hide file tree
Showing 33 changed files with 521 additions and 385 deletions.
7 changes: 3 additions & 4 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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 @@ -54,7 +53,7 @@ func AnteHandlerExtendedTx(
) (anteHandler sdk.AnteHandler) {
switch typeUrl {
case evm.TYPE_URL_ETHEREUM_TX:
anteHandler = evmante.NewAnteHandlerEVM(opts)
anteHandler = NewAnteHandlerEVM(keepers, opts)
case eth.TYPE_URL_DYNAMIC_FEE_TX:
anteHandler = NewAnteHandlerNonEVM(opts)
default:
Expand All @@ -74,7 +73,7 @@ func NewAnteHandlerNonEVM(
opts ante.AnteHandlerOptions,
) sdk.AnteHandler {
return sdk.ChainAnteDecorators(
ante.AnteDecoratorPreventEtheruemTxMsgs{}, // reject MsgEthereumTxs
AnteDecoratorPreventEtheruemTxMsgs{}, // reject MsgEthereumTxs
authante.NewSetUpContextDecorator(),
wasmkeeper.NewLimitSimulationGasDecorator(opts.WasmConfig.SimulationGasLimit),
wasmkeeper.NewCountTXDecorator(opts.TxCounterStoreKey),
Expand Down Expand Up @@ -104,7 +103,7 @@ func NewAnteHandlerNonEVM(
authante.NewSigVerificationDecorator(opts.AccountKeeper, opts.SignModeHandler),
authante.NewIncrementSequenceDecorator(opts.AccountKeeper),
ibcante.NewRedundantRelayDecorator(opts.IBCKeeper),
ante.AnteDecoratorGasWanted{},
AnteDecoratorGasWanted{},
)
}

Expand Down
7 changes: 3 additions & 4 deletions app/ante/handler_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,27 @@ 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
1 change: 0 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ 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
29 changes: 0 additions & 29 deletions app/evmante/evmante_handler.go

This file was deleted.

77 changes: 0 additions & 77 deletions app/evmante/suite_test.go

This file was deleted.

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

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 {
evmKeeper EVMKeeper
AppKeepers
}

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

Expand All @@ -32,8 +33,8 @@ func NewCanTransferDecorator(k EVMKeeper) 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 @@ -44,7 +45,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 @@ -78,10 +79,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,13 +1,12 @@
package evmante_test
package app_test

import (
"math/big"

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

"github.com/NibiruChain/nibiru/app/evmante"
"github.com/NibiruChain/nibiru/app"
"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 @@ -26,7 +25,7 @@ func (s *TestSuite) TestCanTransferDecorator() {
sdb.AddBalance(deps.Sender.EthAddr, big.NewInt(100))
},
txSetup: func(deps *evmtest.TestDeps) sdk.FeeTx {
txMsg := evmtestutil.HappyTransferTx(deps, 0)
txMsg := happyTransfertTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

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

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

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

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

_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, evmtestutil.NextNoOpAnteHandler,
deps.Ctx, tx, false, NextNoOpAnteHandler,
)
if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr)
Expand Down
12 changes: 5 additions & 7 deletions app/evmante/evmante_emit_event.go → app/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 evmante
package app

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

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

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

// AnteHandle emits some basic events for the eth messages
Expand All @@ -30,7 +28,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
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package evmante_test
package app_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"

"github.com/NibiruChain/nibiru/app/evmante"
evmtestutil "github.com/NibiruChain/nibiru/x/common/testutil/evm"
"github.com/NibiruChain/nibiru/x/evm"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/x/evm/evmtest"
tf "github.com/NibiruChain/nibiru/x/tokenfactory/types"
)
Expand All @@ -32,7 +31,7 @@ func (s *TestSuite) TestEthEmitEventDecorator() {
{
name: "happy: eth tx emitted event",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
tx := evmtestutil.HappyCreateContractTx(deps)
tx := happyCreateContractTx(deps)
return tx
},
wantErr: "",
Expand All @@ -43,13 +42,13 @@ func (s *TestSuite) TestEthEmitEventDecorator() {
s.Run(tc.name, func() {
deps := evmtest.NewTestDeps()
stateDB := deps.StateDB()
anteDec := evmante.NewEthEmitEventDecorator(&deps.Chain.AppKeepers.EvmKeeper)
anteDec := app.NewEthEmitEventDecorator(deps.Chain.AppKeepers)

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

_, err := anteDec.AnteHandle(
deps.Ctx, tx, false, evmtestutil.NextNoOpAnteHandler,
deps.Ctx, tx, false, NextNoOpAnteHandler,
)
if tc.wantErr != "" {
s.Require().ErrorContains(err, tc.wantErr)
Expand Down
Loading

0 comments on commit f898bae

Please sign in to comment.