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
8.8 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
  1. package state
  2. import (
  3. "fmt"
  4. abci "github.com/tendermint/abci/types"
  5. wire "github.com/tendermint/go-wire"
  6. "github.com/tendermint/tendermint/types"
  7. cmn "github.com/tendermint/tmlibs/common"
  8. dbm "github.com/tendermint/tmlibs/db"
  9. )
  10. //------------------------------------------------------------------------
  11. func calcValidatorsKey(height int64) []byte {
  12. return []byte(cmn.Fmt("validatorsKey:%v", height))
  13. }
  14. func calcConsensusParamsKey(height int64) []byte {
  15. return []byte(cmn.Fmt("consensusParamsKey:%v", height))
  16. }
  17. func calcABCIResponsesKey(height int64) []byte {
  18. return []byte(cmn.Fmt("abciResponsesKey:%v", height))
  19. }
  20. // LoadStateFromDBOrGenesisFile loads the most recent state from the database,
  21. // or creates a new one from the given genesisFilePath and persists the result
  22. // to the database.
  23. func LoadStateFromDBOrGenesisFile(stateDB dbm.DB, genesisFilePath string) (State, error) {
  24. state := LoadState(stateDB)
  25. if state.IsEmpty() {
  26. var err error
  27. state, err = MakeGenesisStateFromFile(genesisFilePath)
  28. if err != nil {
  29. return state, err
  30. }
  31. SaveState(stateDB, state)
  32. }
  33. return state, nil
  34. }
  35. // LoadStateFromDBOrGenesisDoc loads the most recent state from the database,
  36. // or creates a new one from the given genesisDoc and persists the result
  37. // to the database.
  38. func LoadStateFromDBOrGenesisDoc(stateDB dbm.DB, genesisDoc *types.GenesisDoc) (State, error) {
  39. state := LoadState(stateDB)
  40. if state.IsEmpty() {
  41. var err error
  42. state, err = MakeGenesisState(genesisDoc)
  43. if err != nil {
  44. return state, err
  45. }
  46. SaveState(stateDB, state)
  47. }
  48. return state, nil
  49. }
  50. // LoadState loads the State from the database.
  51. func LoadState(db dbm.DB) State {
  52. return loadState(db, stateKey)
  53. }
  54. func loadState(db dbm.DB, key []byte) (state State) {
  55. buf := db.Get(key)
  56. if len(buf) == 0 {
  57. return state
  58. }
  59. err := wire.UnmarshalBinary(buf, &state)
  60. if err != nil {
  61. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  62. cmn.Exit(cmn.Fmt(`LoadState: Data has been corrupted or its spec has changed:
  63. %v\n`, err))
  64. }
  65. // TODO: ensure that buf is completely read.
  66. return state
  67. }
  68. // SaveState persists the State, the ValidatorsInfo, and the ConsensusParamsInfo to the database.
  69. func SaveState(db dbm.DB, s State) {
  70. saveState(db, s, stateKey)
  71. }
  72. func saveState(db dbm.DB, s State, key []byte) {
  73. nextHeight := s.LastBlockHeight + 1
  74. saveValidatorsInfo(db, nextHeight, s.LastHeightValidatorsChanged, s.Validators)
  75. saveConsensusParamsInfo(db, nextHeight, s.LastHeightConsensusParamsChanged, s.ConsensusParams)
  76. db.SetSync(stateKey, s.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. return &ABCIResponses{
  89. DeliverTx: make([]*abci.ResponseDeliverTx, block.NumTxs),
  90. }
  91. }
  92. // Bytes serializes the ABCIResponse using go-wire
  93. func (a *ABCIResponses) Bytes() []byte {
  94. bz, err := wire.MarshalBinary(*a)
  95. if err != nil {
  96. panic(err)
  97. }
  98. return bz
  99. }
  100. func (a *ABCIResponses) ResultsHash() []byte {
  101. results := types.NewResults(a.DeliverTx)
  102. return results.Hash()
  103. }
  104. // LoadABCIResponses loads the ABCIResponses for the given height from the database.
  105. // This is useful for recovering from crashes where we called app.Commit and before we called
  106. // s.Save(). It can also be used to produce Merkle proofs of the result of txs.
  107. func LoadABCIResponses(db dbm.DB, height int64) (*ABCIResponses, error) {
  108. buf := db.Get(calcABCIResponsesKey(height))
  109. if len(buf) == 0 {
  110. return nil, ErrNoABCIResponsesForHeight{height}
  111. }
  112. abciResponses := new(ABCIResponses)
  113. err := wire.UnmarshalBinary(buf, abciResponses)
  114. if err != nil {
  115. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  116. cmn.Exit(cmn.Fmt(`LoadABCIResponses: Data has been corrupted or its spec has
  117. changed: %v\n`, err))
  118. }
  119. // TODO: ensure that buf is completely read.
  120. return abciResponses, nil
  121. }
  122. // SaveABCIResponses persists the ABCIResponses to the database.
  123. // This is useful in case we crash after app.Commit and before s.Save().
  124. // Responses are indexed by height so they can also be loaded later to produce Merkle proofs.
  125. func saveABCIResponses(db dbm.DB, height int64, abciResponses *ABCIResponses) {
  126. db.SetSync(calcABCIResponsesKey(height), abciResponses.Bytes())
  127. }
  128. //-----------------------------------------------------------------------------
  129. // ValidatorsInfo represents the latest validator set, or the last height it changed
  130. type ValidatorsInfo struct {
  131. ValidatorSet *types.ValidatorSet
  132. LastHeightChanged int64
  133. }
  134. // Bytes serializes the ValidatorsInfo using go-wire
  135. func (valInfo *ValidatorsInfo) Bytes() []byte {
  136. bz, err := wire.MarshalBinary(*valInfo)
  137. if err != nil {
  138. panic(err)
  139. }
  140. return bz
  141. }
  142. // LoadValidators loads the ValidatorSet for a given height.
  143. // Returns ErrNoValSetForHeight if the validator set can't be found for this height.
  144. func LoadValidators(db dbm.DB, height int64) (*types.ValidatorSet, error) {
  145. valInfo := loadValidatorsInfo(db, height)
  146. if valInfo == nil {
  147. return nil, ErrNoValSetForHeight{height}
  148. }
  149. if valInfo.ValidatorSet == nil {
  150. valInfo = loadValidatorsInfo(db, valInfo.LastHeightChanged)
  151. if valInfo == nil {
  152. cmn.PanicSanity(fmt.Sprintf(`Couldn't find validators at height %d as
  153. last changed from height %d`, valInfo.LastHeightChanged, height))
  154. }
  155. }
  156. return valInfo.ValidatorSet, nil
  157. }
  158. func loadValidatorsInfo(db dbm.DB, height int64) *ValidatorsInfo {
  159. buf := db.Get(calcValidatorsKey(height))
  160. if len(buf) == 0 {
  161. return nil
  162. }
  163. v := new(ValidatorsInfo)
  164. err := wire.UnmarshalBinary(buf, v)
  165. if err != nil {
  166. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  167. cmn.Exit(cmn.Fmt(`LoadValidators: Data has been corrupted or its spec has changed:
  168. %v\n`, err))
  169. }
  170. // TODO: ensure that buf is completely read.
  171. return v
  172. }
  173. // saveValidatorsInfo persists the validator set for the next block to disk.
  174. // It should be called from s.Save(), right before the state itself is persisted.
  175. // If the validator set did not change after processing the latest block,
  176. // only the last height for which the validators changed is persisted.
  177. func saveValidatorsInfo(db dbm.DB, nextHeight, changeHeight int64, valSet *types.ValidatorSet) {
  178. valInfo := &ValidatorsInfo{
  179. LastHeightChanged: changeHeight,
  180. }
  181. if changeHeight == nextHeight {
  182. valInfo.ValidatorSet = valSet
  183. }
  184. db.SetSync(calcValidatorsKey(nextHeight), valInfo.Bytes())
  185. }
  186. //-----------------------------------------------------------------------------
  187. // ConsensusParamsInfo represents the latest consensus params, or the last height it changed
  188. type ConsensusParamsInfo struct {
  189. ConsensusParams types.ConsensusParams
  190. LastHeightChanged int64
  191. }
  192. // Bytes serializes the ConsensusParamsInfo using go-wire
  193. func (params ConsensusParamsInfo) Bytes() []byte {
  194. bz, err := wire.MarshalBinary(params)
  195. if err != nil {
  196. panic(err)
  197. }
  198. return bz
  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 := wire.UnmarshalBinary(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. }