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.

533 lines
15 KiB

7 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 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. "github.com/gogo/protobuf/proto"
  5. dbm "github.com/tendermint/tm-db"
  6. abci "github.com/tendermint/tendermint/abci/types"
  7. "github.com/tendermint/tendermint/crypto/merkle"
  8. tmmath "github.com/tendermint/tendermint/libs/math"
  9. tmos "github.com/tendermint/tendermint/libs/os"
  10. tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
  11. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. const (
  15. // persist validators every valSetCheckpointInterval blocks to avoid
  16. // LoadValidators taking too much time.
  17. // https://github.com/tendermint/tendermint/pull/3438
  18. // 100000 results in ~ 100ms to get 100 validators (see BenchmarkLoadValidators)
  19. valSetCheckpointInterval = 100000
  20. )
  21. //------------------------------------------------------------------------
  22. func calcValidatorsKey(height int64) []byte {
  23. return []byte(fmt.Sprintf("validatorsKey:%v", height))
  24. }
  25. func calcConsensusParamsKey(height int64) []byte {
  26. return []byte(fmt.Sprintf("consensusParamsKey:%v", height))
  27. }
  28. func calcABCIResponsesKey(height int64) []byte {
  29. return []byte(fmt.Sprintf("abciResponsesKey:%v", height))
  30. }
  31. // LoadStateFromDBOrGenesisFile loads the most recent state from the database,
  32. // or creates a new one from the given genesisFilePath and persists the result
  33. // to the database.
  34. func LoadStateFromDBOrGenesisFile(stateDB dbm.DB, genesisFilePath string) (State, error) {
  35. state := LoadState(stateDB)
  36. if state.IsEmpty() {
  37. var err error
  38. state, err = MakeGenesisStateFromFile(genesisFilePath)
  39. if err != nil {
  40. return state, err
  41. }
  42. SaveState(stateDB, state)
  43. }
  44. return state, nil
  45. }
  46. // LoadStateFromDBOrGenesisDoc loads the most recent state from the database,
  47. // or creates a new one from the given genesisDoc and persists the result
  48. // to the database.
  49. func LoadStateFromDBOrGenesisDoc(stateDB dbm.DB, genesisDoc *types.GenesisDoc) (State, error) {
  50. state := LoadState(stateDB)
  51. if state.IsEmpty() {
  52. var err error
  53. state, err = MakeGenesisState(genesisDoc)
  54. if err != nil {
  55. return state, err
  56. }
  57. SaveState(stateDB, state)
  58. }
  59. return state, nil
  60. }
  61. // LoadState loads the State from the database.
  62. func LoadState(db dbm.DB) State {
  63. return loadState(db, stateKey)
  64. }
  65. func loadState(db dbm.DB, key []byte) (state State) {
  66. buf, err := db.Get(key)
  67. if err != nil {
  68. panic(err)
  69. }
  70. if len(buf) == 0 {
  71. return state
  72. }
  73. sp := new(tmstate.State)
  74. err = proto.Unmarshal(buf, sp)
  75. if err != nil {
  76. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  77. tmos.Exit(fmt.Sprintf(`LoadState: Data has been corrupted or its spec has changed:
  78. %v\n`, err))
  79. }
  80. sm, err := StateFromProto(sp)
  81. if err != nil {
  82. panic(err)
  83. }
  84. return *sm
  85. }
  86. // SaveState persists the State, the ValidatorsInfo, and the ConsensusParamsInfo to the database.
  87. // This flushes the writes (e.g. calls SetSync).
  88. func SaveState(db dbm.DB, state State) {
  89. saveState(db, state, stateKey)
  90. }
  91. func saveState(db dbm.DB, state State, key []byte) {
  92. nextHeight := state.LastBlockHeight + 1
  93. // If first block, save validators for block 1.
  94. if nextHeight == 1 {
  95. // This extra logic due to Tendermint validator set changes being delayed 1 block.
  96. // It may get overwritten due to InitChain validator updates.
  97. lastHeightVoteChanged := int64(1)
  98. saveValidatorsInfo(db, nextHeight, lastHeightVoteChanged, state.Validators)
  99. }
  100. // Save next validators.
  101. saveValidatorsInfo(db, nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators)
  102. // Save next consensus params.
  103. saveConsensusParamsInfo(db, nextHeight, state.LastHeightConsensusParamsChanged, state.ConsensusParams)
  104. err := db.SetSync(key, state.Bytes())
  105. if err != nil {
  106. panic(err)
  107. }
  108. }
  109. // BootstrapState saves a new state, used e.g. by state sync when starting from non-zero height.
  110. func BootstrapState(db dbm.DB, state State) error {
  111. height := state.LastBlockHeight
  112. saveValidatorsInfo(db, height, height, state.LastValidators)
  113. saveValidatorsInfo(db, height+1, height+1, state.Validators)
  114. saveValidatorsInfo(db, height+2, height+2, state.NextValidators)
  115. saveConsensusParamsInfo(db, height+1, height+1, state.ConsensusParams)
  116. return db.SetSync(stateKey, state.Bytes())
  117. }
  118. // PruneStates deletes states between the given heights (including from, excluding to). It is not
  119. // guaranteed to delete all states, since the last checkpointed state and states being pointed to by
  120. // e.g. `LastHeightChanged` must remain. The state at to must also exist.
  121. //
  122. // The from parameter is necessary since we can't do a key scan in a performant way due to the key
  123. // encoding not preserving ordering: https://github.com/tendermint/tendermint/issues/4567
  124. // This will cause some old states to be left behind when doing incremental partial prunes,
  125. // specifically older checkpoints and LastHeightChanged targets.
  126. func PruneStates(db dbm.DB, from int64, to int64) error {
  127. if from <= 0 || to <= 0 {
  128. return fmt.Errorf("from height %v and to height %v must be greater than 0", from, to)
  129. }
  130. if from >= to {
  131. return fmt.Errorf("from height %v must be lower than to height %v", from, to)
  132. }
  133. valInfo := loadValidatorsInfo(db, to)
  134. if valInfo == nil {
  135. return fmt.Errorf("validators at height %v not found", to)
  136. }
  137. paramsInfo := loadConsensusParamsInfo(db, to)
  138. if paramsInfo == nil {
  139. return fmt.Errorf("consensus params at height %v not found", to)
  140. }
  141. keepVals := make(map[int64]bool)
  142. if valInfo.ValidatorSet == nil {
  143. keepVals[valInfo.LastHeightChanged] = true
  144. keepVals[lastStoredHeightFor(to, valInfo.LastHeightChanged)] = true // keep last checkpoint too
  145. }
  146. keepParams := make(map[int64]bool)
  147. if paramsInfo.ConsensusParams.Equal(&tmproto.ConsensusParams{}) {
  148. keepParams[paramsInfo.LastHeightChanged] = true
  149. }
  150. batch := db.NewBatch()
  151. defer batch.Close()
  152. pruned := uint64(0)
  153. var err error
  154. // We have to delete in reverse order, to avoid deleting previous heights that have validator
  155. // sets and consensus params that we may need to retrieve.
  156. for h := to - 1; h >= from; h-- {
  157. // For heights we keep, we must make sure they have the full validator set or consensus
  158. // params, otherwise they will panic if they're retrieved directly (instead of
  159. // indirectly via a LastHeightChanged pointer).
  160. if keepVals[h] {
  161. v := loadValidatorsInfo(db, h)
  162. if v.ValidatorSet == nil {
  163. vip, err := LoadValidators(db, h)
  164. if err != nil {
  165. return err
  166. }
  167. pvi, err := vip.ToProto()
  168. if err != nil {
  169. return err
  170. }
  171. v.ValidatorSet = pvi
  172. v.LastHeightChanged = h
  173. bz, err := v.Marshal()
  174. if err != nil {
  175. return err
  176. }
  177. err = batch.Set(calcValidatorsKey(h), bz)
  178. if err != nil {
  179. return err
  180. }
  181. }
  182. } else {
  183. err = batch.Delete(calcValidatorsKey(h))
  184. if err != nil {
  185. return err
  186. }
  187. }
  188. if keepParams[h] {
  189. p := loadConsensusParamsInfo(db, h)
  190. if p.ConsensusParams.Equal(&tmproto.ConsensusParams{}) {
  191. p.ConsensusParams, err = LoadConsensusParams(db, h)
  192. if err != nil {
  193. return err
  194. }
  195. p.LastHeightChanged = h
  196. bz, err := p.Marshal()
  197. if err != nil {
  198. return err
  199. }
  200. err = batch.Set(calcConsensusParamsKey(h), bz)
  201. if err != nil {
  202. return err
  203. }
  204. }
  205. } else {
  206. err = batch.Delete(calcConsensusParamsKey(h))
  207. if err != nil {
  208. return err
  209. }
  210. }
  211. err = batch.Delete(calcABCIResponsesKey(h))
  212. if err != nil {
  213. return err
  214. }
  215. pruned++
  216. // avoid batches growing too large by flushing to database regularly
  217. if pruned%1000 == 0 && pruned > 0 {
  218. err := batch.Write()
  219. if err != nil {
  220. return err
  221. }
  222. batch.Close()
  223. batch = db.NewBatch()
  224. defer batch.Close()
  225. }
  226. }
  227. err = batch.WriteSync()
  228. if err != nil {
  229. return err
  230. }
  231. return nil
  232. }
  233. //------------------------------------------------------------------------
  234. // ABCIResponsesResultsHash returns the root hash of a Merkle tree with 3 leafs:
  235. // 1) proto encoded ResponseBeginBlock.Events
  236. // 2) root hash of a Merkle tree of ResponseDeliverTx responses (see ABCIResults.Hash)
  237. // 3) proto encoded ResponseEndBlock.Events
  238. //
  239. // See merkle.SimpleHashFromByteSlices
  240. func ABCIResponsesResultsHash(ar *tmstate.ABCIResponses) []byte {
  241. // proto-encode BeginBlock events.
  242. bbeBytes, err := proto.Marshal(&abci.ResponseBeginBlock{
  243. Events: ar.BeginBlock.Events,
  244. })
  245. if err != nil {
  246. panic(err)
  247. }
  248. // Build a Merkle tree of proto-encoded DeliverTx results and get a hash.
  249. results := types.NewResults(ar.DeliverTxs)
  250. // proto-encode EndBlock events.
  251. ebeBytes, err := proto.Marshal(&abci.ResponseEndBlock{
  252. Events: ar.EndBlock.Events,
  253. })
  254. if err != nil {
  255. panic(err)
  256. }
  257. // Build a Merkle tree out of the above 3 binary slices.
  258. return merkle.HashFromByteSlices([][]byte{bbeBytes, results.Hash(), ebeBytes})
  259. }
  260. // LoadABCIResponses loads the ABCIResponses for the given height from the
  261. // database. If not found, ErrNoABCIResponsesForHeight is returned.
  262. //
  263. // This is useful for recovering from crashes where we called app.Commit and
  264. // before we called s.Save(). It can also be used to produce Merkle proofs of
  265. // the result of txs.
  266. func LoadABCIResponses(db dbm.DB, height int64) (*tmstate.ABCIResponses, error) {
  267. buf, err := db.Get(calcABCIResponsesKey(height))
  268. if err != nil {
  269. return nil, err
  270. }
  271. if len(buf) == 0 {
  272. return nil, ErrNoABCIResponsesForHeight{height}
  273. }
  274. abciResponses := new(tmstate.ABCIResponses)
  275. err = abciResponses.Unmarshal(buf)
  276. if err != nil {
  277. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  278. tmos.Exit(fmt.Sprintf(`LoadABCIResponses: Data has been corrupted or its spec has
  279. changed: %v\n`, err))
  280. }
  281. // TODO: ensure that buf is completely read.
  282. return abciResponses, nil
  283. }
  284. // SaveABCIResponses persists the ABCIResponses to the database.
  285. // This is useful in case we crash after app.Commit and before s.Save().
  286. // Responses are indexed by height so they can also be loaded later to produce
  287. // Merkle proofs.
  288. //
  289. // Exposed for testing.
  290. func SaveABCIResponses(db dbm.DB, height int64, abciResponses *tmstate.ABCIResponses) {
  291. var dtxs []*abci.ResponseDeliverTx
  292. //strip nil values,
  293. for _, tx := range abciResponses.DeliverTxs {
  294. if tx != nil {
  295. dtxs = append(dtxs, tx)
  296. }
  297. }
  298. abciResponses.DeliverTxs = dtxs
  299. bz, err := abciResponses.Marshal()
  300. if err != nil {
  301. panic(err)
  302. }
  303. err = db.SetSync(calcABCIResponsesKey(height), bz)
  304. if err != nil {
  305. panic(err)
  306. }
  307. }
  308. //-----------------------------------------------------------------------------
  309. // LoadValidators loads the ValidatorSet for a given height.
  310. // Returns ErrNoValSetForHeight if the validator set can't be found for this height.
  311. func LoadValidators(db dbm.DB, height int64) (*types.ValidatorSet, error) {
  312. valInfo := loadValidatorsInfo(db, height)
  313. if valInfo == nil {
  314. return nil, ErrNoValSetForHeight{height}
  315. }
  316. if valInfo.ValidatorSet == nil {
  317. lastStoredHeight := lastStoredHeightFor(height, valInfo.LastHeightChanged)
  318. valInfo2 := loadValidatorsInfo(db, lastStoredHeight)
  319. if valInfo2 == nil || valInfo2.ValidatorSet == nil {
  320. panic(
  321. fmt.Sprintf("Couldn't find validators at height %d (height %d was originally requested)",
  322. lastStoredHeight,
  323. height,
  324. ),
  325. )
  326. }
  327. vs, err := types.ValidatorSetFromProto(valInfo2.ValidatorSet)
  328. if err != nil {
  329. return nil, err
  330. }
  331. vs.IncrementProposerPriority(tmmath.SafeConvertInt32(height - lastStoredHeight)) // mutate
  332. vi2, err := vs.ToProto()
  333. if err != nil {
  334. return nil, err
  335. }
  336. valInfo2.ValidatorSet = vi2
  337. valInfo = valInfo2
  338. }
  339. vip, err := types.ValidatorSetFromProto(valInfo.ValidatorSet)
  340. if err != nil {
  341. return nil, err
  342. }
  343. return vip, nil
  344. }
  345. func lastStoredHeightFor(height, lastHeightChanged int64) int64 {
  346. checkpointHeight := height - height%valSetCheckpointInterval
  347. return tmmath.MaxInt64(checkpointHeight, lastHeightChanged)
  348. }
  349. // CONTRACT: Returned ValidatorsInfo can be mutated.
  350. func loadValidatorsInfo(db dbm.DB, height int64) *tmstate.ValidatorsInfo {
  351. buf, err := db.Get(calcValidatorsKey(height))
  352. if err != nil {
  353. panic(err)
  354. }
  355. if len(buf) == 0 {
  356. return nil
  357. }
  358. v := new(tmstate.ValidatorsInfo)
  359. err = v.Unmarshal(buf)
  360. if err != nil {
  361. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  362. tmos.Exit(fmt.Sprintf(`LoadValidators: Data has been corrupted or its spec has changed:
  363. %v\n`, err))
  364. }
  365. // TODO: ensure that buf is completely read.
  366. return v
  367. }
  368. // saveValidatorsInfo persists the validator set.
  369. //
  370. // `height` is the effective height for which the validator is responsible for
  371. // signing. It should be called from s.Save(), right before the state itself is
  372. // persisted.
  373. func saveValidatorsInfo(db dbm.DB, height, lastHeightChanged int64, valSet *types.ValidatorSet) {
  374. if lastHeightChanged > height {
  375. panic("LastHeightChanged cannot be greater than ValidatorsInfo height")
  376. }
  377. valInfo := &tmstate.ValidatorsInfo{
  378. LastHeightChanged: lastHeightChanged,
  379. }
  380. // Only persist validator set if it was updated or checkpoint height (see
  381. // valSetCheckpointInterval) is reached.
  382. if height == lastHeightChanged || height%valSetCheckpointInterval == 0 {
  383. pv, err := valSet.ToProto()
  384. if err != nil {
  385. panic(err)
  386. }
  387. valInfo.ValidatorSet = pv
  388. }
  389. bz, err := valInfo.Marshal()
  390. if err != nil {
  391. panic(err)
  392. }
  393. err = db.Set(calcValidatorsKey(height), bz)
  394. if err != nil {
  395. panic(err)
  396. }
  397. }
  398. //-----------------------------------------------------------------------------
  399. // ConsensusParamsInfo represents the latest consensus params, or the last height it changed
  400. // LoadConsensusParams loads the ConsensusParams for a given height.
  401. func LoadConsensusParams(db dbm.DB, height int64) (tmproto.ConsensusParams, error) {
  402. empty := tmproto.ConsensusParams{}
  403. paramsInfo := loadConsensusParamsInfo(db, height)
  404. if paramsInfo == nil {
  405. return empty, ErrNoConsensusParamsForHeight{height}
  406. }
  407. if paramsInfo.ConsensusParams.Equal(&empty) {
  408. paramsInfo2 := loadConsensusParamsInfo(db, paramsInfo.LastHeightChanged)
  409. if paramsInfo2 == nil {
  410. panic(
  411. fmt.Sprintf(
  412. "Couldn't find consensus params at height %d as last changed from height %d",
  413. paramsInfo.LastHeightChanged,
  414. height,
  415. ),
  416. )
  417. }
  418. paramsInfo = paramsInfo2
  419. }
  420. return paramsInfo.ConsensusParams, nil
  421. }
  422. func loadConsensusParamsInfo(db dbm.DB, height int64) *tmstate.ConsensusParamsInfo {
  423. buf, err := db.Get(calcConsensusParamsKey(height))
  424. if err != nil {
  425. panic(err)
  426. }
  427. if len(buf) == 0 {
  428. return nil
  429. }
  430. paramsInfo := new(tmstate.ConsensusParamsInfo)
  431. if err = paramsInfo.Unmarshal(buf); err != nil {
  432. // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
  433. tmos.Exit(fmt.Sprintf(`LoadConsensusParams: Data has been corrupted or its spec has changed:
  434. %v\n`, err))
  435. }
  436. // TODO: ensure that buf is completely read.
  437. return paramsInfo
  438. }
  439. // saveConsensusParamsInfo persists the consensus params for the next block to disk.
  440. // It should be called from s.Save(), right before the state itself is persisted.
  441. // If the consensus params did not change after processing the latest block,
  442. // only the last height for which they changed is persisted.
  443. func saveConsensusParamsInfo(db dbm.DB, nextHeight, changeHeight int64, params tmproto.ConsensusParams) {
  444. paramsInfo := &tmstate.ConsensusParamsInfo{
  445. LastHeightChanged: changeHeight,
  446. }
  447. if changeHeight == nextHeight {
  448. paramsInfo.ConsensusParams = params
  449. }
  450. bz, err := paramsInfo.Marshal()
  451. if err != nil {
  452. panic(err)
  453. }
  454. err = db.Set(calcConsensusParamsKey(nextHeight), bz)
  455. if err != nil {
  456. panic(err)
  457. }
  458. }