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.

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