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.

252 lines
7.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package kv
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. abci "github.com/tendermint/tendermint/abci/types"
  10. cmn "github.com/tendermint/tendermint/libs/common"
  11. db "github.com/tendermint/tendermint/libs/db"
  12. "github.com/tendermint/tendermint/libs/pubsub/query"
  13. "github.com/tendermint/tendermint/state/txindex"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. func TestTxIndex(t *testing.T) {
  17. indexer := NewTxIndex(db.NewMemDB())
  18. tx := types.Tx("HELLO WORLD")
  19. txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: nil}}
  20. hash := tx.Hash()
  21. batch := txindex.NewBatch(1)
  22. if err := batch.Add(txResult); err != nil {
  23. t.Error(err)
  24. }
  25. err := indexer.AddBatch(batch)
  26. require.NoError(t, err)
  27. loadedTxResult, err := indexer.Get(hash)
  28. require.NoError(t, err)
  29. assert.Equal(t, txResult, loadedTxResult)
  30. tx2 := types.Tx("BYE BYE WORLD")
  31. txResult2 := &types.TxResult{1, 0, tx2, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: nil}}
  32. hash2 := tx2.Hash()
  33. err = indexer.Index(txResult2)
  34. require.NoError(t, err)
  35. loadedTxResult2, err := indexer.Get(hash2)
  36. require.NoError(t, err)
  37. assert.Equal(t, txResult2, loadedTxResult2)
  38. }
  39. func TestTxSearch(t *testing.T) {
  40. allowedTags := []string{"account.number", "account.owner", "account.date"}
  41. indexer := NewTxIndex(db.NewMemDB(), IndexTags(allowedTags))
  42. txResult := txResultWithTags([]cmn.KVPair{
  43. {Key: []byte("account.number"), Value: []byte("1")},
  44. {Key: []byte("account.owner"), Value: []byte("Ivan")},
  45. {Key: []byte("not_allowed"), Value: []byte("Vlad")},
  46. })
  47. hash := txResult.Tx.Hash()
  48. err := indexer.Index(txResult)
  49. require.NoError(t, err)
  50. testCases := []struct {
  51. q string
  52. resultsLength int
  53. }{
  54. // search by hash
  55. {fmt.Sprintf("tx.hash = '%X'", hash), 1},
  56. // search by exact match (one tag)
  57. {"account.number = 1", 1},
  58. // search by exact match (two tags)
  59. {"account.number = 1 AND account.owner = 'Ivan'", 1},
  60. // search by exact match (two tags)
  61. {"account.number = 1 AND account.owner = 'Vlad'", 0},
  62. // search using a prefix of the stored value
  63. {"account.owner = 'Iv'", 0},
  64. // search by range
  65. {"account.number >= 1 AND account.number <= 5", 1},
  66. // search by range (lower bound)
  67. {"account.number >= 1", 1},
  68. // search by range (upper bound)
  69. {"account.number <= 5", 1},
  70. // search using not allowed tag
  71. {"not_allowed = 'boom'", 0},
  72. // search for not existing tx result
  73. {"account.number >= 2 AND account.number <= 5", 0},
  74. // search using not existing tag
  75. {"account.date >= TIME 2013-05-03T14:45:00Z", 0},
  76. // search using CONTAINS
  77. {"account.owner CONTAINS 'an'", 1},
  78. // search using CONTAINS
  79. {"account.owner CONTAINS 'Vlad'", 0},
  80. }
  81. for _, tc := range testCases {
  82. t.Run(tc.q, func(t *testing.T) {
  83. results, err := indexer.Search(query.MustParse(tc.q))
  84. assert.NoError(t, err)
  85. assert.Len(t, results, tc.resultsLength)
  86. if tc.resultsLength > 0 {
  87. assert.Equal(t, []*types.TxResult{txResult}, results)
  88. }
  89. })
  90. }
  91. }
  92. func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) {
  93. allowedTags := []string{"account.number"}
  94. indexer := NewTxIndex(db.NewMemDB(), IndexTags(allowedTags))
  95. txResult := txResultWithTags([]cmn.KVPair{
  96. {Key: []byte("account.number"), Value: []byte("1")},
  97. {Key: []byte("account.number"), Value: []byte("2")},
  98. })
  99. err := indexer.Index(txResult)
  100. require.NoError(t, err)
  101. results, err := indexer.Search(query.MustParse("account.number >= 1"))
  102. assert.NoError(t, err)
  103. assert.Len(t, results, 1)
  104. assert.Equal(t, []*types.TxResult{txResult}, results)
  105. }
  106. func TestTxSearchMultipleTxs(t *testing.T) {
  107. allowedTags := []string{"account.number"}
  108. indexer := NewTxIndex(db.NewMemDB(), IndexTags(allowedTags))
  109. // indexed first, but bigger height (to test the order of transactions)
  110. txResult := txResultWithTags([]cmn.KVPair{
  111. {Key: []byte("account.number"), Value: []byte("1")},
  112. })
  113. txResult.Tx = types.Tx("Bob's account")
  114. txResult.Height = 2
  115. txResult.Index = 1
  116. err := indexer.Index(txResult)
  117. require.NoError(t, err)
  118. // indexed second, but smaller height (to test the order of transactions)
  119. txResult2 := txResultWithTags([]cmn.KVPair{
  120. {Key: []byte("account.number"), Value: []byte("2")},
  121. })
  122. txResult2.Tx = types.Tx("Alice's account")
  123. txResult2.Height = 1
  124. txResult2.Index = 2
  125. err = indexer.Index(txResult2)
  126. require.NoError(t, err)
  127. // indexed third (to test the order of transactions)
  128. txResult3 := txResultWithTags([]cmn.KVPair{
  129. {Key: []byte("account.number"), Value: []byte("3")},
  130. })
  131. txResult3.Tx = types.Tx("Jack's account")
  132. txResult3.Height = 1
  133. txResult3.Index = 1
  134. err = indexer.Index(txResult3)
  135. require.NoError(t, err)
  136. results, err := indexer.Search(query.MustParse("account.number >= 1"))
  137. assert.NoError(t, err)
  138. require.Len(t, results, 3)
  139. assert.Equal(t, []*types.TxResult{txResult3, txResult2, txResult}, results)
  140. }
  141. func TestIndexAllTags(t *testing.T) {
  142. indexer := NewTxIndex(db.NewMemDB(), IndexAllTags())
  143. txResult := txResultWithTags([]cmn.KVPair{
  144. cmn.KVPair{Key: []byte("account.owner"), Value: []byte("Ivan")},
  145. cmn.KVPair{Key: []byte("account.number"), Value: []byte("1")},
  146. })
  147. err := indexer.Index(txResult)
  148. require.NoError(t, err)
  149. results, err := indexer.Search(query.MustParse("account.number >= 1"))
  150. assert.NoError(t, err)
  151. assert.Len(t, results, 1)
  152. assert.Equal(t, []*types.TxResult{txResult}, results)
  153. results, err = indexer.Search(query.MustParse("account.owner = 'Ivan'"))
  154. assert.NoError(t, err)
  155. assert.Len(t, results, 1)
  156. assert.Equal(t, []*types.TxResult{txResult}, results)
  157. }
  158. func txResultWithTags(tags []cmn.KVPair) *types.TxResult {
  159. tx := types.Tx("HELLO WORLD")
  160. return &types.TxResult{
  161. Height: 1,
  162. Index: 0,
  163. Tx: tx,
  164. Result: abci.ResponseDeliverTx{
  165. Data: []byte{0},
  166. Code: abci.CodeTypeOK,
  167. Log: "",
  168. Tags: tags,
  169. },
  170. }
  171. }
  172. func benchmarkTxIndex(txsCount int64, b *testing.B) {
  173. dir, err := ioutil.TempDir("", "tx_index_db")
  174. if err != nil {
  175. b.Fatal(err)
  176. }
  177. defer os.RemoveAll(dir) // nolint: errcheck
  178. store := db.NewDB("tx_index", "leveldb", dir)
  179. indexer := NewTxIndex(store)
  180. batch := txindex.NewBatch(txsCount)
  181. txIndex := uint32(0)
  182. for i := int64(0); i < txsCount; i++ {
  183. tx := cmn.RandBytes(250)
  184. txResult := &types.TxResult{
  185. Height: 1,
  186. Index: txIndex,
  187. Tx: tx,
  188. Result: abci.ResponseDeliverTx{
  189. Data: []byte{0},
  190. Code: abci.CodeTypeOK,
  191. Log: "",
  192. Tags: []cmn.KVPair{},
  193. },
  194. }
  195. if err := batch.Add(txResult); err != nil {
  196. b.Fatal(err)
  197. }
  198. txIndex++
  199. }
  200. b.ResetTimer()
  201. for n := 0; n < b.N; n++ {
  202. err = indexer.AddBatch(batch)
  203. }
  204. if err != nil {
  205. b.Fatal(err)
  206. }
  207. }
  208. func BenchmarkTxIndex1(b *testing.B) { benchmarkTxIndex(1, b) }
  209. func BenchmarkTxIndex500(b *testing.B) { benchmarkTxIndex(500, b) }
  210. func BenchmarkTxIndex1000(b *testing.B) { benchmarkTxIndex(1000, b) }
  211. func BenchmarkTxIndex2000(b *testing.B) { benchmarkTxIndex(2000, b) }
  212. func BenchmarkTxIndex10000(b *testing.B) { benchmarkTxIndex(10000, b) }