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.

169 lines
4.2 KiB

10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "sync"
  6. "time"
  7. . "github.com/tendermint/go-common"
  8. cfg "github.com/tendermint/go-config"
  9. dbm "github.com/tendermint/go-db"
  10. "github.com/tendermint/go-wire"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. var (
  14. stateKey = []byte("stateKey")
  15. )
  16. //-----------------------------------------------------------------------------
  17. // NOTE: not goroutine-safe.
  18. type State struct {
  19. // mtx for writing to db
  20. mtx sync.Mutex
  21. db dbm.DB
  22. // should not change
  23. GenesisDoc *types.GenesisDoc
  24. ChainID string
  25. // updated at end of ExecBlock
  26. LastBlockHeight int // Genesis state has this set to 0. So, Block(H=0) does not exist.
  27. LastBlockID types.BlockID
  28. LastBlockTime time.Time
  29. Validators *types.ValidatorSet
  30. LastValidators *types.ValidatorSet
  31. // AppHash is updated after Commit;
  32. // it's stale after ExecBlock and before Commit
  33. Stale bool
  34. AppHash []byte
  35. }
  36. func LoadState(db dbm.DB) *State {
  37. s := &State{db: db}
  38. buf := db.Get(stateKey)
  39. if len(buf) == 0 {
  40. return nil
  41. } else {
  42. r, n, err := bytes.NewReader(buf), new(int), new(error)
  43. wire.ReadBinaryPtr(&s, r, 0, n, err)
  44. if *err != nil {
  45. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  46. Exit(Fmt("Data has been corrupted or its spec has changed: %v\n", *err))
  47. }
  48. // TODO: ensure that buf is completely read.
  49. }
  50. return s
  51. }
  52. func (s *State) Copy() *State {
  53. return &State{
  54. db: s.db,
  55. GenesisDoc: s.GenesisDoc,
  56. ChainID: s.ChainID,
  57. LastBlockHeight: s.LastBlockHeight,
  58. LastBlockID: s.LastBlockID,
  59. LastBlockTime: s.LastBlockTime,
  60. Validators: s.Validators.Copy(),
  61. LastValidators: s.LastValidators.Copy(),
  62. Stale: s.Stale, // XXX: but really state shouldnt be copied while its stale
  63. AppHash: s.AppHash,
  64. }
  65. }
  66. func (s *State) Save() {
  67. s.mtx.Lock()
  68. defer s.mtx.Unlock()
  69. s.db.Set(stateKey, s.Bytes())
  70. }
  71. func (s *State) Equals(s2 *State) bool {
  72. return bytes.Equal(s.Bytes(), s2.Bytes())
  73. }
  74. func (s *State) Bytes() []byte {
  75. buf, n, err := new(bytes.Buffer), new(int), new(error)
  76. wire.WriteBinary(s, buf, n, err)
  77. if *err != nil {
  78. PanicCrisis(*err)
  79. }
  80. return buf.Bytes()
  81. }
  82. // Mutate state variables to match block and validators
  83. // Since we don't have the new AppHash yet, we set s.Stale=true
  84. func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader, prevValSet, nextValSet *types.ValidatorSet) {
  85. s.LastBlockHeight = header.Height
  86. s.LastBlockID = types.BlockID{header.Hash(), blockPartsHeader}
  87. s.LastBlockTime = header.Time
  88. s.Validators = nextValSet
  89. s.LastValidators = prevValSet
  90. s.Stale = true
  91. }
  92. func (s *State) GetValidators() (*types.ValidatorSet, *types.ValidatorSet) {
  93. return s.LastValidators, s.Validators
  94. }
  95. // Load the most recent state from "state" db,
  96. // or create a new one (and save) from genesis.
  97. func GetState(config cfg.Config, stateDB dbm.DB) *State {
  98. state := LoadState(stateDB)
  99. if state == nil {
  100. state = MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file"))
  101. state.Save()
  102. }
  103. return state
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Genesis
  107. func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State {
  108. genDocJSON, err := ioutil.ReadFile(genDocFile)
  109. if err != nil {
  110. Exit(Fmt("Couldn't read GenesisDoc file: %v", err))
  111. }
  112. genDoc := types.GenesisDocFromJSON(genDocJSON)
  113. return MakeGenesisState(db, genDoc)
  114. }
  115. func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State {
  116. if len(genDoc.Validators) == 0 {
  117. Exit(Fmt("The genesis file has no validators"))
  118. }
  119. if genDoc.GenesisTime.IsZero() {
  120. genDoc.GenesisTime = time.Now()
  121. }
  122. // Make validators slice
  123. validators := make([]*types.Validator, len(genDoc.Validators))
  124. for i, val := range genDoc.Validators {
  125. pubKey := val.PubKey
  126. address := pubKey.Address()
  127. // Make validator
  128. validators[i] = &types.Validator{
  129. Address: address,
  130. PubKey: pubKey,
  131. VotingPower: val.Amount,
  132. }
  133. }
  134. return &State{
  135. db: db,
  136. GenesisDoc: genDoc,
  137. ChainID: genDoc.ChainID,
  138. LastBlockHeight: 0,
  139. LastBlockID: types.BlockID{},
  140. LastBlockTime: genDoc.GenesisTime,
  141. Validators: types.NewValidatorSet(validators),
  142. LastValidators: types.NewValidatorSet(nil),
  143. AppHash: genDoc.AppHash,
  144. }
  145. }