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
|
|
|
|
2021-01-06 20:56:40 +00:00
|
|
|
ethermint "github.com/cosmos/ethermint/types"
|
|
|
|
|
2020-03-09 13:17:23 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2020-06-04 10:40:21 +00:00
|
|
|
// GenesisState defines the evm module genesis state
|
2020-03-09 13:17:23 +00:00
|
|
|
GenesisState struct {
|
2020-09-02 19:41:05 +00:00
|
|
|
Accounts []GenesisAccount `json:"accounts"`
|
|
|
|
TxsLogs []TransactionLogs `json:"txs_logs"`
|
|
|
|
ChainConfig ChainConfig `json:"chain_config"`
|
|
|
|
Params Params `json:"params"`
|
2020-03-09 13:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenesisAccount defines an account to be initialized in the genesis state.
|
2020-05-18 19:21:12 +00:00
|
|
|
// Its main difference between with Geth's GenesisAccount is that it uses a custom
|
|
|
|
// storage type and that it doesn't contain the private key field.
|
2020-12-15 18:43:06 +00:00
|
|
|
// NOTE: balance is omitted as it is imported from the auth account balance.
|
2020-03-09 13:17:23 +00:00
|
|
|
GenesisAccount struct {
|
2021-01-06 20:56:40 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
Code string `json:"code,omitempty"`
|
|
|
|
Storage Storage `json:"storage,omitempty"`
|
2020-03-09 13:17:23 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-05-28 21:22:41 +00:00
|
|
|
// Validate performs a basic validation of a GenesisAccount fields.
|
|
|
|
func (ga GenesisAccount) Validate() error {
|
2021-01-06 20:56:40 +00:00
|
|
|
if ethermint.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.
|
2020-03-09 13:17:23 +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
|
|
|
}
|