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