Skip to content

Commit

Permalink
Add a proto account function to EvmKeeper
Browse files Browse the repository at this point in the history
For chains with multiple supported key algorithms it is essential that
the Ethermint EOA + contract accounts are created as EthAccounts while
maintaining flexibility. This is achieved through a prototypical account
creation function, an example of which ahs been defined in
types/account.go
  • Loading branch information
ChristianBorst committed Apr 18, 2024
1 parent abd1a5d commit 6e467ea
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func NewEthermintApp(
app.EvmKeeper = evmkeeper.NewKeeper(
appCodec, keys[evmtypes.StoreKey], tkeys[evmtypes.TransientKey], app.GetSubspace(evmtypes.ModuleName),
app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.FeeMarketKeeper,
tracer,
tracer, ethermint.ProtoAccountWithAddress,
)

// Create IBC Keeper
Expand Down
9 changes: 9 additions & 0 deletions types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -52,6 +53,14 @@ func ProtoAccount() authtypes.AccountI {
}
}

// ProtoAccountWithAddress is a drop-in replacement for accountKeeper.NewAccountWithAddress(), but requires the caller to
// call accountKeeper.SetAccount() after calling this function
func ProtoAccountWithAddress(addr sdk.AccAddress) authtypes.AccountI {
acc := ProtoAccount()
acc.SetAddress(addr)
return acc
}

// GetBaseAccount returns base account.
func (acc EthAccount) GetBaseAccount() *authtypes.BaseAccount {
return acc.BaseAccount
Expand Down
22 changes: 15 additions & 7 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ package keeper
import (
"math/big"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"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/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
Expand Down Expand Up @@ -53,15 +56,19 @@ type Keeper struct {

// EVM Hooks for tx post-processing
hooks types.EvmHooks

// Creates a basic account used in account initialization
// see x/auth/keeper/keeper.go for a similar example
// ethermint.ProtoAccountWithAddress is recommended here unless another account type is needed
AccountProtoFn func(sdk.AccAddress) authtypes.AccountI
}

// NewKeeper generates new evm module keeper
func NewKeeper(
cdc codec.BinaryCodec,
storeKey, transientKey sdk.StoreKey, paramSpace paramtypes.Subspace,
ak types.AccountKeeper, bankKeeper types.BankKeeper, sk types.StakingKeeper,
fmk types.FeeMarketKeeper,
tracer string,
fmk types.FeeMarketKeeper, tracer string, accountProtoFn func(sdk.AccAddress) authtypes.AccountI,
) *Keeper {
// ensure evm module account is set
if addr := ak.GetModuleAddress(types.ModuleName); addr == nil {
Expand All @@ -84,6 +91,7 @@ func NewKeeper(
storeKey: storeKey,
transientKey: transientKey,
tracer: tracer,
AccountProtoFn: accountProtoFn,
}
}

Expand Down
3 changes: 2 additions & 1 deletion x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ func (k *Keeper) SetAccount(ctx sdk.Context, addr common.Address, account stated
cosmosAddr := sdk.AccAddress(addr.Bytes())
acct := k.accountKeeper.GetAccount(ctx, cosmosAddr)
if acct == nil {
acct = k.accountKeeper.NewAccountWithAddress(ctx, cosmosAddr)
acct = k.AccountProtoFn(cosmosAddr)
acct = k.accountKeeper.NewAccount(ctx, acct)
}

if err := acct.SetSequence(account.Nonce); err != nil {
Expand Down
1 change: 1 addition & 0 deletions x/evm/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
// AccountKeeper defines the expected account keeper interface
type AccountKeeper interface {
NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI
NewAccount(ctx sdk.Context, account authtypes.AccountI) authtypes.AccountI
GetModuleAddress(moduleName string) sdk.AccAddress
GetAllAccounts(ctx sdk.Context) (accounts []authtypes.AccountI)
IterateAccounts(ctx sdk.Context, cb func(account authtypes.AccountI) bool)
Expand Down

0 comments on commit 6e467ea

Please sign in to comment.