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.

157 lines
3.8 KiB

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
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. abci "github.com/tendermint/abci/types"
  7. "github.com/tendermint/tendermint/config/tendermint_test"
  8. "github.com/tendermint/tendermint/types"
  9. . "github.com/tendermint/tmlibs/common"
  10. )
  11. func init() {
  12. config = tendermint_test.ResetConfig("consensus_mempool_test")
  13. }
  14. func TestTxConcurrentWithCommit(t *testing.T) {
  15. state, privVals := randGenesisState(1, false, 10)
  16. cs := newConsensusState(state, privVals[0], NewCounterApplication())
  17. height, round := cs.Height, cs.Round
  18. newBlockCh := subscribeToEvent(cs.evsw, "tester", types.EventStringNewBlock(), 1)
  19. deliverTxsRange := func(start, end int) {
  20. // Deliver some txs.
  21. for i := start; i < end; i++ {
  22. txBytes := make([]byte, 8)
  23. binary.BigEndian.PutUint64(txBytes, uint64(i))
  24. err := cs.mempool.CheckTx(txBytes, nil)
  25. if err != nil {
  26. panic(Fmt("Error after CheckTx: %v", err))
  27. }
  28. // time.Sleep(time.Microsecond * time.Duration(rand.Int63n(3000)))
  29. }
  30. }
  31. NTxs := 10000
  32. go deliverTxsRange(0, NTxs)
  33. startTestRound(cs, height, round)
  34. ticker := time.NewTicker(time.Second * 20)
  35. for nTxs := 0; nTxs < NTxs; {
  36. select {
  37. case b := <-newBlockCh:
  38. nTxs += b.(types.TMEventData).Unwrap().(types.EventDataNewBlock).Block.Header.NumTxs
  39. case <-ticker.C:
  40. panic("Timed out waiting to commit blocks with transactions")
  41. }
  42. }
  43. }
  44. func TestRmBadTx(t *testing.T) {
  45. state, privVals := randGenesisState(1, false, 10)
  46. app := NewCounterApplication()
  47. cs := newConsensusState(state, privVals[0], app)
  48. // increment the counter by 1
  49. txBytes := make([]byte, 8)
  50. binary.BigEndian.PutUint64(txBytes, uint64(0))
  51. app.DeliverTx(txBytes)
  52. app.Commit()
  53. ch := make(chan struct{})
  54. cbCh := make(chan struct{})
  55. go func() {
  56. // Try to send the tx through the mempool.
  57. // CheckTx should not err, but the app should return a bad abci code
  58. // and the tx should get removed from the pool
  59. err := cs.mempool.CheckTx(txBytes, func(r *abci.Response) {
  60. if r.GetCheckTx().Code != abci.CodeType_BadNonce {
  61. t.Fatalf("expected checktx to return bad nonce, got %v", r)
  62. }
  63. cbCh <- struct{}{}
  64. })
  65. if err != nil {
  66. t.Fatal("Error after CheckTx: %v", err)
  67. }
  68. // check for the tx
  69. for {
  70. time.Sleep(time.Second)
  71. txs := cs.mempool.Reap(1)
  72. if len(txs) == 0 {
  73. ch <- struct{}{}
  74. return
  75. }
  76. }
  77. }()
  78. // Wait until the tx returns
  79. ticker := time.After(time.Second * 5)
  80. select {
  81. case <-cbCh:
  82. // success
  83. case <-ticker:
  84. t.Fatalf("Timed out waiting for tx to return")
  85. }
  86. // Wait until the tx is removed
  87. ticker = time.After(time.Second * 5)
  88. select {
  89. case <-ch:
  90. // success
  91. case <-ticker:
  92. t.Fatalf("Timed out waiting for tx to be removed")
  93. }
  94. }
  95. // CounterApplication that maintains a mempool state and resets it upon commit
  96. type CounterApplication struct {
  97. abci.BaseApplication
  98. txCount int
  99. mempoolTxCount int
  100. }
  101. func NewCounterApplication() *CounterApplication {
  102. return &CounterApplication{}
  103. }
  104. func (app *CounterApplication) Info() abci.ResponseInfo {
  105. return abci.ResponseInfo{Data: Fmt("txs:%v", app.txCount)}
  106. }
  107. func (app *CounterApplication) DeliverTx(tx []byte) abci.Result {
  108. return runTx(tx, &app.txCount)
  109. }
  110. func (app *CounterApplication) CheckTx(tx []byte) abci.Result {
  111. return runTx(tx, &app.mempoolTxCount)
  112. }
  113. func runTx(tx []byte, countPtr *int) abci.Result {
  114. count := *countPtr
  115. tx8 := make([]byte, 8)
  116. copy(tx8[len(tx8)-len(tx):], tx)
  117. txValue := binary.BigEndian.Uint64(tx8)
  118. if txValue != uint64(count) {
  119. return abci.ErrBadNonce.AppendLog(Fmt("Invalid nonce. Expected %v, got %v", count, txValue))
  120. }
  121. *countPtr += 1
  122. return abci.OK
  123. }
  124. func (app *CounterApplication) Commit() abci.Result {
  125. app.mempoolTxCount = app.txCount
  126. if app.txCount == 0 {
  127. return abci.OK
  128. } else {
  129. hash := make([]byte, 8)
  130. binary.BigEndian.PutUint64(hash, uint64(app.txCount))
  131. return abci.NewResultOK(hash, "")
  132. }
  133. }