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.

172 lines
4.3 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
  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. AppHashIsStale 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. if s.AppHashIsStale {
  54. PanicSanity(Fmt("App hash is stale: %v", s))
  55. }
  56. return &State{
  57. db: s.db,
  58. GenesisDoc: s.GenesisDoc,
  59. ChainID: s.ChainID,
  60. LastBlockHeight: s.LastBlockHeight,
  61. LastBlockID: s.LastBlockID,
  62. LastBlockTime: s.LastBlockTime,
  63. Validators: s.Validators.Copy(),
  64. LastValidators: s.LastValidators.Copy(),
  65. AppHashIsStale: false,
  66. AppHash: s.AppHash,
  67. }
  68. }
  69. func (s *State) Save() {
  70. s.mtx.Lock()
  71. defer s.mtx.Unlock()
  72. s.db.Set(stateKey, s.Bytes())
  73. }
  74. func (s *State) Equals(s2 *State) bool {
  75. return bytes.Equal(s.Bytes(), s2.Bytes())
  76. }
  77. func (s *State) Bytes() []byte {
  78. buf, n, err := new(bytes.Buffer), new(int), new(error)
  79. wire.WriteBinary(s, buf, n, err)
  80. if *err != nil {
  81. PanicCrisis(*err)
  82. }
  83. return buf.Bytes()
  84. }
  85. // Mutate state variables to match block and validators
  86. // Since we don't have the new AppHash yet, we set s.AppHashIsStale=true
  87. func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader, prevValSet, nextValSet *types.ValidatorSet) {
  88. s.LastBlockHeight = header.Height
  89. s.LastBlockID = types.BlockID{header.Hash(), blockPartsHeader}
  90. s.LastBlockTime = header.Time
  91. s.Validators = nextValSet
  92. s.LastValidators = prevValSet
  93. s.AppHashIsStale = true
  94. }
  95. func (s *State) GetValidators() (*types.ValidatorSet, *types.ValidatorSet) {
  96. return s.LastValidators, s.Validators
  97. }
  98. // Load the most recent state from "state" db,
  99. // or create a new one (and save) from genesis.
  100. func GetState(config cfg.Config, stateDB dbm.DB) *State {
  101. state := LoadState(stateDB)
  102. if state == nil {
  103. state = MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file"))
  104. state.Save()
  105. }
  106. return state
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Genesis
  110. func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State {
  111. genDocJSON, err := ioutil.ReadFile(genDocFile)
  112. if err != nil {
  113. Exit(Fmt("Couldn't read GenesisDoc file: %v", err))
  114. }
  115. genDoc := types.GenesisDocFromJSON(genDocJSON)
  116. return MakeGenesisState(db, genDoc)
  117. }
  118. func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State {
  119. if len(genDoc.Validators) == 0 {
  120. Exit(Fmt("The genesis file has no validators"))
  121. }
  122. if genDoc.GenesisTime.IsZero() {
  123. genDoc.GenesisTime = time.Now()
  124. }
  125. // Make validators slice
  126. validators := make([]*types.Validator, len(genDoc.Validators))
  127. for i, val := range genDoc.Validators {
  128. pubKey := val.PubKey
  129. address := pubKey.Address()
  130. // Make validator
  131. validators[i] = &types.Validator{
  132. Address: address,
  133. PubKey: pubKey,
  134. VotingPower: val.Amount,
  135. }
  136. }
  137. return &State{
  138. db: db,
  139. GenesisDoc: genDoc,
  140. ChainID: genDoc.ChainID,
  141. LastBlockHeight: 0,
  142. LastBlockID: types.BlockID{},
  143. LastBlockTime: genDoc.GenesisTime,
  144. Validators: types.NewValidatorSet(validators),
  145. LastValidators: types.NewValidatorSet(nil),
  146. AppHash: genDoc.AppHash,
  147. }
  148. }