2020-03-09 13:17:23 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2020-05-18 19:21:12 +00:00
|
|
|
"bytes"
|
2020-04-01 18:49:21 +00:00
|
|
|
"errors"
|
2020-05-28 21:22:41 +00:00
|
|
|
"fmt"
|
2020-03-09 13:17:23 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2020-05-29 15:50:22 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2020-03-09 13:17:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-03-09 13:17:23 +00:00
|
|
|
GenesisAccount struct {
|
2020-07-13 20:01:45 +00:00
|
|
|
Address ethcmn.Address `json:"address"`
|
|
|
|
Balance *big.Int `json:"balance"`
|
|
|
|
Code hexutil.Bytes `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 {
|
|
|
|
if bytes.Equal(ga.Address.Bytes(), ethcmn.Address{}.Bytes()) {
|
|
|
|
return fmt.Errorf("address cannot be the zero address %s", ga.Address.String())
|
|
|
|
}
|
|
|
|
if ga.Balance == nil {
|
|
|
|
return errors.New("balance cannot be nil")
|
|
|
|
}
|
|
|
|
if ga.Balance.Sign() == -1 {
|
|
|
|
return errors.New("balance cannot be negative")
|
|
|
|
}
|
|
|
|
if ga.Code != nil && len(ga.Code) == 0 {
|
|
|
|
return errors.New("code bytes cannot be empty")
|
|
|
|
}
|
|
|
|
|
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-05-28 21:22:41 +00:00
|
|
|
if seenAccounts[acc.Address.String()] {
|
|
|
|
return fmt.Errorf("duplicated genesis account %s", acc.Address.String())
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
2020-05-28 21:22:41 +00:00
|
|
|
if err := acc.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("invalid genesis account %s: %w", acc.Address.String(), err)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
2020-05-28 21:22:41 +00:00
|
|
|
seenAccounts[acc.Address.String()] = true
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
2020-06-04 10:40:21 +00:00
|
|
|
for _, tx := range gs.TxsLogs {
|
|
|
|
if seenTxs[tx.Hash.String()] {
|
|
|
|
return fmt.Errorf("duplicated logs from transaction %s", tx.Hash.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("invalid logs from transaction %s: %w", tx.Hash.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
seenTxs[tx.Hash.String()] = true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|