Skip to content

Commit

Permalink
Added tests for InitGenesis in x/evm
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed May 1, 2024
1 parent ee18475 commit 7905fcf
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 76 deletions.
3 changes: 2 additions & 1 deletion x/evm/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ func InitGenesis(
k *keeper.Keeper,
accountKeeper types.AccountKeeper,
data types.GenesisState,
registeredModules []precompile_modules.Module,
) []abci.ValidatorUpdate {
k.WithChainID(ctx)

err := types.CheckIfEnabledPrecompilesAreRegistered(
precompile_modules.RegisteredModules(),
registeredModules,
data.Params.GetEnabledPrecompiles(),
)
if err != nil {
Expand Down
181 changes: 112 additions & 69 deletions x/evm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package evm_test
import (
"math/big"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
precompile_modules "github.com/ethereum/go-ethereum/precompile/modules"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
etherminttypes "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm"
Expand All @@ -18,123 +19,165 @@ func (suite *EvmTestSuite) TestInitGenesis() {
suite.Require().NoError(err)

address := common.HexToAddress(privkey.PubKey().Address().String())
hexAddr1 := "0x1000000000000000000000000000000000000000"

var vmdb *statedb.StateDB

testCases := []struct {
name string
malleate func()
genState *types.GenesisState
expPanic bool
name string
malleate func()
getGenState func() *types.GenesisState
registeredModules []precompile_modules.Module
expPanic bool
}{
{
"default",
func() {},
types.DefaultGenesisState(),
false,
name: "default",
malleate: func() {},
getGenState: func() *types.GenesisState {
return types.DefaultGenesisState()
},
expPanic: false,
},
{
"valid account",
func() {
name: "valid account",
malleate: func() {
vmdb.AddBalance(address, big.NewInt(1))
},
&types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
Storage: types.Storage{
{Key: common.BytesToHash([]byte("key")).String(), Value: common.BytesToHash([]byte("value")).String()},
getGenState: func() *types.GenesisState {
return &types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
Storage: types.Storage{
{Key: common.BytesToHash([]byte("key")).String(), Value: common.BytesToHash([]byte("value")).String()},
},
},
},
},
}
},
false,
expPanic: false,
},
{
"account not found",
func() {},
&types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
name: "account not found",
malleate: func() {},
getGenState: func() *types.GenesisState {
return &types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
},
},
},
}
},
true,
expPanic: true,
},
{
"invalid account type",
func() {
name: "invalid account type",
malleate: func() {
acc := authtypes.NewBaseAccountWithAddress(address.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
},
&types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
getGenState: func() *types.GenesisState {
return &types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
},
},
},
}
},
true,
expPanic: true,
},
{
"invalid code hash",
func() {
name: "invalid code hash",
malleate: func() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
},
&types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
Code: "ffffffff",
getGenState: func() *types.GenesisState {
return &types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
Code: "ffffffff",
},
},
},
}
},
true,
expPanic: true,
},
{
"ignore empty account code checking",
func() {
name: "ignore empty account code checking",
malleate: func() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes())

suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
},
&types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
Code: "",
getGenState: func() *types.GenesisState {
return &types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
Code: "",
},
},
},
}
},
false,
expPanic: false,
},
{
"ignore empty account code checking with non-empty codehash",
func() {
name: "ignore empty account code checking with non-empty codehash",
malleate: func() {
ethAcc := &etherminttypes.EthAccount{
BaseAccount: authtypes.NewBaseAccount(address.Bytes(), nil, 0, 0),
CodeHash: common.BytesToHash([]byte{1, 2, 3}).Hex(),
}

suite.app.AccountKeeper.SetAccount(suite.ctx, ethAcc)
},
&types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
Code: "",
getGenState: func() *types.GenesisState {
return &types.GenesisState{
Params: types.DefaultParams(),
Accounts: []types.GenesisAccount{
{
Address: address.String(),
Code: "",
},
},
}
},
expPanic: false,
},
{
name: "precompile is enabled and registered",
malleate: func() {},
getGenState: func() *types.GenesisState {
defaultGen := types.DefaultGenesisState()
defaultGen.Params.EnabledPrecompiles = []string{hexAddr1}
return defaultGen
},
registeredModules: []precompile_modules.Module{
{
Address: common.HexToAddress(hexAddr1),
},
},
false,
expPanic: false,
},
{
name: "precompile is enabled, but not registered",
malleate: func() {},
getGenState: func() *types.GenesisState {
defaultGen := types.DefaultGenesisState()
defaultGen.Params.EnabledPrecompiles = []string{hexAddr1}
return defaultGen
},
registeredModules: nil,
expPanic: true,
},
}

Expand All @@ -149,13 +192,13 @@ func (suite *EvmTestSuite) TestInitGenesis() {
if tc.expPanic {
suite.Require().Panics(
func() {
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.genState)
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.getGenState(), tc.registeredModules)
},
)
} else {
suite.Require().NotPanics(
func() {
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.genState)
_ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.getGenState(), tc.registeredModules)
},
)
}
Expand Down
11 changes: 5 additions & 6 deletions x/evm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
precompile_modules "github.com/ethereum/go-ethereum/precompile/modules"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

"github.com/evmos/ethermint/x/evm/client/cli"
"github.com/evmos/ethermint/x/evm/keeper"
Expand Down Expand Up @@ -166,7 +165,7 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
InitGenesis(ctx, am.keeper, am.ak, genesisState)
InitGenesis(ctx, am.keeper, am.ak, genesisState, precompile_modules.RegisteredModules())
return []abci.ValidatorUpdate{}
}

Expand Down

0 comments on commit 7905fcf

Please sign in to comment.