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.

363 lines
10 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
internal/proxy: add initial set of abci metrics (#7115) This PR adds an initial set of metrics for use ABCI. The initial metrics enable the calculation of timing histograms and call counts for each of the ABCI methods. The metrics are also labeled as either 'sync' or 'async' to determine if the method call was performed using ABCI's `*Async` methods. An example of these metrics is included here for reference: ``` tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.0001"} 0 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.0004"} 5 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.002"} 12 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.009"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.02"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.1"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="0.65"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="2"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="6"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="25"} 13 tendermint_abci_connection_method_timing_bucket{chain_id="ci",method="commit",type="sync",le="+Inf"} 13 tendermint_abci_connection_method_timing_sum{chain_id="ci",method="commit",type="sync"} 0.007802058000000001 tendermint_abci_connection_method_timing_count{chain_id="ci",method="commit",type="sync"} 13 ``` These metrics can easily be graphed using prometheus's `histogram_quantile(...)` method to pick out a particular quantile to graph or examine. I chose buckets that were somewhat of an estimate of expected range of times for ABCI operations. They start at .0001 seconds and range to 25 seconds. The hope is that this range captures enough possible times to be useful for us and operators.
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
6 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. "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: BlockSyncChannel, MessageType: new(bcproto.Message)}
  58. rts.blockSyncChannels = rts.network.MakeChannelsNoCleanup(t, chDesc)
  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{}), proxy.NopMetrics())
  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, err := config.ResetTestRoot("block_sync_reactor_test")
  155. require.NoError(t, err)
  156. defer os.RemoveAll(cfg.RootDir)
  157. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  158. maxBlockHeight := int64(64)
  159. rts := setup(t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
  160. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  161. rts.start(t)
  162. secondaryPool := rts.reactors[rts.nodes[1]].pool
  163. require.Eventually(
  164. t,
  165. func() bool {
  166. height, _, _ := secondaryPool.GetStatus()
  167. return secondaryPool.MaxPeerHeight() > 0 && height > 0 && height < 10
  168. },
  169. 10*time.Second,
  170. 10*time.Millisecond,
  171. "expected node to be partially synced",
  172. )
  173. // Remove synced node from the syncing node which should not result in any
  174. // deadlocks or race conditions within the context of poolRoutine.
  175. rts.peerChans[rts.nodes[1]] <- p2p.PeerUpdate{
  176. Status: p2p.PeerStatusDown,
  177. NodeID: rts.nodes[0],
  178. }
  179. rts.network.Nodes[rts.nodes[1]].PeerManager.Disconnected(rts.nodes[0])
  180. }
  181. func TestReactor_SyncTime(t *testing.T) {
  182. cfg, err := config.ResetTestRoot("block_sync_reactor_test")
  183. require.NoError(t, err)
  184. defer os.RemoveAll(cfg.RootDir)
  185. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  186. maxBlockHeight := int64(101)
  187. rts := setup(t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
  188. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  189. rts.start(t)
  190. require.Eventually(
  191. t,
  192. func() bool {
  193. return rts.reactors[rts.nodes[1]].GetRemainingSyncTime() > time.Nanosecond &&
  194. rts.reactors[rts.nodes[1]].pool.getLastSyncRate() > 0.001
  195. },
  196. 10*time.Second,
  197. 10*time.Millisecond,
  198. "expected node to be partially synced",
  199. )
  200. }
  201. func TestReactor_NoBlockResponse(t *testing.T) {
  202. cfg, err := config.ResetTestRoot("block_sync_reactor_test")
  203. require.NoError(t, err)
  204. defer os.RemoveAll(cfg.RootDir)
  205. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  206. maxBlockHeight := int64(65)
  207. rts := setup(t, genDoc, privVals[0], []int64{maxBlockHeight, 0}, 0)
  208. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  209. rts.start(t)
  210. testCases := []struct {
  211. height int64
  212. existent bool
  213. }{
  214. {maxBlockHeight + 2, false},
  215. {10, true},
  216. {1, true},
  217. {100, false},
  218. }
  219. secondaryPool := rts.reactors[rts.nodes[1]].pool
  220. require.Eventually(
  221. t,
  222. func() bool { return secondaryPool.MaxPeerHeight() > 0 && secondaryPool.IsCaughtUp() },
  223. 10*time.Second,
  224. 10*time.Millisecond,
  225. "expected node to be fully synced",
  226. )
  227. for _, tc := range testCases {
  228. block := rts.reactors[rts.nodes[1]].store.LoadBlock(tc.height)
  229. if tc.existent {
  230. require.True(t, block != nil)
  231. } else {
  232. require.Nil(t, block)
  233. }
  234. }
  235. }
  236. func TestReactor_BadBlockStopsPeer(t *testing.T) {
  237. // Ultimately, this should be refactored to be less integration test oriented
  238. // and more unit test oriented by simply testing channel sends and receives.
  239. // See: https://github.com/tendermint/tendermint/issues/6005
  240. t.SkipNow()
  241. cfg, err := config.ResetTestRoot("block_sync_reactor_test")
  242. require.NoError(t, err)
  243. defer os.RemoveAll(cfg.RootDir)
  244. maxBlockHeight := int64(48)
  245. genDoc, privVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  246. rts := setup(t, genDoc, privVals[0], []int64{maxBlockHeight, 0, 0, 0, 0}, 1000)
  247. require.Equal(t, maxBlockHeight, rts.reactors[rts.nodes[0]].store.Height())
  248. rts.start(t)
  249. require.Eventually(
  250. t,
  251. func() bool {
  252. caughtUp := true
  253. for _, id := range rts.nodes[1 : len(rts.nodes)-1] {
  254. if rts.reactors[id].pool.MaxPeerHeight() == 0 || !rts.reactors[id].pool.IsCaughtUp() {
  255. caughtUp = false
  256. }
  257. }
  258. return caughtUp
  259. },
  260. 10*time.Minute,
  261. 10*time.Millisecond,
  262. "expected all nodes to be fully synced",
  263. )
  264. for _, id := range rts.nodes[:len(rts.nodes)-1] {
  265. require.Len(t, rts.reactors[id].pool.peers, 3)
  266. }
  267. // Mark testSuites[3] as an invalid peer which will cause newSuite to disconnect
  268. // from this peer.
  269. //
  270. // XXX: This causes a potential race condition.
  271. // See: https://github.com/tendermint/tendermint/issues/6005
  272. otherGenDoc, otherPrivVals := factory.RandGenesisDoc(cfg, 1, false, 30)
  273. newNode := rts.network.MakeNode(t, p2ptest.NodeOptions{
  274. MaxPeers: uint16(len(rts.nodes) + 1),
  275. MaxConnected: uint16(len(rts.nodes) + 1),
  276. })
  277. rts.addNode(t, newNode.NodeID, otherGenDoc, otherPrivVals[0], maxBlockHeight)
  278. // add a fake peer just so we do not wait for the consensus ticker to timeout
  279. rts.reactors[newNode.NodeID].pool.SetPeerRange("00ff", 10, 10)
  280. // wait for the new peer to catch up and become fully synced
  281. require.Eventually(
  282. t,
  283. func() bool {
  284. return rts.reactors[newNode.NodeID].pool.MaxPeerHeight() > 0 && rts.reactors[newNode.NodeID].pool.IsCaughtUp()
  285. },
  286. 10*time.Minute,
  287. 10*time.Millisecond,
  288. "expected new node to be fully synced",
  289. )
  290. require.Eventuallyf(
  291. t,
  292. func() bool { return len(rts.reactors[newNode.NodeID].pool.peers) < len(rts.nodes)-1 },
  293. 10*time.Minute,
  294. 10*time.Millisecond,
  295. "invalid number of peers; expected < %d, got: %d",
  296. len(rts.nodes)-1,
  297. len(rts.reactors[newNode.NodeID].pool.peers),
  298. )
  299. }