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.

270 lines
8.0 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
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
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
7 years ago
8 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
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
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
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
7 years ago
7 years ago
8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package consensus
  2. import (
  3. "context"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/p2p"
  9. "github.com/tendermint/tendermint/types"
  10. cmn "github.com/tendermint/tmlibs/common"
  11. )
  12. func init() {
  13. config = ResetConfig("consensus_byzantine_test")
  14. }
  15. //----------------------------------------------
  16. // byzantine failures
  17. // 4 validators. 1 is byzantine. The other three are partitioned into A (1 val) and B (2 vals).
  18. // byzantine validator sends conflicting proposals into A and B,
  19. // and prevotes/precommits on both of them.
  20. // B sees a commit, A doesn't.
  21. // Byzantine validator refuses to prevote.
  22. // Heal partition and ensure A sees the commit
  23. func TestByzantine(t *testing.T) {
  24. N := 4
  25. logger := consensusLogger().With("test", "byzantine")
  26. css := randConsensusNet(N, "consensus_byzantine_test", newMockTickerFunc(false), newCounter)
  27. // give the byzantine validator a normal ticker
  28. ticker := NewTimeoutTicker()
  29. ticker.SetLogger(css[0].Logger)
  30. css[0].SetTimeoutTicker(ticker)
  31. switches := make([]*p2p.Switch, N)
  32. p2pLogger := logger.With("module", "p2p")
  33. for i := 0; i < N; i++ {
  34. switches[i] = p2p.NewSwitch(config.P2P)
  35. switches[i].SetLogger(p2pLogger.With("validator", i))
  36. }
  37. eventChans := make([]chan interface{}, N)
  38. reactors := make([]p2p.Reactor, N)
  39. for i := 0; i < N; i++ {
  40. // make first val byzantine
  41. if i == 0 {
  42. // NOTE: Now, test validators are MockPV, which by default doesn't
  43. // do any safety checks.
  44. css[i].privValidator.(*types.MockPV).DisableChecks()
  45. css[i].decideProposal = func(j int) func(int64, int) {
  46. return func(height int64, round int) {
  47. byzantineDecideProposalFunc(t, height, round, css[j], switches[j])
  48. }
  49. }(i)
  50. css[i].doPrevote = func(height int64, round int) {}
  51. }
  52. eventBus := types.NewEventBus()
  53. eventBus.SetLogger(logger.With("module", "events", "validator", i))
  54. err := eventBus.Start()
  55. require.NoError(t, err)
  56. defer eventBus.Stop()
  57. eventChans[i] = make(chan interface{}, 1)
  58. err = eventBus.Subscribe(context.Background(), testSubscriber, types.EventQueryNewBlock, eventChans[i])
  59. require.NoError(t, err)
  60. conR := NewConsensusReactor(css[i], true) // so we dont start the consensus states
  61. conR.SetLogger(logger.With("validator", i))
  62. conR.SetEventBus(eventBus)
  63. var conRI p2p.Reactor // nolint: gotype, gosimple
  64. conRI = conR
  65. // make first val byzantine
  66. if i == 0 {
  67. conRI = NewByzantineReactor(conR)
  68. }
  69. reactors[i] = conRI
  70. }
  71. defer func() {
  72. for _, r := range reactors {
  73. if rr, ok := r.(*ByzantineReactor); ok {
  74. rr.reactor.Switch.Stop()
  75. } else {
  76. r.(*ConsensusReactor).Switch.Stop()
  77. }
  78. }
  79. }()
  80. p2p.MakeConnectedSwitches(config.P2P, N, func(i int, s *p2p.Switch) *p2p.Switch {
  81. // ignore new switch s, we already made ours
  82. switches[i].AddReactor("CONSENSUS", reactors[i])
  83. return switches[i]
  84. }, func(sws []*p2p.Switch, i, j int) {
  85. // the network starts partitioned with globally active adversary
  86. if i != 0 {
  87. return
  88. }
  89. p2p.Connect2Switches(sws, i, j)
  90. })
  91. // start the non-byz state machines.
  92. // note these must be started before the byz
  93. for i := 1; i < N; i++ {
  94. cr := reactors[i].(*ConsensusReactor)
  95. cr.SwitchToConsensus(cr.conS.GetState(), 0)
  96. }
  97. // start the byzantine state machine
  98. byzR := reactors[0].(*ByzantineReactor)
  99. s := byzR.reactor.conS.GetState()
  100. byzR.reactor.SwitchToConsensus(s, 0)
  101. // byz proposer sends one block to peers[0]
  102. // and the other block to peers[1] and peers[2].
  103. // note peers and switches order don't match.
  104. peers := switches[0].Peers().List()
  105. // partition A
  106. ind0 := getSwitchIndex(switches, peers[0])
  107. // partition B
  108. ind1 := getSwitchIndex(switches, peers[1])
  109. ind2 := getSwitchIndex(switches, peers[2])
  110. p2p.Connect2Switches(switches, ind1, ind2)
  111. // wait for someone in the big partition (B) to make a block
  112. <-eventChans[ind2]
  113. t.Log("A block has been committed. Healing partition")
  114. p2p.Connect2Switches(switches, ind0, ind1)
  115. p2p.Connect2Switches(switches, ind0, ind2)
  116. // wait till everyone makes the first new block
  117. // (one of them already has)
  118. wg := new(sync.WaitGroup)
  119. wg.Add(2)
  120. for i := 1; i < N-1; i++ {
  121. go func(j int) {
  122. <-eventChans[j]
  123. wg.Done()
  124. }(i)
  125. }
  126. done := make(chan struct{})
  127. go func() {
  128. wg.Wait()
  129. close(done)
  130. }()
  131. tick := time.NewTicker(time.Second * 10)
  132. select {
  133. case <-done:
  134. case <-tick.C:
  135. for i, reactor := range reactors {
  136. t.Log(cmn.Fmt("Consensus Reactor %v", i))
  137. t.Log(cmn.Fmt("%v", reactor))
  138. }
  139. t.Fatalf("Timed out waiting for all validators to commit first block")
  140. }
  141. }
  142. //-------------------------------
  143. // byzantine consensus functions
  144. func byzantineDecideProposalFunc(t *testing.T, height int64, round int, cs *ConsensusState, sw *p2p.Switch) {
  145. // byzantine user should create two proposals and try to split the vote.
  146. // Avoid sending on internalMsgQueue and running consensus state.
  147. // Create a new proposal block from state/txs from the mempool.
  148. block1, blockParts1 := cs.createProposalBlock()
  149. polRound, polBlockID := cs.Votes.POLInfo()
  150. proposal1 := types.NewProposal(height, round, blockParts1.Header(), polRound, polBlockID)
  151. if err := cs.privValidator.SignProposal(cs.state.ChainID, proposal1); err != nil {
  152. t.Error(err)
  153. }
  154. // Create a new proposal block from state/txs from the mempool.
  155. block2, blockParts2 := cs.createProposalBlock()
  156. polRound, polBlockID = cs.Votes.POLInfo()
  157. proposal2 := types.NewProposal(height, round, blockParts2.Header(), polRound, polBlockID)
  158. if err := cs.privValidator.SignProposal(cs.state.ChainID, proposal2); err != nil {
  159. t.Error(err)
  160. }
  161. block1Hash := block1.Hash()
  162. block2Hash := block2.Hash()
  163. // broadcast conflicting proposals/block parts to peers
  164. peers := sw.Peers().List()
  165. t.Logf("Byzantine: broadcasting conflicting proposals to %d peers", len(peers))
  166. for i, peer := range peers {
  167. if i < len(peers)/2 {
  168. go sendProposalAndParts(height, round, cs, peer, proposal1, block1Hash, blockParts1)
  169. } else {
  170. go sendProposalAndParts(height, round, cs, peer, proposal2, block2Hash, blockParts2)
  171. }
  172. }
  173. }
  174. func sendProposalAndParts(height int64, round int, cs *ConsensusState, peer p2p.Peer, proposal *types.Proposal, blockHash []byte, parts *types.PartSet) {
  175. // proposal
  176. msg := &ProposalMessage{Proposal: proposal}
  177. peer.Send(DataChannel, cdc.MustMarshalBinaryBare(msg))
  178. // parts
  179. for i := 0; i < parts.Total(); i++ {
  180. part := parts.GetPart(i)
  181. msg := &BlockPartMessage{
  182. Height: height, // This tells peer that this part applies to us.
  183. Round: round, // This tells peer that this part applies to us.
  184. Part: part,
  185. }
  186. peer.Send(DataChannel, cdc.MustMarshalBinaryBare(msg))
  187. }
  188. // votes
  189. cs.mtx.Lock()
  190. prevote, _ := cs.signVote(types.VoteTypePrevote, blockHash, parts.Header())
  191. precommit, _ := cs.signVote(types.VoteTypePrecommit, blockHash, parts.Header())
  192. cs.mtx.Unlock()
  193. peer.Send(VoteChannel, cdc.MustMarshalBinaryBare(&VoteMessage{prevote}))
  194. peer.Send(VoteChannel, cdc.MustMarshalBinaryBare(&VoteMessage{precommit}))
  195. }
  196. //----------------------------------------
  197. // byzantine consensus reactor
  198. type ByzantineReactor struct {
  199. cmn.Service
  200. reactor *ConsensusReactor
  201. }
  202. func NewByzantineReactor(conR *ConsensusReactor) *ByzantineReactor {
  203. return &ByzantineReactor{
  204. Service: conR,
  205. reactor: conR,
  206. }
  207. }
  208. func (br *ByzantineReactor) SetSwitch(s *p2p.Switch) { br.reactor.SetSwitch(s) }
  209. func (br *ByzantineReactor) GetChannels() []*p2p.ChannelDescriptor { return br.reactor.GetChannels() }
  210. func (br *ByzantineReactor) AddPeer(peer p2p.Peer) {
  211. if !br.reactor.IsRunning() {
  212. return
  213. }
  214. // Create peerState for peer
  215. peerState := NewPeerState(peer).SetLogger(br.reactor.Logger)
  216. peer.Set(types.PeerStateKey, peerState)
  217. // Send our state to peer.
  218. // If we're fast_syncing, broadcast a RoundStepMessage later upon SwitchToConsensus().
  219. if !br.reactor.fastSync {
  220. br.reactor.sendNewRoundStepMessages(peer)
  221. }
  222. }
  223. func (br *ByzantineReactor) RemovePeer(peer p2p.Peer, reason interface{}) {
  224. br.reactor.RemovePeer(peer, reason)
  225. }
  226. func (br *ByzantineReactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) {
  227. br.reactor.Receive(chID, peer, msgBytes)
  228. }