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.

160 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
  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/go-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.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. select {
  72. case <-ch:
  73. default:
  74. txs := cs.mempool.Reap(1)
  75. if len(txs) == 0 {
  76. ch <- struct{}{}
  77. }
  78. }
  79. }
  80. }()
  81. // Wait until the tx returns
  82. ticker := time.After(time.Second * 5)
  83. select {
  84. case <-cbCh:
  85. // success
  86. case <-ticker:
  87. t.Fatalf("Timed out waiting for tx to return")
  88. }
  89. // Wait until the tx is removed
  90. ticker = time.After(time.Second * 5)
  91. select {
  92. case <-ch:
  93. // success
  94. case <-ticker:
  95. t.Fatalf("Timed out waiting for tx to be removed")
  96. }
  97. }
  98. // CounterApplication that maintains a mempool state and resets it upon commit
  99. type CounterApplication struct {
  100. abci.BaseApplication
  101. txCount int
  102. mempoolTxCount int
  103. }
  104. func NewCounterApplication() *CounterApplication {
  105. return &CounterApplication{}
  106. }
  107. func (app *CounterApplication) Info() abci.ResponseInfo {
  108. return abci.ResponseInfo{Data: Fmt("txs:%v", app.txCount)}
  109. }
  110. func (app *CounterApplication) DeliverTx(tx []byte) abci.Result {
  111. return runTx(tx, &app.txCount)
  112. }
  113. func (app *CounterApplication) CheckTx(tx []byte) abci.Result {
  114. return runTx(tx, &app.mempoolTxCount)
  115. }
  116. func runTx(tx []byte, countPtr *int) abci.Result {
  117. count := *countPtr
  118. tx8 := make([]byte, 8)
  119. copy(tx8[len(tx8)-len(tx):], tx)
  120. txValue := binary.BigEndian.Uint64(tx8)
  121. if txValue != uint64(count) {
  122. return abci.ErrBadNonce.AppendLog(Fmt("Invalid nonce. Expected %v, got %v", count, txValue))
  123. }
  124. *countPtr += 1
  125. return abci.OK
  126. }
  127. func (app *CounterApplication) Commit() abci.Result {
  128. app.mempoolTxCount = app.txCount
  129. if app.txCount == 0 {
  130. return abci.OK
  131. } else {
  132. hash := make([]byte, 8)
  133. binary.BigEndian.PutUint64(hash, uint64(app.txCount))
  134. return abci.NewResultOK(hash, "")
  135. }
  136. }