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.

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