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.

141 lines
3.3 KiB

  1. package kv_test
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. abci "github.com/tendermint/tendermint/abci/types"
  8. "github.com/tendermint/tendermint/libs/pubsub/query"
  9. blockidxkv "github.com/tendermint/tendermint/state/indexer/block/kv"
  10. "github.com/tendermint/tendermint/types"
  11. db "github.com/tendermint/tm-db"
  12. )
  13. func TestBlockIndexer(t *testing.T) {
  14. store := db.NewPrefixDB(db.NewMemDB(), []byte("block_events"))
  15. indexer := blockidxkv.New(store)
  16. require.NoError(t, indexer.Index(types.EventDataNewBlockHeader{
  17. Header: types.Header{Height: 1},
  18. ResultBeginBlock: abci.ResponseBeginBlock{
  19. Events: []abci.Event{
  20. {
  21. Type: "begin_event",
  22. Attributes: []abci.EventAttribute{
  23. {
  24. Key: []byte("proposer"),
  25. Value: []byte("FCAA001"),
  26. Index: true,
  27. },
  28. },
  29. },
  30. },
  31. },
  32. ResultEndBlock: abci.ResponseEndBlock{
  33. Events: []abci.Event{
  34. {
  35. Type: "end_event",
  36. Attributes: []abci.EventAttribute{
  37. {
  38. Key: []byte("foo"),
  39. Value: []byte("100"),
  40. Index: true,
  41. },
  42. },
  43. },
  44. },
  45. },
  46. }))
  47. for i := 2; i < 12; i++ {
  48. var index bool
  49. if i%2 == 0 {
  50. index = true
  51. }
  52. require.NoError(t, indexer.Index(types.EventDataNewBlockHeader{
  53. Header: types.Header{Height: int64(i)},
  54. ResultBeginBlock: abci.ResponseBeginBlock{
  55. Events: []abci.Event{
  56. {
  57. Type: "begin_event",
  58. Attributes: []abci.EventAttribute{
  59. {
  60. Key: []byte("proposer"),
  61. Value: []byte("FCAA001"),
  62. Index: true,
  63. },
  64. },
  65. },
  66. },
  67. },
  68. ResultEndBlock: abci.ResponseEndBlock{
  69. Events: []abci.Event{
  70. {
  71. Type: "end_event",
  72. Attributes: []abci.EventAttribute{
  73. {
  74. Key: []byte("foo"),
  75. Value: []byte(fmt.Sprintf("%d", i)),
  76. Index: index,
  77. },
  78. },
  79. },
  80. },
  81. },
  82. }))
  83. }
  84. testCases := map[string]struct {
  85. q *query.Query
  86. results []int64
  87. }{
  88. "block.height = 100": {
  89. q: query.MustParse("block.height = 100"),
  90. results: []int64{},
  91. },
  92. "block.height = 5": {
  93. q: query.MustParse("block.height = 5"),
  94. results: []int64{5},
  95. },
  96. "begin_event.key1 = 'value1'": {
  97. q: query.MustParse("begin_event.key1 = 'value1'"),
  98. results: []int64{},
  99. },
  100. "begin_event.proposer = 'FCAA001'": {
  101. q: query.MustParse("begin_event.proposer = 'FCAA001'"),
  102. results: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
  103. },
  104. "end_event.foo <= 5": {
  105. q: query.MustParse("end_event.foo <= 5"),
  106. results: []int64{2, 4},
  107. },
  108. "end_event.foo >= 100": {
  109. q: query.MustParse("end_event.foo >= 100"),
  110. results: []int64{1},
  111. },
  112. "block.height > 2 AND end_event.foo <= 8": {
  113. q: query.MustParse("block.height > 2 AND end_event.foo <= 8"),
  114. results: []int64{4, 6, 8},
  115. },
  116. "begin_event.proposer CONTAINS 'FFFFFFF'": {
  117. q: query.MustParse("begin_event.proposer CONTAINS 'FFFFFFF'"),
  118. results: []int64{},
  119. },
  120. "begin_event.proposer CONTAINS 'FCAA001'": {
  121. q: query.MustParse("begin_event.proposer CONTAINS 'FCAA001'"),
  122. results: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
  123. },
  124. }
  125. for name, tc := range testCases {
  126. tc := tc
  127. t.Run(name, func(t *testing.T) {
  128. results, err := indexer.Search(context.Background(), tc.q)
  129. require.NoError(t, err)
  130. require.Equal(t, tc.results, results)
  131. })
  132. }
  133. }