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.

230 lines
5.0 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
6 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
8 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
8 years ago
10 years ago
8 years ago
10 years ago
10 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
10 years ago
10 years ago
10 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
  1. package v0
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/libs/log"
  9. tmrand "github.com/tendermint/tendermint/libs/rand"
  10. "github.com/tendermint/tendermint/p2p"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. func init() {
  14. peerTimeout = 2 * time.Second
  15. }
  16. type testPeer struct {
  17. id p2p.ID
  18. base int64
  19. height int64
  20. inputChan chan inputData //make sure each peer's data is sequential
  21. }
  22. type inputData struct {
  23. t *testing.T
  24. pool *BlockPool
  25. request BlockRequest
  26. }
  27. func (p testPeer) runInputRoutine() {
  28. go func() {
  29. for input := range p.inputChan {
  30. p.simulateInput(input)
  31. }
  32. }()
  33. }
  34. // Request desired, pretend like we got the block immediately.
  35. func (p testPeer) simulateInput(input inputData) {
  36. block := &types.Block{Header: types.Header{Height: input.request.Height}}
  37. input.pool.AddBlock(input.request.PeerID, block, 123)
  38. // TODO: uncommenting this creates a race which is detected by:
  39. // https://github.com/golang/go/blob/2bd767b1022dd3254bcec469f0ee164024726486/src/testing/testing.go#L854-L856
  40. // see: https://github.com/tendermint/tendermint/issues/3390#issue-418379890
  41. // input.t.Logf("Added block from peer %v (height: %v)", input.request.PeerID, input.request.Height)
  42. }
  43. type testPeers map[p2p.ID]testPeer
  44. func (ps testPeers) start() {
  45. for _, v := range ps {
  46. v.runInputRoutine()
  47. }
  48. }
  49. func (ps testPeers) stop() {
  50. for _, v := range ps {
  51. close(v.inputChan)
  52. }
  53. }
  54. func makePeers(numPeers int, minHeight, maxHeight int64) testPeers {
  55. peers := make(testPeers, numPeers)
  56. for i := 0; i < numPeers; i++ {
  57. peerID := p2p.ID(tmrand.Str(12))
  58. height := minHeight + tmrand.Int63n(maxHeight-minHeight)
  59. base := minHeight + int64(i)
  60. if base > height {
  61. base = height
  62. }
  63. peers[peerID] = testPeer{peerID, base, height, make(chan inputData, 10)}
  64. }
  65. return peers
  66. }
  67. func TestBlockPoolBasic(t *testing.T) {
  68. start := int64(42)
  69. peers := makePeers(10, start+1, 1000)
  70. errorsCh := make(chan peerError, 1000)
  71. requestsCh := make(chan BlockRequest, 1000)
  72. pool := NewBlockPool(start, requestsCh, errorsCh)
  73. pool.SetLogger(log.TestingLogger())
  74. err := pool.Start()
  75. if err != nil {
  76. t.Error(err)
  77. }
  78. defer pool.Stop()
  79. peers.start()
  80. defer peers.stop()
  81. // Introduce each peer.
  82. go func() {
  83. for _, peer := range peers {
  84. pool.SetPeerRange(peer.id, peer.base, peer.height)
  85. }
  86. }()
  87. // Start a goroutine to pull blocks
  88. go func() {
  89. for {
  90. if !pool.IsRunning() {
  91. return
  92. }
  93. first, second := pool.PeekTwoBlocks()
  94. if first != nil && second != nil {
  95. pool.PopRequest()
  96. } else {
  97. time.Sleep(1 * time.Second)
  98. }
  99. }
  100. }()
  101. // Pull from channels
  102. for {
  103. select {
  104. case err := <-errorsCh:
  105. t.Error(err)
  106. case request := <-requestsCh:
  107. t.Logf("Pulled new BlockRequest %v", request)
  108. if request.Height == 300 {
  109. return // Done!
  110. }
  111. peers[request.PeerID].inputChan <- inputData{t, pool, request}
  112. }
  113. }
  114. }
  115. func TestBlockPoolTimeout(t *testing.T) {
  116. start := int64(42)
  117. peers := makePeers(10, start+1, 1000)
  118. errorsCh := make(chan peerError, 1000)
  119. requestsCh := make(chan BlockRequest, 1000)
  120. pool := NewBlockPool(start, requestsCh, errorsCh)
  121. pool.SetLogger(log.TestingLogger())
  122. err := pool.Start()
  123. if err != nil {
  124. t.Error(err)
  125. }
  126. defer pool.Stop()
  127. for _, peer := range peers {
  128. t.Logf("Peer %v", peer.id)
  129. }
  130. // Introduce each peer.
  131. go func() {
  132. for _, peer := range peers {
  133. pool.SetPeerRange(peer.id, peer.base, peer.height)
  134. }
  135. }()
  136. // Start a goroutine to pull blocks
  137. go func() {
  138. for {
  139. if !pool.IsRunning() {
  140. return
  141. }
  142. first, second := pool.PeekTwoBlocks()
  143. if first != nil && second != nil {
  144. pool.PopRequest()
  145. } else {
  146. time.Sleep(1 * time.Second)
  147. }
  148. }
  149. }()
  150. // Pull from channels
  151. counter := 0
  152. timedOut := map[p2p.ID]struct{}{}
  153. for {
  154. select {
  155. case err := <-errorsCh:
  156. t.Log(err)
  157. // consider error to be always timeout here
  158. if _, ok := timedOut[err.peerID]; !ok {
  159. counter++
  160. if counter == len(peers) {
  161. return // Done!
  162. }
  163. }
  164. case request := <-requestsCh:
  165. t.Logf("Pulled new BlockRequest %+v", request)
  166. }
  167. }
  168. }
  169. func TestBlockPoolRemovePeer(t *testing.T) {
  170. peers := make(testPeers, 10)
  171. for i := 0; i < 10; i++ {
  172. peerID := p2p.ID(fmt.Sprintf("%d", i+1))
  173. height := int64(i + 1)
  174. peers[peerID] = testPeer{peerID, 0, height, make(chan inputData)}
  175. }
  176. requestsCh := make(chan BlockRequest)
  177. errorsCh := make(chan peerError)
  178. pool := NewBlockPool(1, requestsCh, errorsCh)
  179. pool.SetLogger(log.TestingLogger())
  180. err := pool.Start()
  181. require.NoError(t, err)
  182. defer pool.Stop()
  183. // add peers
  184. for peerID, peer := range peers {
  185. pool.SetPeerRange(peerID, peer.base, peer.height)
  186. }
  187. assert.EqualValues(t, 10, pool.MaxPeerHeight())
  188. // remove not-existing peer
  189. assert.NotPanics(t, func() { pool.RemovePeer(p2p.ID("Superman")) })
  190. // remove peer with biggest height
  191. pool.RemovePeer(p2p.ID("10"))
  192. assert.EqualValues(t, 9, pool.MaxPeerHeight())
  193. // remove all peers
  194. for peerID := range peers {
  195. pool.RemovePeer(peerID)
  196. }
  197. assert.EqualValues(t, 0, pool.MaxPeerHeight())
  198. }