Skip to content

Commit

Permalink
Merge pull request #51 from Kava-Labs/yevhenii/remove-vm-wrapper
Browse files Browse the repository at this point in the history
Remove VM wrapper for custom precompiles
  • Loading branch information
evgeniy-scherbina authored Apr 18, 2024
2 parents 4bdbc06 + 05a3a90 commit dc525b7
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 273 deletions.
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

0 comments on commit dc525b7

Please sign in to comment.