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.

230 lines
4.4 KiB

  1. package mempool
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "sort"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. "github.com/tendermint/tendermint/types"
  10. )
  11. func TestTxStore_GetTxBySender(t *testing.T) {
  12. txs := NewTxStore()
  13. wtx := &WrappedTx{
  14. tx: []byte("test_tx"),
  15. sender: "foo",
  16. priority: 1,
  17. timestamp: time.Now(),
  18. }
  19. res := txs.GetTxBySender(wtx.sender)
  20. require.Nil(t, res)
  21. txs.SetTx(wtx)
  22. res = txs.GetTxBySender(wtx.sender)
  23. require.NotNil(t, res)
  24. require.Equal(t, wtx, res)
  25. }
  26. func TestTxStore_GetTxByHash(t *testing.T) {
  27. txs := NewTxStore()
  28. wtx := &WrappedTx{
  29. tx: []byte("test_tx"),
  30. sender: "foo",
  31. priority: 1,
  32. timestamp: time.Now(),
  33. }
  34. key := wtx.tx.Key()
  35. res := txs.GetTxByHash(key)
  36. require.Nil(t, res)
  37. txs.SetTx(wtx)
  38. res = txs.GetTxByHash(key)
  39. require.NotNil(t, res)
  40. require.Equal(t, wtx, res)
  41. }
  42. func TestTxStore_SetTx(t *testing.T) {
  43. txs := NewTxStore()
  44. wtx := &WrappedTx{
  45. tx: []byte("test_tx"),
  46. priority: 1,
  47. timestamp: time.Now(),
  48. }
  49. key := wtx.tx.Key()
  50. txs.SetTx(wtx)
  51. res := txs.GetTxByHash(key)
  52. require.NotNil(t, res)
  53. require.Equal(t, wtx, res)
  54. wtx.sender = "foo"
  55. txs.SetTx(wtx)
  56. res = txs.GetTxByHash(key)
  57. require.NotNil(t, res)
  58. require.Equal(t, wtx, res)
  59. }
  60. func TestTxStore_GetOrSetPeerByTxHash(t *testing.T) {
  61. txs := NewTxStore()
  62. wtx := &WrappedTx{
  63. tx: []byte("test_tx"),
  64. priority: 1,
  65. timestamp: time.Now(),
  66. }
  67. key := wtx.tx.Key()
  68. txs.SetTx(wtx)
  69. res, ok := txs.GetOrSetPeerByTxHash(types.Tx([]byte("test_tx_2")).Key(), 15)
  70. require.Nil(t, res)
  71. require.False(t, ok)
  72. res, ok = txs.GetOrSetPeerByTxHash(key, 15)
  73. require.NotNil(t, res)
  74. require.False(t, ok)
  75. res, ok = txs.GetOrSetPeerByTxHash(key, 15)
  76. require.NotNil(t, res)
  77. require.True(t, ok)
  78. require.True(t, txs.TxHasPeer(key, 15))
  79. require.False(t, txs.TxHasPeer(key, 16))
  80. }
  81. func TestTxStore_RemoveTx(t *testing.T) {
  82. txs := NewTxStore()
  83. wtx := &WrappedTx{
  84. tx: []byte("test_tx"),
  85. priority: 1,
  86. timestamp: time.Now(),
  87. }
  88. txs.SetTx(wtx)
  89. key := wtx.tx.Key()
  90. res := txs.GetTxByHash(key)
  91. require.NotNil(t, res)
  92. txs.RemoveTx(res)
  93. res = txs.GetTxByHash(key)
  94. require.Nil(t, res)
  95. }
  96. func TestTxStore_Size(t *testing.T) {
  97. txStore := NewTxStore()
  98. numTxs := 1000
  99. for i := 0; i < numTxs; i++ {
  100. txStore.SetTx(&WrappedTx{
  101. tx: []byte(fmt.Sprintf("test_tx_%d", i)),
  102. priority: int64(i),
  103. timestamp: time.Now(),
  104. })
  105. }
  106. require.Equal(t, numTxs, txStore.Size())
  107. }
  108. func TestWrappedTxList_Reset(t *testing.T) {
  109. list := NewWrappedTxList(func(wtx1, wtx2 *WrappedTx) bool {
  110. return wtx1.height >= wtx2.height
  111. })
  112. require.Zero(t, list.Size())
  113. for i := 0; i < 100; i++ {
  114. list.Insert(&WrappedTx{height: int64(i)})
  115. }
  116. require.Equal(t, 100, list.Size())
  117. list.Reset()
  118. require.Zero(t, list.Size())
  119. }
  120. func TestWrappedTxList_Insert(t *testing.T) {
  121. list := NewWrappedTxList(func(wtx1, wtx2 *WrappedTx) bool {
  122. return wtx1.height >= wtx2.height
  123. })
  124. rng := rand.New(rand.NewSource(time.Now().UnixNano()))
  125. var expected []int
  126. for i := 0; i < 100; i++ {
  127. height := rng.Int63n(10000)
  128. expected = append(expected, int(height))
  129. list.Insert(&WrappedTx{height: height})
  130. if i%10 == 0 {
  131. list.Insert(&WrappedTx{height: height})
  132. expected = append(expected, int(height))
  133. }
  134. }
  135. got := make([]int, list.Size())
  136. for i, wtx := range list.txs {
  137. got[i] = int(wtx.height)
  138. }
  139. sort.Ints(expected)
  140. require.Equal(t, expected, got)
  141. }
  142. func TestWrappedTxList_Remove(t *testing.T) {
  143. list := NewWrappedTxList(func(wtx1, wtx2 *WrappedTx) bool {
  144. return wtx1.height >= wtx2.height
  145. })
  146. rng := rand.New(rand.NewSource(time.Now().UnixNano()))
  147. var txs []*WrappedTx
  148. for i := 0; i < 100; i++ {
  149. height := rng.Int63n(10000)
  150. tx := &WrappedTx{height: height}
  151. txs = append(txs, tx)
  152. list.Insert(tx)
  153. if i%10 == 0 {
  154. tx = &WrappedTx{height: height}
  155. list.Insert(tx)
  156. txs = append(txs, tx)
  157. }
  158. }
  159. // remove a tx that does not exist
  160. list.Remove(&WrappedTx{height: 20000})
  161. // remove a tx that exists (by height) but not referenced
  162. list.Remove(&WrappedTx{height: txs[0].height})
  163. // remove a few existing txs
  164. for i := 0; i < 25; i++ {
  165. j := rng.Intn(len(txs))
  166. list.Remove(txs[j])
  167. txs = append(txs[:j], txs[j+1:]...)
  168. }
  169. expected := make([]int, len(txs))
  170. for i, tx := range txs {
  171. expected[i] = int(tx.height)
  172. }
  173. got := make([]int, list.Size())
  174. for i, wtx := range list.txs {
  175. got[i] = int(wtx.height)
  176. }
  177. sort.Ints(expected)
  178. require.Equal(t, expected, got)
  179. }