Skip to content

Commit

Permalink
test: Improve x/evm genesis tests (#73)
Browse files Browse the repository at this point in the history
* test: Improve x/evm genesis tests

Previously, the InitGenesis function did not have complete unit
coverage, did not check for panic values, and shared a test suite with
the handler.  This created dependencies on setup for unrelated tests and
made it difficult to add, change, or refactor genesis behavior with
confidence.

These changes use a fixture based approach to testing InitGenesis which
improves the ability to define more complex test cases and run each sub
test in isolation.  Once global state is removed from app setup, this
enables an easier transition to parallel tests.

This comes from the ability to setup the application, define a function
for each test that is provided a context and app, then generates a
fixture with required dependencies and expectations.  This allows
context to be shared across state setup, expectations, and panic values.

In addition, all InitGenesis behavior was checked in TDD fashion and
tests were added where coverage was lacking before.

* refactor: ExpectFunc default should be nil

When defining a test fixture with no expectations, the test fixture
should use a nil function for expectFunc, instead of an empty function
which serves no purpose.

This also ensures we check for nil before calling the set function in
order to prevent panics.

* refactor: Test Cases names should be capitalized

When reading, parsing test names from file, or reading verbose test
output, a capital test name is much clearer to read and comprehend than
an uncapitalized one. We mimic git commit message subjects in this
sense.

* fix: Fix incorrect capitalization of panic value

I screwed this up when renaming test names, correct expected value.
  • Loading branch information
nddeluca authored Aug 2, 2024
1 parent 9ce0e8d commit 0d4e75c
Show file tree
Hide file tree
Showing 2 changed files with 507 additions and 162 deletions.
22 changes: 21 additions & 1 deletion x/evm/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,29 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
precompile_modules "github.com/ethereum/go-ethereum/precompile/modules"

ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/keeper"
"github.com/evmos/ethermint/x/evm/types"
)

// InitGenesis initializes genesis state based on exported genesis
// InitGenesis initializes genesis state based on the provided genesis state which
// is either for a new chain, or from an exported existing chain.
//
// The context is used to provide context to keeper function calls and to load
// the EVM Chain ID, which is required by EIP-155.
//
// The EVM Keeper is used to normalize and store the provided data or genesis state.
//
// The Account Keeper is used to check corresponding accounts exist for the module
// and EVM genesis accounts.
//
// The registered precompiles list is used to ensure that any param enabled precompiles
// exist and are included in the binary.
//
// Since the data provided is assumed to have already passed basic validations,
// we only directly check stateful validations and assumptions of external state
// from the Account Keeper and registered precompile list.
func InitGenesis(
ctx sdk.Context,
k *keeper.Keeper,
Expand All @@ -40,6 +57,8 @@ func InitGenesis(
) []abci.ValidatorUpdate {
k.WithChainID(ctx)

// For an enabled precompile to be valid,
// it must exist in the binary and be registered
err := types.ValidatePrecompileRegistration(
registeredModules,
data.Params.GetEnabledPrecompiles(),
Expand All @@ -61,6 +80,7 @@ func InitGenesis(
for _, account := range data.Accounts {
address := common.HexToAddress(account.Address)
accAddress := sdk.AccAddress(address.Bytes())

// check that the EVM balance the matches the account balance
acc := accountKeeper.GetAccount(ctx, accAddress)
if acc == nil {
Expand Down
Loading

0 comments on commit 0d4e75c

Please sign in to comment.