Skip to content

Commit

Permalink
refactor(evm): move types into x/evm/types folder
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Jun 18, 2024
1 parent 98c2b24 commit 2c8e001
Show file tree
Hide file tree
Showing 116 changed files with 979 additions and 1,016 deletions.
4 changes: 2 additions & 2 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/NibiruChain/nibiru/app/ante"
"github.com/NibiruChain/nibiru/eth"
devgasante "github.com/NibiruChain/nibiru/x/devgas/v1/ante"
"github.com/NibiruChain/nibiru/x/evm"
evmtypes "github.com/NibiruChain/nibiru/x/evm/types"
)

// NewAnteHandler returns and AnteHandler that checks and increments sequence
Expand Down Expand Up @@ -53,7 +53,7 @@ func AnteHandlerExtendedTx(
ctx sdk.Context,
) (anteHandler sdk.AnteHandler) {
switch typeUrl {
case evm.TYPE_URL_ETHEREUM_TX:
case evmtypes.TYPE_URL_ETHEREUM_TX:
anteHandler = NewAnteHandlerEVM(keepers, opts)
case eth.TYPE_URL_DYNAMIC_FEE_TX:
anteHandler = NewAnteHandlerNonEVM(keepers, opts)
Expand Down
16 changes: 7 additions & 9 deletions app/evmante_can_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"math/big"

"cosmossdk.io/errors"
"github.com/NibiruChain/nibiru/x/evm/statedb"
"github.com/NibiruChain/nibiru/x/evm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"

"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"
)
Expand All @@ -34,15 +32,15 @@ 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))
ethCfg := types.EthereumConfig(ctd.EvmKeeper.EthChainID(ctx))
signer := gethcore.MakeSigner(ethCfg, big.NewInt(ctx.BlockHeight()))

for _, msg := range tx.GetMsgs() {
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
msgEthTx, ok := msg.(*types.MsgEthereumTx)
if !ok {
return ctx, errors.Wrapf(
errortypes.ErrUnknownRequest,
"invalid message type %T, expected %T", msg, (*evm.MsgEthereumTx)(nil),
"invalid message type %T, expected %T", msg, (*types.MsgEthereumTx)(nil),
)
}
baseFee := ctd.EvmKeeper.GetBaseFee(ctx)
Expand All @@ -57,7 +55,7 @@ func (ctd CanTransferDecorator) AnteHandle(

if baseFee == nil {
return ctx, errors.Wrap(
evm.ErrInvalidBaseFee,
types.ErrInvalidBaseFee,
"base fee is supported but evm block context value is nil",
)
}
Expand All @@ -82,7 +80,7 @@ func (ctd CanTransferDecorator) AnteHandle(
&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, types.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
12 changes: 6 additions & 6 deletions app/evmante_emit_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/NibiruChain/nibiru/x/evm"
"github.com/NibiruChain/nibiru/x/evm/types"
)

// EthEmitEventDecorator emit events in ante handler in case of tx execution failed (out of block gas limit).
Expand All @@ -31,12 +31,12 @@ func (eeed EthEmitEventDecorator) AnteHandle(
txIndex := eeed.EvmKeeper.EVMState().BlockTxIndex.GetOr(ctx, 0)

for i, msg := range tx.GetMsgs() {
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
msgEthTx, ok := msg.(*types.MsgEthereumTx)
if !ok {
return ctx, errorsmod.Wrapf(
errortypes.ErrUnknownRequest,
"invalid message type %T, expected %T",
msg, (*evm.MsgEthereumTx)(nil),
msg, (*types.MsgEthereumTx)(nil),
)
}

Expand All @@ -45,10 +45,10 @@ func (eeed EthEmitEventDecorator) AnteHandle(
// query failed transaction (out of block gas limit).
ctx.EventManager().EmitEvent(
sdk.NewEvent(
evm.EventTypeEthereumTx,
sdk.NewAttribute(evm.AttributeKeyEthereumTxHash, msgEthTx.Hash),
types.EventTypeEthereumTx,
sdk.NewAttribute(types.AttributeKeyEthereumTxHash, msgEthTx.Hash),
sdk.NewAttribute(
evm.AttributeKeyTxIndex, strconv.FormatUint(txIndex+uint64(i),
types.AttributeKeyTxIndex, strconv.FormatUint(txIndex+uint64(i),
10,
),
), // #nosec G701
Expand Down
11 changes: 5 additions & 6 deletions app/evmante_emit_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"

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

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/x/evm/evmtest"
"github.com/NibiruChain/nibiru/x/evm/types"
tf "github.com/NibiruChain/nibiru/x/tokenfactory/types"
)

Expand Down Expand Up @@ -59,19 +58,19 @@ func (s *TestSuite) TestEthEmitEventDecorator() {

s.Require().Greater(len(events), 0)
event := events[len(events)-1]
s.Require().Equal(evm.EventTypeEthereumTx, event.Type)
s.Require().Equal(types.EventTypeEthereumTx, event.Type)

// Convert tx to msg to get hash
txMsg, ok := tx.GetMsgs()[0].(*evm.MsgEthereumTx)
txMsg, ok := tx.GetMsgs()[0].(*types.MsgEthereumTx)
s.Require().True(ok)

// TX hash attr must present
attr, ok := event.GetAttribute(evm.AttributeKeyEthereumTxHash)
attr, ok := event.GetAttribute(types.AttributeKeyEthereumTxHash)
s.Require().True(ok, "tx hash attribute not found")
s.Require().Equal(txMsg.Hash, attr.Value)

// TX index attr must present
attr, ok = event.GetAttribute(evm.AttributeKeyTxIndex)
attr, ok = event.GetAttribute(types.AttributeKeyTxIndex)
s.Require().True(ok, "tx index attribute not found")
s.Require().Equal("0", attr.Value)
})
Expand Down
8 changes: 4 additions & 4 deletions app/evmante_fee_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"github.com/NibiruChain/nibiru/app/ante"
"github.com/NibiruChain/nibiru/eth"
"github.com/NibiruChain/nibiru/x/evm"
evmkeeper "github.com/NibiruChain/nibiru/x/evm/keeper"
"github.com/NibiruChain/nibiru/x/evm/types"
)

// NewDynamicFeeChecker returns a `TxFeeChecker` that applies a dynamic fee to
Expand Down Expand Up @@ -80,7 +80,7 @@ func NewDynamicFeeChecker(k evmkeeper.Keeper) ante.TxFeeChecker {

// calculate the effective gas price using the EIP-1559 logic.
effectivePrice := sdkmath.NewIntFromBigInt(
evm.EffectiveGasPrice(baseFeeInt.BigInt(), feeCap.BigInt(), maxPriorityPrice.BigInt()),
types.EffectiveGasPrice(baseFeeInt.BigInt(), feeCap.BigInt(), maxPriorityPrice.BigInt()),
)

// NOTE: create a new coins slice without having to validate the denom
Expand All @@ -91,7 +91,7 @@ func NewDynamicFeeChecker(k evmkeeper.Keeper) ante.TxFeeChecker {
},
}

bigPriority := effectivePrice.Sub(baseFeeInt).Quo(evm.DefaultPriorityReduction)
bigPriority := effectivePrice.Sub(baseFeeInt).Quo(types.DefaultPriorityReduction)
priority := int64(math.MaxInt64)

if bigPriority.IsInt64() {
Expand Down Expand Up @@ -142,7 +142,7 @@ func getTxPriority(fees sdk.Coins, gas int64) int64 {

for _, fee := range fees {
gasPrice := fee.Amount.QuoRaw(gas)
amt := gasPrice.Quo(evm.DefaultPriorityReduction)
amt := gasPrice.Quo(types.DefaultPriorityReduction)
p := int64(math.MaxInt64)

if amt.IsInt64() {
Expand Down
10 changes: 4 additions & 6 deletions app/evmante_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"math/big"

"cosmossdk.io/errors"
"github.com/NibiruChain/nibiru/x/evm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"

gethcore "github.com/ethereum/go-ethereum/core/types"

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

var _ sdk.AnteDecorator = EthMinGasPriceDecorator{}
Expand Down Expand Up @@ -48,12 +46,12 @@ func (empd EthMinGasPriceDecorator) AnteHandle(
baseFee := empd.EvmKeeper.GetBaseFee(ctx)

for _, msg := range tx.GetMsgs() {
ethMsg, ok := msg.(*evm.MsgEthereumTx)
ethMsg, ok := msg.(*types.MsgEthereumTx)
if !ok {
return ctx, errors.Wrapf(
errortypes.ErrUnknownRequest,
"invalid message type %T, expected %T",
msg, (*evm.MsgEthereumTx)(nil),
msg, (*types.MsgEthereumTx)(nil),
)
}

Expand All @@ -68,7 +66,7 @@ func (empd EthMinGasPriceDecorator) AnteHandle(
// Transactions with MinGasPrices * gasUsed < tx fees < EffectiveFee are rejected
// by the feemarket AnteHandle

txData, err := evm.UnpackTxData(ethMsg.Data)
txData, err := types.UnpackTxData(ethMsg.Data)
if err != nil {
return ctx, errors.Wrapf(err, "failed to unpack tx data %s", ethMsg.Hash)
}
Expand Down
18 changes: 8 additions & 10 deletions app/evmante_gas_consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (
"math"

"cosmossdk.io/errors"
"github.com/NibiruChain/nibiru/eth"
"github.com/NibiruChain/nibiru/x/evm/keeper"
"github.com/NibiruChain/nibiru/x/evm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"

gethcommon "github.com/ethereum/go-ethereum/common"

"github.com/NibiruChain/nibiru/eth"
"github.com/NibiruChain/nibiru/x/evm"
"github.com/NibiruChain/nibiru/x/evm/keeper"
)

// AnteDecEthGasConsume validates enough intrinsic gas for the transaction and
Expand Down Expand Up @@ -80,17 +78,17 @@ func (anteDec AnteDecEthGasConsume) AnteHandle(
baseFee := anteDec.EvmKeeper.GetBaseFee(ctx)

for _, msg := range tx.GetMsgs() {
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
msgEthTx, ok := msg.(*types.MsgEthereumTx)
if !ok {
return ctx, errors.Wrapf(
errortypes.ErrUnknownRequest,
"invalid message type %T, expected %T",
msg, (*evm.MsgEthereumTx)(nil),
msg, (*types.MsgEthereumTx)(nil),
)
}
from := msgEthTx.GetFrom()

txData, err := evm.UnpackTxData(msgEthTx.Data)
txData, err := types.UnpackTxData(msgEthTx.Data)
if err != nil {
return ctx, errors.Wrap(err, "failed to unpack tx data")
}
Expand Down Expand Up @@ -122,7 +120,7 @@ func (anteDec AnteDecEthGasConsume) AnteHandle(
),
)

priority := evm.GetTxPriority(txData, baseFee)
priority := types.GetTxPriority(txData, baseFee)

if priority < minPriority {
minPriority = priority
Expand All @@ -149,7 +147,7 @@ func (anteDec AnteDecEthGasConsume) AnteHandle(

// Set tx GasMeter with a limit of GasWanted (i.e. gas limit from the Ethereum tx).
// The gas consumed will be then reset to the gas used by the state transition
// in the EVM.
// in the types.

// FIXME: use a custom gas configuration that doesn't add any additional gas and only
// takes into account the gas consumed at the end of the EVM transaction.
Expand Down
4 changes: 2 additions & 2 deletions app/evmante_gas_consume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (

"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"
"github.com/NibiruChain/nibiru/x/evm/types"
)

func (s *TestSuite) TestAnteDecEthGasConsume() {
testCases := []struct {
name string
beforeTxSetup func(deps *evmtest.TestDeps, sdb *statedb.StateDB)
txSetup func(deps *evmtest.TestDeps) *evm.MsgEthereumTx
txSetup func(deps *evmtest.TestDeps) *types.MsgEthereumTx
wantErr string
maxGasWanted uint64
gasMeter sdk.GasMeter
Expand Down
10 changes: 4 additions & 6 deletions app/evmante_increment_sender_seq.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package app

import (
"cosmossdk.io/errors"
"github.com/NibiruChain/nibiru/x/evm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"

gethcommon "github.com/ethereum/go-ethereum/common"

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

// AnteDecEthIncrementSenderSequence increments the sequence of the signers.
Expand All @@ -33,15 +31,15 @@ func (issd AnteDecEthIncrementSenderSequence) AnteHandle(
next sdk.AnteHandler,
) (sdk.Context, error) {
for _, msg := range tx.GetMsgs() {
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
msgEthTx, ok := msg.(*types.MsgEthereumTx)
if !ok {
return ctx, errors.Wrapf(
errortypes.ErrUnknownRequest,
"invalid message type %T, expected %T", msg, (*evm.MsgEthereumTx)(nil),
"invalid message type %T, expected %T", msg, (*types.MsgEthereumTx)(nil),
)
}

txData, err := evm.UnpackTxData(msgEthTx.Data)
txData, err := types.UnpackTxData(msgEthTx.Data)
if err != nil {
return ctx, errors.Wrap(err, "failed to unpack tx data")
}
Expand Down
4 changes: 2 additions & 2 deletions app/evmante_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/vm"

"github.com/NibiruChain/nibiru/x/evm"
evmkeeper "github.com/NibiruChain/nibiru/x/evm/keeper"
"github.com/NibiruChain/nibiru/x/evm/statedb"
"github.com/NibiruChain/nibiru/x/oracle/types"
)

// EVMKeeper defines the expected keeper interface used on the AnteHandler
Expand All @@ -23,7 +23,7 @@ type EVMKeeper interface {
DeductTxCostsFromUserBalance(ctx sdk.Context, fees sdk.Coins, from common.Address) error
GetEvmGasBalance(ctx sdk.Context, addr common.Address) *big.Int
ResetTransientGasUsed(ctx sdk.Context)
GetParams(ctx sdk.Context) evm.Params
GetParams(ctx sdk.Context) types.Params

EVMState() evmkeeper.EvmState
}
Expand Down
5 changes: 2 additions & 3 deletions app/evmante_reject_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package app

import (
"cosmossdk.io/errors"
"github.com/NibiruChain/nibiru/x/evm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"

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

// AnteDecoratorPreventEtheruemTxMsgs prevents invalid msg types from being executed
Expand All @@ -19,7 +18,7 @@ func (rmd AnteDecoratorPreventEtheruemTxMsgs) AnteHandle(
ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler,
) (newCtx sdk.Context, err error) {
for _, msg := range tx.GetMsgs() {
if _, ok := msg.(*evm.MsgEthereumTx); ok {
if _, ok := msg.(*types.MsgEthereumTx); ok {
return ctx, errors.Wrapf(
errortypes.ErrInvalidType,
"MsgEthereumTx needs to be contained within a tx with 'ExtensionOptionsEthereumTx' option",
Expand Down
Loading

0 comments on commit 2c8e001

Please sign in to comment.