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.

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