tendermint/state/genesis.go

132 lines
3.3 KiB
Go
Raw Normal View History

2014-10-22 17:20:44 -07:00
package state
import (
"bytes"
2014-12-31 22:13:49 -08:00
"encoding/hex"
2014-10-22 17:20:44 -07:00
"encoding/json"
"io/ioutil"
"time"
. "github.com/tendermint/tendermint/account"
2014-10-22 17:20:44 -07:00
. "github.com/tendermint/tendermint/binary"
2014-12-17 01:37:13 -08:00
. "github.com/tendermint/tendermint/block"
2014-10-22 17:20:44 -07:00
. "github.com/tendermint/tendermint/common"
db_ "github.com/tendermint/tendermint/db"
"github.com/tendermint/tendermint/merkle"
)
type GenesisAccount struct {
Address string
Amount uint64
}
type GenesisValidator struct {
PubKey string
Amount uint64
UnbondTo []GenesisAccount
}
2014-10-22 17:20:44 -07:00
type GenesisDoc struct {
GenesisTime time.Time
Accounts []GenesisAccount
Validators []GenesisValidator
2014-10-22 17:20:44 -07:00
}
func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) {
2014-10-22 17:20:44 -07:00
err := json.Unmarshal(jsonBlob, &genState)
if err != nil {
2014-12-29 18:09:06 -08:00
panic(Fmt("Couldn't read GenesisDoc: %v", err))
2014-10-22 17:20:44 -07:00
}
return
}
2014-12-23 23:20:49 -08:00
func MakeGenesisStateFromFile(db db_.DB, genDocFile string) *State {
2014-10-22 17:20:44 -07:00
jsonBlob, err := ioutil.ReadFile(genDocFile)
if err != nil {
2014-12-29 18:09:06 -08:00
panic(Fmt("Couldn't read GenesisDoc file: %v", err))
2014-10-22 17:20:44 -07:00
}
genDoc := GenesisDocFromJSON(jsonBlob)
2014-12-23 23:20:49 -08:00
return MakeGenesisState(db, genDoc)
2014-10-22 17:20:44 -07:00
}
2014-12-23 23:20:49 -08:00
func MakeGenesisState(db db_.DB, genDoc *GenesisDoc) *State {
if len(genDoc.Validators) == 0 {
2014-12-29 18:09:06 -08:00
Exit(Fmt("The genesis file has no validators"))
2014-10-24 18:21:30 -07:00
}
if genDoc.GenesisTime.IsZero() {
genDoc.GenesisTime = time.Now()
2014-10-22 17:20:44 -07:00
}
// Make accounts state tree
accounts := merkle.NewIAVLTree(BasicCodec, AccountCodec, defaultAccountsCacheCapacity, db)
for _, acc := range genDoc.Accounts {
2014-12-31 22:13:49 -08:00
address, err := hex.DecodeString(acc.Address)
if err != nil {
2014-12-29 18:09:06 -08:00
Exit(Fmt("Invalid account address: %v", acc.Address))
}
account := &Account{
Address: address,
PubKey: PubKeyNil{},
Sequence: 0,
Balance: acc.Amount,
}
accounts.Set(address, account)
2014-10-22 17:20:44 -07:00
}
// Make validatorInfos state tree && validators slice
2014-12-17 01:37:13 -08:00
validatorInfos := merkle.NewIAVLTree(BasicCodec, ValidatorInfoCodec, 0, db)
validators := make([]*Validator, len(genDoc.Validators))
for i, val := range genDoc.Validators {
2014-12-31 22:13:49 -08:00
pubKeyBytes, err := hex.DecodeString(val.PubKey)
if err != nil {
2014-12-29 18:09:06 -08:00
Exit(Fmt("Invalid validator pubkey: %v", val.PubKey))
}
pubKey := ReadBinary(PubKeyEd25519{},
bytes.NewBuffer(pubKeyBytes), new(int64), &err).(PubKeyEd25519)
if err != nil {
2014-12-29 18:09:06 -08:00
Exit(Fmt("Invalid validator pubkey: %v", val.PubKey))
}
address := pubKey.Address()
// Make ValidatorInfo
valInfo := &ValidatorInfo{
Address: address,
PubKey: pubKey,
UnbondTo: make([]*TxOutput, len(val.UnbondTo)),
FirstBondHeight: 0,
FirstBondAmount: val.Amount,
}
for i, unbondTo := range val.UnbondTo {
2014-12-31 22:13:49 -08:00
address, err := hex.DecodeString(unbondTo.Address)
if err != nil {
2014-12-29 18:09:06 -08:00
Exit(Fmt("Invalid unbond-to address: %v", unbondTo.Address))
}
valInfo.UnbondTo[i] = &TxOutput{
Address: address,
Amount: unbondTo.Amount,
}
}
validatorInfos.Set(address, valInfo)
// Make validator
2014-12-17 01:37:13 -08:00
validators[i] = &Validator{
Address: address,
PubKey: pubKey,
VotingPower: val.Amount,
2014-12-17 01:37:13 -08:00
}
}
2014-10-22 17:20:44 -07:00
return &State{
DB: db,
LastBlockHeight: 0,
LastBlockHash: nil,
LastBlockParts: PartSetHeader{},
LastBlockTime: genDoc.GenesisTime,
2014-12-17 01:37:13 -08:00
BondedValidators: NewValidatorSet(validators),
2014-10-22 17:20:44 -07:00
UnbondingValidators: NewValidatorSet(nil),
accounts: accounts,
2014-12-17 01:37:13 -08:00
validatorInfos: validatorInfos,
2014-10-22 17:20:44 -07:00
}
}