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.

294 lines
9.0 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. cmn "github.com/tendermint/tendermint/libs/common"
  6. dbm "github.com/tendermint/tendermint/libs/db"
  7. "github.com/tendermint/tendermint/types"
  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. // This flushes the writes (e.g. calls SetSync).
  69. func SaveState(db dbm.DB, state State) {
  70. saveState(db, state, stateKey)
  71. }
  72. func saveState(db dbm.DB, state State, key []byte) {
  73. nextHeight := state.LastBlockHeight + 1
  74. saveValidatorsInfo(db, nextHeight, state.LastHeightValidatorsChanged, state.Validators)
  75. saveConsensusParamsInfo(db, nextHeight, state.LastHeightConsensusParamsChanged, state.ConsensusParams)
  76. db.SetSync(stateKey, state.Bytes())
  77. }
  78. //------------------------------------------------------------------------
  79. // ABCIResponses retains the responses
  80. // of the various ABCI calls during block processing.
  81. // It is persisted to disk for each height before calling Commit.
  82. type ABCIResponses struct {
  83. DeliverTx []*abci.ResponseDeliverTx
  84. EndBlock *abci.ResponseEndBlock
  85. }
  86. // NewABCIResponses returns a new ABCIResponses
  87. func NewABCIResponses(block *types.Block) *ABCIResponses {
  88. resDeliverTxs := make([]*abci.ResponseDeliverTx, block.NumTxs)
  89. if block.NumTxs == 0 {
  90. // This makes Amino encoding/decoding consistent.
  91. resDeliverTxs = nil
  92. }
  93. return &ABCIResponses{
  94. DeliverTx: resDeliverTxs,
  95. }
  96. }
  97. // Bytes serializes the ABCIResponse using go-amino.
  98. func (arz *ABCIResponses) Bytes() []byte {
  99. return cdc.MustMarshalBinaryBare(arz)
  100. }
  101. func (arz *ABCIResponses) ResultsHash() []byte {
  102. results := types.NewResults(arz.DeliverTx)
  103. return results.Hash()
  104. }
  105. // LoadABCIResponses loads the ABCIResponses for the given height from the database.
  106. // This is useful for recovering from crashes where we called app.Commit and before we called
  107. // s.Save(). It can also be used to produce Merkle proofs of the result of txs.
  108. func LoadABCIResponses(db dbm.DB, height int64) (*ABCIResponses, error) {
  109. buf := db.Get(calcABCIResponsesKey(height))
  110. if len(buf) == 0 {
  111. return nil, ErrNoABCIResponsesForHeight{height}
  112. }
  113. abciResponses := new(ABCIResponses)
  114. err := cdc.UnmarshalBinaryBare(buf, abciResponses)
  115. if err != nil {
  116. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  117. cmn.Exit(cmn.Fmt(`LoadABCIResponses: Data has been corrupted or its spec has
  118. changed: %v\n`, err))
  119. }
  120. // TODO: ensure that buf is completely read.
  121. return abciResponses, nil
  122. }
  123. // SaveABCIResponses persists the ABCIResponses to the database.
  124. // This is useful in case we crash after app.Commit and before s.Save().
  125. // Responses are indexed by height so they can also be loaded later to produce Merkle proofs.
  126. func saveABCIResponses(db dbm.DB, height int64, abciResponses *ABCIResponses) {
  127. db.SetSync(calcABCIResponsesKey(height), abciResponses.Bytes())
  128. }
  129. //-----------------------------------------------------------------------------
  130. // ValidatorsInfo represents the latest validator set, or the last height it changed
  131. type ValidatorsInfo struct {
  132. ValidatorSet *types.ValidatorSet
  133. LastHeightChanged int64
  134. }
  135. // Bytes serializes the ValidatorsInfo using go-amino.
  136. func (valInfo *ValidatorsInfo) Bytes() []byte {
  137. return cdc.MustMarshalBinaryBare(valInfo)
  138. }
  139. // LoadValidators loads the ValidatorSet for a given height.
  140. // Returns ErrNoValSetForHeight if the validator set can't be found for this height.
  141. func LoadValidators(db dbm.DB, height int64) (*types.ValidatorSet, error) {
  142. valInfo := loadValidatorsInfo(db, height)
  143. if valInfo == nil {
  144. return nil, ErrNoValSetForHeight{height}
  145. }
  146. if valInfo.ValidatorSet == nil {
  147. valInfo2 := loadValidatorsInfo(db, valInfo.LastHeightChanged)
  148. if valInfo2 == nil {
  149. panic(
  150. fmt.Sprintf(
  151. "Couldn't find validators at height %d as last changed from height %d",
  152. valInfo.LastHeightChanged,
  153. height,
  154. ),
  155. )
  156. }
  157. valInfo = valInfo2
  158. }
  159. return valInfo.ValidatorSet, nil
  160. }
  161. func loadValidatorsInfo(db dbm.DB, height int64) *ValidatorsInfo {
  162. buf := db.Get(calcValidatorsKey(height))
  163. if len(buf) == 0 {
  164. return nil
  165. }
  166. v := new(ValidatorsInfo)
  167. err := cdc.UnmarshalBinaryBare(buf, v)
  168. if err != nil {
  169. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  170. cmn.Exit(cmn.Fmt(`LoadValidators: Data has been corrupted or its spec has changed:
  171. %v\n`, err))
  172. }
  173. // TODO: ensure that buf is completely read.
  174. return v
  175. }
  176. // saveValidatorsInfo persists the validator set for the next block to disk.
  177. // It should be called from s.Save(), right before the state itself is persisted.
  178. // If the validator set did not change after processing the latest block,
  179. // only the last height for which the validators changed is persisted.
  180. func saveValidatorsInfo(db dbm.DB, nextHeight, changeHeight int64, valSet *types.ValidatorSet) {
  181. valInfo := &ValidatorsInfo{
  182. LastHeightChanged: changeHeight,
  183. }
  184. if changeHeight == nextHeight {
  185. valInfo.ValidatorSet = valSet
  186. }
  187. db.Set(calcValidatorsKey(nextHeight), valInfo.Bytes())
  188. }
  189. //-----------------------------------------------------------------------------
  190. // ConsensusParamsInfo represents the latest consensus params, or the last height it changed
  191. type ConsensusParamsInfo struct {
  192. ConsensusParams types.ConsensusParams
  193. LastHeightChanged int64
  194. }
  195. // Bytes serializes the ConsensusParamsInfo using go-amino.
  196. func (params ConsensusParamsInfo) Bytes() []byte {
  197. return cdc.MustMarshalBinaryBare(params)
  198. }
  199. // LoadConsensusParams loads the ConsensusParams for a given height.
  200. func LoadConsensusParams(db dbm.DB, height int64) (types.ConsensusParams, error) {
  201. empty := types.ConsensusParams{}
  202. paramsInfo := loadConsensusParamsInfo(db, height)
  203. if paramsInfo == nil {
  204. return empty, ErrNoConsensusParamsForHeight{height}
  205. }
  206. if paramsInfo.ConsensusParams == empty {
  207. paramsInfo2 := loadConsensusParamsInfo(db, paramsInfo.LastHeightChanged)
  208. if paramsInfo2 == nil {
  209. panic(
  210. fmt.Sprintf(
  211. "Couldn't find consensus params at height %d as last changed from height %d",
  212. paramsInfo.LastHeightChanged,
  213. height,
  214. ),
  215. )
  216. }
  217. paramsInfo = paramsInfo2
  218. }
  219. return paramsInfo.ConsensusParams, nil
  220. }
  221. func loadConsensusParamsInfo(db dbm.DB, height int64) *ConsensusParamsInfo {
  222. buf := db.Get(calcConsensusParamsKey(height))
  223. if len(buf) == 0 {
  224. return nil
  225. }
  226. paramsInfo := new(ConsensusParamsInfo)
  227. err := cdc.UnmarshalBinaryBare(buf, paramsInfo)
  228. if err != nil {
  229. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  230. cmn.Exit(cmn.Fmt(`LoadConsensusParams: Data has been corrupted or its spec has changed:
  231. %v\n`, err))
  232. }
  233. // TODO: ensure that buf is completely read.
  234. return paramsInfo
  235. }
  236. // saveConsensusParamsInfo persists the consensus params for the next block to disk.
  237. // It should be called from s.Save(), right before the state itself is persisted.
  238. // If the consensus params did not change after processing the latest block,
  239. // only the last height for which they changed is persisted.
  240. func saveConsensusParamsInfo(db dbm.DB, nextHeight, changeHeight int64, params types.ConsensusParams) {
  241. paramsInfo := &ConsensusParamsInfo{
  242. LastHeightChanged: changeHeight,
  243. }
  244. if changeHeight == nextHeight {
  245. paramsInfo.ConsensusParams = params
  246. }
  247. db.Set(calcConsensusParamsKey(nextHeight), paramsInfo.Bytes())
  248. }