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-03-09 13:17:23 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
var zeroAddrBytes = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
|
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"`
|
|
|
|
Code []byte `json:"code,omitempty"`
|
|
|
|
Storage []GenesisStorage `json:"storage,omitempty"`
|
2020-03-09 13:17:23 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
for _, acc := range gs.Accounts {
|
|
|
|
if bytes.Equal(acc.Address.Bytes(), zeroAddrBytes) {
|
|
|
|
return errors.New("invalid GenesisAccount: address cannot be empty")
|
|
|
|
}
|
|
|
|
if acc.Balance == nil {
|
|
|
|
return errors.New("invalid GenesisAccount: balance cannot be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|