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.

220 lines
6.6 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
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
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
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
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package consensus
  2. import (
  3. "encoding/binary"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. abci "github.com/tendermint/abci/types"
  8. "github.com/tendermint/tendermint/types"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. )
  11. func init() {
  12. config = ResetConfig("consensus_mempool_test")
  13. }
  14. func TestNoProgressUntilTxsAvailable(t *testing.T) {
  15. config := ResetConfig("consensus_mempool_txs_available_test")
  16. config.Consensus.CreateEmptyBlocks = false
  17. state, privVals := randGenesisState(1, false, 10)
  18. cs := newConsensusStateWithConfig(config, state, privVals[0], NewCounterApplication())
  19. cs.mempool.EnableTxsAvailable()
  20. height, round := cs.Height, cs.Round
  21. newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
  22. startTestRound(cs, height, round)
  23. ensureNewStep(newBlockCh) // first block gets committed
  24. ensureNoNewStep(newBlockCh)
  25. deliverTxsRange(cs, 0, 1)
  26. ensureNewStep(newBlockCh) // commit txs
  27. ensureNewStep(newBlockCh) // commit updated app hash
  28. ensureNoNewStep(newBlockCh)
  29. }
  30. func TestProgressAfterCreateEmptyBlocksInterval(t *testing.T) {
  31. config := ResetConfig("consensus_mempool_txs_available_test")
  32. config.Consensus.CreateEmptyBlocksInterval = int(ensureTimeout.Seconds())
  33. state, privVals := randGenesisState(1, false, 10)
  34. cs := newConsensusStateWithConfig(config, state, privVals[0], NewCounterApplication())
  35. cs.mempool.EnableTxsAvailable()
  36. height, round := cs.Height, cs.Round
  37. newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
  38. startTestRound(cs, height, round)
  39. ensureNewStep(newBlockCh) // first block gets committed
  40. ensureNoNewStep(newBlockCh) // then we dont make a block ...
  41. ensureNewStep(newBlockCh) // until the CreateEmptyBlocksInterval has passed
  42. }
  43. func TestProgressInHigherRound(t *testing.T) {
  44. config := ResetConfig("consensus_mempool_txs_available_test")
  45. config.Consensus.CreateEmptyBlocks = false
  46. state, privVals := randGenesisState(1, false, 10)
  47. cs := newConsensusStateWithConfig(config, state, privVals[0], NewCounterApplication())
  48. cs.mempool.EnableTxsAvailable()
  49. height, round := cs.Height, cs.Round
  50. newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
  51. newRoundCh := subscribe(cs.eventBus, types.EventQueryNewRound)
  52. timeoutCh := subscribe(cs.eventBus, types.EventQueryTimeoutPropose)
  53. cs.setProposal = func(proposal *types.Proposal) error {
  54. if cs.Height == 2 && cs.Round == 0 {
  55. // dont set the proposal in round 0 so we timeout and
  56. // go to next round
  57. cs.Logger.Info("Ignoring set proposal at height 2, round 0")
  58. return nil
  59. }
  60. return cs.defaultSetProposal(proposal)
  61. }
  62. startTestRound(cs, height, round)
  63. ensureNewStep(newRoundCh) // first round at first height
  64. ensureNewStep(newBlockCh) // first block gets committed
  65. ensureNewStep(newRoundCh) // first round at next height
  66. deliverTxsRange(cs, 0, 1) // we deliver txs, but dont set a proposal so we get the next round
  67. <-timeoutCh
  68. ensureNewStep(newRoundCh) // wait for the next round
  69. ensureNewStep(newBlockCh) // now we can commit the block
  70. }
  71. func deliverTxsRange(cs *ConsensusState, start, end int) {
  72. // Deliver some txs.
  73. for i := start; i < end; i++ {
  74. txBytes := make([]byte, 8)
  75. binary.BigEndian.PutUint64(txBytes, uint64(i))
  76. err := cs.mempool.CheckTx(txBytes, nil)
  77. if err != nil {
  78. panic(cmn.Fmt("Error after CheckTx: %v", err))
  79. }
  80. }
  81. }
  82. func TestTxConcurrentWithCommit(t *testing.T) {
  83. state, privVals := randGenesisState(1, false, 10)
  84. cs := newConsensusState(state, privVals[0], NewCounterApplication())
  85. height, round := cs.Height, cs.Round
  86. newBlockCh := subscribe(cs.eventBus, types.EventQueryNewBlock)
  87. NTxs := 10000
  88. go deliverTxsRange(cs, 0, NTxs)
  89. startTestRound(cs, height, round)
  90. ticker := time.NewTicker(time.Second * 20)
  91. for nTxs := 0; nTxs < NTxs; {
  92. select {
  93. case b := <-newBlockCh:
  94. nTxs += b.(types.TMEventData).Unwrap().(types.EventDataNewBlock).Block.Header.NumTxs
  95. case <-ticker.C:
  96. panic("Timed out waiting to commit blocks with transactions")
  97. }
  98. }
  99. }
  100. func TestRmBadTx(t *testing.T) {
  101. state, privVals := randGenesisState(1, false, 10)
  102. app := NewCounterApplication()
  103. cs := newConsensusState(state, privVals[0], app)
  104. // increment the counter by 1
  105. txBytes := make([]byte, 8)
  106. binary.BigEndian.PutUint64(txBytes, uint64(0))
  107. resDeliver := app.DeliverTx(txBytes)
  108. assert.False(t, resDeliver.IsErr(), cmn.Fmt("expected no error. got %v", resDeliver))
  109. resCommit := app.Commit()
  110. assert.False(t, resCommit.IsErr(), cmn.Fmt("expected no error. got %v", resCommit))
  111. emptyMempoolCh := make(chan struct{})
  112. checkTxRespCh := make(chan struct{})
  113. go func() {
  114. // Try to send the tx through the mempool.
  115. // CheckTx should not err, but the app should return a bad abci code
  116. // and the tx should get removed from the pool
  117. err := cs.mempool.CheckTx(txBytes, func(r *abci.Response) {
  118. if r.GetCheckTx().Code != abci.CodeType_BadNonce {
  119. t.Fatalf("expected checktx to return bad nonce, got %v", r)
  120. }
  121. checkTxRespCh <- struct{}{}
  122. })
  123. if err != nil {
  124. t.Fatalf("Error after CheckTx: %v", err)
  125. }
  126. // check for the tx
  127. for {
  128. txs := cs.mempool.Reap(1)
  129. if len(txs) == 0 {
  130. emptyMempoolCh <- struct{}{}
  131. }
  132. time.Sleep(10 * time.Millisecond)
  133. }
  134. }()
  135. // Wait until the tx returns
  136. ticker := time.After(time.Second * 5)
  137. select {
  138. case <-checkTxRespCh:
  139. // success
  140. case <-ticker:
  141. t.Fatalf("Timed out waiting for tx to return")
  142. }
  143. // Wait until the tx is removed
  144. ticker = time.After(time.Second * 5)
  145. select {
  146. case <-emptyMempoolCh:
  147. // success
  148. case <-ticker:
  149. t.Fatalf("Timed out waiting for tx to be removed")
  150. }
  151. }
  152. // CounterApplication that maintains a mempool state and resets it upon commit
  153. type CounterApplication struct {
  154. abci.BaseApplication
  155. txCount int
  156. mempoolTxCount int
  157. }
  158. func NewCounterApplication() *CounterApplication {
  159. return &CounterApplication{}
  160. }
  161. func (app *CounterApplication) Info(req abci.RequestInfo) abci.ResponseInfo {
  162. return abci.ResponseInfo{Data: cmn.Fmt("txs:%v", app.txCount)}
  163. }
  164. func (app *CounterApplication) DeliverTx(tx []byte) abci.Result {
  165. return runTx(tx, &app.txCount)
  166. }
  167. func (app *CounterApplication) CheckTx(tx []byte) abci.Result {
  168. return runTx(tx, &app.mempoolTxCount)
  169. }
  170. func runTx(tx []byte, countPtr *int) abci.Result {
  171. count := *countPtr
  172. tx8 := make([]byte, 8)
  173. copy(tx8[len(tx8)-len(tx):], tx)
  174. txValue := binary.BigEndian.Uint64(tx8)
  175. if txValue != uint64(count) {
  176. return abci.ErrBadNonce.AppendLog(cmn.Fmt("Invalid nonce. Expected %v, got %v", count, txValue))
  177. }
  178. *countPtr += 1
  179. return abci.OK
  180. }
  181. func (app *CounterApplication) Commit() abci.Result {
  182. app.mempoolTxCount = app.txCount
  183. if app.txCount == 0 {
  184. return abci.OK
  185. } else {
  186. hash := make([]byte, 8)
  187. binary.BigEndian.PutUint64(hash, uint64(app.txCount))
  188. return abci.NewResultOK(hash, "")
  189. }
  190. }