tendermint/state/genesis.go

82 lines
2.1 KiB
Go
Raw Normal View History

2014-10-22 17:20:44 -07:00
package state
import (
"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 GenesisDoc struct {
GenesisTime time.Time
Accounts []*Account
2014-12-17 01:37:13 -08:00
Validators []*ValidatorInfo
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 {
Panicf("Couldn't read GenesisDoc: %v", err)
}
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 {
Panicf("Couldn't read GenesisDoc file: %v", err)
}
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 {
panic("Must have some 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 {
accounts.Set(acc.Address, acc)
2014-10-22 17:20:44 -07:00
}
2014-12-17 01:37:13 -08:00
// Make validatorInfos state tree
validatorInfos := merkle.NewIAVLTree(BasicCodec, ValidatorInfoCodec, 0, db)
for _, valInfo := range genDoc.Validators {
validatorInfos.Set(valInfo.Address, valInfo)
}
// Make validators
validators := make([]*Validator, len(genDoc.Validators))
for i, valInfo := range genDoc.Validators {
validators[i] = &Validator{
Address: valInfo.Address,
PubKey: valInfo.PubKey,
VotingPower: valInfo.FirstBondAmount,
}
}
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
}
}