You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

  1. package state
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "time"
  6. . "github.com/tendermint/tendermint/binary"
  7. . "github.com/tendermint/tendermint/blocks"
  8. . "github.com/tendermint/tendermint/common"
  9. db_ "github.com/tendermint/tendermint/db"
  10. "github.com/tendermint/tendermint/merkle"
  11. )
  12. type GenesisDoc struct {
  13. GenesisTime time.Time
  14. AccountDetails []*AccountDetail
  15. }
  16. func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) {
  17. err := json.Unmarshal(jsonBlob, &genState)
  18. if err != nil {
  19. Panicf("Couldn't read GenesisDoc: %v", err)
  20. }
  21. return
  22. }
  23. func GenesisStateFromFile(db db_.DB, genDocFile string) *State {
  24. jsonBlob, err := ioutil.ReadFile(genDocFile)
  25. if err != nil {
  26. Panicf("Couldn't read GenesisDoc file: %v", err)
  27. }
  28. genDoc := GenesisDocFromJSON(jsonBlob)
  29. return GenesisStateFromDoc(db, genDoc)
  30. }
  31. func GenesisStateFromDoc(db db_.DB, genDoc *GenesisDoc) *State {
  32. return GenesisState(db, genDoc.GenesisTime, genDoc.AccountDetails)
  33. }
  34. func GenesisState(db db_.DB, genesisTime time.Time, accDets []*AccountDetail) *State {
  35. if genesisTime.IsZero() {
  36. genesisTime = time.Now()
  37. }
  38. // TODO: Use "uint64Codec" instead of BasicCodec
  39. accountDetails := merkle.NewIAVLTree(BasicCodec, AccountDetailCodec, defaultAccountDetailsCacheCapacity, db)
  40. validators := []*Validator{}
  41. for _, accDet := range accDets {
  42. accountDetails.Set(accDet.Id, accDet)
  43. if accDet.Status == AccountStatusBonded {
  44. validators = append(validators, &Validator{
  45. Account: accDet.Account,
  46. BondHeight: 0,
  47. VotingPower: accDet.Balance,
  48. Accum: 0,
  49. })
  50. }
  51. }
  52. if len(validators) == 0 {
  53. panic("Must have some validators")
  54. }
  55. return &State{
  56. DB: db,
  57. LastBlockHeight: 0,
  58. LastBlockHash: nil,
  59. LastBlockParts: PartSetHeader{},
  60. LastBlockTime: genesisTime,
  61. BondedValidators: NewValidatorSet(validators),
  62. UnbondingValidators: NewValidatorSet(nil),
  63. accountDetails: accountDetails,
  64. }
  65. }