Skip to content

Commit

Permalink
Revert "refactor(evm): Move StateDB types, use StateDB interface in k…
Browse files Browse the repository at this point in the history
…eeper"

This reverts commit 1ddf435.
  • Loading branch information
drklee3 committed Feb 9, 2024
1 parent d5c1221 commit a54e471
Show file tree
Hide file tree
Showing 23 changed files with 156 additions and 218 deletions.
4 changes: 2 additions & 2 deletions x/evm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/evmos/ethermint/crypto/ethsecp256k1"
etherminttypes "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
"github.com/evmos/ethermint/x/evm/vm"
)

func (suite *EvmTestSuite) TestInitGenesis() {
Expand All @@ -19,7 +19,7 @@ func (suite *EvmTestSuite) TestInitGenesis() {

address := common.HexToAddress(privkey.PubKey().Address().String())

var vmdb vm.StateDB
var vmdb *statedb.StateDB

testCases := []struct {
name string
Expand Down
5 changes: 2 additions & 3 deletions x/evm/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/evmos/ethermint/x/evm/keeper"
"github.com/evmos/ethermint/x/evm/vm"

sdkmath "cosmossdk.io/math"
"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -182,8 +181,8 @@ func (suite *EvmTestSuite) SignTx(tx *types.MsgEthereumTx) {
suite.Require().NoError(err)
}

func (suite *EvmTestSuite) StateDB() vm.StateDB {
return statedb.New(suite.ctx, suite.app.EvmKeeper, types.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash().Bytes())))
func (suite *EvmTestSuite) StateDB() *statedb.StateDB {
return statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash().Bytes())))
}

func TestEvmTestSuite(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions x/evm/keeper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (k *Keeper) EVMConfig(ctx sdk.Context, proposerAddress sdk.ConsAddress, cha
}

// TxConfig loads `TxConfig` from current transient storage
func (k *Keeper) TxConfig(ctx sdk.Context, txHash common.Hash) types.TxConfig {
return types.NewTxConfig(
func (k *Keeper) TxConfig(ctx sdk.Context, txHash common.Hash) statedb.TxConfig {
return statedb.NewTxConfig(
common.BytesToHash(ctx.HeaderHash()), // BlockHash
txHash, // TxHash
uint(k.GetTxIndexTransient(ctx)), // TxIndex
Expand Down
10 changes: 5 additions & 5 deletions x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (k Keeper) EthCall(c context.Context, req *types.EthCallRequest) (*types.Ms
return nil, status.Error(codes.InvalidArgument, err.Error())
}

txConfig := types.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))

// pass false to not commit StateDB
res, err := k.ApplyMessageWithConfig(ctx, msg, nil, false, cfg, txConfig)
Expand Down Expand Up @@ -324,7 +324,7 @@ func (k Keeper) EstimateGas(c context.Context, req *types.EthCallRequest) (*type
nonce := k.GetNonce(ctx, args.GetFrom())
args.Nonce = (*hexutil.Uint64)(&nonce)

txConfig := types.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))

// convert the tx args to an ethereum message
msg, err := args.ToMessage(req.GasCap, cfg.BaseFee)
Expand Down Expand Up @@ -423,7 +423,7 @@ func (k Keeper) TraceTx(c context.Context, req *types.QueryTraceTxRequest) (*typ
}
signer := ethtypes.MakeSigner(cfg.ChainConfig, big.NewInt(ctx.BlockHeight()))

txConfig := types.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))
for i, tx := range req.Predecessors {
ethTx := tx.AsTransaction()
msg, err := ethTx.AsMessage(signer, cfg.BaseFee)
Expand Down Expand Up @@ -503,7 +503,7 @@ func (k Keeper) TraceBlock(c context.Context, req *types.QueryTraceBlockRequest)
txsLength := len(req.Txs)
results := make([]*types.TxTraceResult, 0, txsLength)

txConfig := types.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))
for i, tx := range req.Txs {
result := types.TxTraceResult{}
ethTx := tx.AsTransaction()
Expand Down Expand Up @@ -533,7 +533,7 @@ func (k Keeper) TraceBlock(c context.Context, req *types.QueryTraceBlockRequest)
func (k *Keeper) traceTx(
ctx sdk.Context,
cfg *statedb.EVMConfig,
txConfig types.TxConfig,
txConfig statedb.TxConfig,
signer ethtypes.Signer,
tx *ethtypes.Transaction,
traceConfig *types.TraceConfig,
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (suite *KeeperTestSuite) TestQueryTxLogs() {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

vmdb := statedb.New(suite.ctx, suite.app.EvmKeeper, types.NewTxConfig(common.BytesToHash(suite.ctx.HeaderHash().Bytes()), txHash, txIndex, logIndex))
vmdb := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewTxConfig(common.BytesToHash(suite.ctx.HeaderHash().Bytes()), txHash, txIndex, logIndex))
tc.malleate(vmdb)
suite.Require().NoError(vmdb.Commit())

Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (suite *KeeperTestSuite) TestEvmHooks() {
k := suite.app.EvmKeeper
ctx := suite.ctx
txHash := common.BigToHash(big.NewInt(1))
vmdb := statedb.New(ctx, k, types.NewTxConfig(
vmdb := statedb.New(ctx, k, statedb.NewTxConfig(
common.BytesToHash(ctx.HeaderHash().Bytes()),
txHash,
0,
Expand Down
39 changes: 17 additions & 22 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/tendermint/tendermint/libs/log"

ethermint "github.com/evmos/ethermint/types"
"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"
Expand Down Expand Up @@ -76,10 +77,6 @@ type Keeper struct {

// evm constructor function
evmConstructor evm.Constructor

// stateDB constructor function
stateDBConstructor evm.StateDBConstructor

// Legacy subspace
ss paramstypes.Subspace
}
Expand All @@ -95,7 +92,6 @@ func NewKeeper(
fmk types.FeeMarketKeeper,
customPrecompiles evm.PrecompiledContracts,
evmConstructor evm.Constructor,
stateDBConstructor evm.StateDBConstructor,
tracer string,
ss paramstypes.Subspace,
) *Keeper {
Expand All @@ -115,19 +111,18 @@ 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,
stateDBConstructor: stateDBConstructor,
tracer: tracer,
ss: ss,
cdc: cdc,
authority: authority,
accountKeeper: ak,
bankKeeper: bankKeeper,
stakingKeeper: sk,
feeMarketKeeper: fmk,
storeKey: storeKey,
transientKey: transientKey,
customPrecompiles: customPrecompiles,
evmConstructor: evmConstructor,
tracer: tracer,
ss: ss,
}
}

Expand Down Expand Up @@ -284,7 +279,7 @@ func (k Keeper) Tracer(ctx sdk.Context, msg core.Message, ethCfg *params.ChainCo

// GetAccountWithoutBalance load nonce and codehash without balance,
// more efficient in cases where balance is not needed.
func (k *Keeper) GetAccountWithoutBalance(ctx sdk.Context, addr common.Address) *types.StateDBAccount {
func (k *Keeper) GetAccountWithoutBalance(ctx sdk.Context, addr common.Address) *statedb.Account {
cosmosAddr := sdk.AccAddress(addr.Bytes())
acct := k.accountKeeper.GetAccount(ctx, cosmosAddr)
if acct == nil {
Expand All @@ -297,21 +292,21 @@ func (k *Keeper) GetAccountWithoutBalance(ctx sdk.Context, addr common.Address)
codeHash = ethAcct.GetCodeHash().Bytes()
}

return &types.StateDBAccount{
return &statedb.Account{
Nonce: acct.GetSequence(),
CodeHash: codeHash,
}
}

// GetAccountOrEmpty returns empty account if not exist, returns error if it's not `EthAccount`
func (k *Keeper) GetAccountOrEmpty(ctx sdk.Context, addr common.Address) types.StateDBAccount {
func (k *Keeper) GetAccountOrEmpty(ctx sdk.Context, addr common.Address) statedb.Account {
acct := k.GetAccount(ctx, addr)
if acct != nil {
return *acct
}

// empty account
return types.StateDBAccount{
return statedb.Account{
Balance: new(big.Int),
CodeHash: types.EmptyCodeHash,
}
Expand Down
11 changes: 3 additions & 8 deletions x/evm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/evmos/ethermint/x/evm/vm"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -253,12 +252,8 @@ func (suite *KeeperTestSuite) Commit() {
suite.queryClient = types.NewQueryClient(queryHelper)
}

func (suite *KeeperTestSuite) StateDB() vm.StateDB {
return statedb.New(
suite.ctx,
suite.app.EvmKeeper,
types.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash().Bytes())),
)
func (suite *KeeperTestSuite) StateDB() *statedb.StateDB {
return statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash().Bytes())))
}

// DeployTestContract deploy a test erc20 contract and returns the contract address
Expand Down Expand Up @@ -503,7 +498,7 @@ func (suite *KeeperTestSuite) TestGetAccountStorage() {
}

func (suite *KeeperTestSuite) TestGetAccountOrEmpty() {
empty := types.StateDBAccount{
empty := statedb.Account{
Balance: new(big.Int),
CodeHash: types.EmptyCodeHash,
}
Expand Down
4 changes: 2 additions & 2 deletions x/evm/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (

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

func (suite *KeeperTestSuite) TestEthereumTx() {
var (
err error
msg *types.MsgEthereumTx
signer ethtypes.Signer
vmdb vm.StateDB
vmdb *statedb.StateDB
chainCfg *params.ChainConfig
expectedGasUsed uint64
)
Expand Down
4 changes: 0 additions & 4 deletions x/evm/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/evmos/ethermint/app"
"github.com/evmos/ethermint/encoding"
"github.com/evmos/ethermint/x/evm/keeper"
"github.com/evmos/ethermint/x/evm/statedb"
"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"
Expand Down Expand Up @@ -153,7 +152,6 @@ func (suite *KeeperTestSuite) TestLegacyParamsKeyTableRegistration() {
ak,
nil, nil, nil, nil, // OK to pass nil in for these since we only instantiate and use params
geth.NewEVM,
statedb.New,
"",
unregisteredSubspace,
)
Expand Down Expand Up @@ -211,7 +209,6 @@ func (suite *KeeperTestSuite) TestRenamedFieldReturnsProperValueForLegacyParams(
ak,
nil, nil, nil, nil,
geth.NewEVM,
statedb.New,
"",
subspace,
)
Expand Down Expand Up @@ -244,7 +241,6 @@ func (suite *KeeperTestSuite) TestNilLegacyParamsDoNotPanic() {
ak,
nil, nil, nil, nil, // OK to pass nil in for these since we only instantiate and use params
geth.NewEVM,
statedb.New,
"",
subspace,
)
Expand Down
6 changes: 3 additions & 3 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (k *Keeper) ApplyMessage(ctx sdk.Context, msg core.Message, tracer vm.EVMLo
return nil, errorsmod.Wrap(err, "failed to load evm config")
}

txConfig := types.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
return k.ApplyMessageWithConfig(ctx, msg, tracer, commit, cfg, txConfig)
}

Expand Down Expand Up @@ -315,7 +315,7 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,
tracer vm.EVMLogger,
commit bool,
cfg *statedb.EVMConfig,
txConfig types.TxConfig,
txConfig statedb.TxConfig,
) (*types.MsgEthereumTxResponse, error) {
var (
ret []byte // return bytes from evm execution
Expand All @@ -329,7 +329,7 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context,
return nil, errorsmod.Wrap(types.ErrCallDisabled, "failed to call contract")
}

stateDB := k.stateDBConstructor(ctx, k, txConfig)
stateDB := statedb.New(ctx, k, txConfig)
evm := k.NewEVM(ctx, msg, cfg, tracer, stateDB)

leftoverGas := msg.Gas()
Expand Down
5 changes: 2 additions & 3 deletions x/evm/keeper/state_transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"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/params"
"github.com/evmos/ethermint/tests"
"github.com/evmos/ethermint/x/evm/keeper"
Expand Down Expand Up @@ -575,8 +574,8 @@ func (suite *KeeperTestSuite) TestApplyMessageWithConfig() {
config *statedb.EVMConfig
keeperParams types.Params
signer ethtypes.Signer
vmdb vm.StateDB
txConfig types.TxConfig
vmdb *statedb.StateDB
txConfig statedb.TxConfig
chainCfg *params.ChainConfig
)

Expand Down
8 changes: 4 additions & 4 deletions x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
"github.com/evmos/ethermint/x/evm/vm"
)

var _ vm.StateDBKeeper = &Keeper{}
var _ statedb.Keeper = &Keeper{}

// ----------------------------------------------------------------------------
// StateDB Keeper implementation
// ----------------------------------------------------------------------------

// GetAccount returns nil if account is not exist, returns error if it's not `EthAccountI`
func (k *Keeper) GetAccount(ctx sdk.Context, addr common.Address) *types.StateDBAccount {
func (k *Keeper) GetAccount(ctx sdk.Context, addr common.Address) *statedb.Account {
acct := k.GetAccountWithoutBalance(ctx, addr)
if acct == nil {
return nil
Expand Down Expand Up @@ -119,7 +119,7 @@ func (k *Keeper) SetBalance(ctx sdk.Context, addr common.Address, amount *big.In
}

// SetAccount updates nonce/balance/codeHash together.
func (k *Keeper) SetAccount(ctx sdk.Context, addr common.Address, account types.StateDBAccount) error {
func (k *Keeper) SetAccount(ctx sdk.Context, addr common.Address, account statedb.Account) error {
// update account
cosmosAddr := sdk.AccAddress(addr.Bytes())
acct := k.accountKeeper.GetAccount(ctx, cosmosAddr)
Expand Down
Loading

0 comments on commit a54e471

Please sign in to comment.