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 (
|
|
|
|
// GenesisState defines the application's genesis state. It contains all the
|
|
|
|
// information required and accounts to initialize the blockchain.
|
|
|
|
GenesisState struct {
|
|
|
|
Accounts []GenesisAccount `json:"accounts"`
|
|
|
|
}
|
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
// GenesisStorage represents the GenesisAccount Storage map as single key value
|
|
|
|
// pairs. This is to prevent non determinism at genesis initialization or export.
|
|
|
|
GenesisStorage struct {
|
|
|
|
Key ethcmn.Hash `json:"key"`
|
|
|
|
Value ethcmn.Hash `json:"value"`
|
|
|
|
}
|
|
|
|
|
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-05-18 19:21:12 +00:00
|
|
|
Address ethcmn.Address `json:"address"`
|
|
|
|
Balance *big.Int `json:"balance"`
|
2020-05-29 15:50:22 +00:00
|
|
|
Code hexutil.Bytes `json:"code,omitempty"`
|
2020-05-18 19:21:12 +00:00
|
|
|
Storage []GenesisStorage `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")
|
|
|
|
}
|
|
|
|
|
|
|
|
seenStorage := make(map[string]bool)
|
|
|
|
for i, state := range ga.Storage {
|
|
|
|
if seenStorage[state.Key.String()] {
|
|
|
|
return fmt.Errorf("duplicate state key %d", i)
|
|
|
|
}
|
|
|
|
if bytes.Equal(state.Key.Bytes(), ethcmn.Hash{}.Bytes()) {
|
|
|
|
return fmt.Errorf("state %d key hash cannot be empty", i)
|
|
|
|
}
|
|
|
|
// NOTE: state value can be empty
|
|
|
|
seenStorage[state.Key.String()] = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
// NewGenesisStorage creates a new GenesisStorage instance
|
|
|
|
func NewGenesisStorage(key, value ethcmn.Hash) GenesisStorage {
|
|
|
|
return GenesisStorage{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
2020-03-09 13:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
// DefaultGenesisState sets default evm genesis state with empty accounts.
|
2020-03-09 13:17:23 +00:00
|
|
|
func DefaultGenesisState() GenesisState {
|
|
|
|
return GenesisState{
|
|
|
|
Accounts: []GenesisAccount{},
|
|
|
|
}
|
|
|
|
}
|
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-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
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|