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.

134 lines
2.4 KiB

  1. package v1
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/internal/mempool"
  8. )
  9. func TestTxStore_GetTxBySender(t *testing.T) {
  10. txs := NewTxStore()
  11. wtx := &WrappedTx{
  12. tx: []byte("test_tx"),
  13. sender: "foo",
  14. priority: 1,
  15. timestamp: time.Now(),
  16. }
  17. res := txs.GetTxBySender(wtx.sender)
  18. require.Nil(t, res)
  19. txs.SetTx(wtx)
  20. res = txs.GetTxBySender(wtx.sender)
  21. require.NotNil(t, res)
  22. require.Equal(t, wtx, res)
  23. }
  24. func TestTxStore_GetTxByHash(t *testing.T) {
  25. txs := NewTxStore()
  26. wtx := &WrappedTx{
  27. tx: []byte("test_tx"),
  28. sender: "foo",
  29. priority: 1,
  30. timestamp: time.Now(),
  31. }
  32. key := mempool.TxKey(wtx.tx)
  33. res := txs.GetTxByHash(key)
  34. require.Nil(t, res)
  35. txs.SetTx(wtx)
  36. res = txs.GetTxByHash(key)
  37. require.NotNil(t, res)
  38. require.Equal(t, wtx, res)
  39. }
  40. func TestTxStore_SetTx(t *testing.T) {
  41. txs := NewTxStore()
  42. wtx := &WrappedTx{
  43. tx: []byte("test_tx"),
  44. priority: 1,
  45. timestamp: time.Now(),
  46. }
  47. key := mempool.TxKey(wtx.tx)
  48. txs.SetTx(wtx)
  49. res := txs.GetTxByHash(key)
  50. require.NotNil(t, res)
  51. require.Equal(t, wtx, res)
  52. wtx.sender = "foo"
  53. txs.SetTx(wtx)
  54. res = txs.GetTxByHash(key)
  55. require.NotNil(t, res)
  56. require.Equal(t, wtx, res)
  57. }
  58. func TestTxStore_GetOrSetPeerByTxHash(t *testing.T) {
  59. txs := NewTxStore()
  60. wtx := &WrappedTx{
  61. tx: []byte("test_tx"),
  62. priority: 1,
  63. timestamp: time.Now(),
  64. }
  65. key := mempool.TxKey(wtx.tx)
  66. txs.SetTx(wtx)
  67. res, ok := txs.GetOrSetPeerByTxHash(mempool.TxKey([]byte("test_tx_2")), 15)
  68. require.Nil(t, res)
  69. require.False(t, ok)
  70. res, ok = txs.GetOrSetPeerByTxHash(key, 15)
  71. require.NotNil(t, res)
  72. require.False(t, ok)
  73. res, ok = txs.GetOrSetPeerByTxHash(key, 15)
  74. require.NotNil(t, res)
  75. require.True(t, ok)
  76. require.True(t, txs.TxHasPeer(key, 15))
  77. require.False(t, txs.TxHasPeer(key, 16))
  78. }
  79. func TestTxStore_RemoveTx(t *testing.T) {
  80. txs := NewTxStore()
  81. wtx := &WrappedTx{
  82. tx: []byte("test_tx"),
  83. priority: 1,
  84. timestamp: time.Now(),
  85. }
  86. txs.SetTx(wtx)
  87. key := mempool.TxKey(wtx.tx)
  88. res := txs.GetTxByHash(key)
  89. require.NotNil(t, res)
  90. txs.RemoveTx(res)
  91. res = txs.GetTxByHash(key)
  92. require.Nil(t, res)
  93. }
  94. func TestTxStore_Size(t *testing.T) {
  95. txStore := NewTxStore()
  96. numTxs := 1000
  97. for i := 0; i < numTxs; i++ {
  98. txStore.SetTx(&WrappedTx{
  99. tx: []byte(fmt.Sprintf("test_tx_%d", i)),
  100. priority: int64(i),
  101. timestamp: time.Now(),
  102. })
  103. }
  104. require.Equal(t, numTxs, txStore.Size())
  105. }