tendermint/state/genesis.go

127 lines
3.4 KiB
Go
Raw Normal View History

2014-10-22 17:20:44 -07:00
package state
import (
"io/ioutil"
"time"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
dbm "github.com/tendermint/tendermint/db"
"github.com/tendermint/tendermint/merkle"
"github.com/tendermint/tendermint/types"
2014-10-22 17:20:44 -07:00
)
type GenesisAccount struct {
2015-05-01 17:26:49 -07:00
Address []byte `json:"address"`
Amount uint64 `json:"amount"`
}
type GenesisValidator struct {
2015-05-01 17:26:49 -07:00
PubKey account.PubKeyEd25519 `json:"pub_key"`
Amount uint64 `json:"amount"`
UnbondTo []GenesisAccount `json:"unbond_to"`
}
2014-10-22 17:20:44 -07:00
type GenesisDoc struct {
2015-05-01 17:26:49 -07:00
GenesisTime time.Time `json:"genesis_time"`
ChainID string `json:"chain_id"`
2015-05-01 17:26:49 -07:00
Accounts []GenesisAccount `json:"accounts"`
Validators []GenesisValidator `json:"validators"`
2014-10-22 17:20:44 -07:00
}
func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) {
var err error
binary.ReadJSON(&genState, jsonBlob, &err)
2014-10-22 17:20:44 -07:00
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
}
func MakeGenesisStateFromFile(db dbm.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
}
func MakeGenesisState(db dbm.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(binary.BasicCodec, account.AccountCodec, defaultAccountsCacheCapacity, db)
for _, genAcc := range genDoc.Accounts {
acc := &account.Account{
Address: genAcc.Address,
PubKey: nil,
Sequence: 0,
Balance: genAcc.Amount,
}
accounts.Set(acc.Address, acc)
2014-10-22 17:20:44 -07:00
}
// Make validatorInfos state tree && validators slice
validatorInfos := merkle.NewIAVLTree(binary.BasicCodec, ValidatorInfoCodec, 0, db)
2014-12-17 01:37:13 -08:00
validators := make([]*Validator, len(genDoc.Validators))
for i, val := range genDoc.Validators {
pubKey := val.PubKey
address := pubKey.Address()
// Make ValidatorInfo
valInfo := &ValidatorInfo{
Address: address,
PubKey: pubKey,
UnbondTo: make([]*types.TxOutput, len(val.UnbondTo)),
FirstBondHeight: 0,
FirstBondAmount: val.Amount,
}
for i, unbondTo := range val.UnbondTo {
valInfo.UnbondTo[i] = &types.TxOutput{
Address: unbondTo.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
}
}
2015-05-22 13:53:10 -07:00
// Make namereg tree
nameReg := merkle.NewIAVLTree(binary.BasicCodec, NameRegCodec, 0, db)
// TODO: add names to genesis.json
2014-12-31 23:21:47 -08:00
// IAVLTrees must be persisted before copy operations.
accounts.Save()
validatorInfos.Save()
2015-05-22 13:53:10 -07:00
nameReg.Save()
2014-12-31 23:21:47 -08:00
2014-10-22 17:20:44 -07:00
return &State{
DB: db,
ChainID: genDoc.ChainID,
LastBlockHeight: 0,
LastBlockHash: nil,
LastBlockParts: types.PartSetHeader{},
LastBlockTime: genDoc.GenesisTime,
BondedValidators: NewValidatorSet(validators),
LastBondedValidators: NewValidatorSet(nil),
UnbondingValidators: NewValidatorSet(nil),
accounts: accounts,
validatorInfos: validatorInfos,
2015-05-22 13:53:10 -07:00
nameReg: nameReg,
2014-10-22 17:20:44 -07:00
}
}