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.

204 lines
5.3 KiB

8 years ago
8 years ago
8 years ago
9 years ago
  1. package mempool
  2. import (
  3. "crypto/rand"
  4. "encoding/binary"
  5. "testing"
  6. "time"
  7. "github.com/tendermint/abci/example/counter"
  8. "github.com/tendermint/abci/example/dummy"
  9. "github.com/tendermint/tmlibs/log"
  10. cfg "github.com/tendermint/tendermint/config"
  11. "github.com/tendermint/tendermint/proxy"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. func newMempoolWithApp(t *testing.T, cc proxy.ClientCreator) *Mempool {
  15. config := cfg.ResetTestRoot("mempool_test")
  16. appConnMem, _ := cc.NewABCIClient()
  17. appConnMem.SetLogger(log.TestingLogger().With("module", "abci-client", "connection", "mempool"))
  18. if _, err := appConnMem.Start(); err != nil {
  19. t.Fatalf("Error starting ABCI client: %v", err.Error())
  20. }
  21. mempool := NewMempool(config.Mempool, appConnMem, 0)
  22. mempool.SetLogger(log.TestingLogger())
  23. return mempool
  24. }
  25. func ensureNoFire(t *testing.T, ch <-chan int, timeoutMS int) {
  26. timer := time.NewTimer(time.Duration(timeoutMS) * time.Millisecond)
  27. select {
  28. case <-ch:
  29. t.Fatal("Expected not to fire")
  30. case <-timer.C:
  31. }
  32. }
  33. func ensureFire(t *testing.T, ch <-chan int, timeoutMS int) {
  34. timer := time.NewTimer(time.Duration(timeoutMS) * time.Millisecond)
  35. select {
  36. case <-ch:
  37. case <-timer.C:
  38. t.Fatal("Expected to fire")
  39. }
  40. }
  41. func sendTxs(t *testing.T, mempool *Mempool, count int) types.Txs {
  42. txs := make(types.Txs, count)
  43. for i := 0; i < count; i++ {
  44. txBytes := make([]byte, 20)
  45. txs[i] = txBytes
  46. rand.Read(txBytes)
  47. err := mempool.CheckTx(txBytes, nil)
  48. if err != nil {
  49. t.Fatal("Error after CheckTx: %v", err)
  50. }
  51. }
  52. return txs
  53. }
  54. func TestTxsAvailable(t *testing.T) {
  55. app := dummy.NewDummyApplication()
  56. cc := proxy.NewLocalClientCreator(app)
  57. mempool := newMempoolWithApp(t, cc)
  58. mempool.EnableTxsAvailable()
  59. timeoutMS := 500
  60. // with no txs, it shouldnt fire
  61. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  62. // send a bunch of txs, it should only fire once
  63. txs := sendTxs(t, mempool, 100)
  64. ensureFire(t, mempool.TxsAvailable(), timeoutMS)
  65. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  66. // call update with half the txs.
  67. // it should fire once now for the new height
  68. // since there are still txs left
  69. committedTxs, txs := txs[:50], txs[50:]
  70. mempool.Update(1, committedTxs)
  71. ensureFire(t, mempool.TxsAvailable(), timeoutMS)
  72. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  73. // send a bunch more txs. we already fired for this height so it shouldnt fire again
  74. moreTxs := sendTxs(t, mempool, 50)
  75. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  76. // now call update with all the txs. it should not fire as there are no txs left
  77. committedTxs = append(txs, moreTxs...)
  78. mempool.Update(2, committedTxs)
  79. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  80. // send a bunch more txs, it should only fire once
  81. sendTxs(t, mempool, 100)
  82. ensureFire(t, mempool.TxsAvailable(), timeoutMS)
  83. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  84. }
  85. func TestSerialReap(t *testing.T) {
  86. app := counter.NewCounterApplication(true)
  87. app.SetOption("serial", "on")
  88. cc := proxy.NewLocalClientCreator(app)
  89. mempool := newMempoolWithApp(t, cc)
  90. appConnCon, _ := cc.NewABCIClient()
  91. appConnCon.SetLogger(log.TestingLogger().With("module", "abci-client", "connection", "consensus"))
  92. if _, err := appConnCon.Start(); err != nil {
  93. t.Fatalf("Error starting ABCI client: %v", err.Error())
  94. }
  95. deliverTxsRange := func(start, end int) {
  96. // Deliver some txs.
  97. for i := start; i < end; i++ {
  98. // This will succeed
  99. txBytes := make([]byte, 8)
  100. binary.BigEndian.PutUint64(txBytes, uint64(i))
  101. err := mempool.CheckTx(txBytes, nil)
  102. if err != nil {
  103. t.Fatal("Error after CheckTx: %v", err)
  104. }
  105. // This will fail because not serial (incrementing)
  106. // However, error should still be nil.
  107. // It just won't show up on Reap().
  108. err = mempool.CheckTx(txBytes, nil)
  109. if err != nil {
  110. t.Fatal("Error after CheckTx: %v", err)
  111. }
  112. }
  113. }
  114. reapCheck := func(exp int) {
  115. txs := mempool.Reap(-1)
  116. if len(txs) != exp {
  117. t.Fatalf("Expected to reap %v txs but got %v", exp, len(txs))
  118. }
  119. }
  120. updateRange := func(start, end int) {
  121. txs := make([]types.Tx, 0)
  122. for i := start; i < end; i++ {
  123. txBytes := make([]byte, 8)
  124. binary.BigEndian.PutUint64(txBytes, uint64(i))
  125. txs = append(txs, txBytes)
  126. }
  127. mempool.Update(0, txs)
  128. }
  129. commitRange := func(start, end int) {
  130. // Deliver some txs.
  131. for i := start; i < end; i++ {
  132. txBytes := make([]byte, 8)
  133. binary.BigEndian.PutUint64(txBytes, uint64(i))
  134. res := appConnCon.DeliverTxSync(txBytes)
  135. if !res.IsOK() {
  136. t.Errorf("Error committing tx. Code:%v result:%X log:%v",
  137. res.Code, res.Data, res.Log)
  138. }
  139. }
  140. res := appConnCon.CommitSync()
  141. if len(res.Data) != 8 {
  142. t.Errorf("Error committing. Hash:%X log:%v", res.Data, res.Log)
  143. }
  144. }
  145. //----------------------------------------
  146. // Deliver some txs.
  147. deliverTxsRange(0, 100)
  148. // Reap the txs.
  149. reapCheck(100)
  150. // Reap again. We should get the same amount
  151. reapCheck(100)
  152. // Deliver 0 to 999, we should reap 900 new txs
  153. // because 100 were already counted.
  154. deliverTxsRange(0, 1000)
  155. // Reap the txs.
  156. reapCheck(1000)
  157. // Reap again. We should get the same amount
  158. reapCheck(1000)
  159. // Commit from the conensus AppConn
  160. commitRange(0, 500)
  161. updateRange(0, 500)
  162. // We should have 500 left.
  163. reapCheck(500)
  164. // Deliver 100 invalid txs and 100 valid txs
  165. deliverTxsRange(900, 1100)
  166. // We should have 600 now.
  167. reapCheck(600)
  168. }