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.

593 lines
16 KiB

blockchain: Reorg reactor (#3561) * go routines in blockchain reactor * Added reference to the go routine diagram * Initial commit * cleanup * Undo testing_logger change, committed by mistake * Fix the test loggers * pulled some fsm code into pool.go * added pool tests * changes to the design added block requests under peer moved the request trigger in the reactor poolRoutine, triggered now by a ticker in general moved everything required for making block requests smarter in the poolRoutine added a simple map of heights to keep track of what will need to be requested next added a few more tests * send errors to FSM in a different channel than blocks send errors (RemovePeer) from switch on a different channel than the one receiving blocks renamed channels added more pool tests * more pool tests * lint errors * more tests * more tests * switch fast sync to new implementation * fixed data race in tests * cleanup * finished fsm tests * address golangci comments :) * address golangci comments :) * Added timeout on next block needed to advance * updating docs and cleanup * fix issue in test from previous cleanup * cleanup * Added termination scenarios, tests and more cleanup * small fixes to adr, comments and cleanup * Fix bug in sendRequest() If we tried to send a request to a peer not present in the switch, a missing continue statement caused the request to be blackholed in a peer that was removed and never retried. While this bug was manifesting, the reactor kept asking for other blocks that would be stored and never consumed. Added the number of unconsumed blocks in the math for requesting blocks ahead of current processing height so eventually there will be no more blocks requested until the already received ones are consumed. * remove bpPeer's didTimeout field * Use distinct err codes for peer timeout and FSM timeouts * Don't allow peers to update with lower height * review comments from Ethan and Zarko * some cleanup, renaming, comments * Move block execution in separate goroutine * Remove pool's numPending * review comments * fix lint, remove old blockchain reactor and duplicates in fsm tests * small reorg around peer after review comments * add the reactor spec * verify block only once * review comments * change to int for max number of pending requests * cleanup and godoc * Add configuration flag fast sync version * golangci fixes * fix config template * move both reactor versions under blockchain * cleanup, golint, renaming stuff * updated documentation, fixed more golint warnings * integrate with behavior package * sync with master * gofmt * add changelog_pending entry * move to improvments * suggestion to changelog entry
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
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
10 years ago
8 years ago
  1. package store
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strconv"
  6. "github.com/gogo/protobuf/proto"
  7. "github.com/google/orderedcode"
  8. dbm "github.com/tendermint/tm-db"
  9. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  10. "github.com/tendermint/tendermint/types"
  11. )
  12. /*
  13. BlockStore is a simple low level store for blocks.
  14. There are three types of information stored:
  15. - BlockMeta: Meta information about each block
  16. - Block part: Parts of each block, aggregated w/ PartSet
  17. - Commit: The commit part of each block, for gossiping precommit votes
  18. Currently the precommit signatures are duplicated in the Block parts as
  19. well as the Commit. In the future this may change, perhaps by moving
  20. the Commit data outside the Block. (TODO)
  21. The store can be assumed to contain all contiguous blocks between base and height (inclusive).
  22. // NOTE: BlockStore methods will panic if they encounter errors
  23. // deserializing loaded data, indicating probable corruption on disk.
  24. */
  25. type BlockStore struct {
  26. db dbm.DB
  27. }
  28. // NewBlockStore returns a new BlockStore with the given DB,
  29. // initialized to the last height that was committed to the DB.
  30. func NewBlockStore(db dbm.DB) *BlockStore {
  31. return &BlockStore{db}
  32. }
  33. // Base returns the first known contiguous block height, or 0 for empty block stores.
  34. func (bs *BlockStore) Base() int64 {
  35. iter, err := bs.db.Iterator(
  36. blockMetaKey(1),
  37. blockMetaKey(1<<63-1),
  38. )
  39. if err != nil {
  40. panic(err)
  41. }
  42. defer iter.Close()
  43. if iter.Valid() {
  44. height, err := decodeBlockMetaKey(iter.Key())
  45. if err == nil {
  46. return height
  47. }
  48. }
  49. if err := iter.Error(); err != nil {
  50. panic(err)
  51. }
  52. return 0
  53. }
  54. // Height returns the last known contiguous block height, or 0 for empty block stores.
  55. func (bs *BlockStore) Height() int64 {
  56. iter, err := bs.db.ReverseIterator(
  57. blockMetaKey(1),
  58. blockMetaKey(1<<63-1),
  59. )
  60. if err != nil {
  61. panic(err)
  62. }
  63. defer iter.Close()
  64. if iter.Valid() {
  65. height, err := decodeBlockMetaKey(iter.Key())
  66. if err == nil {
  67. return height
  68. }
  69. }
  70. if err := iter.Error(); err != nil {
  71. panic(err)
  72. }
  73. return 0
  74. }
  75. // Size returns the number of blocks in the block store.
  76. func (bs *BlockStore) Size() int64 {
  77. height := bs.Height()
  78. if height == 0 {
  79. return 0
  80. }
  81. return height + 1 - bs.Base()
  82. }
  83. // LoadBase atomically loads the base block meta, or returns nil if no base is found.
  84. func (bs *BlockStore) LoadBaseMeta() *types.BlockMeta {
  85. iter, err := bs.db.Iterator(
  86. blockMetaKey(1),
  87. blockMetaKey(1<<63-1),
  88. )
  89. if err != nil {
  90. return nil
  91. }
  92. defer iter.Close()
  93. if iter.Valid() {
  94. var pbbm = new(tmproto.BlockMeta)
  95. err = proto.Unmarshal(iter.Value(), pbbm)
  96. if err != nil {
  97. panic(fmt.Errorf("unmarshal to tmproto.BlockMeta: %w", err))
  98. }
  99. blockMeta, err := types.BlockMetaFromProto(pbbm)
  100. if err != nil {
  101. panic(fmt.Errorf("error from proto blockMeta: %w", err))
  102. }
  103. return blockMeta
  104. }
  105. return nil
  106. }
  107. // LoadBlock returns the block with the given height.
  108. // If no block is found for that height, it returns nil.
  109. func (bs *BlockStore) LoadBlock(height int64) *types.Block {
  110. var blockMeta = bs.LoadBlockMeta(height)
  111. if blockMeta == nil {
  112. return nil
  113. }
  114. pbb := new(tmproto.Block)
  115. buf := []byte{}
  116. for i := 0; i < int(blockMeta.BlockID.PartSetHeader.Total); i++ {
  117. part := bs.LoadBlockPart(height, i)
  118. // If the part is missing (e.g. since it has been deleted after we
  119. // loaded the block meta) we consider the whole block to be missing.
  120. if part == nil {
  121. return nil
  122. }
  123. buf = append(buf, part.Bytes...)
  124. }
  125. err := proto.Unmarshal(buf, pbb)
  126. if err != nil {
  127. // NOTE: The existence of meta should imply the existence of the
  128. // block. So, make sure meta is only saved after blocks are saved.
  129. panic(fmt.Sprintf("Error reading block: %v", err))
  130. }
  131. block, err := types.BlockFromProto(pbb)
  132. if err != nil {
  133. panic(fmt.Errorf("error from proto block: %w", err))
  134. }
  135. return block
  136. }
  137. // LoadBlockByHash returns the block with the given hash.
  138. // If no block is found for that hash, it returns nil.
  139. // Panics if it fails to parse height associated with the given hash.
  140. func (bs *BlockStore) LoadBlockByHash(hash []byte) *types.Block {
  141. bz, err := bs.db.Get(blockHashKey(hash))
  142. if err != nil {
  143. panic(err)
  144. }
  145. if len(bz) == 0 {
  146. return nil
  147. }
  148. s := string(bz)
  149. height, err := strconv.ParseInt(s, 10, 64)
  150. if err != nil {
  151. panic(fmt.Sprintf("failed to extract height from %s: %v", s, err))
  152. }
  153. return bs.LoadBlock(height)
  154. }
  155. // LoadBlockPart returns the Part at the given index
  156. // from the block at the given height.
  157. // If no part is found for the given height and index, it returns nil.
  158. func (bs *BlockStore) LoadBlockPart(height int64, index int) *types.Part {
  159. var pbpart = new(tmproto.Part)
  160. bz, err := bs.db.Get(blockPartKey(height, index))
  161. if err != nil {
  162. panic(err)
  163. }
  164. if len(bz) == 0 {
  165. return nil
  166. }
  167. err = proto.Unmarshal(bz, pbpart)
  168. if err != nil {
  169. panic(fmt.Errorf("unmarshal to tmproto.Part failed: %w", err))
  170. }
  171. part, err := types.PartFromProto(pbpart)
  172. if err != nil {
  173. panic(fmt.Sprintf("Error reading block part: %v", err))
  174. }
  175. return part
  176. }
  177. // LoadBlockMeta returns the BlockMeta for the given height.
  178. // If no block is found for the given height, it returns nil.
  179. func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
  180. var pbbm = new(tmproto.BlockMeta)
  181. bz, err := bs.db.Get(blockMetaKey(height))
  182. if err != nil {
  183. panic(err)
  184. }
  185. if len(bz) == 0 {
  186. return nil
  187. }
  188. err = proto.Unmarshal(bz, pbbm)
  189. if err != nil {
  190. panic(fmt.Errorf("unmarshal to tmproto.BlockMeta: %w", err))
  191. }
  192. blockMeta, err := types.BlockMetaFromProto(pbbm)
  193. if err != nil {
  194. panic(fmt.Errorf("error from proto blockMeta: %w", err))
  195. }
  196. return blockMeta
  197. }
  198. // LoadBlockCommit returns the Commit for the given height.
  199. // This commit consists of the +2/3 and other Precommit-votes for block at `height`,
  200. // and it comes from the block.LastCommit for `height+1`.
  201. // If no commit is found for the given height, it returns nil.
  202. func (bs *BlockStore) LoadBlockCommit(height int64) *types.Commit {
  203. var pbc = new(tmproto.Commit)
  204. bz, err := bs.db.Get(blockCommitKey(height))
  205. if err != nil {
  206. panic(err)
  207. }
  208. if len(bz) == 0 {
  209. return nil
  210. }
  211. err = proto.Unmarshal(bz, pbc)
  212. if err != nil {
  213. panic(fmt.Errorf("error reading block commit: %w", err))
  214. }
  215. commit, err := types.CommitFromProto(pbc)
  216. if err != nil {
  217. panic(fmt.Sprintf("Error reading block commit: %v", err))
  218. }
  219. return commit
  220. }
  221. // LoadSeenCommit returns the locally seen Commit for the given height.
  222. // This is useful when we've seen a commit, but there has not yet been
  223. // a new block at `height + 1` that includes this commit in its block.LastCommit.
  224. func (bs *BlockStore) LoadSeenCommit(height int64) *types.Commit {
  225. var pbc = new(tmproto.Commit)
  226. bz, err := bs.db.Get(seenCommitKey(height))
  227. if err != nil {
  228. panic(err)
  229. }
  230. if len(bz) == 0 {
  231. return nil
  232. }
  233. err = proto.Unmarshal(bz, pbc)
  234. if err != nil {
  235. panic(fmt.Sprintf("error reading block seen commit: %v", err))
  236. }
  237. commit, err := types.CommitFromProto(pbc)
  238. if err != nil {
  239. panic(fmt.Errorf("error from proto commit: %w", err))
  240. }
  241. return commit
  242. }
  243. // PruneBlocks removes block up to (but not including) a height. It returns the number of blocks pruned.
  244. func (bs *BlockStore) PruneBlocks(height int64) (uint64, error) {
  245. if height <= 0 {
  246. return 0, fmt.Errorf("height must be greater than 0")
  247. }
  248. if height > bs.Height() {
  249. return 0, fmt.Errorf("height must be equal to or less than the latest height %d", bs.Height())
  250. }
  251. // when removing the block meta, use the hash to remove the hash key at the same time
  252. removeBlockHash := func(key, value []byte, batch dbm.Batch) error {
  253. // unmarshal block meta
  254. var pbbm = new(tmproto.BlockMeta)
  255. err := proto.Unmarshal(value, pbbm)
  256. if err != nil {
  257. return fmt.Errorf("unmarshal to tmproto.BlockMeta: %w", err)
  258. }
  259. blockMeta, err := types.BlockMetaFromProto(pbbm)
  260. if err != nil {
  261. return fmt.Errorf("error from proto blockMeta: %w", err)
  262. }
  263. // delete the hash key corresponding to the block meta's hash
  264. if err := batch.Delete(blockHashKey(blockMeta.BlockID.Hash)); err != nil {
  265. return fmt.Errorf("failed to delete hash key: %X: %w", blockHashKey(blockMeta.BlockID.Hash), err)
  266. }
  267. return nil
  268. }
  269. // remove block meta first as this is used to indicate whether the block exists.
  270. // For this reason, we also use ony block meta as a measure of the amount of blocks pruned
  271. pruned, err := bs.pruneRange(blockMetaKey(0), blockMetaKey(height), removeBlockHash)
  272. if err != nil {
  273. return pruned, err
  274. }
  275. if _, err := bs.pruneRange(blockPartKey(0, 0), blockPartKey(height, 0), nil); err != nil {
  276. return pruned, err
  277. }
  278. if _, err := bs.pruneRange(blockCommitKey(0), blockCommitKey(height), nil); err != nil {
  279. return pruned, err
  280. }
  281. if _, err := bs.pruneRange(seenCommitKey(0), seenCommitKey(height), nil); err != nil {
  282. return pruned, err
  283. }
  284. return pruned, nil
  285. }
  286. // pruneRange is a generic function for deleting a range of values based on the lowest
  287. // height up to but excluding retainHeight. For each key/value pair, an optional hook can be
  288. // executed before the deletion itself is made. pruneRange will use batch delete to delete
  289. // keys in batches of at most 1000 keys.
  290. func (bs *BlockStore) pruneRange(
  291. start []byte,
  292. end []byte,
  293. preDeletionHook func(key, value []byte, batch dbm.Batch) error,
  294. ) (uint64, error) {
  295. var (
  296. err error
  297. pruned uint64
  298. totalPruned uint64 = 0
  299. )
  300. batch := bs.db.NewBatch()
  301. defer batch.Close()
  302. pruned, start, err = bs.batchDelete(batch, start, end, preDeletionHook)
  303. if err != nil {
  304. return totalPruned, err
  305. }
  306. // loop until we have finished iterating over all the keys by writing, opening a new batch
  307. // and incrementing through the next range of keys.
  308. for !bytes.Equal(start, end) {
  309. if err := batch.Write(); err != nil {
  310. return totalPruned, err
  311. }
  312. totalPruned += pruned
  313. if err := batch.Close(); err != nil {
  314. return totalPruned, err
  315. }
  316. batch = bs.db.NewBatch()
  317. pruned, start, err = bs.batchDelete(batch, start, end, preDeletionHook)
  318. if err != nil {
  319. return totalPruned, err
  320. }
  321. }
  322. // once we looped over all keys we do a final flush to disk
  323. if err := batch.WriteSync(); err != nil {
  324. return totalPruned, err
  325. }
  326. totalPruned += pruned
  327. return totalPruned, nil
  328. }
  329. // batchDelete runs an iterator over a set of keys, first preforming a pre deletion hook before adding it to the batch.
  330. // The function ends when either 1000 keys have been added to the batch or the iterator has reached the end.
  331. func (bs *BlockStore) batchDelete(
  332. batch dbm.Batch,
  333. start, end []byte,
  334. preDeletionHook func(key, value []byte, batch dbm.Batch) error,
  335. ) (uint64, []byte, error) {
  336. var pruned uint64 = 0
  337. iter, err := bs.db.Iterator(start, end)
  338. if err != nil {
  339. return pruned, start, err
  340. }
  341. defer iter.Close()
  342. for ; iter.Valid(); iter.Next() {
  343. key := iter.Key()
  344. if preDeletionHook != nil {
  345. if err := preDeletionHook(key, iter.Value(), batch); err != nil {
  346. return 0, start, fmt.Errorf("pruning error at key %X: %w", iter.Key(), err)
  347. }
  348. }
  349. if err := batch.Delete(key); err != nil {
  350. return 0, start, fmt.Errorf("pruning error at key %X: %w", iter.Key(), err)
  351. }
  352. pruned++
  353. if pruned == 1000 {
  354. return pruned, iter.Key(), iter.Error()
  355. }
  356. }
  357. return pruned, end, iter.Error()
  358. }
  359. // SaveBlock persists the given block, blockParts, and seenCommit to the underlying db.
  360. // blockParts: Must be parts of the block
  361. // seenCommit: The +2/3 precommits that were seen which committed at height.
  362. // If all the nodes restart after committing a block,
  363. // we need this to reload the precommits to catch-up nodes to the
  364. // most recent height. Otherwise they'd stall at H-1.
  365. func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
  366. if block == nil {
  367. panic("BlockStore can only save a non-nil block")
  368. }
  369. batch := bs.db.NewBatch()
  370. height := block.Height
  371. hash := block.Hash()
  372. if g, w := height, bs.Height()+1; bs.Base() > 0 && g != w {
  373. panic(fmt.Sprintf("BlockStore can only save contiguous blocks. Wanted %v, got %v", w, g))
  374. }
  375. if !blockParts.IsComplete() {
  376. panic("BlockStore can only save complete block part sets")
  377. }
  378. // Save block parts. This must be done before the block meta, since callers
  379. // typically load the block meta first as an indication that the block exists
  380. // and then go on to load block parts - we must make sure the block is
  381. // complete as soon as the block meta is written.
  382. for i := 0; i < int(blockParts.Total()); i++ {
  383. part := blockParts.GetPart(i)
  384. bs.saveBlockPart(height, i, part, batch)
  385. }
  386. blockMeta := types.NewBlockMeta(block, blockParts)
  387. pbm := blockMeta.ToProto()
  388. if pbm == nil {
  389. panic("nil blockmeta")
  390. }
  391. metaBytes := mustEncode(pbm)
  392. if err := batch.Set(blockMetaKey(height), metaBytes); err != nil {
  393. panic(err)
  394. }
  395. if err := batch.Set(blockHashKey(hash), []byte(fmt.Sprintf("%d", height))); err != nil {
  396. panic(err)
  397. }
  398. pbc := block.LastCommit.ToProto()
  399. blockCommitBytes := mustEncode(pbc)
  400. if err := batch.Set(blockCommitKey(height-1), blockCommitBytes); err != nil {
  401. panic(err)
  402. }
  403. // Save seen commit (seen +2/3 precommits for block)
  404. // NOTE: we can delete this at a later height
  405. pbsc := seenCommit.ToProto()
  406. seenCommitBytes := mustEncode(pbsc)
  407. if err := batch.Set(seenCommitKey(height), seenCommitBytes); err != nil {
  408. panic(err)
  409. }
  410. if err := batch.WriteSync(); err != nil {
  411. panic(err)
  412. }
  413. if err := batch.Close(); err != nil {
  414. panic(err)
  415. }
  416. }
  417. func (bs *BlockStore) saveBlockPart(height int64, index int, part *types.Part, batch dbm.Batch) {
  418. pbp, err := part.ToProto()
  419. if err != nil {
  420. panic(fmt.Errorf("unable to make part into proto: %w", err))
  421. }
  422. partBytes := mustEncode(pbp)
  423. if err := batch.Set(blockPartKey(height, index), partBytes); err != nil {
  424. panic(err)
  425. }
  426. }
  427. // SaveSeenCommit saves a seen commit, used by e.g. the state sync reactor when bootstrapping node.
  428. func (bs *BlockStore) SaveSeenCommit(height int64, seenCommit *types.Commit) error {
  429. pbc := seenCommit.ToProto()
  430. seenCommitBytes, err := proto.Marshal(pbc)
  431. if err != nil {
  432. return fmt.Errorf("unable to marshal commit: %w", err)
  433. }
  434. return bs.db.Set(seenCommitKey(height), seenCommitBytes)
  435. }
  436. //---------------------------------- KEY ENCODING -----------------------------------------
  437. // key prefixes
  438. const (
  439. // prefixes are unique across all tm db's
  440. prefixBlockMeta = int64(0)
  441. prefixBlockPart = int64(1)
  442. prefixBlockCommit = int64(2)
  443. prefixSeenCommit = int64(3)
  444. prefixBlockHash = int64(4)
  445. )
  446. func blockMetaKey(height int64) []byte {
  447. key, err := orderedcode.Append(nil, prefixBlockMeta, height)
  448. if err != nil {
  449. panic(err)
  450. }
  451. return key
  452. }
  453. func decodeBlockMetaKey(key []byte) (height int64, err error) {
  454. var prefix int64
  455. remaining, err := orderedcode.Parse(string(key), &prefix, &height)
  456. if err != nil {
  457. return
  458. }
  459. if len(remaining) != 0 {
  460. return -1, fmt.Errorf("expected complete key but got remainder: %s", remaining)
  461. }
  462. if prefix != prefixBlockMeta {
  463. return -1, fmt.Errorf("incorrect prefix. Expected %v, got %v", prefixBlockMeta, prefix)
  464. }
  465. return
  466. }
  467. func blockPartKey(height int64, partIndex int) []byte {
  468. key, err := orderedcode.Append(nil, prefixBlockPart, height, int64(partIndex))
  469. if err != nil {
  470. panic(err)
  471. }
  472. return key
  473. }
  474. func blockCommitKey(height int64) []byte {
  475. key, err := orderedcode.Append(nil, prefixBlockCommit, height)
  476. if err != nil {
  477. panic(err)
  478. }
  479. return key
  480. }
  481. func seenCommitKey(height int64) []byte {
  482. key, err := orderedcode.Append(nil, prefixSeenCommit, height)
  483. if err != nil {
  484. panic(err)
  485. }
  486. return key
  487. }
  488. func blockHashKey(hash []byte) []byte {
  489. key, err := orderedcode.Append(nil, prefixBlockHash, string(hash))
  490. if err != nil {
  491. panic(err)
  492. }
  493. return key
  494. }
  495. //-----------------------------------------------------------------------------
  496. // mustEncode proto encodes a proto.message and panics if fails
  497. func mustEncode(pb proto.Message) []byte {
  498. bz, err := proto.Marshal(pb)
  499. if err != nil {
  500. panic(fmt.Errorf("unable to marshal: %w", err))
  501. }
  502. return bz
  503. }