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.

367 lines
11 KiB

8 years ago
8 years ago
8 years ago
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
8 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "sync"
  7. "time"
  8. abci "github.com/tendermint/abci/types"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. dbm "github.com/tendermint/tmlibs/db"
  11. "github.com/tendermint/tmlibs/log"
  12. wire "github.com/tendermint/go-wire"
  13. "github.com/tendermint/tendermint/state/txindex"
  14. "github.com/tendermint/tendermint/state/txindex/null"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. var (
  18. stateKey = []byte("stateKey")
  19. abciResponsesKey = []byte("abciResponsesKey")
  20. )
  21. func calcValidatorsKey(height int) []byte {
  22. return []byte(cmn.Fmt("validatorsKey:%v", height))
  23. }
  24. //-----------------------------------------------------------------------------
  25. // State represents the latest committed state of the Tendermint consensus,
  26. // including the last committed block and validator set.
  27. // Newly committed blocks are validated and executed against the State.
  28. // NOTE: not goroutine-safe.
  29. type State struct {
  30. // mtx for writing to db
  31. mtx sync.Mutex
  32. db dbm.DB
  33. // should not change
  34. GenesisDoc *types.GenesisDoc
  35. ChainID string
  36. // These fields are updated by SetBlockAndValidators.
  37. // LastBlockHeight=0 at genesis (ie. block(H=0) does not exist)
  38. // LastValidators is used to validate block.LastCommit.
  39. LastBlockHeight int
  40. LastBlockID types.BlockID
  41. LastBlockTime time.Time
  42. Validators *types.ValidatorSet
  43. LastValidators *types.ValidatorSet
  44. // AppHash is updated after Commit
  45. AppHash []byte
  46. TxIndexer txindex.TxIndexer `json:"-"` // Transaction indexer
  47. // When a block returns a validator set change via EndBlock,
  48. // the change only applies to the next block.
  49. // So, if s.LastBlockHeight causes a valset change,
  50. // we set s.LastHeightValidatorsChanged = s.LastBlockHeight + 1
  51. LastHeightValidatorsChanged int
  52. logger log.Logger
  53. }
  54. // GetState loads the most recent state from the database,
  55. // or creates a new one from the given genesisFile and persists the result
  56. // to the database.
  57. func GetState(stateDB dbm.DB, genesisFile string) *State {
  58. state := LoadState(stateDB)
  59. if state == nil {
  60. state = MakeGenesisStateFromFile(stateDB, genesisFile)
  61. state.Save()
  62. }
  63. return state
  64. }
  65. // LoadState loads the State from the database.
  66. func LoadState(db dbm.DB) *State {
  67. return loadState(db, stateKey)
  68. }
  69. func loadState(db dbm.DB, key []byte) *State {
  70. s := &State{db: db, TxIndexer: &null.TxIndex{}}
  71. buf := db.Get(key)
  72. if len(buf) == 0 {
  73. return nil
  74. } else {
  75. r, n, err := bytes.NewReader(buf), new(int), new(error)
  76. wire.ReadBinaryPtr(&s, r, 0, n, err)
  77. if *err != nil {
  78. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  79. cmn.Exit(cmn.Fmt("LoadState: Data has been corrupted or its spec has changed: %v\n", *err))
  80. }
  81. // TODO: ensure that buf is completely read.
  82. }
  83. return s
  84. }
  85. // SetLogger sets the logger on the State.
  86. func (s *State) SetLogger(l log.Logger) {
  87. s.logger = l
  88. }
  89. // Copy makes a copy of the State for mutating.
  90. func (s *State) Copy() *State {
  91. return &State{
  92. db: s.db,
  93. GenesisDoc: s.GenesisDoc,
  94. ChainID: s.ChainID,
  95. LastBlockHeight: s.LastBlockHeight,
  96. LastBlockID: s.LastBlockID,
  97. LastBlockTime: s.LastBlockTime,
  98. Validators: s.Validators.Copy(),
  99. LastValidators: s.LastValidators.Copy(),
  100. AppHash: s.AppHash,
  101. TxIndexer: s.TxIndexer, // pointer here, not value
  102. LastHeightValidatorsChanged: s.LastHeightValidatorsChanged,
  103. logger: s.logger,
  104. }
  105. }
  106. // Save persists the State to the database.
  107. func (s *State) Save() {
  108. s.mtx.Lock()
  109. defer s.mtx.Unlock()
  110. s.saveValidatorsInfo()
  111. s.db.SetSync(stateKey, s.Bytes())
  112. }
  113. // SaveABCIResponses persists the ABCIResponses to the database.
  114. // This is useful in case we crash after app.Commit and before s.Save().
  115. func (s *State) SaveABCIResponses(abciResponses *ABCIResponses) {
  116. s.db.SetSync(abciResponsesKey, abciResponses.Bytes())
  117. }
  118. // LoadABCIResponses loads the ABCIResponses from the database.
  119. func (s *State) LoadABCIResponses() *ABCIResponses {
  120. abciResponses := new(ABCIResponses)
  121. buf := s.db.Get(abciResponsesKey)
  122. if len(buf) != 0 {
  123. r, n, err := bytes.NewReader(buf), new(int), new(error)
  124. wire.ReadBinaryPtr(abciResponses, r, 0, n, err)
  125. if *err != nil {
  126. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  127. cmn.Exit(cmn.Fmt("LoadABCIResponses: Data has been corrupted or its spec has changed: %v\n", *err))
  128. }
  129. // TODO: ensure that buf is completely read.
  130. }
  131. return abciResponses
  132. }
  133. // LoadValidators loads the ValidatorSet for a given height.
  134. func (s *State) LoadValidators(height int) (*types.ValidatorSet, error) {
  135. v := s.loadValidators(height)
  136. if v == nil {
  137. return nil, ErrNoValSetForHeight{height}
  138. }
  139. if v.ValidatorSet == nil {
  140. v = s.loadValidators(v.LastHeightChanged)
  141. if v == nil {
  142. cmn.PanicSanity(fmt.Sprintf(`Couldn't find validators at
  143. height %d as last changed from height %d`, v.LastHeightChanged, height))
  144. }
  145. }
  146. return v.ValidatorSet, nil
  147. }
  148. func (s *State) loadValidators(height int) *ValidatorsInfo {
  149. buf := s.db.Get(calcValidatorsKey(height))
  150. if len(buf) == 0 {
  151. return nil
  152. }
  153. v := new(ValidatorsInfo)
  154. r, n, err := bytes.NewReader(buf), new(int), new(error)
  155. wire.ReadBinaryPtr(v, r, 0, n, err)
  156. if *err != nil {
  157. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  158. cmn.Exit(cmn.Fmt("LoadValidators: Data has been corrupted or its spec has changed: %v\n", *err))
  159. }
  160. // TODO: ensure that buf is completely read.
  161. return v
  162. }
  163. // saveValidatorsInfo persists the validator set for the next block to disk.
  164. // It should be called from s.Save(), right before the state itself is persisted.
  165. // If the validator set did not change after processing the latest block,
  166. // only the last height for which the validators changed is persisted.
  167. func (s *State) saveValidatorsInfo() {
  168. changeHeight := s.LastHeightValidatorsChanged
  169. nextHeight := s.LastBlockHeight + 1
  170. vi := &ValidatorsInfo{
  171. LastHeightChanged: changeHeight,
  172. }
  173. if changeHeight == nextHeight {
  174. vi.ValidatorSet = s.Validators
  175. }
  176. s.db.SetSync(calcValidatorsKey(nextHeight), vi.Bytes())
  177. }
  178. // Equals returns true if the States are identical.
  179. func (s *State) Equals(s2 *State) bool {
  180. return bytes.Equal(s.Bytes(), s2.Bytes())
  181. }
  182. // Bytes serializes the State using go-wire.
  183. func (s *State) Bytes() []byte {
  184. return wire.BinaryBytes(s)
  185. }
  186. // SetBlockAndValidators mutates State variables to update block and validators after running EndBlock.
  187. func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader, abciResponses *ABCIResponses) {
  188. // copy the valset so we can apply changes from EndBlock
  189. // and update s.LastValidators and s.Validators
  190. prevValSet := s.Validators.Copy()
  191. nextValSet := prevValSet.Copy()
  192. // update the validator set with the latest abciResponses
  193. if len(abciResponses.EndBlock.Diffs) > 0 {
  194. err := updateValidators(nextValSet, abciResponses.EndBlock.Diffs)
  195. if err != nil {
  196. s.logger.Error("Error changing validator set", "err", err)
  197. // TODO: err or carry on?
  198. }
  199. // change results from this height but only applies to the next height
  200. s.LastHeightValidatorsChanged = header.Height + 1
  201. }
  202. // Update validator accums and set state variables
  203. nextValSet.IncrementAccum(1)
  204. s.setBlockAndValidators(header.Height,
  205. types.BlockID{header.Hash(), blockPartsHeader},
  206. header.Time,
  207. prevValSet, nextValSet)
  208. }
  209. func (s *State) setBlockAndValidators(
  210. height int, blockID types.BlockID, blockTime time.Time,
  211. prevValSet, nextValSet *types.ValidatorSet) {
  212. s.LastBlockHeight = height
  213. s.LastBlockID = blockID
  214. s.LastBlockTime = blockTime
  215. s.Validators = nextValSet
  216. s.LastValidators = prevValSet
  217. }
  218. // GetValidators returns the last and current validator sets.
  219. func (s *State) GetValidators() (*types.ValidatorSet, *types.ValidatorSet) {
  220. return s.LastValidators, s.Validators
  221. }
  222. // Params returns the consensus parameters used for
  223. // validating blocks
  224. func (s *State) Params() types.ConsensusParams {
  225. // TODO: this should move into the State proper
  226. // when we allow the app to change it
  227. return s.GenesisDoc.ConsensusParams
  228. }
  229. //------------------------------------------------------------------------
  230. // ABCIResponses retains the responses of the various ABCI calls during block processing.
  231. // It is persisted to disk before calling Commit.
  232. type ABCIResponses struct {
  233. Height int
  234. DeliverTx []*abci.ResponseDeliverTx
  235. EndBlock abci.ResponseEndBlock
  236. txs types.Txs // reference for indexing results by hash
  237. }
  238. // NewABCIResponses returns a new ABCIResponses
  239. func NewABCIResponses(block *types.Block) *ABCIResponses {
  240. return &ABCIResponses{
  241. Height: block.Height,
  242. DeliverTx: make([]*abci.ResponseDeliverTx, block.NumTxs),
  243. txs: block.Data.Txs,
  244. }
  245. }
  246. // Bytes serializes the ABCIResponse using go-wire
  247. func (a *ABCIResponses) Bytes() []byte {
  248. return wire.BinaryBytes(*a)
  249. }
  250. //-----------------------------------------------------------------------------
  251. // ValidatorsInfo represents the latest validator set, or the last time it changed
  252. type ValidatorsInfo struct {
  253. ValidatorSet *types.ValidatorSet
  254. LastHeightChanged int
  255. }
  256. // Bytes serializes the ValidatorsInfo using go-wire
  257. func (vi *ValidatorsInfo) Bytes() []byte {
  258. return wire.BinaryBytes(*vi)
  259. }
  260. //------------------------------------------------------------------------
  261. // Genesis
  262. // MakeGenesisStateFromFile reads and unmarshals state from the given
  263. // file.
  264. //
  265. // Used during replay and in tests.
  266. func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State {
  267. genDocJSON, err := ioutil.ReadFile(genDocFile)
  268. if err != nil {
  269. cmn.Exit(cmn.Fmt("Couldn't read GenesisDoc file: %v", err))
  270. }
  271. genDoc, err := types.GenesisDocFromJSON(genDocJSON)
  272. if err != nil {
  273. cmn.Exit(cmn.Fmt("Error reading GenesisDoc: %v", err))
  274. }
  275. return MakeGenesisState(db, genDoc)
  276. }
  277. // MakeGenesisState creates state from types.GenesisDoc.
  278. //
  279. // Used in tests.
  280. func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State {
  281. err := genDoc.ValidateAndComplete()
  282. if err != nil {
  283. cmn.Exit(cmn.Fmt("Error in genesis file: %v", err))
  284. }
  285. // Make validators slice
  286. validators := make([]*types.Validator, len(genDoc.Validators))
  287. for i, val := range genDoc.Validators {
  288. pubKey := val.PubKey
  289. address := pubKey.Address()
  290. // Make validator
  291. validators[i] = &types.Validator{
  292. Address: address,
  293. PubKey: pubKey,
  294. VotingPower: val.Amount,
  295. }
  296. }
  297. return &State{
  298. db: db,
  299. GenesisDoc: genDoc,
  300. ChainID: genDoc.ChainID,
  301. LastBlockHeight: 0,
  302. LastBlockID: types.BlockID{},
  303. LastBlockTime: genDoc.GenesisTime,
  304. Validators: types.NewValidatorSet(validators),
  305. LastValidators: types.NewValidatorSet(nil),
  306. AppHash: genDoc.AppHash,
  307. TxIndexer: &null.TxIndex{}, // we do not need indexer during replay and in tests
  308. LastHeightValidatorsChanged: 1,
  309. }
  310. }