2020-03-09 13:17:23 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2020-04-01 18:49:21 +00:00
|
|
|
"errors"
|
2020-05-28 21:22:41 +00:00
|
|
|
"fmt"
|
2020-11-27 18:42:04 +00:00
|
|
|
|
2020-03-09 13:17:23 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2020-05-28 21:22:41 +00:00
|
|
|
// Validate performs a basic validation of a GenesisAccount fields.
|
|
|
|
func (ga GenesisAccount) Validate() error {
|
2021-04-18 15:54:18 +00:00
|
|
|
if IsZeroAddress(ga.Address) {
|
2020-11-27 18:42:04 +00:00
|
|
|
return fmt.Errorf("address cannot be the zero address %s", ga.Address)
|
2020-05-28 21:22:41 +00:00
|
|
|
}
|
2021-01-06 20:56:40 +00:00
|
|
|
if len(ethcmn.Hex2Bytes(ga.Code)) == 0 {
|
|
|
|
return errors.New("code cannot be empty")
|
2020-05-28 21:22:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 20:01:45 +00:00
|
|
|
return ga.Storage.Validate()
|
2020-03-09 13:17:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
// DefaultGenesisState sets default evm genesis state with empty accounts and default params and
|
|
|
|
// chain config values.
|
2021-04-17 10:00:07 +00:00
|
|
|
func DefaultGenesisState() *GenesisState {
|
|
|
|
return &GenesisState{
|
2020-09-02 19:41:05 +00:00
|
|
|
Accounts: []GenesisAccount{},
|
|
|
|
TxsLogs: []TransactionLogs{},
|
|
|
|
ChainConfig: DefaultChainConfig(),
|
|
|
|
Params: DefaultParams(),
|
2020-03-09 13:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-18 19:21:12 +00:00
|
|
|
|
|
|
|
// Validate performs basic genesis state validation returning an error upon any
|
|
|
|
// failure.
|
|
|
|
func (gs GenesisState) Validate() error {
|
2020-05-28 21:22:41 +00:00
|
|
|
seenAccounts := make(map[string]bool)
|
2020-06-04 10:40:21 +00:00
|
|
|
seenTxs := make(map[string]bool)
|
2020-05-18 19:21:12 +00:00
|
|
|
for _, acc := range gs.Accounts {
|
2020-11-27 18:42:04 +00:00
|
|
|
if seenAccounts[acc.Address] {
|
|
|
|
return fmt.Errorf("duplicated genesis account %s", acc.Address)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
2020-05-28 21:22:41 +00:00
|
|
|
if err := acc.Validate(); err != nil {
|
2020-11-27 18:42:04 +00:00
|
|
|
return fmt.Errorf("invalid genesis account %s: %w", acc.Address, err)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
2020-11-27 18:42:04 +00:00
|
|
|
seenAccounts[acc.Address] = true
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
2020-11-27 18:42:04 +00:00
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
for _, tx := range gs.TxsLogs {
|
2021-01-06 20:56:40 +00:00
|
|
|
if seenTxs[tx.Hash] {
|
|
|
|
return fmt.Errorf("duplicated logs from transaction %s", tx.Hash)
|
2020-06-04 10:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Validate(); err != nil {
|
2021-01-06 20:56:40 +00:00
|
|
|
return fmt.Errorf("invalid logs from transaction %s: %w", tx.Hash, err)
|
2020-06-04 10:40:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
seenTxs[tx.Hash] = true
|
2020-06-04 10:40:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
if err := gs.ChainConfig.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gs.Params.Validate()
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|