Skip to content

Commit

Permalink
Improve x/evm genesis state testing (#69)
Browse files Browse the repository at this point in the history
* refactor param test cases to allow sharing up to genesis tests; add
extra param sorting and uniqueness tests; remove legacy validations used
for old param store; add enabled precompiles to params constructor and
default params

* refactor storage validations to prevent false positives; improve
complexity of test cases; prevent access of key before validation; and
use empty struct map for uniq checks

* remove dead storage slice copy code; this test checked that the data
was the same, however did not check it was actually a copy, so instead
of improving tests, remove the unused code

* use types_test package for genesis tests; refactor genesis account tests

* update genesis tests to remove false positives; removed outdated tests
that did not match test name; increase test coverage and confidence;
refactor uniqueness check to use struct map and to not access account
fields until after validations have run
  • Loading branch information
nddeluca committed Jul 19, 2024
1 parent d18ee9b commit 92626e4
Show file tree
Hide file tree
Showing 6 changed files with 674 additions and 622 deletions.
39 changes: 23 additions & 16 deletions x/evm/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
ethermint "github.com/evmos/ethermint/types"
)

// Validate performs a basic validation of a GenesisAccount fields.
func (ga GenesisAccount) Validate() error {
if err := ethermint.ValidateAddress(ga.Address); err != nil {
return err
// NewGenesisState creates a new genesis state.
func NewGenesisState(params Params, accounts []GenesisAccount) *GenesisState {
return &GenesisState{
Accounts: accounts,
Params: params,
}
return ga.Storage.Validate()
}

// DefaultGenesisState sets default evm genesis state with empty accounts and default params and
Expand All @@ -38,27 +38,34 @@ func DefaultGenesisState() *GenesisState {
}
}

// NewGenesisState creates a new genesis state.
func NewGenesisState(params Params, accounts []GenesisAccount) *GenesisState {
return &GenesisState{
Accounts: accounts,
Params: params,
// Validate performs a basic validation of a GenesisAccount fields.
func (ga GenesisAccount) Validate() error {
if err := ethermint.ValidateAddress(ga.Address); err != nil {
return err
}
return ga.Storage.Validate()
}

// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
seenAccounts := make(map[string]bool)
seenAccounts := make(map[string]struct{})

for _, acc := range gs.Accounts {
if seenAccounts[acc.Address] {
return fmt.Errorf("duplicated genesis account %s", acc.Address)
}
if err := acc.Validate(); err != nil {
return fmt.Errorf("invalid genesis account %s: %w", acc.Address, err)
}
seenAccounts[acc.Address] = true

if _, ok := seenAccounts[acc.Address]; ok {
return fmt.Errorf("duplicated genesis account %s", acc.Address)
}

seenAccounts[acc.Address] = struct{}{}
}

if err := gs.Params.Validate(); err != nil {
return fmt.Errorf("invalid params: %w", err)
}

return gs.Params.Validate()
return nil
}
Loading

0 comments on commit 92626e4

Please sign in to comment.