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.

328 lines
8.9 KiB

blockchain: Reorg reactor (#3561) * go routines in blockchain reactor * Added reference to the go routine diagram * Initial commit * cleanup * Undo testing_logger change, committed by mistake * Fix the test loggers * pulled some fsm code into pool.go * added pool tests * changes to the design added block requests under peer moved the request trigger in the reactor poolRoutine, triggered now by a ticker in general moved everything required for making block requests smarter in the poolRoutine added a simple map of heights to keep track of what will need to be requested next added a few more tests * send errors to FSM in a different channel than blocks send errors (RemovePeer) from switch on a different channel than the one receiving blocks renamed channels added more pool tests * more pool tests * lint errors * more tests * more tests * switch fast sync to new implementation * fixed data race in tests * cleanup * finished fsm tests * address golangci comments :) * address golangci comments :) * Added timeout on next block needed to advance * updating docs and cleanup * fix issue in test from previous cleanup * cleanup * Added termination scenarios, tests and more cleanup * small fixes to adr, comments and cleanup * Fix bug in sendRequest() If we tried to send a request to a peer not present in the switch, a missing continue statement caused the request to be blackholed in a peer that was removed and never retried. While this bug was manifesting, the reactor kept asking for other blocks that would be stored and never consumed. Added the number of unconsumed blocks in the math for requesting blocks ahead of current processing height so eventually there will be no more blocks requested until the already received ones are consumed. * remove bpPeer's didTimeout field * Use distinct err codes for peer timeout and FSM timeouts * Don't allow peers to update with lower height * review comments from Ethan and Zarko * some cleanup, renaming, comments * Move block execution in separate goroutine * Remove pool's numPending * review comments * fix lint, remove old blockchain reactor and duplicates in fsm tests * small reorg around peer after review comments * add the reactor spec * verify block only once * review comments * change to int for max number of pending requests * cleanup and godoc * Add configuration flag fast sync version * golangci fixes * fix config template * move both reactor versions under blockchain * cleanup, golint, renaming stuff * updated documentation, fixed more golint warnings * integrate with behavior package * sync with master * gofmt * add changelog_pending entry * move to improvments * suggestion to changelog entry
5 years ago
blockchain: Reorg reactor (#3561) * go routines in blockchain reactor * Added reference to the go routine diagram * Initial commit * cleanup * Undo testing_logger change, committed by mistake * Fix the test loggers * pulled some fsm code into pool.go * added pool tests * changes to the design added block requests under peer moved the request trigger in the reactor poolRoutine, triggered now by a ticker in general moved everything required for making block requests smarter in the poolRoutine added a simple map of heights to keep track of what will need to be requested next added a few more tests * send errors to FSM in a different channel than blocks send errors (RemovePeer) from switch on a different channel than the one receiving blocks renamed channels added more pool tests * more pool tests * lint errors * more tests * more tests * switch fast sync to new implementation * fixed data race in tests * cleanup * finished fsm tests * address golangci comments :) * address golangci comments :) * Added timeout on next block needed to advance * updating docs and cleanup * fix issue in test from previous cleanup * cleanup * Added termination scenarios, tests and more cleanup * small fixes to adr, comments and cleanup * Fix bug in sendRequest() If we tried to send a request to a peer not present in the switch, a missing continue statement caused the request to be blackholed in a peer that was removed and never retried. While this bug was manifesting, the reactor kept asking for other blocks that would be stored and never consumed. Added the number of unconsumed blocks in the math for requesting blocks ahead of current processing height so eventually there will be no more blocks requested until the already received ones are consumed. * remove bpPeer's didTimeout field * Use distinct err codes for peer timeout and FSM timeouts * Don't allow peers to update with lower height * review comments from Ethan and Zarko * some cleanup, renaming, comments * Move block execution in separate goroutine * Remove pool's numPending * review comments * fix lint, remove old blockchain reactor and duplicates in fsm tests * small reorg around peer after review comments * add the reactor spec * verify block only once * review comments * change to int for max number of pending requests * cleanup and godoc * Add configuration flag fast sync version * golangci fixes * fix config template * move both reactor versions under blockchain * cleanup, golint, renaming stuff * updated documentation, fixed more golint warnings * integrate with behavior package * sync with master * gofmt * add changelog_pending entry * move to improvments * suggestion to changelog entry
5 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
test/blockchain/v0: mitigate test data race (#4886) Mitigates the below data race. The proper fix involves not fiddling with reactor internals, which needs a rewrite of the test and possible additional reactor infrastructure. ``` ================== WARNING: DATA RACE Write at 0x00c001118e78 by goroutine 187: github.com/tendermint/tendermint/blockchain/v0.TestBadBlockStopsPeer() /go/src/github.com/tendermint/tendermint/blockchain/v0/reactor_test.go:234 +0x9d7 testing.tRunner() /usr/local/go/src/testing/testing.go:992 +0x1eb Previous read at 0x00c001118e78 by goroutine 326: [failed to restore the stack] Goroutine 187 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:1043 +0x660 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1285 +0xa6 testing.tRunner() /usr/local/go/src/testing/testing.go:992 +0x1eb testing.runTests() /usr/local/go/src/testing/testing.go:1283 +0x527 testing.(*M).Run() /usr/local/go/src/testing/testing.go:1200 +0x2ff main.main() _testmain.go:112 +0x337 Goroutine 326 (running) created at: github.com/tendermint/tendermint/blockchain/v0.(*BlockchainReactor).OnStart() /go/src/github.com/tendermint/tendermint/blockchain/v0/reactor.go:118 +0x12c github.com/tendermint/tendermint/libs/service.(*BaseService).Start() /go/src/github.com/tendermint/tendermint/libs/service/service.go:140 +0x504 github.com/tendermint/tendermint/blockchain/v0.(*BlockchainReactor).Start() <autogenerated>:1 +0x43 github.com/tendermint/tendermint/p2p.(*Switch).OnStart() /go/src/github.com/tendermint/tendermint/p2p/switch.go:225 +0x120 github.com/tendermint/tendermint/libs/service.(*BaseService).Start() /go/src/github.com/tendermint/tendermint/libs/service/service.go:140 +0x504 github.com/tendermint/tendermint/p2p.StartSwitches() /go/src/github.com/tendermint/tendermint/p2p/test_util.go:168 +0x75 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches() /go/src/github.com/tendermint/tendermint/p2p/test_util.go:89 +0x17d github.com/tendermint/tendermint/blockchain/v0.TestBadBlockStopsPeer() /go/src/github.com/tendermint/tendermint/blockchain/v0/reactor_test.go:209 +0x768 testing.tRunner() /usr/local/go/src/testing/testing.go:992 +0x1eb ================== panic: BlockStore can only save contiguous blocks. Wanted 149, got 147 goroutine 1259 [running]: github.com/tendermint/tendermint/store.(*BlockStore).SaveBlock(0xc000ff9cc0, 0xc001997180, 0xc0010c6a00, 0xc0013b3000) /go/src/github.com/tendermint/tendermint/store/store.go:276 +0xbc4 github.com/tendermint/tendermint/blockchain/v0.(*BlockchainReactor).poolRoutine(0xc001118d00, 0x107c000) /go/src/github.com/tendermint/tendermint/blockchain/v0/reactor.go:355 +0xe90 created by github.com/tendermint/tendermint/blockchain/v0.(*BlockchainReactor).OnStart /go/src/github.com/tendermint/tendermint/blockchain/v0/reactor.go:118 +0x12d FAIL github.com/tendermint/tendermint/blockchain/v0 11.447s FAIL ```
5 years ago
test/blockchain/v0: mitigate test data race (#4886) Mitigates the below data race. The proper fix involves not fiddling with reactor internals, which needs a rewrite of the test and possible additional reactor infrastructure. ``` ================== WARNING: DATA RACE Write at 0x00c001118e78 by goroutine 187: github.com/tendermint/tendermint/blockchain/v0.TestBadBlockStopsPeer() /go/src/github.com/tendermint/tendermint/blockchain/v0/reactor_test.go:234 +0x9d7 testing.tRunner() /usr/local/go/src/testing/testing.go:992 +0x1eb Previous read at 0x00c001118e78 by goroutine 326: [failed to restore the stack] Goroutine 187 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:1043 +0x660 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1285 +0xa6 testing.tRunner() /usr/local/go/src/testing/testing.go:992 +0x1eb testing.runTests() /usr/local/go/src/testing/testing.go:1283 +0x527 testing.(*M).Run() /usr/local/go/src/testing/testing.go:1200 +0x2ff main.main() _testmain.go:112 +0x337 Goroutine 326 (running) created at: github.com/tendermint/tendermint/blockchain/v0.(*BlockchainReactor).OnStart() /go/src/github.com/tendermint/tendermint/blockchain/v0/reactor.go:118 +0x12c github.com/tendermint/tendermint/libs/service.(*BaseService).Start() /go/src/github.com/tendermint/tendermint/libs/service/service.go:140 +0x504 github.com/tendermint/tendermint/blockchain/v0.(*BlockchainReactor).Start() <autogenerated>:1 +0x43 github.com/tendermint/tendermint/p2p.(*Switch).OnStart() /go/src/github.com/tendermint/tendermint/p2p/switch.go:225 +0x120 github.com/tendermint/tendermint/libs/service.(*BaseService).Start() /go/src/github.com/tendermint/tendermint/libs/service/service.go:140 +0x504 github.com/tendermint/tendermint/p2p.StartSwitches() /go/src/github.com/tendermint/tendermint/p2p/test_util.go:168 +0x75 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches() /go/src/github.com/tendermint/tendermint/p2p/test_util.go:89 +0x17d github.com/tendermint/tendermint/blockchain/v0.TestBadBlockStopsPeer() /go/src/github.com/tendermint/tendermint/blockchain/v0/reactor_test.go:209 +0x768 testing.tRunner() /usr/local/go/src/testing/testing.go:992 +0x1eb ================== panic: BlockStore can only save contiguous blocks. Wanted 149, got 147 goroutine 1259 [running]: github.com/tendermint/tendermint/store.(*BlockStore).SaveBlock(0xc000ff9cc0, 0xc001997180, 0xc0010c6a00, 0xc0013b3000) /go/src/github.com/tendermint/tendermint/store/store.go:276 +0xbc4 github.com/tendermint/tendermint/blockchain/v0.(*BlockchainReactor).poolRoutine(0xc001118d00, 0x107c000) /go/src/github.com/tendermint/tendermint/blockchain/v0/reactor.go:355 +0xe90 created by github.com/tendermint/tendermint/blockchain/v0.(*BlockchainReactor).OnStart /go/src/github.com/tendermint/tendermint/blockchain/v0/reactor.go:118 +0x12d FAIL github.com/tendermint/tendermint/blockchain/v0 11.447s FAIL ```
5 years ago
abci: Refactor tagging events using list of lists (#3643) ## PR This PR introduces a fundamental breaking change to the structure of ABCI response and tx tags and the way they're processed. Namely, the SDK can support more complex and aggregated events for distribution and slashing. In addition, block responses can include duplicate keys in events. Implement new Event type. An event has a type and a list of KV pairs (ie. list-of-lists). Typical events may look like: "rewards": [{"amount": "5000uatom", "validator": "...", "recipient": "..."}] "sender": [{"address": "...", "balance": "100uatom"}] The events are indexed by {even.type}.{even.attribute[i].key}/.... In this case a client would subscribe or query for rewards.recipient='...' ABCI response types and related types now include Events []Event instead of Tags []cmn.KVPair. PubSub logic now publishes/matches against map[string][]string instead of map[string]string to support duplicate keys in response events (from #1385). A match is successful if the value is found in the slice of strings. closes: #1859 closes: #2905 ## Commits: * Implement Event ABCI type and updates responses to use events * Update messages_test.go * Update kvstore.go * Update event_bus.go * Update subscription.go * Update pubsub.go * Update kvstore.go * Update query logic to handle slice of strings in events * Update Empty#Matches and unit tests * Update pubsub logic * Update EventBus#Publish * Update kv tx indexer * Update godocs * Update ResultEvent to use slice of strings; update RPC * Update more tests * Update abci.md * Check for key in validateAndStringifyEvents * Fix KV indexer to skip empty keys * Fix linting errors * Update CHANGELOG_PENDING.md * Update docs/spec/abci/abci.md Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update abci/types/types.proto Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update docs/spec/abci/abci.md Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update libs/pubsub/query/query.go Co-Authored-By: Ethan Buchman <ethan@coinculture.info> * Update match function to match if ANY value matches * Implement TestSubscribeDuplicateKeys * Update TestMatches to include multi-key test cases * Update events.go * Update Query interface godoc * Update match godoc * Add godoc for matchValue * DRY-up tx indexing * Return error from PublishWithEvents in EventBus#Publish * Update PublishEventNewBlockHeader to return an error * Fix build * Update events doc in ABCI * Update ABCI events godoc * Implement TestEventBusPublishEventTxDuplicateKeys * Update TestSubscribeDuplicateKeys to be table-driven * Remove mod file * Remove markdown from events godoc * Implement TestTxSearchDeprecatedIndexing test
6 years ago
  1. package v0
  2. import (
  3. "fmt"
  4. "os"
  5. "sort"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. dbm "github.com/tendermint/tm-db"
  11. abci "github.com/tendermint/tendermint/abci/types"
  12. cfg "github.com/tendermint/tendermint/config"
  13. "github.com/tendermint/tendermint/libs/log"
  14. "github.com/tendermint/tendermint/mempool/mock"
  15. "github.com/tendermint/tendermint/p2p"
  16. "github.com/tendermint/tendermint/proxy"
  17. sm "github.com/tendermint/tendermint/state"
  18. "github.com/tendermint/tendermint/store"
  19. "github.com/tendermint/tendermint/types"
  20. tmtime "github.com/tendermint/tendermint/types/time"
  21. )
  22. var config *cfg.Config
  23. func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.GenesisDoc, []types.PrivValidator) {
  24. validators := make([]types.GenesisValidator, numValidators)
  25. privValidators := make([]types.PrivValidator, numValidators)
  26. for i := 0; i < numValidators; i++ {
  27. val, privVal := types.RandValidator(randPower, minPower)
  28. validators[i] = types.GenesisValidator{
  29. PubKey: val.PubKey,
  30. Power: val.VotingPower,
  31. }
  32. privValidators[i] = privVal
  33. }
  34. sort.Sort(types.PrivValidatorsByAddress(privValidators))
  35. return &types.GenesisDoc{
  36. GenesisTime: tmtime.Now(),
  37. ChainID: config.ChainID(),
  38. Validators: validators,
  39. }, privValidators
  40. }
  41. type BlockchainReactorPair struct {
  42. reactor *BlockchainReactor
  43. app proxy.AppConns
  44. }
  45. func newBlockchainReactor(
  46. logger log.Logger,
  47. genDoc *types.GenesisDoc,
  48. privVals []types.PrivValidator,
  49. maxBlockHeight int64) BlockchainReactorPair {
  50. if len(privVals) != 1 {
  51. panic("only support one validator")
  52. }
  53. app := &testApp{}
  54. cc := proxy.NewLocalClientCreator(app)
  55. proxyApp := proxy.NewAppConns(cc)
  56. err := proxyApp.Start()
  57. if err != nil {
  58. panic(fmt.Errorf("error start app: %w", err))
  59. }
  60. blockDB := dbm.NewMemDB()
  61. stateDB := dbm.NewMemDB()
  62. stateStore := sm.NewStore(stateDB)
  63. blockStore := store.NewBlockStore(blockDB)
  64. state, err := stateStore.LoadFromDBOrGenesisDoc(genDoc)
  65. if err != nil {
  66. panic(fmt.Errorf("error constructing state from genesis file: %w", err))
  67. }
  68. // Make the BlockchainReactor itself.
  69. // NOTE we have to create and commit the blocks first because
  70. // pool.height is determined from the store.
  71. fastSync := true
  72. db := dbm.NewMemDB()
  73. stateStore = sm.NewStore(db)
  74. blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(),
  75. mock.Mempool{}, sm.EmptyEvidencePool{})
  76. if err = stateStore.Save(state); err != nil {
  77. panic(err)
  78. }
  79. // let's add some blocks in
  80. for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
  81. lastCommit := types.NewCommit(blockHeight-1, 0, types.BlockID{}, nil)
  82. if blockHeight > 1 {
  83. lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1)
  84. lastBlock := blockStore.LoadBlock(blockHeight - 1)
  85. vote, err := types.MakeVote(
  86. lastBlock.Header.Height,
  87. lastBlockMeta.BlockID,
  88. state.Validators,
  89. privVals[0],
  90. lastBlock.Header.ChainID,
  91. time.Now(),
  92. )
  93. if err != nil {
  94. panic(err)
  95. }
  96. lastCommit = types.NewCommit(vote.Height, vote.Round,
  97. lastBlockMeta.BlockID, []types.CommitSig{vote.CommitSig()})
  98. }
  99. thisBlock := makeBlock(blockHeight, state, lastCommit)
  100. thisParts := thisBlock.MakePartSet(types.BlockPartSizeBytes)
  101. blockID := types.BlockID{Hash: thisBlock.Hash(), PartSetHeader: thisParts.Header()}
  102. state, _, err = blockExec.ApplyBlock(state, blockID, thisBlock)
  103. if err != nil {
  104. panic(fmt.Errorf("error apply block: %w", err))
  105. }
  106. blockStore.SaveBlock(thisBlock, thisParts, lastCommit)
  107. }
  108. bcReactor := NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync)
  109. bcReactor.SetLogger(logger.With("module", "blockchain"))
  110. return BlockchainReactorPair{bcReactor, proxyApp}
  111. }
  112. func TestNoBlockResponse(t *testing.T) {
  113. config = cfg.ResetTestRoot("blockchain_reactor_test")
  114. defer os.RemoveAll(config.RootDir)
  115. genDoc, privVals := randGenesisDoc(1, false, 30)
  116. maxBlockHeight := int64(65)
  117. reactorPairs := make([]BlockchainReactorPair, 2)
  118. reactorPairs[0] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, maxBlockHeight)
  119. reactorPairs[1] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
  120. p2p.MakeConnectedSwitches(config.P2P, 2, func(i int, s *p2p.Switch) *p2p.Switch {
  121. s.AddReactor("BLOCKCHAIN", reactorPairs[i].reactor)
  122. return s
  123. }, p2p.Connect2Switches)
  124. defer func() {
  125. for _, r := range reactorPairs {
  126. err := r.reactor.Stop()
  127. require.NoError(t, err)
  128. err = r.app.Stop()
  129. require.NoError(t, err)
  130. }
  131. }()
  132. tests := []struct {
  133. height int64
  134. existent bool
  135. }{
  136. {maxBlockHeight + 2, false},
  137. {10, true},
  138. {1, true},
  139. {100, false},
  140. }
  141. for {
  142. if reactorPairs[1].reactor.pool.IsCaughtUp() {
  143. break
  144. }
  145. time.Sleep(10 * time.Millisecond)
  146. }
  147. assert.Equal(t, maxBlockHeight, reactorPairs[0].reactor.store.Height())
  148. for _, tt := range tests {
  149. block := reactorPairs[1].reactor.store.LoadBlock(tt.height)
  150. if tt.existent {
  151. assert.True(t, block != nil)
  152. } else {
  153. assert.True(t, block == nil)
  154. }
  155. }
  156. }
  157. // NOTE: This is too hard to test without
  158. // an easy way to add test peer to switch
  159. // or without significant refactoring of the module.
  160. // Alternatively we could actually dial a TCP conn but
  161. // that seems extreme.
  162. func TestBadBlockStopsPeer(t *testing.T) {
  163. config = cfg.ResetTestRoot("blockchain_reactor_test")
  164. defer os.RemoveAll(config.RootDir)
  165. genDoc, privVals := randGenesisDoc(1, false, 30)
  166. maxBlockHeight := int64(148)
  167. // Other chain needs a different validator set
  168. otherGenDoc, otherPrivVals := randGenesisDoc(1, false, 30)
  169. otherChain := newBlockchainReactor(log.TestingLogger(), otherGenDoc, otherPrivVals, maxBlockHeight)
  170. defer func() {
  171. err := otherChain.reactor.Stop()
  172. require.Error(t, err)
  173. err = otherChain.app.Stop()
  174. require.NoError(t, err)
  175. }()
  176. reactorPairs := make([]BlockchainReactorPair, 4)
  177. reactorPairs[0] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, maxBlockHeight)
  178. reactorPairs[1] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
  179. reactorPairs[2] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
  180. reactorPairs[3] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
  181. switches := p2p.MakeConnectedSwitches(config.P2P, 4, func(i int, s *p2p.Switch) *p2p.Switch {
  182. s.AddReactor("BLOCKCHAIN", reactorPairs[i].reactor)
  183. return s
  184. }, p2p.Connect2Switches)
  185. defer func() {
  186. for _, r := range reactorPairs {
  187. err := r.reactor.Stop()
  188. require.NoError(t, err)
  189. err = r.app.Stop()
  190. require.NoError(t, err)
  191. }
  192. }()
  193. for {
  194. time.Sleep(1 * time.Second)
  195. caughtUp := true
  196. for _, r := range reactorPairs {
  197. if !r.reactor.pool.IsCaughtUp() {
  198. caughtUp = false
  199. }
  200. }
  201. if caughtUp {
  202. break
  203. }
  204. }
  205. //at this time, reactors[0-3] is the newest
  206. assert.Equal(t, 3, reactorPairs[1].reactor.Switch.Peers().Size())
  207. // Mark reactorPairs[3] as an invalid peer. Fiddling with .store without a mutex is a data
  208. // race, but can't be easily avoided.
  209. reactorPairs[3].reactor.store = otherChain.reactor.store
  210. lastReactorPair := newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
  211. reactorPairs = append(reactorPairs, lastReactorPair)
  212. switches = append(switches, p2p.MakeConnectedSwitches(config.P2P, 1, func(i int, s *p2p.Switch) *p2p.Switch {
  213. s.AddReactor("BLOCKCHAIN", reactorPairs[len(reactorPairs)-1].reactor)
  214. return s
  215. }, p2p.Connect2Switches)...)
  216. for i := 0; i < len(reactorPairs)-1; i++ {
  217. p2p.Connect2Switches(switches, i, len(reactorPairs)-1)
  218. }
  219. for {
  220. if lastReactorPair.reactor.pool.IsCaughtUp() || lastReactorPair.reactor.Switch.Peers().Size() == 0 {
  221. break
  222. }
  223. time.Sleep(1 * time.Second)
  224. }
  225. assert.True(t, lastReactorPair.reactor.Switch.Peers().Size() < len(reactorPairs)-1)
  226. }
  227. //----------------------------------------------
  228. // utility funcs
  229. func makeTxs(height int64) (txs []types.Tx) {
  230. for i := 0; i < 10; i++ {
  231. txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
  232. }
  233. return txs
  234. }
  235. func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Block {
  236. block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, nil, state.Validators.GetProposer().Address)
  237. return block
  238. }
  239. type testApp struct {
  240. abci.BaseApplication
  241. }
  242. var _ abci.Application = (*testApp)(nil)
  243. func (app *testApp) Info(req abci.RequestInfo) (resInfo abci.ResponseInfo) {
  244. return abci.ResponseInfo{}
  245. }
  246. func (app *testApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlock {
  247. return abci.ResponseBeginBlock{}
  248. }
  249. func (app *testApp) EndBlock(req abci.RequestEndBlock) abci.ResponseEndBlock {
  250. return abci.ResponseEndBlock{}
  251. }
  252. func (app *testApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
  253. return abci.ResponseDeliverTx{Events: []abci.Event{}}
  254. }
  255. func (app *testApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
  256. return abci.ResponseCheckTx{}
  257. }
  258. func (app *testApp) Commit() abci.ResponseCommit {
  259. return abci.ResponseCommit{}
  260. }
  261. func (app *testApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
  262. return
  263. }