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.

358 lines
10 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
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 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
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
  1. package v0
  2. import (
  3. "os"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. dbm "github.com/tendermint/tm-db"
  8. abciclient "github.com/tendermint/tendermint/abci/client"
  9. abci "github.com/tendermint/tendermint/abci/types"
  10. "github.com/tendermint/tendermint/config"
  11. "github.com/tendermint/tendermint/internal/consensus"
  12. "github.com/tendermint/tendermint/internal/mempool/mock"
  13. "github.com/tendermint/tendermint/internal/p2p"
  14. "github.com/tendermint/tendermint/internal/p2p/p2ptest"
  15. "github.com/tendermint/tendermint/internal/proxy"
  16. sm "github.com/tendermint/tendermint/internal/state"
  17. sf "github.com/tendermint/tendermint/internal/state/test/factory"
  18. "github.com/tendermint/tendermint/internal/store"
  19. "github.com/tendermint/tendermint/internal/test/factory"
  20. "github.com/tendermint/tendermint/libs/log"
  21. bcproto "github.com/tendermint/tendermint/proto/tendermint/blocksync"
  22. "github.com/tendermint/tendermint/types"
  23. )
  24. type reactorTestSuite struct {
  25. network *p2ptest.Network
  26. logger log.Logger
  27. nodes []types.NodeID
  28. reactors map[types.NodeID]*Reactor
  29. app map[types.NodeID]proxy.AppConns
  30. blockSyncChannels map[types.NodeID]*p2p.Channel
  31. peerChans map[types.NodeID]chan p2p.PeerUpdate
  32. peerUpdates map[types.NodeID]*p2p.PeerUpdates
  33. blockSync bool
  34. }
  35. func setup(
  36. t *testing.T,
  37. genDoc *types.GenesisDoc,
  38. privVal types.PrivValidator,
  39. maxBlockHeights []int64,
  40. chBuf uint,
  41. ) *reactorTestSuite {
  42. t.Helper()
  43. numNodes := len(maxBlockHeights)
  44. require.True(t, numNodes >= 1,
  45. "must specify at least one block height (nodes)")
  46. rts := &reactorTestSuite{
  47. logger: log.TestingLogger().With("module", "block_sync", "testCase", t.Name()),
  48. network: p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: numNodes}),
  49. nodes: make([]types.NodeID, 0, numNodes),
  50. reactors: make(map[types.NodeID]*Reactor, numNodes),
  51. app: make(map[types.NodeID]proxy.AppConns, numNodes),
  52. blockSyncChannels: make(map[types.NodeID]*p2p.Channel, numNodes),
  53. peerChans: make(map[types.NodeID]chan p2p.PeerUpdate, numNodes),
  54. peerUpdates: make(map[types.NodeID]*p2p.PeerUpdates, numNodes),
  55. blockSync: true,
  56. }
  57. chDesc := p2p.ChannelDescriptor{ID: byte(BlockSyncChannel)}
  58. rts.blockSyncChannels = rts.network.MakeChannelsNoCleanup(t, chDesc, new(bcproto.Message), int(chBuf))
  59. i := 0
  60. for nodeID := range rts.network.Nodes {
  61. rts.addNode(t, nodeID, genDoc, privVal, maxBlockHeights[i])
  62. i++
  63. }
  64. t.Cleanup(func() {
  65. for _, nodeID := range rts.nodes {
  66. rts.peerUpdates[nodeID].Close()
  67. if rts.reactors[nodeID].IsRunning() {
  68. require.NoError(t, rts.reactors[nodeID].Stop())
  69. require.NoError(t, rts.app[nodeID].Stop())
  70. require.False(t, rts.reactors[nodeID].IsRunning())
  71. }
  72. }
  73. })
  74. return rts
  75. }
  76. func (rts *reactorTestSuite) addNode(t *testing.T,
  77. nodeID types.NodeID,
  78. genDoc *types.GenesisDoc,
  79. privVal types.PrivValidator,
  80. maxBlockHeight int64,
  81. ) {
  82. t.Helper()
  83. rts.nodes = append(rts.nodes, nodeID)
  84. rts.app[nodeID] = proxy.NewAppConns(abciclient.NewLocalCreator(&abci.BaseApplication{}))
  85. require.NoError(t, rts.app[nodeID].Start())
  86. blockDB := dbm.NewMemDB()
  87. stateDB := dbm.NewMemDB()
  88. stateStore := sm.NewStore(stateDB)
  89. blockStore := store.NewBlockStore(blockDB)
  90. state, err := sm.MakeGenesisState(genDoc)
  91. require.NoError(t, err)
  92. require.NoError(t, stateStore.Save(state))
  93. blockExec := sm.NewBlockExecutor(
  94. stateStore,
  95. log.TestingLogger(),
  96. rts.app[nodeID].Consensus(),
  97. mock.Mempool{},
  98. sm.EmptyEvidencePool{},
  99. blockStore,
  100. )
  101. for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
  102. lastCommit := types.NewCommit(blockHeight-1, 0, types.BlockID{}, nil)
  103. if blockHeight > 1 {
  104. lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1)
  105. lastBlock := blockStore.LoadBlock(blockHeight - 1)
  106. vote, err := factory.MakeVote(
  107. privVal,
  108. lastBlock.Header.ChainID, 0,
  109. lastBlock.Header.Height, 0, 2,
  110. lastBlockMeta.BlockID,
  111. time.Now(),
  112. )
  113. require.NoError(t, err)
  114. lastCommit = types.NewCommit(
  115. vote.Height,
  116. vote.Round,
  117. lastBlockMeta.BlockID,
  118. []types.CommitSig{vote.CommitSig()},
  119. )
  120. }
  121. thisBlock := sf.MakeBlock(state, blockHeight, lastCommit)
  122. thisParts := thisBlock.MakePartSet(types.BlockPartSizeBytes)
  123. blockID := types.BlockID{Hash: thisBlock.Hash(), PartSetHeader: thisParts.Header()}
  124. state, err = blockExec.ApplyBlock(state, blockID, thisBlock)
  125. require.NoError(t, err)
  126. blockStore.SaveBlock(thisBlock, thisParts, lastCommit)
  127. }
  128. rts.peerChans[nodeID] = make(chan p2p.PeerUpdate)
  129. rts.peerUpdates[nodeID] = p2p.NewPeerUpdates(rts.peerChans[nodeID], 1)
  130. rts.network.Nodes[nodeID].PeerManager.Register(rts.peerUpdates[nodeID])
  131. rts.reactors[nodeID], err = NewReactor(
  132. rts.logger.With("nodeID", nodeID),
  133. state.Copy(),
  134. blockExec,
  135. blockStore,
  136. nil,
  137. rts.blockSyncChannels[nodeID],
  138. rts.peerUpdates[nodeID],
  139. rts.blockSync,
  140. consensus.NopMetrics())
  141. require.NoError(t, err)
  142. require.NoError(t, rts.reactors[nodeID].Start())
  143. require.True(t, rts.reactors[nodeID].IsRunning())
  144. }
  145. func (rts *reactorTestSuite) start(t *testing.T) {
  146. t.Helper()
  147. rts.network.Start(t)
  148. require.Len(t,
  149. rts.network.RandomNode().PeerManager.Peers(),
  150. len(rts.nodes)-1,
  151. "network does not have expected number of nodes")
  152. }
  153. func TestReactor_AbruptDisconnect(t *testing.T) {
  154. cfg := config.ResetTestRoot("block_sync_reactor_test")
  155. defer os.RemoveAll(cfg.RootDir)
  156. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  157. maxBlockHeight := int64(64)
  158. rts := setup(t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
  159. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  160. rts.start(t)
  161. secondaryPool := rts.reactors[rts.nodes[1]].pool
  162. require.Eventually(
  163. t,
  164. func() bool {
  165. height, _, _ := secondaryPool.GetStatus()
  166. return secondaryPool.MaxPeerHeight() > 0 && height > 0 && height < 10
  167. },
  168. 10*time.Second,
  169. 10*time.Millisecond,
  170. "expected node to be partially synced",
  171. )
  172. // Remove synced node from the syncing node which should not result in any
  173. // deadlocks or race conditions within the context of poolRoutine.
  174. rts.peerChans[rts.nodes[1]] <- p2p.PeerUpdate{
  175. Status: p2p.PeerStatusDown,
  176. NodeID: rts.nodes[0],
  177. }
  178. rts.network.Nodes[rts.nodes[1]].PeerManager.Disconnected(rts.nodes[0])
  179. }
  180. func TestReactor_SyncTime(t *testing.T) {
  181. cfg := config.ResetTestRoot("block_sync_reactor_test")
  182. defer os.RemoveAll(cfg.RootDir)
  183. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  184. maxBlockHeight := int64(101)
  185. rts := setup(t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
  186. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  187. rts.start(t)
  188. require.Eventually(
  189. t,
  190. func() bool {
  191. return rts.reactors[rts.nodes[1]].GetRemainingSyncTime() > time.Nanosecond &&
  192. rts.reactors[rts.nodes[1]].pool.getLastSyncRate() > 0.001
  193. },
  194. 10*time.Second,
  195. 10*time.Millisecond,
  196. "expected node to be partially synced",
  197. )
  198. }
  199. func TestReactor_NoBlockResponse(t *testing.T) {
  200. cfg := config.ResetTestRoot("block_sync_reactor_test")
  201. defer os.RemoveAll(cfg.RootDir)
  202. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  203. maxBlockHeight := int64(65)
  204. rts := setup(t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
  205. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  206. rts.start(t)
  207. testCases := []struct {
  208. height int64
  209. existent bool
  210. }{
  211. {maxBlockHeight + 2, false},
  212. {10, true},
  213. {1, true},
  214. {100, false},
  215. }
  216. secondaryPool := rts.reactors[rts.nodes[1]].pool
  217. require.Eventually(
  218. t,
  219. func() bool { return secondaryPool.MaxPeerHeight() > 0 && secondaryPool.IsCaughtUp() },
  220. 10*time.Second,
  221. 10*time.Millisecond,
  222. "expected node to be fully synced",
  223. )
  224. for _, tc := range testCases {
  225. block := rts.reactors[rts.nodes[1]].store.LoadBlock(tc.height)
  226. if tc.existent {
  227. require.True(t, block != nil)
  228. } else {
  229. require.Nil(t, block)
  230. }
  231. }
  232. }
  233. func TestReactor_BadBlockStopsPeer(t *testing.T) {
  234. // Ultimately, this should be refactored to be less integration test oriented
  235. // and more unit test oriented by simply testing channel sends and receives.
  236. // See: https://github.com/tendermint/tendermint/issues/6005
  237. t.SkipNow()
  238. cfg := config.ResetTestRoot("block_sync_reactor_test")
  239. defer os.RemoveAll(cfg.RootDir)
  240. maxBlockHeight := int64(48)
  241. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  242. rts := setup(t, genDoc, privVals[0], []int64{maxBlockHeight, 0, 0, 0, 0}, 1000)
  243. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  244. rts.start(t)
  245. require.Eventually(
  246. t,
  247. func() bool {
  248. caughtUp := true
  249. for _, id := range rts.nodes[1 : len(rts.nodes)-1] {
  250. if rts.reactors[id].pool.MaxPeerHeight() == 0 || !rts.reactors[id].pool.IsCaughtUp() {
  251. caughtUp = false
  252. }
  253. }
  254. return caughtUp
  255. },
  256. 10*time.Minute,
  257. 10*time.Millisecond,
  258. "expected all nodes to be fully synced",
  259. )
  260. for _, id := range rts.nodes[:len(rts.nodes)-1] {
  261. require.Len(t, rts.reactors[id].pool.peers, 3)
  262. }
  263. // Mark testSuites[3] as an invalid peer which will cause newSuite to disconnect
  264. // from this peer.
  265. //
  266. // XXX: This causes a potential race condition.
  267. // See: https://github.com/tendermint/tendermint/issues/6005
  268. otherGenDoc, otherPrivVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  269. newNode := rts.network.MakeNode(t, p2ptest.NodeOptions{
  270. MaxPeers: uint16(len(rts.nodes) + 1),
  271. MaxConnected: uint16(len(rts.nodes) + 1),
  272. })
  273. rts.addNode(t, newNode.NodeID, otherGenDoc, otherPrivVals[0], maxBlockHeight)
  274. // add a fake peer just so we do not wait for the consensus ticker to timeout
  275. rts.reactors[newNode.NodeID].pool.SetPeerRange("00ff", 10, 10)
  276. // wait for the new peer to catch up and become fully synced
  277. require.Eventually(
  278. t,
  279. func() bool {
  280. return rts.reactors[newNode.NodeID].pool.MaxPeerHeight() > 0 && rts.reactors[newNode.NodeID].pool.IsCaughtUp()
  281. },
  282. 10*time.Minute,
  283. 10*time.Millisecond,
  284. "expected new node to be fully synced",
  285. )
  286. require.Eventuallyf(
  287. t,
  288. func() bool { return len(rts.reactors[newNode.NodeID].pool.peers) < len(rts.nodes)-1 },
  289. 10*time.Minute,
  290. 10*time.Millisecond,
  291. "invalid number of peers; expected < %d, got: %d",
  292. len(rts.nodes)-1,
  293. len(rts.reactors[newNode.NodeID].pool.peers),
  294. )
  295. }