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.

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