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.

702 lines
20 KiB

new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
7 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
pubsub 2.0 (#3227) * green pubsub tests :OK: * get rid of clientToQueryMap * Subscribe and SubscribeUnbuffered * start adapting other pkgs to new pubsub * nope * rename MsgAndTags to Message * remove TagMap it does not bring any additional benefits * bring back EventSubscriber * fix test * fix data race in TestStartNextHeightCorrectly ``` Write at 0x00c0001c7418 by goroutine 796: github.com/tendermint/tendermint/consensus.TestStartNextHeightCorrectly() /go/src/github.com/tendermint/tendermint/consensus/state_test.go:1296 +0xad testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Previous read at 0x00c0001c7418 by goroutine 858: github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote() /go/src/github.com/tendermint/tendermint/consensus/state.go:1631 +0x1366 github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote() /go/src/github.com/tendermint/tendermint/consensus/state.go:1476 +0x8f github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg() /go/src/github.com/tendermint/tendermint/consensus/state.go:667 +0xa1e github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine() /go/src/github.com/tendermint/tendermint/consensus/state.go:628 +0x794 Goroutine 796 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1119 +0xa8 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 testing.runTests() /usr/local/go/src/testing/testing.go:1117 +0x4ee testing.(*M).Run() /usr/local/go/src/testing/testing.go:1034 +0x2ee main.main() _testmain.go:214 +0x332 Goroutine 858 (running) created at: github.com/tendermint/tendermint/consensus.(*ConsensusState).startRoutines() /go/src/github.com/tendermint/tendermint/consensus/state.go:334 +0x221 github.com/tendermint/tendermint/consensus.startTestRound() /go/src/github.com/tendermint/tendermint/consensus/common_test.go:122 +0x63 github.com/tendermint/tendermint/consensus.TestStateFullRound1() /go/src/github.com/tendermint/tendermint/consensus/state_test.go:255 +0x397 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ``` * fixes after my own review * fix formatting * wait 100ms before kicking a subscriber out + a test for indexer_service * fixes after my second review * no timeout * add changelog entries * fix merge conflicts * fix typos after Thane's review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * reformat code * rewrite indexer service in the attempt to fix failing test https://github.com/tendermint/tendermint/pull/3227/#issuecomment-462316527 * Revert "rewrite indexer service in the attempt to fix failing test" This reverts commit 0d9107a098230de7138abb1c201877c246e89ed1. * another attempt to fix indexer * fixes after Ethan's review * use unbuffered channel when indexing transactions Refs https://github.com/tendermint/tendermint/pull/3227#discussion_r258786716 * add a comment for EventBus#SubscribeUnbuffered * format code
6 years ago
new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
7 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
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
new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
7 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
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
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
  1. package consensus
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path"
  7. "sync"
  8. "testing"
  9. "time"
  10. "github.com/fortytw2/leaktest"
  11. "github.com/stretchr/testify/mock"
  12. "github.com/stretchr/testify/require"
  13. abcicli "github.com/tendermint/tendermint/abci/client"
  14. "github.com/tendermint/tendermint/abci/example/kvstore"
  15. abci "github.com/tendermint/tendermint/abci/types"
  16. cfg "github.com/tendermint/tendermint/config"
  17. cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
  18. "github.com/tendermint/tendermint/libs/log"
  19. tmsync "github.com/tendermint/tendermint/libs/sync"
  20. mempl "github.com/tendermint/tendermint/mempool"
  21. "github.com/tendermint/tendermint/p2p"
  22. "github.com/tendermint/tendermint/p2p/p2ptest"
  23. tmcons "github.com/tendermint/tendermint/proto/tendermint/consensus"
  24. sm "github.com/tendermint/tendermint/state"
  25. statemocks "github.com/tendermint/tendermint/state/mocks"
  26. "github.com/tendermint/tendermint/store"
  27. "github.com/tendermint/tendermint/types"
  28. dbm "github.com/tendermint/tm-db"
  29. )
  30. var (
  31. defaultTestTime = time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
  32. )
  33. type reactorTestSuite struct {
  34. network *p2ptest.Network
  35. states map[p2p.NodeID]*State
  36. reactors map[p2p.NodeID]*Reactor
  37. subs map[p2p.NodeID]types.Subscription
  38. stateChannels map[p2p.NodeID]*p2p.Channel
  39. dataChannels map[p2p.NodeID]*p2p.Channel
  40. voteChannels map[p2p.NodeID]*p2p.Channel
  41. voteSetBitsChannels map[p2p.NodeID]*p2p.Channel
  42. }
  43. func setup(t *testing.T, numNodes int, states []*State, size int) *reactorTestSuite {
  44. t.Helper()
  45. rts := &reactorTestSuite{
  46. network: p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: numNodes}),
  47. states: make(map[p2p.NodeID]*State),
  48. reactors: make(map[p2p.NodeID]*Reactor, numNodes),
  49. subs: make(map[p2p.NodeID]types.Subscription, numNodes),
  50. }
  51. rts.stateChannels = rts.network.MakeChannelsNoCleanup(t, StateChannel, new(tmcons.Message), size)
  52. rts.dataChannels = rts.network.MakeChannelsNoCleanup(t, DataChannel, new(tmcons.Message), size)
  53. rts.voteChannels = rts.network.MakeChannelsNoCleanup(t, VoteChannel, new(tmcons.Message), size)
  54. rts.voteSetBitsChannels = rts.network.MakeChannelsNoCleanup(t, VoteSetBitsChannel, new(tmcons.Message), size)
  55. i := 0
  56. for nodeID, node := range rts.network.Nodes {
  57. state := states[i]
  58. reactor := NewReactor(
  59. state.Logger.With("node", nodeID),
  60. state,
  61. rts.stateChannels[nodeID],
  62. rts.dataChannels[nodeID],
  63. rts.voteChannels[nodeID],
  64. rts.voteSetBitsChannels[nodeID],
  65. node.MakePeerUpdates(t),
  66. true,
  67. )
  68. reactor.SetEventBus(state.eventBus)
  69. blocksSub, err := state.eventBus.Subscribe(context.Background(), testSubscriber, types.EventQueryNewBlock, size)
  70. require.NoError(t, err)
  71. rts.states[nodeID] = state
  72. rts.subs[nodeID] = blocksSub
  73. rts.reactors[nodeID] = reactor
  74. // simulate handle initChain in handshake
  75. if state.state.LastBlockHeight == 0 {
  76. require.NoError(t, state.blockExec.Store().Save(state.state))
  77. }
  78. require.NoError(t, reactor.Start())
  79. require.True(t, reactor.IsRunning())
  80. i++
  81. }
  82. require.Len(t, rts.reactors, numNodes)
  83. // start the in-memory network and connect all peers with each other
  84. rts.network.Start(t)
  85. t.Cleanup(func() {
  86. for nodeID, r := range rts.reactors {
  87. require.NoError(t, rts.states[nodeID].eventBus.Stop())
  88. require.NoError(t, r.Stop())
  89. require.False(t, r.IsRunning())
  90. }
  91. leaktest.Check(t)
  92. })
  93. return rts
  94. }
  95. func validateBlock(block *types.Block, activeVals map[string]struct{}) error {
  96. if block.LastCommit.Size() != len(activeVals) {
  97. return fmt.Errorf(
  98. "commit size doesn't match number of active validators. Got %d, expected %d",
  99. block.LastCommit.Size(), len(activeVals),
  100. )
  101. }
  102. for _, commitSig := range block.LastCommit.Signatures {
  103. if _, ok := activeVals[string(commitSig.ValidatorAddress)]; !ok {
  104. return fmt.Errorf("found vote for inactive validator %X", commitSig.ValidatorAddress)
  105. }
  106. }
  107. return nil
  108. }
  109. func waitForAndValidateBlock(
  110. t *testing.T,
  111. n int,
  112. activeVals map[string]struct{},
  113. blocksSubs []types.Subscription,
  114. states []*State,
  115. txs ...[]byte,
  116. ) {
  117. fn := func(j int) {
  118. msg := <-blocksSubs[j].Out()
  119. newBlock := msg.Data().(types.EventDataNewBlock).Block
  120. require.NoError(t, validateBlock(newBlock, activeVals))
  121. for _, tx := range txs {
  122. require.NoError(t, assertMempool(states[j].txNotifier).CheckTx(tx, nil, mempl.TxInfo{}))
  123. }
  124. }
  125. var wg sync.WaitGroup
  126. wg.Add(n)
  127. for i := 0; i < n; i++ {
  128. go func(j int) {
  129. fn(j)
  130. wg.Done()
  131. }(i)
  132. }
  133. wg.Wait()
  134. }
  135. func waitForAndValidateBlockWithTx(
  136. t *testing.T,
  137. n int,
  138. activeVals map[string]struct{},
  139. blocksSubs []types.Subscription,
  140. states []*State,
  141. txs ...[]byte,
  142. ) {
  143. fn := func(j int) {
  144. ntxs := 0
  145. BLOCK_TX_LOOP:
  146. for {
  147. msg := <-blocksSubs[j].Out()
  148. newBlock := msg.Data().(types.EventDataNewBlock).Block
  149. require.NoError(t, validateBlock(newBlock, activeVals))
  150. // check that txs match the txs we're waiting for.
  151. // note they could be spread over multiple blocks,
  152. // but they should be in order.
  153. for _, tx := range newBlock.Data.Txs {
  154. require.EqualValues(t, txs[ntxs], tx)
  155. ntxs++
  156. }
  157. if ntxs == len(txs) {
  158. break BLOCK_TX_LOOP
  159. }
  160. }
  161. }
  162. var wg sync.WaitGroup
  163. wg.Add(n)
  164. for i := 0; i < n; i++ {
  165. go func(j int) {
  166. fn(j)
  167. wg.Done()
  168. }(i)
  169. }
  170. wg.Wait()
  171. }
  172. func waitForBlockWithUpdatedValsAndValidateIt(
  173. t *testing.T,
  174. n int,
  175. updatedVals map[string]struct{},
  176. blocksSubs []types.Subscription,
  177. css []*State,
  178. ) {
  179. fn := func(j int) {
  180. var newBlock *types.Block
  181. LOOP:
  182. for {
  183. msg := <-blocksSubs[j].Out()
  184. newBlock = msg.Data().(types.EventDataNewBlock).Block
  185. if newBlock.LastCommit.Size() == len(updatedVals) {
  186. break LOOP
  187. }
  188. }
  189. require.NoError(t, validateBlock(newBlock, updatedVals))
  190. }
  191. var wg sync.WaitGroup
  192. wg.Add(n)
  193. for i := 0; i < n; i++ {
  194. go func(j int) {
  195. fn(j)
  196. wg.Done()
  197. }(i)
  198. }
  199. wg.Wait()
  200. }
  201. func TestReactorBasic(t *testing.T) {
  202. config := configSetup(t)
  203. n := 4
  204. states, cleanup := randConsensusState(config, n, "consensus_reactor_test", newMockTickerFunc(true), newCounter)
  205. t.Cleanup(cleanup)
  206. rts := setup(t, n, states, 100) // buffer must be large enough to not deadlock
  207. for _, reactor := range rts.reactors {
  208. state := reactor.state.GetState()
  209. reactor.SwitchToConsensus(state, false)
  210. }
  211. var wg sync.WaitGroup
  212. for _, sub := range rts.subs {
  213. wg.Add(1)
  214. // wait till everyone makes the first new block
  215. go func(s types.Subscription) {
  216. <-s.Out()
  217. wg.Done()
  218. }(sub)
  219. }
  220. wg.Wait()
  221. }
  222. func TestReactorWithEvidence(t *testing.T) {
  223. config := configSetup(t)
  224. n := 4
  225. testName := "consensus_reactor_test"
  226. tickerFunc := newMockTickerFunc(true)
  227. appFunc := newCounter
  228. genDoc, privVals := randGenesisDoc(config, n, false, 30)
  229. states := make([]*State, n)
  230. logger := consensusLogger()
  231. for i := 0; i < n; i++ {
  232. stateDB := dbm.NewMemDB() // each state needs its own db
  233. stateStore := sm.NewStore(stateDB)
  234. state, _ := stateStore.LoadFromDBOrGenesisDoc(genDoc)
  235. thisConfig := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
  236. defer os.RemoveAll(thisConfig.RootDir)
  237. ensureDir(path.Dir(thisConfig.Consensus.WalFile()), 0700) // dir for wal
  238. app := appFunc()
  239. vals := types.TM2PB.ValidatorUpdates(state.Validators)
  240. app.InitChain(abci.RequestInitChain{Validators: vals})
  241. pv := privVals[i]
  242. blockDB := dbm.NewMemDB()
  243. blockStore := store.NewBlockStore(blockDB)
  244. // one for mempool, one for consensus
  245. mtx := new(tmsync.RWMutex)
  246. proxyAppConnMem := abcicli.NewLocalClient(mtx, app)
  247. proxyAppConnCon := abcicli.NewLocalClient(mtx, app)
  248. mempool := mempl.NewCListMempool(thisConfig.Mempool, proxyAppConnMem, 0)
  249. mempool.SetLogger(log.TestingLogger().With("module", "mempool"))
  250. if thisConfig.Consensus.WaitForTxs() {
  251. mempool.EnableTxsAvailable()
  252. }
  253. // mock the evidence pool
  254. // everyone includes evidence of another double signing
  255. vIdx := (i + 1) % n
  256. ev := types.NewMockDuplicateVoteEvidenceWithValidator(1, defaultTestTime, privVals[vIdx], config.ChainID())
  257. evpool := &statemocks.EvidencePool{}
  258. evpool.On("CheckEvidence", mock.AnythingOfType("types.EvidenceList")).Return(nil)
  259. evpool.On("PendingEvidence", mock.AnythingOfType("int64")).Return([]types.Evidence{
  260. ev}, int64(len(ev.Bytes())))
  261. evpool.On("Update", mock.AnythingOfType("state.State"), mock.AnythingOfType("types.EvidenceList")).Return()
  262. evpool2 := sm.EmptyEvidencePool{}
  263. blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyAppConnCon, mempool, evpool)
  264. cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool2)
  265. cs.SetLogger(log.TestingLogger().With("module", "consensus"))
  266. cs.SetPrivValidator(pv)
  267. eventBus := types.NewEventBus()
  268. eventBus.SetLogger(log.TestingLogger().With("module", "events"))
  269. err := eventBus.Start()
  270. require.NoError(t, err)
  271. cs.SetEventBus(eventBus)
  272. cs.SetTimeoutTicker(tickerFunc())
  273. cs.SetLogger(logger.With("validator", i, "module", "consensus"))
  274. states[i] = cs
  275. }
  276. rts := setup(t, n, states, 100) // buffer must be large enough to not deadlock
  277. for _, reactor := range rts.reactors {
  278. state := reactor.state.GetState()
  279. reactor.SwitchToConsensus(state, false)
  280. }
  281. var wg sync.WaitGroup
  282. for _, sub := range rts.subs {
  283. wg.Add(1)
  284. // We expect for each validator that is the proposer to propose one piece of
  285. // evidence.
  286. go func(s types.Subscription) {
  287. msg := <-s.Out()
  288. block := msg.Data().(types.EventDataNewBlock).Block
  289. require.Len(t, block.Evidence.Evidence, 1)
  290. wg.Done()
  291. }(sub)
  292. }
  293. wg.Wait()
  294. }
  295. func TestReactorCreatesBlockWhenEmptyBlocksFalse(t *testing.T) {
  296. config := configSetup(t)
  297. n := 4
  298. states, cleanup := randConsensusState(
  299. config,
  300. n,
  301. "consensus_reactor_test",
  302. newMockTickerFunc(true),
  303. newCounter,
  304. func(c *cfg.Config) {
  305. c.Consensus.CreateEmptyBlocks = false
  306. },
  307. )
  308. t.Cleanup(cleanup)
  309. rts := setup(t, n, states, 100) // buffer must be large enough to not deadlock
  310. for _, reactor := range rts.reactors {
  311. state := reactor.state.GetState()
  312. reactor.SwitchToConsensus(state, false)
  313. }
  314. // send a tx
  315. require.NoError(t, assertMempool(states[3].txNotifier).CheckTx([]byte{1, 2, 3}, nil, mempl.TxInfo{}))
  316. var wg sync.WaitGroup
  317. for _, sub := range rts.subs {
  318. wg.Add(1)
  319. // wait till everyone makes the first new block
  320. go func(s types.Subscription) {
  321. <-s.Out()
  322. wg.Done()
  323. }(sub)
  324. }
  325. wg.Wait()
  326. }
  327. func TestReactorRecordsVotesAndBlockParts(t *testing.T) {
  328. config := configSetup(t)
  329. n := 4
  330. states, cleanup := randConsensusState(config, n, "consensus_reactor_test", newMockTickerFunc(true), newCounter)
  331. t.Cleanup(cleanup)
  332. rts := setup(t, n, states, 100) // buffer must be large enough to not deadlock
  333. for _, reactor := range rts.reactors {
  334. state := reactor.state.GetState()
  335. reactor.SwitchToConsensus(state, false)
  336. }
  337. var wg sync.WaitGroup
  338. for _, sub := range rts.subs {
  339. wg.Add(1)
  340. // wait till everyone makes the first new block
  341. go func(s types.Subscription) {
  342. <-s.Out()
  343. wg.Done()
  344. }(sub)
  345. }
  346. wg.Wait()
  347. // Require at least one node to have sent block parts, but we can't know which
  348. // peer sent it.
  349. require.Eventually(
  350. t,
  351. func() bool {
  352. for _, reactor := range rts.reactors {
  353. for _, ps := range reactor.peers {
  354. if ps.BlockPartsSent() > 0 {
  355. return true
  356. }
  357. }
  358. }
  359. return false
  360. },
  361. time.Second,
  362. 10*time.Millisecond,
  363. "number of block parts sent should've increased",
  364. )
  365. nodeID := rts.network.RandomNode().NodeID
  366. reactor := rts.reactors[nodeID]
  367. peers := rts.network.Peers(nodeID)
  368. ps, ok := reactor.GetPeerState(peers[0].NodeID)
  369. require.True(t, ok)
  370. require.NotNil(t, ps)
  371. require.Greater(t, ps.VotesSent(), 0, "number of votes sent should've increased")
  372. }
  373. func TestReactorVotingPowerChange(t *testing.T) {
  374. config := configSetup(t)
  375. n := 4
  376. states, cleanup := randConsensusState(
  377. config,
  378. n,
  379. "consensus_voting_power_changes_test",
  380. newMockTickerFunc(true),
  381. newPersistentKVStore,
  382. )
  383. t.Cleanup(cleanup)
  384. rts := setup(t, n, states, 100) // buffer must be large enough to not deadlock
  385. for _, reactor := range rts.reactors {
  386. state := reactor.state.GetState()
  387. reactor.SwitchToConsensus(state, false)
  388. }
  389. // map of active validators
  390. activeVals := make(map[string]struct{})
  391. for i := 0; i < n; i++ {
  392. pubKey, err := states[i].privValidator.GetPubKey(context.Background())
  393. require.NoError(t, err)
  394. addr := pubKey.Address()
  395. activeVals[string(addr)] = struct{}{}
  396. }
  397. var wg sync.WaitGroup
  398. for _, sub := range rts.subs {
  399. wg.Add(1)
  400. // wait till everyone makes the first new block
  401. go func(s types.Subscription) {
  402. <-s.Out()
  403. wg.Done()
  404. }(sub)
  405. }
  406. wg.Wait()
  407. blocksSubs := []types.Subscription{}
  408. for _, sub := range rts.subs {
  409. blocksSubs = append(blocksSubs, sub)
  410. }
  411. val1PubKey, err := states[0].privValidator.GetPubKey(context.Background())
  412. require.NoError(t, err)
  413. val1PubKeyABCI, err := cryptoenc.PubKeyToProto(val1PubKey)
  414. require.NoError(t, err)
  415. updateValidatorTx := kvstore.MakeValSetChangeTx(val1PubKeyABCI, 25)
  416. previousTotalVotingPower := states[0].GetRoundState().LastValidators.TotalVotingPower()
  417. waitForAndValidateBlock(t, n, activeVals, blocksSubs, states, updateValidatorTx)
  418. waitForAndValidateBlockWithTx(t, n, activeVals, blocksSubs, states, updateValidatorTx)
  419. waitForAndValidateBlock(t, n, activeVals, blocksSubs, states)
  420. waitForAndValidateBlock(t, n, activeVals, blocksSubs, states)
  421. require.NotEqualf(
  422. t, previousTotalVotingPower, states[0].GetRoundState().LastValidators.TotalVotingPower(),
  423. "expected voting power to change (before: %d, after: %d)",
  424. previousTotalVotingPower,
  425. states[0].GetRoundState().LastValidators.TotalVotingPower(),
  426. )
  427. updateValidatorTx = kvstore.MakeValSetChangeTx(val1PubKeyABCI, 2)
  428. previousTotalVotingPower = states[0].GetRoundState().LastValidators.TotalVotingPower()
  429. waitForAndValidateBlock(t, n, activeVals, blocksSubs, states, updateValidatorTx)
  430. waitForAndValidateBlockWithTx(t, n, activeVals, blocksSubs, states, updateValidatorTx)
  431. waitForAndValidateBlock(t, n, activeVals, blocksSubs, states)
  432. waitForAndValidateBlock(t, n, activeVals, blocksSubs, states)
  433. require.NotEqualf(
  434. t, states[0].GetRoundState().LastValidators.TotalVotingPower(), previousTotalVotingPower,
  435. "expected voting power to change (before: %d, after: %d)",
  436. previousTotalVotingPower, states[0].GetRoundState().LastValidators.TotalVotingPower(),
  437. )
  438. updateValidatorTx = kvstore.MakeValSetChangeTx(val1PubKeyABCI, 26)
  439. previousTotalVotingPower = states[0].GetRoundState().LastValidators.TotalVotingPower()
  440. waitForAndValidateBlock(t, n, activeVals, blocksSubs, states, updateValidatorTx)
  441. waitForAndValidateBlockWithTx(t, n, activeVals, blocksSubs, states, updateValidatorTx)
  442. waitForAndValidateBlock(t, n, activeVals, blocksSubs, states)
  443. waitForAndValidateBlock(t, n, activeVals, blocksSubs, states)
  444. require.NotEqualf(
  445. t, previousTotalVotingPower, states[0].GetRoundState().LastValidators.TotalVotingPower(),
  446. "expected voting power to change (before: %d, after: %d)",
  447. previousTotalVotingPower,
  448. states[0].GetRoundState().LastValidators.TotalVotingPower(),
  449. )
  450. }
  451. func TestReactorValidatorSetChanges(t *testing.T) {
  452. config := configSetup(t)
  453. nPeers := 7
  454. nVals := 4
  455. states, _, _, cleanup := randConsensusNetWithPeers(
  456. config,
  457. nVals,
  458. nPeers,
  459. "consensus_val_set_changes_test",
  460. newMockTickerFunc(true),
  461. newPersistentKVStoreWithPath,
  462. )
  463. t.Cleanup(cleanup)
  464. rts := setup(t, nPeers, states, 100) // buffer must be large enough to not deadlock
  465. for _, reactor := range rts.reactors {
  466. state := reactor.state.GetState()
  467. reactor.SwitchToConsensus(state, false)
  468. }
  469. // map of active validators
  470. activeVals := make(map[string]struct{})
  471. for i := 0; i < nVals; i++ {
  472. pubKey, err := states[i].privValidator.GetPubKey(context.Background())
  473. require.NoError(t, err)
  474. activeVals[string(pubKey.Address())] = struct{}{}
  475. }
  476. var wg sync.WaitGroup
  477. for _, sub := range rts.subs {
  478. wg.Add(1)
  479. // wait till everyone makes the first new block
  480. go func(s types.Subscription) {
  481. <-s.Out()
  482. wg.Done()
  483. }(sub)
  484. }
  485. wg.Wait()
  486. newValidatorPubKey1, err := states[nVals].privValidator.GetPubKey(context.Background())
  487. require.NoError(t, err)
  488. valPubKey1ABCI, err := cryptoenc.PubKeyToProto(newValidatorPubKey1)
  489. require.NoError(t, err)
  490. newValidatorTx1 := kvstore.MakeValSetChangeTx(valPubKey1ABCI, testMinPower)
  491. blocksSubs := []types.Subscription{}
  492. for _, sub := range rts.subs {
  493. blocksSubs = append(blocksSubs, sub)
  494. }
  495. // wait till everyone makes block 2
  496. // ensure the commit includes all validators
  497. // send newValTx to change vals in block 3
  498. waitForAndValidateBlock(t, nPeers, activeVals, blocksSubs, states, newValidatorTx1)
  499. // wait till everyone makes block 3.
  500. // it includes the commit for block 2, which is by the original validator set
  501. waitForAndValidateBlockWithTx(t, nPeers, activeVals, blocksSubs, states, newValidatorTx1)
  502. // wait till everyone makes block 4.
  503. // it includes the commit for block 3, which is by the original validator set
  504. waitForAndValidateBlock(t, nPeers, activeVals, blocksSubs, states)
  505. // the commits for block 4 should be with the updated validator set
  506. activeVals[string(newValidatorPubKey1.Address())] = struct{}{}
  507. // wait till everyone makes block 5
  508. // it includes the commit for block 4, which should have the updated validator set
  509. waitForBlockWithUpdatedValsAndValidateIt(t, nPeers, activeVals, blocksSubs, states)
  510. updateValidatorPubKey1, err := states[nVals].privValidator.GetPubKey(context.Background())
  511. require.NoError(t, err)
  512. updatePubKey1ABCI, err := cryptoenc.PubKeyToProto(updateValidatorPubKey1)
  513. require.NoError(t, err)
  514. updateValidatorTx1 := kvstore.MakeValSetChangeTx(updatePubKey1ABCI, 25)
  515. previousTotalVotingPower := states[nVals].GetRoundState().LastValidators.TotalVotingPower()
  516. waitForAndValidateBlock(t, nPeers, activeVals, blocksSubs, states, updateValidatorTx1)
  517. waitForAndValidateBlockWithTx(t, nPeers, activeVals, blocksSubs, states, updateValidatorTx1)
  518. waitForAndValidateBlock(t, nPeers, activeVals, blocksSubs, states)
  519. waitForBlockWithUpdatedValsAndValidateIt(t, nPeers, activeVals, blocksSubs, states)
  520. require.NotEqualf(
  521. t, states[nVals].GetRoundState().LastValidators.TotalVotingPower(), previousTotalVotingPower,
  522. "expected voting power to change (before: %d, after: %d)",
  523. previousTotalVotingPower, states[nVals].GetRoundState().LastValidators.TotalVotingPower(),
  524. )
  525. newValidatorPubKey2, err := states[nVals+1].privValidator.GetPubKey(context.Background())
  526. require.NoError(t, err)
  527. newVal2ABCI, err := cryptoenc.PubKeyToProto(newValidatorPubKey2)
  528. require.NoError(t, err)
  529. newValidatorTx2 := kvstore.MakeValSetChangeTx(newVal2ABCI, testMinPower)
  530. newValidatorPubKey3, err := states[nVals+2].privValidator.GetPubKey(context.Background())
  531. require.NoError(t, err)
  532. newVal3ABCI, err := cryptoenc.PubKeyToProto(newValidatorPubKey3)
  533. require.NoError(t, err)
  534. newValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, testMinPower)
  535. waitForAndValidateBlock(t, nPeers, activeVals, blocksSubs, states, newValidatorTx2, newValidatorTx3)
  536. waitForAndValidateBlockWithTx(t, nPeers, activeVals, blocksSubs, states, newValidatorTx2, newValidatorTx3)
  537. waitForAndValidateBlock(t, nPeers, activeVals, blocksSubs, states)
  538. activeVals[string(newValidatorPubKey2.Address())] = struct{}{}
  539. activeVals[string(newValidatorPubKey3.Address())] = struct{}{}
  540. waitForBlockWithUpdatedValsAndValidateIt(t, nPeers, activeVals, blocksSubs, states)
  541. removeValidatorTx2 := kvstore.MakeValSetChangeTx(newVal2ABCI, 0)
  542. removeValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, 0)
  543. waitForAndValidateBlock(t, nPeers, activeVals, blocksSubs, states, removeValidatorTx2, removeValidatorTx3)
  544. waitForAndValidateBlockWithTx(t, nPeers, activeVals, blocksSubs, states, removeValidatorTx2, removeValidatorTx3)
  545. waitForAndValidateBlock(t, nPeers, activeVals, blocksSubs, states)
  546. delete(activeVals, string(newValidatorPubKey2.Address()))
  547. delete(activeVals, string(newValidatorPubKey3.Address()))
  548. waitForBlockWithUpdatedValsAndValidateIt(t, nPeers, activeVals, blocksSubs, states)
  549. }