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.

355 lines
9.7 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
  1. package kv
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. dbm "github.com/tendermint/tm-db"
  7. "github.com/gogo/protobuf/proto"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. abci "github.com/tendermint/tendermint/abci/types"
  11. "github.com/tendermint/tendermint/internal/pubsub/query"
  12. "github.com/tendermint/tendermint/internal/state/indexer"
  13. kvtx "github.com/tendermint/tendermint/internal/state/indexer/tx/kv"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. func TestType(t *testing.T) {
  17. kvSink := NewEventSink(dbm.NewMemDB())
  18. assert.Equal(t, indexer.KV, kvSink.Type())
  19. }
  20. func TestStop(t *testing.T) {
  21. kvSink := NewEventSink(dbm.NewMemDB())
  22. assert.Nil(t, kvSink.Stop())
  23. }
  24. func TestBlockFuncs(t *testing.T) {
  25. store := dbm.NewPrefixDB(dbm.NewMemDB(), []byte("block_events"))
  26. indexer := NewEventSink(store)
  27. require.NoError(t, indexer.IndexBlockEvents(types.EventDataNewBlockHeader{
  28. Header: types.Header{Height: 1},
  29. ResultBeginBlock: abci.ResponseBeginBlock{
  30. Events: []abci.Event{
  31. {
  32. Type: "begin_event",
  33. Attributes: []abci.EventAttribute{
  34. {
  35. Key: "proposer",
  36. Value: "FCAA001",
  37. Index: true,
  38. },
  39. },
  40. },
  41. },
  42. },
  43. ResultEndBlock: abci.ResponseEndBlock{
  44. Events: []abci.Event{
  45. {
  46. Type: "end_event",
  47. Attributes: []abci.EventAttribute{
  48. {
  49. Key: "foo",
  50. Value: "100",
  51. Index: true,
  52. },
  53. },
  54. },
  55. },
  56. },
  57. }))
  58. b, e := indexer.HasBlock(1)
  59. assert.Nil(t, e)
  60. assert.True(t, b)
  61. for i := 2; i < 12; i++ {
  62. var index bool
  63. if i%2 == 0 {
  64. index = true
  65. }
  66. require.NoError(t, indexer.IndexBlockEvents(types.EventDataNewBlockHeader{
  67. Header: types.Header{Height: int64(i)},
  68. ResultBeginBlock: abci.ResponseBeginBlock{
  69. Events: []abci.Event{
  70. {
  71. Type: "begin_event",
  72. Attributes: []abci.EventAttribute{
  73. {
  74. Key: "proposer",
  75. Value: "FCAA001",
  76. Index: true,
  77. },
  78. },
  79. },
  80. },
  81. },
  82. ResultEndBlock: abci.ResponseEndBlock{
  83. Events: []abci.Event{
  84. {
  85. Type: "end_event",
  86. Attributes: []abci.EventAttribute{
  87. {
  88. Key: "foo",
  89. Value: fmt.Sprintf("%d", i),
  90. Index: index,
  91. },
  92. },
  93. },
  94. },
  95. },
  96. }))
  97. }
  98. testCases := map[string]struct {
  99. q *query.Query
  100. results []int64
  101. }{
  102. "block.height = 100": {
  103. q: query.MustCompile(`block.height = 100`),
  104. results: []int64{},
  105. },
  106. "block.height = 5": {
  107. q: query.MustCompile(`block.height = 5`),
  108. results: []int64{5},
  109. },
  110. "begin_event.key1 = 'value1'": {
  111. q: query.MustCompile(`begin_event.key1 = 'value1'`),
  112. results: []int64{},
  113. },
  114. "begin_event.proposer = 'FCAA001'": {
  115. q: query.MustCompile(`begin_event.proposer = 'FCAA001'`),
  116. results: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
  117. },
  118. "end_event.foo <= 5": {
  119. q: query.MustCompile(`end_event.foo <= 5`),
  120. results: []int64{2, 4},
  121. },
  122. "end_event.foo >= 100": {
  123. q: query.MustCompile(`end_event.foo >= 100`),
  124. results: []int64{1},
  125. },
  126. "block.height > 2 AND end_event.foo <= 8": {
  127. q: query.MustCompile(`block.height > 2 AND end_event.foo <= 8`),
  128. results: []int64{4, 6, 8},
  129. },
  130. "begin_event.proposer CONTAINS 'FFFFFFF'": {
  131. q: query.MustCompile(`begin_event.proposer CONTAINS 'FFFFFFF'`),
  132. results: []int64{},
  133. },
  134. "begin_event.proposer CONTAINS 'FCAA001'": {
  135. q: query.MustCompile(`begin_event.proposer CONTAINS 'FCAA001'`),
  136. results: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
  137. },
  138. }
  139. for name, tc := range testCases {
  140. tc := tc
  141. t.Run(name, func(t *testing.T) {
  142. ctx, cancel := context.WithCancel(context.Background())
  143. defer cancel()
  144. results, err := indexer.SearchBlockEvents(ctx, tc.q)
  145. require.NoError(t, err)
  146. require.Equal(t, tc.results, results)
  147. })
  148. }
  149. }
  150. func TestTxSearchWithCancelation(t *testing.T) {
  151. indexer := NewEventSink(dbm.NewMemDB())
  152. txResult := txResultWithEvents([]abci.Event{
  153. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "1", Index: true}}},
  154. {Type: "account", Attributes: []abci.EventAttribute{{Key: "owner", Value: "Ivan", Index: true}}},
  155. {Type: "", Attributes: []abci.EventAttribute{{Key: "not_allowed", Value: "Vlad", Index: true}}},
  156. })
  157. err := indexer.IndexTxEvents([]*abci.TxResult{txResult})
  158. require.NoError(t, err)
  159. r, e := indexer.GetTxByHash(types.Tx("HELLO WORLD").Hash())
  160. assert.Nil(t, e)
  161. assert.Equal(t, r, txResult)
  162. ctx, cancel := context.WithCancel(context.Background())
  163. cancel()
  164. results, err := indexer.SearchTxEvents(ctx, query.MustCompile(`account.number = 1`))
  165. assert.NoError(t, err)
  166. assert.Empty(t, results)
  167. }
  168. func TestTxSearchDeprecatedIndexing(t *testing.T) {
  169. esdb := dbm.NewMemDB()
  170. indexer := NewEventSink(esdb)
  171. // index tx using events indexing (composite key)
  172. txResult1 := txResultWithEvents([]abci.Event{
  173. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "1", Index: true}}},
  174. })
  175. hash1 := types.Tx(txResult1.Tx).Hash()
  176. err := indexer.IndexTxEvents([]*abci.TxResult{txResult1})
  177. require.NoError(t, err)
  178. // index tx also using deprecated indexing (event as key)
  179. txResult2 := txResultWithEvents(nil)
  180. txResult2.Tx = types.Tx("HELLO WORLD 2")
  181. hash2 := types.Tx(txResult2.Tx).Hash()
  182. b := esdb.NewBatch()
  183. rawBytes, err := proto.Marshal(txResult2)
  184. require.NoError(t, err)
  185. depKey := []byte(fmt.Sprintf("%s/%s/%d/%d",
  186. "sender",
  187. "addr1",
  188. txResult2.Height,
  189. txResult2.Index,
  190. ))
  191. err = b.Set(depKey, hash2)
  192. require.NoError(t, err)
  193. err = b.Set(kvtx.KeyFromHeight(txResult2), hash2)
  194. require.NoError(t, err)
  195. err = b.Set(hash2, rawBytes)
  196. require.NoError(t, err)
  197. err = b.Write()
  198. require.NoError(t, err)
  199. testCases := []struct {
  200. q string
  201. results []*abci.TxResult
  202. }{
  203. // search by hash
  204. {fmt.Sprintf("tx.hash = '%X'", hash1), []*abci.TxResult{txResult1}},
  205. // search by hash
  206. {fmt.Sprintf("tx.hash = '%X'", hash2), []*abci.TxResult{txResult2}},
  207. // search by exact match (one key)
  208. {"account.number = 1", []*abci.TxResult{txResult1}},
  209. {"account.number >= 1 AND account.number <= 5", []*abci.TxResult{txResult1}},
  210. // search by range (lower bound)
  211. {"account.number >= 1", []*abci.TxResult{txResult1}},
  212. // search by range (upper bound)
  213. {"account.number <= 5", []*abci.TxResult{txResult1}},
  214. // search using not allowed key
  215. {"not_allowed = 'boom'", []*abci.TxResult{}},
  216. // search for not existing tx result
  217. {"account.number >= 2 AND account.number <= 5", []*abci.TxResult{}},
  218. // search using not existing key
  219. {"account.date >= TIME 2013-05-03T14:45:00Z", []*abci.TxResult{}},
  220. // search by deprecated key
  221. {"sender = 'addr1'", []*abci.TxResult{txResult2}},
  222. }
  223. ctx := context.Background()
  224. for _, tc := range testCases {
  225. tc := tc
  226. t.Run(tc.q, func(t *testing.T) {
  227. results, err := indexer.SearchTxEvents(ctx, query.MustCompile(tc.q))
  228. require.NoError(t, err)
  229. for _, txr := range results {
  230. for _, tr := range tc.results {
  231. assert.True(t, proto.Equal(tr, txr))
  232. }
  233. }
  234. })
  235. }
  236. }
  237. func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) {
  238. indexer := NewEventSink(dbm.NewMemDB())
  239. txResult := txResultWithEvents([]abci.Event{
  240. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "1", Index: true}}},
  241. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "2", Index: true}}},
  242. })
  243. err := indexer.IndexTxEvents([]*abci.TxResult{txResult})
  244. require.NoError(t, err)
  245. ctx := context.Background()
  246. results, err := indexer.SearchTxEvents(ctx, query.MustCompile(`account.number >= 1`))
  247. assert.NoError(t, err)
  248. assert.Len(t, results, 1)
  249. for _, txr := range results {
  250. assert.True(t, proto.Equal(txResult, txr))
  251. }
  252. }
  253. func TestTxSearchMultipleTxs(t *testing.T) {
  254. indexer := NewEventSink(dbm.NewMemDB())
  255. // indexed first, but bigger height (to test the order of transactions)
  256. txResult := txResultWithEvents([]abci.Event{
  257. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "1", Index: true}}},
  258. })
  259. txResult.Tx = types.Tx("Bob's account")
  260. txResult.Height = 2
  261. txResult.Index = 1
  262. err := indexer.IndexTxEvents([]*abci.TxResult{txResult})
  263. require.NoError(t, err)
  264. // indexed second, but smaller height (to test the order of transactions)
  265. txResult2 := txResultWithEvents([]abci.Event{
  266. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "2", Index: true}}},
  267. })
  268. txResult2.Tx = types.Tx("Alice's account")
  269. txResult2.Height = 1
  270. txResult2.Index = 2
  271. err = indexer.IndexTxEvents([]*abci.TxResult{txResult2})
  272. require.NoError(t, err)
  273. // indexed third (to test the order of transactions)
  274. txResult3 := txResultWithEvents([]abci.Event{
  275. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "3", Index: true}}},
  276. })
  277. txResult3.Tx = types.Tx("Jack's account")
  278. txResult3.Height = 1
  279. txResult3.Index = 1
  280. err = indexer.IndexTxEvents([]*abci.TxResult{txResult3})
  281. require.NoError(t, err)
  282. // indexed fourth (to test we don't include txs with similar events)
  283. // https://github.com/tendermint/tendermint/issues/2908
  284. txResult4 := txResultWithEvents([]abci.Event{
  285. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number.id", Value: "1", Index: true}}},
  286. })
  287. txResult4.Tx = types.Tx("Mike's account")
  288. txResult4.Height = 2
  289. txResult4.Index = 2
  290. err = indexer.IndexTxEvents([]*abci.TxResult{txResult4})
  291. require.NoError(t, err)
  292. ctx := context.Background()
  293. results, err := indexer.SearchTxEvents(ctx, query.MustCompile(`account.number >= 1`))
  294. assert.NoError(t, err)
  295. require.Len(t, results, 3)
  296. }
  297. func txResultWithEvents(events []abci.Event) *abci.TxResult {
  298. tx := types.Tx("HELLO WORLD")
  299. return &abci.TxResult{
  300. Height: 1,
  301. Index: 0,
  302. Tx: tx,
  303. Result: abci.ResponseDeliverTx{
  304. Data: []byte{0},
  305. Code: abci.CodeTypeOK,
  306. Log: "",
  307. Events: events,
  308. },
  309. }
  310. }