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.

287 lines
7.9 KiB

8 years ago
8 years ago
8 years ago
  1. package mempool
  2. import (
  3. "crypto/md5"
  4. "crypto/rand"
  5. "encoding/binary"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. "time"
  12. "github.com/tendermint/abci/example/counter"
  13. "github.com/tendermint/abci/example/dummy"
  14. abci "github.com/tendermint/abci/types"
  15. "github.com/tendermint/tmlibs/log"
  16. cfg "github.com/tendermint/tendermint/config"
  17. "github.com/tendermint/tendermint/proxy"
  18. "github.com/tendermint/tendermint/types"
  19. "github.com/stretchr/testify/require"
  20. )
  21. func newMempoolWithApp(cc proxy.ClientCreator) *Mempool {
  22. config := cfg.ResetTestRoot("mempool_test")
  23. appConnMem, _ := cc.NewABCIClient()
  24. appConnMem.SetLogger(log.TestingLogger().With("module", "abci-client", "connection", "mempool"))
  25. err := appConnMem.Start()
  26. if err != nil {
  27. panic(err)
  28. }
  29. mempool := NewMempool(config.Mempool, appConnMem, 0)
  30. mempool.SetLogger(log.TestingLogger())
  31. return mempool
  32. }
  33. func ensureNoFire(t *testing.T, ch <-chan int, timeoutMS int) {
  34. timer := time.NewTimer(time.Duration(timeoutMS) * time.Millisecond)
  35. select {
  36. case <-ch:
  37. t.Fatal("Expected not to fire")
  38. case <-timer.C:
  39. }
  40. }
  41. func ensureFire(t *testing.T, ch <-chan int, timeoutMS int) {
  42. timer := time.NewTimer(time.Duration(timeoutMS) * time.Millisecond)
  43. select {
  44. case <-ch:
  45. case <-timer.C:
  46. t.Fatal("Expected to fire")
  47. }
  48. }
  49. func checkTxs(t *testing.T, mempool *Mempool, count int) types.Txs {
  50. txs := make(types.Txs, count)
  51. for i := 0; i < count; i++ {
  52. txBytes := make([]byte, 20)
  53. txs[i] = txBytes
  54. _, err := rand.Read(txBytes)
  55. if err != nil {
  56. t.Error(err)
  57. }
  58. if err := mempool.CheckTx(txBytes, nil); err != nil {
  59. t.Fatalf("Error after CheckTx: %v", err)
  60. }
  61. }
  62. return txs
  63. }
  64. func TestTxsAvailable(t *testing.T) {
  65. app := dummy.NewDummyApplication()
  66. cc := proxy.NewLocalClientCreator(app)
  67. mempool := newMempoolWithApp(cc)
  68. mempool.EnableTxsAvailable()
  69. timeoutMS := 500
  70. // with no txs, it shouldnt fire
  71. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  72. // send a bunch of txs, it should only fire once
  73. txs := checkTxs(t, mempool, 100)
  74. ensureFire(t, mempool.TxsAvailable(), timeoutMS)
  75. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  76. // call update with half the txs.
  77. // it should fire once now for the new height
  78. // since there are still txs left
  79. committedTxs, txs := txs[:50], txs[50:]
  80. if err := mempool.Update(1, committedTxs); err != nil {
  81. t.Error(err)
  82. }
  83. ensureFire(t, mempool.TxsAvailable(), timeoutMS)
  84. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  85. // send a bunch more txs. we already fired for this height so it shouldnt fire again
  86. moreTxs := checkTxs(t, mempool, 50)
  87. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  88. // now call update with all the txs. it should not fire as there are no txs left
  89. committedTxs = append(txs, moreTxs...)
  90. if err := mempool.Update(2, committedTxs); err != nil {
  91. t.Error(err)
  92. }
  93. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  94. // send a bunch more txs, it should only fire once
  95. checkTxs(t, mempool, 100)
  96. ensureFire(t, mempool.TxsAvailable(), timeoutMS)
  97. ensureNoFire(t, mempool.TxsAvailable(), timeoutMS)
  98. }
  99. func TestSerialReap(t *testing.T) {
  100. app := counter.NewCounterApplication(true)
  101. app.SetOption(abci.RequestSetOption{"serial", "on"})
  102. cc := proxy.NewLocalClientCreator(app)
  103. mempool := newMempoolWithApp(cc)
  104. appConnCon, _ := cc.NewABCIClient()
  105. appConnCon.SetLogger(log.TestingLogger().With("module", "abci-client", "connection", "consensus"))
  106. if err := appConnCon.Start(); err != nil {
  107. t.Fatalf("Error starting ABCI client: %v", err.Error())
  108. }
  109. deliverTxsRange := func(start, end int) {
  110. // Deliver some txs.
  111. for i := start; i < end; i++ {
  112. // This will succeed
  113. txBytes := make([]byte, 8)
  114. binary.BigEndian.PutUint64(txBytes, uint64(i))
  115. err := mempool.CheckTx(txBytes, nil)
  116. if err != nil {
  117. t.Fatalf("Error after CheckTx: %v", err)
  118. }
  119. // This will fail because not serial (incrementing)
  120. // However, error should still be nil.
  121. // It just won't show up on Reap().
  122. err = mempool.CheckTx(txBytes, nil)
  123. if err != nil {
  124. t.Fatalf("Error after CheckTx: %v", err)
  125. }
  126. }
  127. }
  128. reapCheck := func(exp int) {
  129. txs := mempool.Reap(-1)
  130. if len(txs) != exp {
  131. t.Fatalf("Expected to reap %v txs but got %v", exp, len(txs))
  132. }
  133. }
  134. updateRange := func(start, end int) {
  135. txs := make([]types.Tx, 0)
  136. for i := start; i < end; i++ {
  137. txBytes := make([]byte, 8)
  138. binary.BigEndian.PutUint64(txBytes, uint64(i))
  139. txs = append(txs, txBytes)
  140. }
  141. if err := mempool.Update(0, txs); err != nil {
  142. t.Error(err)
  143. }
  144. }
  145. commitRange := func(start, end int) {
  146. // Deliver some txs.
  147. for i := start; i < end; i++ {
  148. txBytes := make([]byte, 8)
  149. binary.BigEndian.PutUint64(txBytes, uint64(i))
  150. res, err := appConnCon.DeliverTxSync(txBytes)
  151. if err != nil {
  152. t.Errorf("Client error committing tx: %v", err)
  153. }
  154. if res.IsErr() {
  155. t.Errorf("Error committing tx. Code:%v result:%X log:%v",
  156. res.Code, res.Data, res.Log)
  157. }
  158. }
  159. res, err := appConnCon.CommitSync()
  160. if err != nil {
  161. t.Errorf("Client error committing: %v", err)
  162. }
  163. if len(res.Data) != 8 {
  164. t.Errorf("Error committing. Hash:%X log:%v", res.Data, res.Log)
  165. }
  166. }
  167. //----------------------------------------
  168. // Deliver some txs.
  169. deliverTxsRange(0, 100)
  170. // Reap the txs.
  171. reapCheck(100)
  172. // Reap again. We should get the same amount
  173. reapCheck(100)
  174. // Deliver 0 to 999, we should reap 900 new txs
  175. // because 100 were already counted.
  176. deliverTxsRange(0, 1000)
  177. // Reap the txs.
  178. reapCheck(1000)
  179. // Reap again. We should get the same amount
  180. reapCheck(1000)
  181. // Commit from the conensus AppConn
  182. commitRange(0, 500)
  183. updateRange(0, 500)
  184. // We should have 500 left.
  185. reapCheck(500)
  186. // Deliver 100 invalid txs and 100 valid txs
  187. deliverTxsRange(900, 1100)
  188. // We should have 600 now.
  189. reapCheck(600)
  190. }
  191. func TestMempoolCloseWAL(t *testing.T) {
  192. // 1. Create the temporary directory for mempool and WAL testing.
  193. rootDir, err := ioutil.TempDir("", "mempool-test")
  194. require.Nil(t, err, "expecting successful tmpdir creation")
  195. defer os.RemoveAll(rootDir)
  196. // 2. Ensure that it doesn't contain any elements -- Sanity check
  197. m1, err := filepath.Glob(filepath.Join(rootDir, "*"))
  198. require.Nil(t, err, "successful globbing expected")
  199. require.Equal(t, 0, len(m1), "no matches yet")
  200. // 3. Create the mempool
  201. wcfg := *(cfg.DefaultMempoolConfig())
  202. wcfg.RootDir = rootDir
  203. app := dummy.NewDummyApplication()
  204. cc := proxy.NewLocalClientCreator(app)
  205. appConnMem, _ := cc.NewABCIClient()
  206. mempool := NewMempool(&wcfg, appConnMem, 10)
  207. // 4. Ensure that the directory contains the WAL file
  208. m2, err := filepath.Glob(filepath.Join(rootDir, "*"))
  209. require.Nil(t, err, "successful globbing expected")
  210. require.Equal(t, 1, len(m2), "expecting the wal match in")
  211. // 5. Write some contents to the WAL
  212. mempool.CheckTx(types.Tx([]byte("foo")), nil)
  213. walFilepath := mempool.wal.Path
  214. sum1 := checksumFile(walFilepath, t)
  215. // 6. Sanity check to ensure that the written TX matches the expectation.
  216. require.Equal(t, sum1, checksumIt([]byte("foo\n")), "foo with a newline should be written")
  217. // 7. Invoke CloseWAL() and ensure it discards the
  218. // WAL thus any other write won't go through.
  219. require.True(t, mempool.CloseWAL(), "CloseWAL should CloseWAL")
  220. mempool.CheckTx(types.Tx([]byte("bar")), nil)
  221. sum2 := checksumFile(walFilepath, t)
  222. require.Equal(t, sum1, sum2, "expected no change to the WAL after invoking CloseWAL() since it was discarded")
  223. // 8. Second CloseWAL should do nothing
  224. require.False(t, mempool.CloseWAL(), "CloseWAL should CloseWAL")
  225. // 9. Sanity check to ensure that the WAL file still exists
  226. m3, err := filepath.Glob(filepath.Join(rootDir, "*"))
  227. require.Nil(t, err, "successful globbing expected")
  228. require.Equal(t, 1, len(m3), "expecting the wal match in")
  229. }
  230. func checksumIt(data []byte) string {
  231. h := md5.New()
  232. h.Write(data)
  233. return fmt.Sprintf("%x", h.Sum(nil))
  234. }
  235. func checksumFile(p string, t *testing.T) string {
  236. data, err := ioutil.ReadFile(p)
  237. require.Nil(t, err, "expecting successful read of %q", p)
  238. return checksumIt(data)
  239. }