Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove VM wrapper for custom precompiles #51

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/ante/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (ctd CanTransferDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate

// 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 && !evm.Context.CanTransfer(stateDB, coreMsg.From(), coreMsg.Value()) {
return ctx, errorsmod.Wrapf(
errortypes.ErrInsufficientFunds,
"failed to transfer %s from address %s using the EVM block context transfer function",
Expand Down
3 changes: 1 addition & 2 deletions app/ante/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/evmos/ethermint/x/evm/statedb"
evmtypes "github.com/evmos/ethermint/x/evm/types"
evm "github.com/evmos/ethermint/x/evm/vm"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
)

Expand All @@ -44,7 +43,7 @@ type EVMKeeper interface {
statedb.Keeper
DynamicFeeEVMKeeper

NewEVM(ctx sdk.Context, msg core.Message, cfg *statedb.EVMConfig, tracer vm.EVMLogger, stateDB vm.StateDB) evm.EVM
NewEVM(ctx sdk.Context, msg core.Message, cfg *statedb.EVMConfig, tracer vm.EVMLogger, stateDB vm.StateDB) *vm.EVM
DeductTxCostsFromUserBalance(ctx sdk.Context, fees sdk.Coins, from common.Address) error
GetBalance(ctx sdk.Context, addr common.Address) *big.Int
ResetTransientGasUsed(ctx sdk.Context)
Expand Down
11 changes: 5 additions & 6 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
"os"
"path/filepath"

"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"

dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
tmos "github.com/cometbft/cometbft/libs/os"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"

"cosmossdk.io/simapp"
simappparams "cosmossdk.io/simapp/params"
Expand Down Expand Up @@ -127,7 +127,6 @@ import (
evmkeeper "github.com/evmos/ethermint/x/evm/keeper"
evmtypes "github.com/evmos/ethermint/x/evm/types"
legacyevmtypes "github.com/evmos/ethermint/x/evm/types/legacy"
"github.com/evmos/ethermint/x/evm/vm/geth"
"github.com/evmos/ethermint/x/feemarket"
feemarketkeeper "github.com/evmos/ethermint/x/feemarket/keeper"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
Expand Down Expand Up @@ -427,7 +426,7 @@ func NewEthermintApp(
appCodec, keys[evmtypes.StoreKey], tkeys[evmtypes.TransientKey],
authtypes.NewModuleAddress(govtypes.ModuleName),
app.AccountKeeper, app.BankKeeper, stakingKeeper, app.FeeMarketKeeper,
nil, geth.NewEVM, tracer, evmSs,
vm.NewEVM, tracer, evmSs,
)

// Create IBC Keeper
Expand Down
32 changes: 13 additions & 19 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
legacytypes "github.com/evmos/ethermint/x/evm/types/legacy"
evm "github.com/evmos/ethermint/x/evm/vm"
)

// Keeper grants access to the EVM module state and implements the go-ethereum StateDB interface.
Expand Down Expand Up @@ -72,11 +71,8 @@ type Keeper struct {
// EVM Hooks for tx post-processing
hooks types.EvmHooks

// custom stateless precompiled smart contracts
customPrecompiles evm.PrecompiledContracts

// evm constructor function
evmConstructor evm.Constructor
evmConstructor types.Constructor
// Legacy subspace
ss paramstypes.Subspace
}
Expand All @@ -90,8 +86,7 @@ func NewKeeper(
bankKeeper types.BankKeeper,
sk types.StakingKeeper,
fmk types.FeeMarketKeeper,
customPrecompiles evm.PrecompiledContracts,
evmConstructor evm.Constructor,
evmConstructor types.Constructor,
tracer string,
ss paramstypes.Subspace,
) *Keeper {
Expand All @@ -111,18 +106,17 @@ func NewKeeper(

// NOTE: we pass in the parameter space to the CommitStateDB in order to use custom denominations for the EVM operations
return &Keeper{
cdc: cdc,
authority: authority,
accountKeeper: ak,
bankKeeper: bankKeeper,
stakingKeeper: sk,
feeMarketKeeper: fmk,
storeKey: storeKey,
transientKey: transientKey,
customPrecompiles: customPrecompiles,
evmConstructor: evmConstructor,
tracer: tracer,
ss: ss,
cdc: cdc,
authority: authority,
accountKeeper: ak,
bankKeeper: bankKeeper,
stakingKeeper: sk,
feeMarketKeeper: fmk,
storeKey: storeKey,
transientKey: transientKey,
evmConstructor: evmConstructor,
tracer: tracer,
ss: ss,
}
}

Expand Down
14 changes: 7 additions & 7 deletions x/evm/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/evmos/ethermint/app"
"github.com/evmos/ethermint/encoding"
"github.com/evmos/ethermint/x/evm/keeper"
"github.com/evmos/ethermint/x/evm/types"
legacytypes "github.com/evmos/ethermint/x/evm/types/legacy"
legacytestutil "github.com/evmos/ethermint/x/evm/types/legacy/testutil"
"github.com/evmos/ethermint/x/evm/vm/geth"
)

func (suite *KeeperTestSuite) TestParams() {
Expand Down Expand Up @@ -150,8 +150,8 @@ func (suite *KeeperTestSuite) TestLegacyParamsKeyTableRegistration() {
return keeper.NewKeeper(
cdc, storeKey, tKey, authtypes.NewModuleAddress("gov"),
ak,
nil, nil, nil, nil, // OK to pass nil in for these since we only instantiate and use params
geth.NewEVM,
nil, nil, nil, // OK to pass nil in for these since we only instantiate and use params
vm.NewEVM,
"",
unregisteredSubspace,
)
Expand Down Expand Up @@ -207,8 +207,8 @@ func (suite *KeeperTestSuite) TestRenamedFieldReturnsProperValueForLegacyParams(
k := keeper.NewKeeper(
cdc, storeKey, tKey, authtypes.NewModuleAddress("gov"),
ak,
nil, nil, nil, nil,
geth.NewEVM,
nil, nil, nil,
vm.NewEVM,
"",
subspace,
)
Expand Down Expand Up @@ -239,8 +239,8 @@ func (suite *KeeperTestSuite) TestNilLegacyParamsDoNotPanic() {
k := keeper.NewKeeper(
cdc, storeKey, tKey, authtypes.NewModuleAddress("gov"),
ak,
nil, nil, nil, nil, // OK to pass nil in for these since we only instantiate and use params
geth.NewEVM,
nil, nil, nil, // OK to pass nil in for these since we only instantiate and use params
vm.NewEVM,
"",
subspace,
)
Expand Down
18 changes: 8 additions & 10 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"

ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
evm "github.com/evmos/ethermint/x/evm/vm"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
)

// NewEVM generates a go-ethereum VM from the provided Message fields and the chain parameters
Expand All @@ -51,7 +49,7 @@ func (k *Keeper) NewEVM(
cfg *statedb.EVMConfig,
tracer vm.EVMLogger,
stateDB vm.StateDB,
) evm.EVM {
) *vm.EVM {
blockCtx := vm.BlockContext{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
Expand All @@ -70,7 +68,7 @@ func (k *Keeper) NewEVM(
tracer = k.Tracer(ctx, msg, cfg.ChainConfig)
}
vmConfig := k.VMConfig(ctx, msg, cfg, tracer)
return k.evmConstructor(blockCtx, txCtx, stateDB, cfg.ChainConfig, vmConfig, k.customPrecompiles)
return k.evmConstructor(blockCtx, txCtx, stateDB, cfg.ChainConfig, vmConfig)
}

// GetHashFn implements vm.GetHashFunc for Ethermint. It handles 3 cases:
Expand Down Expand Up @@ -335,7 +333,7 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,
leftoverGas := msg.Gas()

// Allow the tracer captures the tx level events, mainly the gas consumption.
vmCfg := evm.Config()
vmCfg := evm.Config
if vmCfg.Debug {
vmCfg.Tracer.CaptureTxStart(leftoverGas)
defer func() {
Expand All @@ -345,7 +343,7 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,

sender := vm.AccountRef(msg.From())
contractCreation := msg.To() == nil
isLondon := cfg.ChainConfig.IsLondon(evm.Context().BlockNumber)
isLondon := cfg.ChainConfig.IsLondon(evm.Context.BlockNumber)

intrinsicGas, err := k.GetEthIntrinsicGas(ctx, msg, cfg.ChainConfig, contractCreation)
if err != nil {
Expand All @@ -363,7 +361,7 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,
// access list preparation is moved from ante handler to here, because it's needed when `ApplyMessage` is called
// under contexts where ante handlers are not run, for example `eth_call` and `eth_estimateGas`.
if rules := cfg.ChainConfig.Rules(big.NewInt(ctx.BlockHeight()), cfg.ChainConfig.MergeNetsplitBlock != nil); rules.IsBerlin {
stateDB.PrepareAccessList(msg.From(), msg.To(), evm.ActivePrecompiles(rules), msg.AccessList())
stateDB.PrepareAccessList(msg.From(), msg.To(), vm.ActivePrecompiles(rules), msg.AccessList())
}

if contractCreation {
Expand Down
6 changes: 3 additions & 3 deletions x/evm/migrations/v3/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package v3_test
import (
"testing"

"github.com/ethereum/go-ethereum/core/vm"
"github.com/evmos/ethermint/x/evm/keeper"
"github.com/evmos/ethermint/x/evm/types"
"github.com/evmos/ethermint/x/evm/vm/geth"
"github.com/stretchr/testify/require"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -149,8 +149,8 @@ func TestKeyTableCompatiabilityWithKeeper(t *testing.T) {
keeper.NewKeeper(
cdc, storeKey, tKey, authtypes.NewModuleAddress("gov"),
ak,
nil, nil, nil, nil,
geth.NewEVM,
nil, nil, nil,
vm.NewEVM,
"",
subspace,
)
Expand Down
17 changes: 14 additions & 3 deletions x/evm/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package types
import (
"math/big"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/ethereum/go-ethereum/core"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"

feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
)

Expand Down Expand Up @@ -83,3 +84,13 @@ type (
WithKeyTable(table paramtypes.KeyTable) paramtypes.Subspace
}
)

// Constructor defines the function used to instantiate the EVM on
// each state transition.
type Constructor func(
blockCtx vm.BlockContext,
txCtx vm.TxContext,
stateDB vm.StateDB,
chainConfig *params.ChainConfig,
config vm.Config,
) *vm.EVM
95 changes: 0 additions & 95 deletions x/evm/vm/geth/geth.go

This file was deleted.

Loading
Loading