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.

117 lines
2.7 KiB

  1. package mempool
  2. import (
  3. "encoding/binary"
  4. "testing"
  5. "github.com/tendermint/tendermint/proxy"
  6. "github.com/tendermint/tendermint/types"
  7. "github.com/tendermint/tmsp/example/golang"
  8. tmsp "github.com/tendermint/tmsp/types"
  9. )
  10. func TestSerialReap(t *testing.T) {
  11. app := example.NewCounterApplication(true)
  12. appCtxMempool := app.Open()
  13. proxyAppCtx := proxy.NewLocalAppContext(appCtxMempool)
  14. mempool := NewMempool(proxyAppCtx)
  15. // Create another AppContext for committing.
  16. appCtxConsensus := app.Open()
  17. appCtxConsensus.SetOption("serial", "on")
  18. appendTxsRange := func(start, end int) {
  19. // Append some txs.
  20. for i := start; i < end; i++ {
  21. // This will succeed
  22. txBytes := make([]byte, 32)
  23. binary.LittleEndian.PutUint64(txBytes, uint64(i))
  24. err := mempool.AppendTx(txBytes)
  25. if err != nil {
  26. t.Fatal("Error after AppendTx: %v", err)
  27. }
  28. // This will fail because not serial (incrementing)
  29. // However, error should still be nil.
  30. // It just won't show up on Reap().
  31. err = mempool.AppendTx(txBytes)
  32. if err != nil {
  33. t.Fatal("Error after AppendTx: %v", err)
  34. }
  35. }
  36. }
  37. reapCheck := func(exp int) {
  38. txs, _, err := mempool.Reap()
  39. if err != nil {
  40. t.Error("Error in mempool.Reap()", err)
  41. }
  42. if len(txs) != exp {
  43. t.Fatalf("Expected to reap %v txs but got %v", exp, len(txs))
  44. }
  45. }
  46. updateRange := func(start, end int) {
  47. txs := make([]types.Tx, 0)
  48. for i := start; i < end; i++ {
  49. txBytes := make([]byte, 32)
  50. binary.LittleEndian.PutUint64(txBytes, uint64(i))
  51. txs = append(txs, txBytes)
  52. }
  53. blockHeader := &types.Header{Height: 0}
  54. blockData := &types.Data{Txs: txs}
  55. block := &types.Block{Header: blockHeader, Data: blockData}
  56. err := mempool.Update(block)
  57. if err != nil {
  58. t.Error("Error in mempool.Update()", err)
  59. }
  60. }
  61. commitRange := func(start, end int) {
  62. // Append some txs.
  63. for i := start; i < end; i++ {
  64. txBytes := make([]byte, 32)
  65. binary.LittleEndian.PutUint64(txBytes, uint64(i))
  66. _, retCode := appCtxConsensus.AppendTx(txBytes)
  67. if retCode != tmsp.RetCodeOK {
  68. t.Error("Error committing tx", retCode)
  69. }
  70. }
  71. retCode := appCtxConsensus.Commit()
  72. if retCode != tmsp.RetCodeOK {
  73. t.Error("Error committing range", retCode)
  74. }
  75. }
  76. //----------------------------------------
  77. // Append some txs.
  78. appendTxsRange(0, 100)
  79. // Reap the txs.
  80. reapCheck(100)
  81. // Reap again. We should get the same amount
  82. reapCheck(100)
  83. // Append 0 to 999, we should reap 900 txs
  84. // because 100 were already counted.
  85. appendTxsRange(0, 1000)
  86. // Reap the txs.
  87. reapCheck(1000)
  88. // Reap again. We should get the same amount
  89. reapCheck(1000)
  90. // Commit from the conensus AppContext
  91. commitRange(0, 500)
  92. updateRange(0, 500)
  93. // We should have 500 left.
  94. reapCheck(500)
  95. }