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.

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