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.

386 lines
11 KiB

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 blocksync
  2. import (
  3. "context"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/require"
  8. dbm "github.com/tendermint/tm-db"
  9. abciclient "github.com/tendermint/tendermint/abci/client"
  10. abci "github.com/tendermint/tendermint/abci/types"
  11. "github.com/tendermint/tendermint/config"
  12. "github.com/tendermint/tendermint/internal/consensus"
  13. "github.com/tendermint/tendermint/internal/mempool/mock"
  14. "github.com/tendermint/tendermint/internal/p2p"
  15. "github.com/tendermint/tendermint/internal/p2p/p2ptest"
  16. "github.com/tendermint/tendermint/internal/proxy"
  17. sm "github.com/tendermint/tendermint/internal/state"
  18. sf "github.com/tendermint/tendermint/internal/state/test/factory"
  19. "github.com/tendermint/tendermint/internal/store"
  20. "github.com/tendermint/tendermint/internal/test/factory"
  21. "github.com/tendermint/tendermint/libs/log"
  22. bcproto "github.com/tendermint/tendermint/proto/tendermint/blocksync"
  23. "github.com/tendermint/tendermint/types"
  24. )
  25. type reactorTestSuite struct {
  26. network *p2ptest.Network
  27. logger log.Logger
  28. nodes []types.NodeID
  29. reactors map[types.NodeID]*Reactor
  30. app map[types.NodeID]proxy.AppConns
  31. blockSyncChannels map[types.NodeID]*p2p.Channel
  32. peerChans map[types.NodeID]chan p2p.PeerUpdate
  33. peerUpdates map[types.NodeID]*p2p.PeerUpdates
  34. blockSync bool
  35. }
  36. func setup(
  37. ctx context.Context,
  38. t *testing.T,
  39. genDoc *types.GenesisDoc,
  40. privVal types.PrivValidator,
  41. maxBlockHeights []int64,
  42. chBuf uint,
  43. ) *reactorTestSuite {
  44. t.Helper()
  45. var cancel context.CancelFunc
  46. ctx, cancel = context.WithCancel(ctx)
  47. numNodes := len(maxBlockHeights)
  48. require.True(t, numNodes >= 1,
  49. "must specify at least one block height (nodes)")
  50. rts := &reactorTestSuite{
  51. logger: log.TestingLogger().With("module", "block_sync", "testCase", t.Name()),
  52. network: p2ptest.MakeNetwork(ctx, t, p2ptest.NetworkOptions{NumNodes: numNodes}),
  53. nodes: make([]types.NodeID, 0, numNodes),
  54. reactors: make(map[types.NodeID]*Reactor, numNodes),
  55. app: make(map[types.NodeID]proxy.AppConns, numNodes),
  56. blockSyncChannels: make(map[types.NodeID]*p2p.Channel, numNodes),
  57. peerChans: make(map[types.NodeID]chan p2p.PeerUpdate, numNodes),
  58. peerUpdates: make(map[types.NodeID]*p2p.PeerUpdates, numNodes),
  59. blockSync: true,
  60. }
  61. chDesc := &p2p.ChannelDescriptor{ID: BlockSyncChannel, MessageType: new(bcproto.Message)}
  62. rts.blockSyncChannels = rts.network.MakeChannelsNoCleanup(t, chDesc)
  63. i := 0
  64. for nodeID := range rts.network.Nodes {
  65. rts.addNode(ctx, t, nodeID, genDoc, privVal, maxBlockHeights[i])
  66. i++
  67. }
  68. t.Cleanup(func() {
  69. cancel()
  70. for _, nodeID := range rts.nodes {
  71. rts.peerUpdates[nodeID].Close()
  72. if rts.reactors[nodeID].IsRunning() {
  73. rts.reactors[nodeID].Wait()
  74. rts.app[nodeID].Wait()
  75. require.False(t, rts.reactors[nodeID].IsRunning())
  76. }
  77. }
  78. })
  79. return rts
  80. }
  81. func (rts *reactorTestSuite) addNode(
  82. ctx context.Context,
  83. t *testing.T,
  84. nodeID types.NodeID,
  85. genDoc *types.GenesisDoc,
  86. privVal types.PrivValidator,
  87. maxBlockHeight int64,
  88. ) {
  89. t.Helper()
  90. logger := log.TestingLogger()
  91. rts.nodes = append(rts.nodes, nodeID)
  92. rts.app[nodeID] = proxy.NewAppConns(abciclient.NewLocalCreator(&abci.BaseApplication{}), logger, proxy.NopMetrics())
  93. require.NoError(t, rts.app[nodeID].Start(ctx))
  94. blockDB := dbm.NewMemDB()
  95. stateDB := dbm.NewMemDB()
  96. stateStore := sm.NewStore(stateDB)
  97. blockStore := store.NewBlockStore(blockDB)
  98. state, err := sm.MakeGenesisState(genDoc)
  99. require.NoError(t, err)
  100. require.NoError(t, stateStore.Save(state))
  101. blockExec := sm.NewBlockExecutor(
  102. stateStore,
  103. log.TestingLogger(),
  104. rts.app[nodeID].Consensus(),
  105. mock.Mempool{},
  106. sm.EmptyEvidencePool{},
  107. blockStore,
  108. )
  109. for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
  110. lastCommit := types.NewCommit(blockHeight-1, 0, types.BlockID{}, nil)
  111. if blockHeight > 1 {
  112. lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1)
  113. lastBlock := blockStore.LoadBlock(blockHeight - 1)
  114. vote, err := factory.MakeVote(
  115. privVal,
  116. lastBlock.Header.ChainID, 0,
  117. lastBlock.Header.Height, 0, 2,
  118. lastBlockMeta.BlockID,
  119. time.Now(),
  120. )
  121. require.NoError(t, err)
  122. lastCommit = types.NewCommit(
  123. vote.Height,
  124. vote.Round,
  125. lastBlockMeta.BlockID,
  126. []types.CommitSig{vote.CommitSig()},
  127. )
  128. }
  129. thisBlock := sf.MakeBlock(state, blockHeight, lastCommit)
  130. thisParts := thisBlock.MakePartSet(types.BlockPartSizeBytes)
  131. blockID := types.BlockID{Hash: thisBlock.Hash(), PartSetHeader: thisParts.Header()}
  132. state, err = blockExec.ApplyBlock(state, blockID, thisBlock)
  133. require.NoError(t, err)
  134. blockStore.SaveBlock(thisBlock, thisParts, lastCommit)
  135. }
  136. rts.peerChans[nodeID] = make(chan p2p.PeerUpdate)
  137. rts.peerUpdates[nodeID] = p2p.NewPeerUpdates(rts.peerChans[nodeID], 1)
  138. rts.network.Nodes[nodeID].PeerManager.Register(rts.peerUpdates[nodeID])
  139. rts.reactors[nodeID], err = NewReactor(
  140. rts.logger.With("nodeID", nodeID),
  141. state.Copy(),
  142. blockExec,
  143. blockStore,
  144. nil,
  145. rts.blockSyncChannels[nodeID],
  146. rts.peerUpdates[nodeID],
  147. rts.blockSync,
  148. consensus.NopMetrics())
  149. require.NoError(t, err)
  150. require.NoError(t, rts.reactors[nodeID].Start(ctx))
  151. require.True(t, rts.reactors[nodeID].IsRunning())
  152. }
  153. func (rts *reactorTestSuite) start(t *testing.T) {
  154. t.Helper()
  155. rts.network.Start(t)
  156. require.Len(t,
  157. rts.network.RandomNode().PeerManager.Peers(),
  158. len(rts.nodes)-1,
  159. "network does not have expected number of nodes")
  160. }
  161. func TestReactor_AbruptDisconnect(t *testing.T) {
  162. ctx, cancel := context.WithCancel(context.Background())
  163. defer cancel()
  164. cfg, err := config.ResetTestRoot("block_sync_reactor_test")
  165. require.NoError(t, err)
  166. defer os.RemoveAll(cfg.RootDir)
  167. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  168. maxBlockHeight := int64(64)
  169. rts := setup(ctx, t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
  170. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  171. rts.start(t)
  172. secondaryPool := rts.reactors[rts.nodes[1]].pool
  173. require.Eventually(
  174. t,
  175. func() bool {
  176. height, _, _ := secondaryPool.GetStatus()
  177. return secondaryPool.MaxPeerHeight() > 0 && height > 0 && height < 10
  178. },
  179. 10*time.Second,
  180. 10*time.Millisecond,
  181. "expected node to be partially synced",
  182. )
  183. // Remove synced node from the syncing node which should not result in any
  184. // deadlocks or race conditions within the context of poolRoutine.
  185. rts.peerChans[rts.nodes[1]] <- p2p.PeerUpdate{
  186. Status: p2p.PeerStatusDown,
  187. NodeID: rts.nodes[0],
  188. }
  189. rts.network.Nodes[rts.nodes[1]].PeerManager.Disconnected(rts.nodes[0])
  190. }
  191. func TestReactor_SyncTime(t *testing.T) {
  192. ctx, cancel := context.WithCancel(context.Background())
  193. defer cancel()
  194. cfg, err := config.ResetTestRoot("block_sync_reactor_test")
  195. require.NoError(t, err)
  196. defer os.RemoveAll(cfg.RootDir)
  197. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  198. maxBlockHeight := int64(101)
  199. rts := setup(ctx, t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
  200. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  201. rts.start(t)
  202. require.Eventually(
  203. t,
  204. func() bool {
  205. return rts.reactors[rts.nodes[1]].GetRemainingSyncTime() > time.Nanosecond &&
  206. rts.reactors[rts.nodes[1]].pool.getLastSyncRate() > 0.001
  207. },
  208. 10*time.Second,
  209. 10*time.Millisecond,
  210. "expected node to be partially synced",
  211. )
  212. }
  213. func TestReactor_NoBlockResponse(t *testing.T) {
  214. ctx, cancel := context.WithCancel(context.Background())
  215. defer cancel()
  216. cfg, err := config.ResetTestRoot("block_sync_reactor_test")
  217. require.NoError(t, err)
  218. defer os.RemoveAll(cfg.RootDir)
  219. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  220. maxBlockHeight := int64(65)
  221. rts := setup(ctx, t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
  222. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  223. rts.start(t)
  224. testCases := []struct {
  225. height int64
  226. existent bool
  227. }{
  228. {maxBlockHeight + 2, false},
  229. {10, true},
  230. {1, true},
  231. {100, false},
  232. }
  233. secondaryPool := rts.reactors[rts.nodes[1]].pool
  234. require.Eventually(
  235. t,
  236. func() bool { return secondaryPool.MaxPeerHeight() > 0 && secondaryPool.IsCaughtUp() },
  237. 10*time.Second,
  238. 10*time.Millisecond,
  239. "expected node to be fully synced",
  240. )
  241. for _, tc := range testCases {
  242. block := rts.reactors[rts.nodes[1]].store.LoadBlock(tc.height)
  243. if tc.existent {
  244. require.True(t, block != nil)
  245. } else {
  246. require.Nil(t, block)
  247. }
  248. }
  249. }
  250. func TestReactor_BadBlockStopsPeer(t *testing.T) {
  251. // Ultimately, this should be refactored to be less integration test oriented
  252. // and more unit test oriented by simply testing channel sends and receives.
  253. // See: https://github.com/tendermint/tendermint/issues/6005
  254. t.SkipNow()
  255. ctx, cancel := context.WithCancel(context.Background())
  256. defer cancel()
  257. cfg, err := config.ResetTestRoot("block_sync_reactor_test")
  258. require.NoError(t, err)
  259. defer os.RemoveAll(cfg.RootDir)
  260. maxBlockHeight := int64(48)
  261. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  262. rts := setup(ctx, t, genDoc, privVals[0], []int64{maxBlockHeight, 0, 0, 0, 0}, 1000)
  263. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  264. rts.start(t)
  265. require.Eventually(
  266. t,
  267. func() bool {
  268. caughtUp := true
  269. for _, id := range rts.nodes[1 : len(rts.nodes)-1] {
  270. if rts.reactors[id].pool.MaxPeerHeight() == 0 || !rts.reactors[id].pool.IsCaughtUp() {
  271. caughtUp = false
  272. }
  273. }
  274. return caughtUp
  275. },
  276. 10*time.Minute,
  277. 10*time.Millisecond,
  278. "expected all nodes to be fully synced",
  279. )
  280. for _, id := range rts.nodes[:len(rts.nodes)-1] {
  281. require.Len(t, rts.reactors[id].pool.peers, 3)
  282. }
  283. // Mark testSuites[3] as an invalid peer which will cause newSuite to disconnect
  284. // from this peer.
  285. //
  286. // XXX: This causes a potential race condition.
  287. // See: https://github.com/tendermint/tendermint/issues/6005
  288. otherGenDoc, otherPrivVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  289. newNode := rts.network.MakeNode(ctx, t, p2ptest.NodeOptions{
  290. MaxPeers: uint16(len(rts.nodes) + 1),
  291. MaxConnected: uint16(len(rts.nodes) + 1),
  292. })
  293. rts.addNode(ctx, t, newNode.NodeID, otherGenDoc, otherPrivVals[0], maxBlockHeight)
  294. // add a fake peer just so we do not wait for the consensus ticker to timeout
  295. rts.reactors[newNode.NodeID].pool.SetPeerRange("00ff", 10, 10)
  296. // wait for the new peer to catch up and become fully synced
  297. require.Eventually(
  298. t,
  299. func() bool {
  300. return rts.reactors[newNode.NodeID].pool.MaxPeerHeight() > 0 && rts.reactors[newNode.NodeID].pool.IsCaughtUp()
  301. },
  302. 10*time.Minute,
  303. 10*time.Millisecond,
  304. "expected new node to be fully synced",
  305. )
  306. require.Eventuallyf(
  307. t,
  308. func() bool { return len(rts.reactors[newNode.NodeID].pool.peers) < len(rts.nodes)-1 },
  309. 10*time.Minute,
  310. 10*time.Millisecond,
  311. "invalid number of peers; expected < %d, got: %d",
  312. len(rts.nodes)-1,
  313. len(rts.reactors[newNode.NodeID].pool.peers),
  314. )
  315. }