tendermint/state/genesis.go

65 lines
1.6 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"
. "github.com/tendermint/tendermint/blocks"
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
Validators []*Validator
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
}
func GenesisStateFromFile(db db_.DB, genDocFile string) *State {
jsonBlob, err := ioutil.ReadFile(genDocFile)
if err != nil {
Panicf("Couldn't read GenesisDoc file: %v", err)
}
genDoc := GenesisDocFromJSON(jsonBlob)
return GenesisState(db, genDoc)
2014-10-22 17:20:44 -07:00
}
func GenesisState(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
}
return &State{
DB: db,
LastBlockHeight: 0,
LastBlockHash: nil,
LastBlockParts: PartSetHeader{},
LastBlockTime: genDoc.GenesisTime,
BondedValidators: NewValidatorSet(genDoc.Validators),
2014-10-22 17:20:44 -07:00
UnbondingValidators: NewValidatorSet(nil),
accounts: accounts,
2014-10-22 17:20:44 -07:00
}
}