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.

942 lines
29 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
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
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
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
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
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
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
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
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
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
  1. package v1
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. cmn "github.com/tendermint/tendermint/libs/common"
  8. "github.com/tendermint/tendermint/libs/log"
  9. "github.com/tendermint/tendermint/p2p"
  10. "github.com/tendermint/tendermint/types"
  11. )
  12. type lastBlockRequestT struct {
  13. peerID p2p.ID
  14. height int64
  15. }
  16. type lastPeerErrorT struct {
  17. peerID p2p.ID
  18. err error
  19. }
  20. // reactor for FSM testing
  21. type testReactor struct {
  22. logger log.Logger
  23. fsm *BcReactorFSM
  24. numStatusRequests int
  25. numBlockRequests int
  26. lastBlockRequest lastBlockRequestT
  27. lastPeerError lastPeerErrorT
  28. stateTimerStarts map[string]int
  29. }
  30. func sendEventToFSM(fsm *BcReactorFSM, ev bReactorEvent, data bReactorEventData) error {
  31. return fsm.Handle(&bcReactorMessage{event: ev, data: data})
  32. }
  33. type fsmStepTestValues struct {
  34. currentState string
  35. event bReactorEvent
  36. data bReactorEventData
  37. wantErr error
  38. wantState string
  39. wantStatusReqSent bool
  40. wantReqIncreased bool
  41. wantNewBlocks []int64
  42. wantRemovedPeers []p2p.ID
  43. }
  44. // ---------------------------------------------------------------------------
  45. // helper test function for different FSM events, state and expected behavior
  46. func sStopFSMEv(current, expected string) fsmStepTestValues {
  47. return fsmStepTestValues{
  48. currentState: current,
  49. event: stopFSMEv,
  50. wantState: expected,
  51. wantErr: errNoErrorFinished}
  52. }
  53. func sUnknownFSMEv(current string) fsmStepTestValues {
  54. return fsmStepTestValues{
  55. currentState: current,
  56. event: 1234,
  57. wantState: current,
  58. wantErr: errInvalidEvent}
  59. }
  60. func sStartFSMEv() fsmStepTestValues {
  61. return fsmStepTestValues{
  62. currentState: "unknown",
  63. event: startFSMEv,
  64. wantState: "waitForPeer",
  65. wantStatusReqSent: true}
  66. }
  67. func sStateTimeoutEv(current, expected string, timedoutState string, wantErr error) fsmStepTestValues {
  68. return fsmStepTestValues{
  69. currentState: current,
  70. event: stateTimeoutEv,
  71. data: bReactorEventData{
  72. stateName: timedoutState,
  73. },
  74. wantState: expected,
  75. wantErr: wantErr,
  76. }
  77. }
  78. func sProcessedBlockEv(current, expected string, reactorError error) fsmStepTestValues {
  79. return fsmStepTestValues{
  80. currentState: current,
  81. event: processedBlockEv,
  82. data: bReactorEventData{
  83. err: reactorError,
  84. },
  85. wantState: expected,
  86. wantErr: reactorError,
  87. }
  88. }
  89. func sStatusEv(current, expected string, peerID p2p.ID, height int64, err error) fsmStepTestValues {
  90. return fsmStepTestValues{
  91. currentState: current,
  92. event: statusResponseEv,
  93. data: bReactorEventData{peerID: peerID, height: height},
  94. wantState: expected,
  95. wantErr: err}
  96. }
  97. func sMakeRequestsEv(current, expected string, maxPendingRequests int) fsmStepTestValues {
  98. return fsmStepTestValues{
  99. currentState: current,
  100. event: makeRequestsEv,
  101. data: bReactorEventData{maxNumRequests: maxPendingRequests},
  102. wantState: expected,
  103. wantReqIncreased: true,
  104. }
  105. }
  106. func sMakeRequestsEvErrored(current, expected string,
  107. maxPendingRequests int, err error, peersRemoved []p2p.ID) fsmStepTestValues {
  108. return fsmStepTestValues{
  109. currentState: current,
  110. event: makeRequestsEv,
  111. data: bReactorEventData{maxNumRequests: maxPendingRequests},
  112. wantState: expected,
  113. wantErr: err,
  114. wantRemovedPeers: peersRemoved,
  115. wantReqIncreased: true,
  116. }
  117. }
  118. func sBlockRespEv(current, expected string, peerID p2p.ID, height int64, prevBlocks []int64) fsmStepTestValues {
  119. txs := []types.Tx{types.Tx("foo"), types.Tx("bar")}
  120. return fsmStepTestValues{
  121. currentState: current,
  122. event: blockResponseEv,
  123. data: bReactorEventData{
  124. peerID: peerID,
  125. height: height,
  126. block: types.MakeBlock(height, txs, nil, nil),
  127. length: 100},
  128. wantState: expected,
  129. wantNewBlocks: append(prevBlocks, height),
  130. }
  131. }
  132. func sBlockRespEvErrored(current, expected string,
  133. peerID p2p.ID, height int64, prevBlocks []int64, wantErr error, peersRemoved []p2p.ID) fsmStepTestValues {
  134. txs := []types.Tx{types.Tx("foo"), types.Tx("bar")}
  135. return fsmStepTestValues{
  136. currentState: current,
  137. event: blockResponseEv,
  138. data: bReactorEventData{
  139. peerID: peerID,
  140. height: height,
  141. block: types.MakeBlock(height, txs, nil, nil),
  142. length: 100},
  143. wantState: expected,
  144. wantErr: wantErr,
  145. wantRemovedPeers: peersRemoved,
  146. wantNewBlocks: prevBlocks,
  147. }
  148. }
  149. func sPeerRemoveEv(current, expected string, peerID p2p.ID, err error, peersRemoved []p2p.ID) fsmStepTestValues {
  150. return fsmStepTestValues{
  151. currentState: current,
  152. event: peerRemoveEv,
  153. data: bReactorEventData{
  154. peerID: peerID,
  155. err: err,
  156. },
  157. wantState: expected,
  158. wantRemovedPeers: peersRemoved,
  159. }
  160. }
  161. // --------------------------------------------
  162. func newTestReactor(height int64) *testReactor {
  163. testBcR := &testReactor{logger: log.TestingLogger(), stateTimerStarts: make(map[string]int)}
  164. testBcR.fsm = NewFSM(height, testBcR)
  165. testBcR.fsm.SetLogger(testBcR.logger)
  166. return testBcR
  167. }
  168. func fixBlockResponseEvStep(step *fsmStepTestValues, testBcR *testReactor) {
  169. // There is currently no good way to know to which peer a block request was sent.
  170. // So in some cases where it does not matter, before we simulate a block response
  171. // we cheat and look where it is expected from.
  172. if step.event == blockResponseEv {
  173. height := step.data.height
  174. peerID, ok := testBcR.fsm.pool.blocks[height]
  175. if ok {
  176. step.data.peerID = peerID
  177. }
  178. }
  179. }
  180. type testFields struct {
  181. name string
  182. startingHeight int64
  183. maxRequestsPerPeer int
  184. maxPendingRequests int
  185. steps []fsmStepTestValues
  186. }
  187. func executeFSMTests(t *testing.T, tests []testFields, matchRespToReq bool) {
  188. for _, tt := range tests {
  189. tt := tt
  190. t.Run(tt.name, func(t *testing.T) {
  191. // Create test reactor
  192. testBcR := newTestReactor(tt.startingHeight)
  193. if tt.maxRequestsPerPeer != 0 {
  194. maxRequestsPerPeer = tt.maxRequestsPerPeer
  195. }
  196. for _, step := range tt.steps {
  197. step := step
  198. assert.Equal(t, step.currentState, testBcR.fsm.state.name)
  199. var heightBefore int64
  200. if step.event == processedBlockEv && step.data.err == errBlockVerificationFailure {
  201. heightBefore = testBcR.fsm.pool.Height
  202. }
  203. oldNumStatusRequests := testBcR.numStatusRequests
  204. oldNumBlockRequests := testBcR.numBlockRequests
  205. if matchRespToReq {
  206. fixBlockResponseEvStep(&step, testBcR)
  207. }
  208. fsmErr := sendEventToFSM(testBcR.fsm, step.event, step.data)
  209. assert.Equal(t, step.wantErr, fsmErr)
  210. if step.wantStatusReqSent {
  211. assert.Equal(t, oldNumStatusRequests+1, testBcR.numStatusRequests)
  212. } else {
  213. assert.Equal(t, oldNumStatusRequests, testBcR.numStatusRequests)
  214. }
  215. if step.wantReqIncreased {
  216. assert.True(t, oldNumBlockRequests < testBcR.numBlockRequests)
  217. } else {
  218. assert.Equal(t, oldNumBlockRequests, testBcR.numBlockRequests)
  219. }
  220. for _, height := range step.wantNewBlocks {
  221. _, err := testBcR.fsm.pool.BlockAndPeerAtHeight(height)
  222. assert.Nil(t, err)
  223. }
  224. if step.event == processedBlockEv && step.data.err == errBlockVerificationFailure {
  225. heightAfter := testBcR.fsm.pool.Height
  226. assert.Equal(t, heightBefore, heightAfter)
  227. firstAfter, err1 := testBcR.fsm.pool.BlockAndPeerAtHeight(testBcR.fsm.pool.Height)
  228. secondAfter, err2 := testBcR.fsm.pool.BlockAndPeerAtHeight(testBcR.fsm.pool.Height + 1)
  229. assert.NotNil(t, err1)
  230. assert.NotNil(t, err2)
  231. assert.Nil(t, firstAfter)
  232. assert.Nil(t, secondAfter)
  233. }
  234. assert.Equal(t, step.wantState, testBcR.fsm.state.name)
  235. if step.wantState == "finished" {
  236. assert.True(t, testBcR.fsm.isCaughtUp())
  237. }
  238. }
  239. })
  240. }
  241. }
  242. func TestFSMBasic(t *testing.T) {
  243. tests := []testFields{
  244. {
  245. name: "one block, one peer - TS2",
  246. startingHeight: 1,
  247. maxRequestsPerPeer: 2,
  248. steps: []fsmStepTestValues{
  249. sStartFSMEv(),
  250. sStatusEv("waitForPeer", "waitForBlock", "P1", 2, nil),
  251. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  252. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 1, []int64{}),
  253. sBlockRespEv("waitForBlock", "waitForBlock", "P2", 2, []int64{1}),
  254. sProcessedBlockEv("waitForBlock", "finished", nil),
  255. },
  256. },
  257. {
  258. name: "multi block, multi peer - TS2",
  259. startingHeight: 1,
  260. maxRequestsPerPeer: 2,
  261. steps: []fsmStepTestValues{
  262. sStartFSMEv(),
  263. sStatusEv("waitForPeer", "waitForBlock", "P1", 4, nil),
  264. sStatusEv("waitForBlock", "waitForBlock", "P2", 4, nil),
  265. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  266. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 1, []int64{}),
  267. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 2, []int64{1}),
  268. sBlockRespEv("waitForBlock", "waitForBlock", "P2", 3, []int64{1, 2}),
  269. sBlockRespEv("waitForBlock", "waitForBlock", "P2", 4, []int64{1, 2, 3}),
  270. sProcessedBlockEv("waitForBlock", "waitForBlock", nil),
  271. sProcessedBlockEv("waitForBlock", "waitForBlock", nil),
  272. sProcessedBlockEv("waitForBlock", "finished", nil),
  273. },
  274. },
  275. }
  276. executeFSMTests(t, tests, true)
  277. }
  278. func TestFSMBlockVerificationFailure(t *testing.T) {
  279. tests := []testFields{
  280. {
  281. name: "block verification failure - TS2 variant",
  282. startingHeight: 1,
  283. maxRequestsPerPeer: 3,
  284. steps: []fsmStepTestValues{
  285. sStartFSMEv(),
  286. // add P1 and get blocks 1-3 from it
  287. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  288. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  289. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 1, []int64{}),
  290. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 2, []int64{1}),
  291. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 3, []int64{1, 2}),
  292. // add P2
  293. sStatusEv("waitForBlock", "waitForBlock", "P2", 3, nil),
  294. // process block failure, should remove P1 and all blocks
  295. sProcessedBlockEv("waitForBlock", "waitForBlock", errBlockVerificationFailure),
  296. // get blocks 1-3 from P2
  297. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  298. sBlockRespEv("waitForBlock", "waitForBlock", "P2", 1, []int64{}),
  299. sBlockRespEv("waitForBlock", "waitForBlock", "P2", 2, []int64{1}),
  300. sBlockRespEv("waitForBlock", "waitForBlock", "P2", 3, []int64{1, 2}),
  301. // finish after processing blocks 1 and 2
  302. sProcessedBlockEv("waitForBlock", "waitForBlock", nil),
  303. sProcessedBlockEv("waitForBlock", "finished", nil),
  304. },
  305. },
  306. }
  307. executeFSMTests(t, tests, false)
  308. }
  309. func TestFSMBadBlockFromPeer(t *testing.T) {
  310. tests := []testFields{
  311. {
  312. name: "block we haven't asked for",
  313. startingHeight: 1,
  314. maxRequestsPerPeer: 3,
  315. steps: []fsmStepTestValues{
  316. sStartFSMEv(),
  317. // add P1 and ask for blocks 1-3
  318. sStatusEv("waitForPeer", "waitForBlock", "P1", 300, nil),
  319. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  320. // blockResponseEv for height 100 should cause an error
  321. sBlockRespEvErrored("waitForBlock", "waitForPeer",
  322. "P1", 100, []int64{}, errMissingBlock, []p2p.ID{}),
  323. },
  324. },
  325. {
  326. name: "block we already have",
  327. startingHeight: 1,
  328. maxRequestsPerPeer: 3,
  329. steps: []fsmStepTestValues{
  330. sStartFSMEv(),
  331. // add P1 and get block 1
  332. sStatusEv("waitForPeer", "waitForBlock", "P1", 100, nil),
  333. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  334. sBlockRespEv("waitForBlock", "waitForBlock",
  335. "P1", 1, []int64{}),
  336. // Get block 1 again. Since peer is removed together with block 1,
  337. // the blocks present in the pool should be {}
  338. sBlockRespEvErrored("waitForBlock", "waitForPeer",
  339. "P1", 1, []int64{}, errDuplicateBlock, []p2p.ID{"P1"}),
  340. },
  341. },
  342. {
  343. name: "block from unknown peer",
  344. startingHeight: 1,
  345. maxRequestsPerPeer: 3,
  346. steps: []fsmStepTestValues{
  347. sStartFSMEv(),
  348. // add P1 and get block 1
  349. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  350. // get block 1 from unknown peer P2
  351. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  352. sBlockRespEvErrored("waitForBlock", "waitForBlock",
  353. "P2", 1, []int64{}, errBadDataFromPeer, []p2p.ID{"P2"}),
  354. },
  355. },
  356. {
  357. name: "block from wrong peer",
  358. startingHeight: 1,
  359. maxRequestsPerPeer: 3,
  360. steps: []fsmStepTestValues{
  361. sStartFSMEv(),
  362. // add P1, make requests for blocks 1-3 to P1
  363. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  364. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  365. // add P2
  366. sStatusEv("waitForBlock", "waitForBlock", "P2", 3, nil),
  367. // receive block 1 from P2
  368. sBlockRespEvErrored("waitForBlock", "waitForBlock",
  369. "P2", 1, []int64{}, errBadDataFromPeer, []p2p.ID{"P2"}),
  370. },
  371. },
  372. }
  373. executeFSMTests(t, tests, false)
  374. }
  375. func TestFSMBlockAtCurrentHeightDoesNotArriveInTime(t *testing.T) {
  376. tests := []testFields{
  377. {
  378. name: "block at current height undelivered - TS5",
  379. startingHeight: 1,
  380. maxRequestsPerPeer: 3,
  381. steps: []fsmStepTestValues{
  382. sStartFSMEv(),
  383. // add P1, get blocks 1 and 2, process block 1
  384. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  385. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  386. sBlockRespEv("waitForBlock", "waitForBlock",
  387. "P1", 1, []int64{}),
  388. sBlockRespEv("waitForBlock", "waitForBlock",
  389. "P1", 2, []int64{1}),
  390. sProcessedBlockEv("waitForBlock", "waitForBlock", nil),
  391. // add P2
  392. sStatusEv("waitForBlock", "waitForBlock", "P2", 3, nil),
  393. // timeout on block 3, P1 should be removed
  394. sStateTimeoutEv("waitForBlock", "waitForBlock", "waitForBlock", errNoPeerResponseForCurrentHeights),
  395. // make requests and finish by receiving blocks 2 and 3 from P2
  396. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  397. sBlockRespEv("waitForBlock", "waitForBlock", "P2", 2, []int64{}),
  398. sBlockRespEv("waitForBlock", "waitForBlock", "P2", 3, []int64{2}),
  399. sProcessedBlockEv("waitForBlock", "finished", nil),
  400. },
  401. },
  402. {
  403. name: "block at current height undelivered, at maxPeerHeight after peer removal - TS3",
  404. startingHeight: 1,
  405. maxRequestsPerPeer: 3,
  406. steps: []fsmStepTestValues{
  407. sStartFSMEv(),
  408. // add P1, request blocks 1-3 from P1
  409. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  410. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  411. // add P2 (tallest)
  412. sStatusEv("waitForBlock", "waitForBlock", "P2", 30, nil),
  413. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  414. // receive blocks 1-3 from P1
  415. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 1, []int64{}),
  416. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 2, []int64{1}),
  417. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 3, []int64{1, 2}),
  418. // process blocks at heights 1 and 2
  419. sProcessedBlockEv("waitForBlock", "waitForBlock", nil),
  420. sProcessedBlockEv("waitForBlock", "waitForBlock", nil),
  421. // timeout on block at height 4
  422. sStateTimeoutEv("waitForBlock", "finished", "waitForBlock", nil),
  423. },
  424. },
  425. }
  426. executeFSMTests(t, tests, true)
  427. }
  428. func TestFSMPeerRelatedEvents(t *testing.T) {
  429. tests := []testFields{
  430. {
  431. name: "peer remove event with no blocks",
  432. startingHeight: 1,
  433. steps: []fsmStepTestValues{
  434. sStartFSMEv(),
  435. // add P1, P2, P3
  436. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  437. sStatusEv("waitForBlock", "waitForBlock", "P2", 3, nil),
  438. sStatusEv("waitForBlock", "waitForBlock", "P3", 3, nil),
  439. // switch removes P2
  440. sPeerRemoveEv("waitForBlock", "waitForBlock", "P2", errSwitchRemovesPeer, []p2p.ID{"P2"}),
  441. },
  442. },
  443. {
  444. name: "only peer removed while in waitForBlock state",
  445. startingHeight: 100,
  446. steps: []fsmStepTestValues{
  447. sStartFSMEv(),
  448. // add P1
  449. sStatusEv("waitForPeer", "waitForBlock", "P1", 200, nil),
  450. // switch removes P1
  451. sPeerRemoveEv("waitForBlock", "waitForPeer", "P1", errSwitchRemovesPeer, []p2p.ID{"P1"}),
  452. },
  453. },
  454. {
  455. name: "highest peer removed while in waitForBlock state, node reaches maxPeerHeight - TS4 ",
  456. startingHeight: 100,
  457. maxRequestsPerPeer: 3,
  458. steps: []fsmStepTestValues{
  459. sStartFSMEv(),
  460. // add P1 and make requests
  461. sStatusEv("waitForPeer", "waitForBlock", "P1", 101, nil),
  462. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  463. // add P2
  464. sStatusEv("waitForBlock", "waitForBlock", "P2", 200, nil),
  465. // get blocks 100 and 101 from P1 and process block at height 100
  466. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 100, []int64{}),
  467. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 101, []int64{100}),
  468. sProcessedBlockEv("waitForBlock", "waitForBlock", nil),
  469. // switch removes peer P1, should be finished
  470. sPeerRemoveEv("waitForBlock", "finished", "P2", errSwitchRemovesPeer, []p2p.ID{"P2"}),
  471. },
  472. },
  473. {
  474. name: "highest peer lowers its height in waitForBlock state, node reaches maxPeerHeight - TS4",
  475. startingHeight: 100,
  476. maxRequestsPerPeer: 3,
  477. steps: []fsmStepTestValues{
  478. sStartFSMEv(),
  479. // add P1 and make requests
  480. sStatusEv("waitForPeer", "waitForBlock", "P1", 101, nil),
  481. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  482. // add P2
  483. sStatusEv("waitForBlock", "waitForBlock", "P2", 200, nil),
  484. // get blocks 100 and 101 from P1
  485. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 100, []int64{}),
  486. sBlockRespEv("waitForBlock", "waitForBlock", "P1", 101, []int64{100}),
  487. // processed block at heights 100
  488. sProcessedBlockEv("waitForBlock", "waitForBlock", nil),
  489. // P2 becomes short
  490. sStatusEv("waitForBlock", "finished", "P2", 100, errPeerLowersItsHeight),
  491. },
  492. },
  493. {
  494. name: "new short peer while in waitForPeer state",
  495. startingHeight: 100,
  496. steps: []fsmStepTestValues{
  497. sStartFSMEv(),
  498. sStatusEv("waitForPeer", "waitForPeer", "P1", 3, errPeerTooShort),
  499. },
  500. },
  501. {
  502. name: "new short peer while in waitForBlock state",
  503. startingHeight: 100,
  504. steps: []fsmStepTestValues{
  505. sStartFSMEv(),
  506. sStatusEv("waitForPeer", "waitForBlock", "P1", 200, nil),
  507. sStatusEv("waitForBlock", "waitForBlock", "P2", 3, errPeerTooShort),
  508. },
  509. },
  510. {
  511. name: "only peer updated with low height while in waitForBlock state",
  512. startingHeight: 100,
  513. steps: []fsmStepTestValues{
  514. sStartFSMEv(),
  515. sStatusEv("waitForPeer", "waitForBlock", "P1", 200, nil),
  516. sStatusEv("waitForBlock", "waitForPeer", "P1", 3, errPeerLowersItsHeight),
  517. },
  518. },
  519. {
  520. name: "peer does not exist in the switch",
  521. startingHeight: 9999999,
  522. maxRequestsPerPeer: 3,
  523. steps: []fsmStepTestValues{
  524. sStartFSMEv(),
  525. // add P1
  526. sStatusEv("waitForPeer", "waitForBlock", "P1", 20000000, nil),
  527. // send request for block 9999999
  528. // Note: For this block request the "switch missing the peer" error is simulated,
  529. // see implementation of bcReactor interface, sendBlockRequest(), in this file.
  530. sMakeRequestsEvErrored("waitForBlock", "waitForBlock",
  531. maxNumRequests, nil, []p2p.ID{"P1"}),
  532. },
  533. },
  534. }
  535. executeFSMTests(t, tests, true)
  536. }
  537. func TestFSMStopFSM(t *testing.T) {
  538. tests := []testFields{
  539. {
  540. name: "stopFSMEv in unknown",
  541. steps: []fsmStepTestValues{
  542. sStopFSMEv("unknown", "finished"),
  543. },
  544. },
  545. {
  546. name: "stopFSMEv in waitForPeer",
  547. startingHeight: 1,
  548. steps: []fsmStepTestValues{
  549. sStartFSMEv(),
  550. sStopFSMEv("waitForPeer", "finished"),
  551. },
  552. },
  553. {
  554. name: "stopFSMEv in waitForBlock",
  555. startingHeight: 1,
  556. steps: []fsmStepTestValues{
  557. sStartFSMEv(),
  558. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  559. sStopFSMEv("waitForBlock", "finished"),
  560. },
  561. },
  562. }
  563. executeFSMTests(t, tests, false)
  564. }
  565. func TestFSMUnknownElements(t *testing.T) {
  566. tests := []testFields{
  567. {
  568. name: "unknown event for state unknown",
  569. steps: []fsmStepTestValues{
  570. sUnknownFSMEv("unknown"),
  571. },
  572. },
  573. {
  574. name: "unknown event for state waitForPeer",
  575. steps: []fsmStepTestValues{
  576. sStartFSMEv(),
  577. sUnknownFSMEv("waitForPeer"),
  578. },
  579. },
  580. {
  581. name: "unknown event for state waitForBlock",
  582. startingHeight: 1,
  583. steps: []fsmStepTestValues{
  584. sStartFSMEv(),
  585. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  586. sUnknownFSMEv("waitForBlock"),
  587. },
  588. },
  589. }
  590. executeFSMTests(t, tests, false)
  591. }
  592. func TestFSMPeerStateTimeoutEvent(t *testing.T) {
  593. tests := []testFields{
  594. {
  595. name: "timeout event for state waitForPeer while in state waitForPeer - TS1",
  596. startingHeight: 1,
  597. maxRequestsPerPeer: 3,
  598. steps: []fsmStepTestValues{
  599. sStartFSMEv(),
  600. sStateTimeoutEv("waitForPeer", "finished", "waitForPeer", errNoTallerPeer),
  601. },
  602. },
  603. {
  604. name: "timeout event for state waitForPeer while in a state != waitForPeer",
  605. startingHeight: 1,
  606. maxRequestsPerPeer: 3,
  607. steps: []fsmStepTestValues{
  608. sStartFSMEv(),
  609. sStateTimeoutEv("waitForPeer", "waitForPeer", "waitForBlock", errTimeoutEventWrongState),
  610. },
  611. },
  612. {
  613. name: "timeout event for state waitForBlock while in state waitForBlock ",
  614. startingHeight: 1,
  615. maxRequestsPerPeer: 3,
  616. steps: []fsmStepTestValues{
  617. sStartFSMEv(),
  618. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  619. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  620. sStateTimeoutEv("waitForBlock", "waitForPeer", "waitForBlock", errNoPeerResponseForCurrentHeights),
  621. },
  622. },
  623. {
  624. name: "timeout event for state waitForBlock while in a state != waitForBlock",
  625. startingHeight: 1,
  626. maxRequestsPerPeer: 3,
  627. steps: []fsmStepTestValues{
  628. sStartFSMEv(),
  629. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  630. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  631. sStateTimeoutEv("waitForBlock", "waitForBlock", "waitForPeer", errTimeoutEventWrongState),
  632. },
  633. },
  634. {
  635. name: "timeout event for state waitForBlock with multiple peers",
  636. startingHeight: 1,
  637. maxRequestsPerPeer: 3,
  638. steps: []fsmStepTestValues{
  639. sStartFSMEv(),
  640. sStatusEv("waitForPeer", "waitForBlock", "P1", 3, nil),
  641. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  642. sStatusEv("waitForBlock", "waitForBlock", "P2", 3, nil),
  643. sStateTimeoutEv("waitForBlock", "waitForBlock", "waitForBlock", errNoPeerResponseForCurrentHeights),
  644. },
  645. },
  646. }
  647. executeFSMTests(t, tests, false)
  648. }
  649. func makeCorrectTransitionSequence(startingHeight int64, numBlocks int64, numPeers int, randomPeerHeights bool,
  650. maxRequestsPerPeer int, maxPendingRequests int) testFields {
  651. // Generate numPeers peers with random or numBlocks heights according to the randomPeerHeights flag.
  652. peerHeights := make([]int64, numPeers)
  653. for i := 0; i < numPeers; i++ {
  654. if i == 0 {
  655. peerHeights[0] = numBlocks
  656. continue
  657. }
  658. if randomPeerHeights {
  659. peerHeights[i] = int64(cmn.MaxInt(cmn.RandIntn(int(numBlocks)), int(startingHeight)+1))
  660. } else {
  661. peerHeights[i] = numBlocks
  662. }
  663. }
  664. // Approximate the slice capacity to save time for appends.
  665. testSteps := make([]fsmStepTestValues, 0, 3*numBlocks+int64(numPeers))
  666. testName := fmt.Sprintf("%v-blocks %v-startingHeight %v-peers %v-maxRequestsPerPeer %v-maxNumRequests",
  667. numBlocks, startingHeight, numPeers, maxRequestsPerPeer, maxPendingRequests)
  668. // Add startFSMEv step.
  669. testSteps = append(testSteps, sStartFSMEv())
  670. // For each peer, add statusResponseEv step.
  671. for i := 0; i < numPeers; i++ {
  672. peerName := fmt.Sprintf("P%d", i)
  673. if i == 0 {
  674. testSteps = append(
  675. testSteps,
  676. sStatusEv("waitForPeer", "waitForBlock", p2p.ID(peerName), peerHeights[i], nil))
  677. } else {
  678. testSteps = append(testSteps,
  679. sStatusEv("waitForBlock", "waitForBlock", p2p.ID(peerName), peerHeights[i], nil))
  680. }
  681. }
  682. height := startingHeight
  683. numBlocksReceived := 0
  684. prevBlocks := make([]int64, 0, maxPendingRequests)
  685. forLoop:
  686. for i := 0; i < int(numBlocks); i++ {
  687. // Add the makeRequestEv step periodically.
  688. if i%maxRequestsPerPeer == 0 {
  689. testSteps = append(
  690. testSteps,
  691. sMakeRequestsEv("waitForBlock", "waitForBlock", maxNumRequests),
  692. )
  693. }
  694. // Add the blockRespEv step
  695. testSteps = append(
  696. testSteps,
  697. sBlockRespEv("waitForBlock", "waitForBlock",
  698. "P0", height, prevBlocks))
  699. prevBlocks = append(prevBlocks, height)
  700. height++
  701. numBlocksReceived++
  702. // Add the processedBlockEv step periodically.
  703. if numBlocksReceived >= maxRequestsPerPeer || height >= numBlocks {
  704. for j := int(height) - numBlocksReceived; j < int(height); j++ {
  705. if j >= int(numBlocks) {
  706. // This is the last block that is processed, we should be in "finished" state.
  707. testSteps = append(
  708. testSteps,
  709. sProcessedBlockEv("waitForBlock", "finished", nil))
  710. break forLoop
  711. }
  712. testSteps = append(
  713. testSteps,
  714. sProcessedBlockEv("waitForBlock", "waitForBlock", nil))
  715. }
  716. numBlocksReceived = 0
  717. prevBlocks = make([]int64, 0, maxPendingRequests)
  718. }
  719. }
  720. return testFields{
  721. name: testName,
  722. startingHeight: startingHeight,
  723. maxRequestsPerPeer: maxRequestsPerPeer,
  724. maxPendingRequests: maxPendingRequests,
  725. steps: testSteps,
  726. }
  727. }
  728. const (
  729. maxStartingHeightTest = 100
  730. maxRequestsPerPeerTest = 20
  731. maxTotalPendingRequestsTest = 600
  732. maxNumPeersTest = 1000
  733. maxNumBlocksInChainTest = 10000 //should be smaller than 9999999
  734. )
  735. func makeCorrectTransitionSequenceWithRandomParameters() testFields {
  736. // Generate a starting height for fast sync.
  737. startingHeight := int64(cmn.RandIntn(maxStartingHeightTest) + 1)
  738. // Generate the number of requests per peer.
  739. maxRequestsPerPeer := cmn.RandIntn(maxRequestsPerPeerTest) + 1
  740. // Generate the maximum number of total pending requests, >= maxRequestsPerPeer.
  741. maxPendingRequests := cmn.RandIntn(maxTotalPendingRequestsTest-maxRequestsPerPeer) + maxRequestsPerPeer
  742. // Generate the number of blocks to be synced.
  743. numBlocks := int64(cmn.RandIntn(maxNumBlocksInChainTest)) + startingHeight
  744. // Generate a number of peers.
  745. numPeers := cmn.RandIntn(maxNumPeersTest) + 1
  746. return makeCorrectTransitionSequence(startingHeight, numBlocks, numPeers, true, maxRequestsPerPeer, maxPendingRequests)
  747. }
  748. func shouldApplyProcessedBlockEvStep(step *fsmStepTestValues, testBcR *testReactor) bool {
  749. if step.event == processedBlockEv {
  750. _, err := testBcR.fsm.pool.BlockAndPeerAtHeight(testBcR.fsm.pool.Height)
  751. if err == errMissingBlock {
  752. return false
  753. }
  754. _, err = testBcR.fsm.pool.BlockAndPeerAtHeight(testBcR.fsm.pool.Height + 1)
  755. if err == errMissingBlock {
  756. return false
  757. }
  758. }
  759. return true
  760. }
  761. func TestFSMCorrectTransitionSequences(t *testing.T) {
  762. tests := []testFields{
  763. makeCorrectTransitionSequence(1, 100, 10, true, 10, 40),
  764. makeCorrectTransitionSequenceWithRandomParameters(),
  765. }
  766. for _, tt := range tests {
  767. tt := tt
  768. t.Run(tt.name, func(t *testing.T) {
  769. // Create test reactor
  770. testBcR := newTestReactor(tt.startingHeight)
  771. if tt.maxRequestsPerPeer != 0 {
  772. maxRequestsPerPeer = tt.maxRequestsPerPeer
  773. }
  774. for _, step := range tt.steps {
  775. step := step
  776. assert.Equal(t, step.currentState, testBcR.fsm.state.name)
  777. oldNumStatusRequests := testBcR.numStatusRequests
  778. fixBlockResponseEvStep(&step, testBcR)
  779. if !shouldApplyProcessedBlockEvStep(&step, testBcR) {
  780. continue
  781. }
  782. fsmErr := sendEventToFSM(testBcR.fsm, step.event, step.data)
  783. assert.Equal(t, step.wantErr, fsmErr)
  784. if step.wantStatusReqSent {
  785. assert.Equal(t, oldNumStatusRequests+1, testBcR.numStatusRequests)
  786. } else {
  787. assert.Equal(t, oldNumStatusRequests, testBcR.numStatusRequests)
  788. }
  789. assert.Equal(t, step.wantState, testBcR.fsm.state.name)
  790. if step.wantState == "finished" {
  791. assert.True(t, testBcR.fsm.isCaughtUp())
  792. }
  793. }
  794. })
  795. }
  796. }
  797. // ----------------------------------------
  798. // implements the bcRNotifier
  799. func (testR *testReactor) sendPeerError(err error, peerID p2p.ID) {
  800. testR.logger.Info("Reactor received sendPeerError call from FSM", "peer", peerID, "err", err)
  801. testR.lastPeerError.peerID = peerID
  802. testR.lastPeerError.err = err
  803. }
  804. func (testR *testReactor) sendStatusRequest() {
  805. testR.logger.Info("Reactor received sendStatusRequest call from FSM")
  806. testR.numStatusRequests++
  807. }
  808. func (testR *testReactor) sendBlockRequest(peerID p2p.ID, height int64) error {
  809. testR.logger.Info("Reactor received sendBlockRequest call from FSM", "peer", peerID, "height", height)
  810. testR.numBlockRequests++
  811. testR.lastBlockRequest.peerID = peerID
  812. testR.lastBlockRequest.height = height
  813. if height == 9999999 {
  814. // simulate switch does not have peer
  815. return errNilPeerForBlockRequest
  816. }
  817. return nil
  818. }
  819. func (testR *testReactor) resetStateTimer(name string, timer **time.Timer, timeout time.Duration) {
  820. testR.logger.Info("Reactor received resetStateTimer call from FSM", "state", name, "timeout", timeout)
  821. if _, ok := testR.stateTimerStarts[name]; !ok {
  822. testR.stateTimerStarts[name] = 1
  823. } else {
  824. testR.stateTimerStarts[name]++
  825. }
  826. }
  827. func (testR *testReactor) switchToConsensus() {
  828. }
  829. // ----------------------------------------