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.

289 lines
9.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package state
  2. import (
  3. "fmt"
  4. abci "github.com/tendermint/tendermint/abci/types"
  5. "github.com/tendermint/tendermint/types"
  6. cmn "github.com/tendermint/tmlibs/common"
  7. dbm "github.com/tendermint/tmlibs/db"
  8. )
  9. //------------------------------------------------------------------------
  10. func calcValidatorsKey(height int64) []byte {
  11. return []byte(cmn.Fmt("validatorsKey:%v", height))
  12. }
  13. func calcConsensusParamsKey(height int64) []byte {
  14. return []byte(cmn.Fmt("consensusParamsKey:%v", height))
  15. }
  16. func calcABCIResponsesKey(height int64) []byte {
  17. return []byte(cmn.Fmt("abciResponsesKey:%v", height))
  18. }
  19. // LoadStateFromDBOrGenesisFile loads the most recent state from the database,
  20. // or creates a new one from the given genesisFilePath and persists the result
  21. // to the database.
  22. func LoadStateFromDBOrGenesisFile(stateDB dbm.DB, genesisFilePath string) (State, error) {
  23. state := LoadState(stateDB)
  24. if state.IsEmpty() {
  25. var err error
  26. state, err = MakeGenesisStateFromFile(genesisFilePath)
  27. if err != nil {
  28. return state, err
  29. }
  30. SaveState(stateDB, state)
  31. }
  32. return state, nil
  33. }
  34. // LoadStateFromDBOrGenesisDoc loads the most recent state from the database,
  35. // or creates a new one from the given genesisDoc and persists the result
  36. // to the database.
  37. func LoadStateFromDBOrGenesisDoc(stateDB dbm.DB, genesisDoc *types.GenesisDoc) (State, error) {
  38. state := LoadState(stateDB)
  39. if state.IsEmpty() {
  40. var err error
  41. state, err = MakeGenesisState(genesisDoc)
  42. if err != nil {
  43. return state, err
  44. }
  45. SaveState(stateDB, state)
  46. }
  47. return state, nil
  48. }
  49. // LoadState loads the State from the database.
  50. func LoadState(db dbm.DB) State {
  51. return loadState(db, stateKey)
  52. }
  53. func loadState(db dbm.DB, key []byte) (state State) {
  54. buf := db.Get(key)
  55. if len(buf) == 0 {
  56. return state
  57. }
  58. err := cdc.UnmarshalBinaryBare(buf, &state)
  59. if err != nil {
  60. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  61. cmn.Exit(cmn.Fmt(`LoadState: Data has been corrupted or its spec has changed:
  62. %v\n`, err))
  63. }
  64. // TODO: ensure that buf is completely read.
  65. return state
  66. }
  67. // SaveState persists the State, the ValidatorsInfo, and the ConsensusParamsInfo to the database.
  68. func SaveState(db dbm.DB, state State) {
  69. saveState(db, state, stateKey)
  70. }
  71. func saveState(db dbm.DB, state State, key []byte) {
  72. nextHeight := state.LastBlockHeight + 1
  73. // If first block, save validators for block 1.
  74. if nextHeight == 1 {
  75. lastHeightVoteChanged := int64(1) // Due to Tendermint validator set changes being delayed 1 block.
  76. saveValidatorsInfo(db, nextHeight, lastHeightVoteChanged, state.Validators)
  77. }
  78. // Save next validators.
  79. saveValidatorsInfo(db, nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators)
  80. // Save next consensus params.
  81. saveConsensusParamsInfo(db, nextHeight, state.LastHeightConsensusParamsChanged, state.ConsensusParams)
  82. db.SetSync(stateKey, state.Bytes())
  83. }
  84. //------------------------------------------------------------------------
  85. // ABCIResponses retains the responses
  86. // of the various ABCI calls during block processing.
  87. // It is persisted to disk for each height before calling Commit.
  88. type ABCIResponses struct {
  89. DeliverTx []*abci.ResponseDeliverTx
  90. EndBlock *abci.ResponseEndBlock
  91. }
  92. // NewABCIResponses returns a new ABCIResponses
  93. func NewABCIResponses(block *types.Block) *ABCIResponses {
  94. resDeliverTxs := make([]*abci.ResponseDeliverTx, block.NumTxs)
  95. if block.NumTxs == 0 {
  96. // This makes Amino encoding/decoding consistent.
  97. resDeliverTxs = nil
  98. }
  99. return &ABCIResponses{
  100. DeliverTx: resDeliverTxs,
  101. }
  102. }
  103. // Bytes serializes the ABCIResponse using go-amino.
  104. func (arz *ABCIResponses) Bytes() []byte {
  105. return cdc.MustMarshalBinaryBare(arz)
  106. }
  107. func (arz *ABCIResponses) ResultsHash() []byte {
  108. results := types.NewResults(arz.DeliverTx)
  109. return results.Hash()
  110. }
  111. // LoadABCIResponses loads the ABCIResponses for the given height from the database.
  112. // This is useful for recovering from crashes where we called app.Commit and before we called
  113. // s.Save(). It can also be used to produce Merkle proofs of the result of txs.
  114. func LoadABCIResponses(db dbm.DB, height int64) (*ABCIResponses, error) {
  115. buf := db.Get(calcABCIResponsesKey(height))
  116. if len(buf) == 0 {
  117. return nil, ErrNoABCIResponsesForHeight{height}
  118. }
  119. abciResponses := new(ABCIResponses)
  120. err := cdc.UnmarshalBinaryBare(buf, abciResponses)
  121. if err != nil {
  122. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  123. cmn.Exit(cmn.Fmt(`LoadABCIResponses: Data has been corrupted or its spec has
  124. changed: %v\n`, err))
  125. }
  126. // TODO: ensure that buf is completely read.
  127. return abciResponses, nil
  128. }
  129. // SaveABCIResponses persists the ABCIResponses to the database.
  130. // This is useful in case we crash after app.Commit and before s.Save().
  131. // Responses are indexed by height so they can also be loaded later to produce Merkle proofs.
  132. func saveABCIResponses(db dbm.DB, height int64, abciResponses *ABCIResponses) {
  133. db.SetSync(calcABCIResponsesKey(height), abciResponses.Bytes())
  134. }
  135. //-----------------------------------------------------------------------------
  136. // ValidatorsInfo represents the latest validator set, or the last height it changed
  137. type ValidatorsInfo struct {
  138. ValidatorSet *types.ValidatorSet
  139. LastHeightChanged int64
  140. }
  141. // Bytes serializes the ValidatorsInfo using go-amino.
  142. func (valInfo *ValidatorsInfo) Bytes() []byte {
  143. return cdc.MustMarshalBinaryBare(valInfo)
  144. }
  145. // LoadValidators loads the ValidatorSet for a given height.
  146. // Returns ErrNoValSetForHeight if the validator set can't be found for this height.
  147. func LoadValidators(db dbm.DB, height int64) (*types.ValidatorSet, error) {
  148. valInfo := loadValidatorsInfo(db, height)
  149. if valInfo == nil {
  150. return nil, ErrNoValSetForHeight{height}
  151. }
  152. if valInfo.ValidatorSet == nil {
  153. valInfo2 := loadValidatorsInfo(db, valInfo.LastHeightChanged)
  154. if valInfo2 == nil {
  155. cmn.PanicSanity(fmt.Sprintf(`Couldn't find validators at height %d as
  156. last changed from height %d`, valInfo.LastHeightChanged, height))
  157. }
  158. valInfo = valInfo2
  159. }
  160. return valInfo.ValidatorSet, nil
  161. }
  162. func loadValidatorsInfo(db dbm.DB, height int64) *ValidatorsInfo {
  163. buf := db.Get(calcValidatorsKey(height))
  164. if len(buf) == 0 {
  165. return nil
  166. }
  167. v := new(ValidatorsInfo)
  168. err := cdc.UnmarshalBinaryBare(buf, v)
  169. if err != nil {
  170. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  171. cmn.Exit(cmn.Fmt(`LoadValidators: Data has been corrupted or its spec has changed:
  172. %v\n`, err))
  173. }
  174. // TODO: ensure that buf is completely read.
  175. return v
  176. }
  177. // saveValidatorsInfo persists the validator set for the next block to disk.
  178. // It should be called from s.Save(), right before the state itself is persisted.
  179. // If the validator set did not change after processing the latest block,
  180. // only the last height for which the validators changed is persisted.
  181. func saveValidatorsInfo(db dbm.DB, nextHeight, changeHeight int64, valSet *types.ValidatorSet) {
  182. valInfo := &ValidatorsInfo{
  183. LastHeightChanged: changeHeight,
  184. }
  185. if changeHeight == nextHeight {
  186. valInfo.ValidatorSet = valSet
  187. }
  188. db.SetSync(calcValidatorsKey(nextHeight), valInfo.Bytes())
  189. }
  190. //-----------------------------------------------------------------------------
  191. // ConsensusParamsInfo represents the latest consensus params, or the last height it changed
  192. type ConsensusParamsInfo struct {
  193. ConsensusParams types.ConsensusParams
  194. LastHeightChanged int64
  195. }
  196. // Bytes serializes the ConsensusParamsInfo using go-amino.
  197. func (params ConsensusParamsInfo) Bytes() []byte {
  198. return cdc.MustMarshalBinaryBare(params)
  199. }
  200. // LoadConsensusParams loads the ConsensusParams for a given height.
  201. func LoadConsensusParams(db dbm.DB, height int64) (types.ConsensusParams, error) {
  202. empty := types.ConsensusParams{}
  203. paramsInfo := loadConsensusParamsInfo(db, height)
  204. if paramsInfo == nil {
  205. return empty, ErrNoConsensusParamsForHeight{height}
  206. }
  207. if paramsInfo.ConsensusParams == empty {
  208. paramsInfo = loadConsensusParamsInfo(db, paramsInfo.LastHeightChanged)
  209. if paramsInfo == nil {
  210. cmn.PanicSanity(fmt.Sprintf(`Couldn't find consensus params at height %d as
  211. last changed from height %d`, paramsInfo.LastHeightChanged, height))
  212. }
  213. }
  214. return paramsInfo.ConsensusParams, nil
  215. }
  216. func loadConsensusParamsInfo(db dbm.DB, height int64) *ConsensusParamsInfo {
  217. buf := db.Get(calcConsensusParamsKey(height))
  218. if len(buf) == 0 {
  219. return nil
  220. }
  221. paramsInfo := new(ConsensusParamsInfo)
  222. err := cdc.UnmarshalBinaryBare(buf, paramsInfo)
  223. if err != nil {
  224. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  225. cmn.Exit(cmn.Fmt(`LoadConsensusParams: Data has been corrupted or its spec has changed:
  226. %v\n`, err))
  227. }
  228. // TODO: ensure that buf is completely read.
  229. return paramsInfo
  230. }
  231. // saveConsensusParamsInfo persists the consensus params for the next block to disk.
  232. // It should be called from s.Save(), right before the state itself is persisted.
  233. // If the consensus params did not change after processing the latest block,
  234. // only the last height for which they changed is persisted.
  235. func saveConsensusParamsInfo(db dbm.DB, nextHeight, changeHeight int64, params types.ConsensusParams) {
  236. paramsInfo := &ConsensusParamsInfo{
  237. LastHeightChanged: changeHeight,
  238. }
  239. if changeHeight == nextHeight {
  240. paramsInfo.ConsensusParams = params
  241. }
  242. db.SetSync(calcConsensusParamsKey(nextHeight), paramsInfo.Bytes())
  243. }