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.

175 lines
4.4 KiB

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