Skip to content

Commit

Permalink
refactors to align genesis states with example chain and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Aug 20, 2024
1 parent b99dfdc commit 50f5a5f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 34 deletions.
8 changes: 2 additions & 6 deletions example_chain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,16 +799,12 @@ func (app *ExampleChain) TxConfig() client.TxConfig {
func (a *ExampleChain) DefaultGenesis() map[string]json.RawMessage {
genesis := ModuleBasics.DefaultGenesis(a.appCodec)

// NOTE: for the example chain implementation we need to set the default EVM denomination
evmGenState := evmtypes.DefaultGenesisState()
evmGenState.Params.EvmDenom = ExampleChainDenom
evmGenState := NewEVMGenesisState()
genesis[evmtypes.ModuleName] = a.appCodec.MustMarshalJSON(evmGenState)

// NOTE: for the example chain implementation we are also adding a default token pair,
// which is the base denomination of the chain (i.e. the WEVMOS contract)
erc20GenState := erc20types.DefaultGenesisState()
erc20GenState.TokenPairs = ExampleTokenPairs
erc20GenState.Params.NativePrecompiles = append(erc20GenState.Params.NativePrecompiles, WEVMOSContractMainnet)
erc20GenState := NewErc20GenesisState()
genesis[erc20types.ModuleName] = a.appCodec.MustMarshalJSON(erc20GenState)

return genesis
Expand Down
34 changes: 30 additions & 4 deletions example_chain/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"cosmossdk.io/simapp"
"github.com/evmos/os/encoding"
erc20types "github.com/evmos/os/x/erc20/types"
evmtypes "github.com/evmos/os/x/evm/types"
)

Expand All @@ -26,10 +27,35 @@ func NewDefaultGenesisState() simapp.GenesisState {

genesisState := ModuleBasics.DefaultGenesis(encCfg.Codec)

// define new chain-specific EVM genesis state with correct EVM denom
evmGenesis := evmtypes.DefaultGenesisState()
evmGenesis.Params.EvmDenom = ExampleChainDenom
genesisState[evmtypes.ModuleName] = encCfg.Codec.MustMarshalJSON(evmGenesis)
evmGenState := NewEVMGenesisState()
genesisState[evmtypes.ModuleName] = encCfg.Codec.MustMarshalJSON(evmGenState)

erc20GenState := NewErc20GenesisState()
genesisState[erc20types.ModuleName] = encCfg.Codec.MustMarshalJSON(erc20GenState)

return genesisState
}

// NewEVMGenesisState returns the default genesis state for the EVM module.
//
// NOTE: for the example chain implementation we need to set the default EVM denomination
// and enable ALL precompiles.
func NewEVMGenesisState() *evmtypes.GenesisState {
evmGenState := evmtypes.DefaultGenesisState()
evmGenState.Params.EvmDenom = ExampleChainDenom
evmGenState.Params.ActiveStaticPrecompiles = evmtypes.AvailableStaticPrecompiles

return evmGenState
}

// NewErc20GenesisState returns the default genesis state for the ERC20 module.
//
// NOTE: for the example chain implementation we are also adding a default token pair,
// which is the base denomination of the chain (i.e. the WEVMOS contract).
func NewErc20GenesisState() *erc20types.GenesisState {
erc20GenState := erc20types.DefaultGenesisState()
erc20GenState.TokenPairs = ExampleTokenPairs
erc20GenState.Params.NativePrecompiles = append(erc20GenState.Params.NativePrecompiles, WEVMOSContractMainnet)

return erc20GenState
}
2 changes: 1 addition & 1 deletion example_chain/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,6 @@ func SetupTestingApp(chainID string) func() (ibctesting.TestingApp, map[string]j
simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome),
baseapp.SetChainID(chainID),
)
return app, NewDefaultGenesisState()
return app, app.DefaultGenesis()
}
}
3 changes: 3 additions & 0 deletions example_chain/testutil/eth_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func EthSetupWithDB(isCheckTx bool, chainID string, patchGenesis func(*example_a
// NewTestGenesisState generate genesis state with single validator
//
// It is also setting up the EVM parameters to use sensible defaults.
//
// TODO: are these different genesis functions necessary or can they all be refactored into one?
// there's also other genesis state functions; some like app.DefaultGenesis() or others in test helpers only.
func NewTestGenesisState(codec codec.Codec) simapp.GenesisState {
privVal := mock.NewPV()
pubKey, err := privVal.GetPubKey()
Expand Down
2 changes: 1 addition & 1 deletion precompiles/staking/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func (s *PrecompileTestSuite) TestRun() {

precompiles, found, err := s.app.EVMKeeper.GetPrecompileInstance(s.ctx, contractAddr)
s.Require().NoError(err, "failed to instantiate precompile")
s.Require().True(found, "not found precompile")
s.Require().True(found, "precompile not found")
evm.WithPrecompiles(precompiles.Map, precompiles.Addresses)

// Run precompiled contract
Expand Down
10 changes: 5 additions & 5 deletions testutil/integration/os/network/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,16 @@ func setDefaultGovGenesisState(exampleApp *example_app.ExampleChain, genesisStat
}

func setDefaultErc20GenesisState(exampleApp *example_app.ExampleChain, genesisState simapp.GenesisState) simapp.GenesisState {
erc20Gen := erc20types.DefaultGenesisState()
// TODO: add test case to ensure that this is the same as the default genesis for the example app
erc20Gen := example_app.NewErc20GenesisState()

genesisState[erc20types.ModuleName] = exampleApp.AppCodec().MustMarshalJSON(erc20Gen)
return genesisState
}

func setDefaultEVMGenesisState(exampleApp *example_app.ExampleChain, genesisState simapp.GenesisState) simapp.GenesisState {
evmGen := evmtypes.DefaultGenesisState()

// Set the EVM denomination for the given chain
evmGen.Params.EvmDenom = testutil.ExampleAttoDenom
// TODO: add test case to ensure that this is the same as the default genesis for the example app
evmGen := example_app.NewEVMGenesisState()

genesisState[evmtypes.ModuleName] = exampleApp.AppCodec().MustMarshalJSON(evmGen)
return genesisState
Expand Down
33 changes: 16 additions & 17 deletions testutil/integration/os/utils/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package utils
import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
example_app "github.com/evmos/os/example_chain"
"github.com/evmos/os/testutil"
testkeyring "github.com/evmos/os/testutil/integration/os/keyring"
"github.com/evmos/os/testutil/integration/os/network"
erc20types "github.com/evmos/os/x/erc20/types"
evmtypes "github.com/evmos/os/x/evm/types"
)

const (
Expand Down Expand Up @@ -44,26 +44,25 @@ func CreateGenesisWithTokenPairs(keyring testkeyring.Keyring) network.CustomGene
}

// Add token pairs to genesis
erc20GenesisState := erc20types.DefaultGenesisState()
erc20GenesisState.TokenPairs = []erc20types.TokenPair{{
Erc20Address: erc20TokenPairHex,
Denom: "xmpl",
Enabled: true,
ContractOwner: erc20types.OWNER_MODULE, // NOTE: Owner is the module account since it's a native token and was registered through governance
}, {
Erc20Address: testutil.WEVMOSContractTestnet,
Denom: testutil.ExampleAttoDenom,
Enabled: true,
ContractOwner: erc20types.OWNER_MODULE, // NOTE: Owner is the module account since it's a native token and was registered through governance
}}

// Add the smart contracts to the EVM genesis
evmGenesisState := evmtypes.DefaultGenesisState()
erc20GenesisState := example_app.NewErc20GenesisState()
erc20GenesisState.TokenPairs = append(erc20GenesisState.TokenPairs,
erc20types.TokenPair{
Erc20Address: erc20TokenPairHex,
Denom: "xmpl",
Enabled: true,
ContractOwner: erc20types.OWNER_MODULE, // NOTE: Owner is the module account since it's a native token and was registered through governance
},
erc20types.TokenPair{
Erc20Address: testutil.WEVMOSContractTestnet,
Denom: testutil.ExampleAttoDenom,
Enabled: true,
ContractOwner: erc20types.OWNER_MODULE, // NOTE: Owner is the module account since it's a native token and was registered through governance
},
)

// Combine module genesis states
return network.CustomGenesisState{
authtypes.ModuleName: accGenesisState,
erc20types.ModuleName: erc20GenesisState,
evmtypes.ModuleName: evmGenesisState,
}
}

0 comments on commit 50f5a5f

Please sign in to comment.