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.

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