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.

348 lines
9.6 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. ResultFinalizeBlock: abci.ResponseFinalizeBlock{
  30. Events: []abci.Event{
  31. {
  32. Type: "finalize_eventA",
  33. Attributes: []abci.EventAttribute{
  34. {
  35. Key: "proposer",
  36. Value: "FCAA001",
  37. Index: true,
  38. },
  39. },
  40. },
  41. {
  42. Type: "finalize_eventB",
  43. Attributes: []abci.EventAttribute{
  44. {
  45. Key: "foo",
  46. Value: "100",
  47. Index: true,
  48. },
  49. },
  50. },
  51. },
  52. },
  53. }))
  54. b, e := indexer.HasBlock(1)
  55. assert.Nil(t, e)
  56. assert.True(t, b)
  57. for i := 2; i < 12; i++ {
  58. var index bool
  59. if i%2 == 0 {
  60. index = true
  61. }
  62. require.NoError(t, indexer.IndexBlockEvents(types.EventDataNewBlockHeader{
  63. Header: types.Header{Height: int64(i)},
  64. ResultFinalizeBlock: abci.ResponseFinalizeBlock{
  65. Events: []abci.Event{
  66. {
  67. Type: "finalize_eventA",
  68. Attributes: []abci.EventAttribute{
  69. {
  70. Key: "proposer",
  71. Value: "FCAA001",
  72. Index: true,
  73. },
  74. },
  75. },
  76. {
  77. Type: "finalize_eventB",
  78. Attributes: []abci.EventAttribute{
  79. {
  80. Key: "foo",
  81. Value: fmt.Sprintf("%d", i),
  82. Index: index,
  83. },
  84. },
  85. },
  86. },
  87. },
  88. }))
  89. }
  90. testCases := map[string]struct {
  91. q *query.Query
  92. results []int64
  93. }{
  94. "block.height = 100": {
  95. q: query.MustCompile(`block.height = 100`),
  96. results: []int64{},
  97. },
  98. "block.height = 5": {
  99. q: query.MustCompile(`block.height = 5`),
  100. results: []int64{5},
  101. },
  102. "finalize_eventA.key1 = 'value1'": {
  103. q: query.MustCompile(`finalize_eventA.key1 = 'value1'`),
  104. results: []int64{},
  105. },
  106. "finalize_eventA.proposer = 'FCAA001'": {
  107. q: query.MustCompile(`finalize_eventA.proposer = 'FCAA001'`),
  108. results: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
  109. },
  110. "finalize_eventB.foo <= 5": {
  111. q: query.MustCompile(`finalize_eventB.foo <= 5`),
  112. results: []int64{2, 4},
  113. },
  114. "finalize_eventB.foo >= 100": {
  115. q: query.MustCompile(`finalize_eventB.foo >= 100`),
  116. results: []int64{1},
  117. },
  118. "block.height > 2 AND finalize_eventB.foo <= 8": {
  119. q: query.MustCompile(`block.height > 2 AND finalize_eventB.foo <= 8`),
  120. results: []int64{4, 6, 8},
  121. },
  122. "finalize_eventA.proposer CONTAINS 'FFFFFFF'": {
  123. q: query.MustCompile(`finalize_eventA.proposer CONTAINS 'FFFFFFF'`),
  124. results: []int64{},
  125. },
  126. "finalize_eventA.proposer CONTAINS 'FCAA001'": {
  127. q: query.MustCompile(`finalize_eventA.proposer CONTAINS 'FCAA001'`),
  128. results: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
  129. },
  130. }
  131. for name, tc := range testCases {
  132. tc := tc
  133. t.Run(name, func(t *testing.T) {
  134. ctx, cancel := context.WithCancel(context.Background())
  135. defer cancel()
  136. results, err := indexer.SearchBlockEvents(ctx, tc.q)
  137. require.NoError(t, err)
  138. require.Equal(t, tc.results, results)
  139. })
  140. }
  141. }
  142. func TestTxSearchWithCancelation(t *testing.T) {
  143. indexer := NewEventSink(dbm.NewMemDB())
  144. txResult := txResultWithEvents([]abci.Event{
  145. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "1", Index: true}}},
  146. {Type: "account", Attributes: []abci.EventAttribute{{Key: "owner", Value: "Ivan", Index: true}}},
  147. {Type: "", Attributes: []abci.EventAttribute{{Key: "not_allowed", Value: "Vlad", Index: true}}},
  148. })
  149. err := indexer.IndexTxEvents([]*abci.TxResult{txResult})
  150. require.NoError(t, err)
  151. r, e := indexer.GetTxByHash(types.Tx("HELLO WORLD").Hash())
  152. assert.Nil(t, e)
  153. assert.Equal(t, r, txResult)
  154. ctx, cancel := context.WithCancel(context.Background())
  155. cancel()
  156. results, err := indexer.SearchTxEvents(ctx, query.MustCompile(`account.number = 1`))
  157. assert.NoError(t, err)
  158. assert.Empty(t, results)
  159. }
  160. func TestTxSearchDeprecatedIndexing(t *testing.T) {
  161. esdb := dbm.NewMemDB()
  162. indexer := NewEventSink(esdb)
  163. // index tx using events indexing (composite key)
  164. txResult1 := txResultWithEvents([]abci.Event{
  165. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "1", Index: true}}},
  166. })
  167. hash1 := types.Tx(txResult1.Tx).Hash()
  168. err := indexer.IndexTxEvents([]*abci.TxResult{txResult1})
  169. require.NoError(t, err)
  170. // index tx also using deprecated indexing (event as key)
  171. txResult2 := txResultWithEvents(nil)
  172. txResult2.Tx = types.Tx("HELLO WORLD 2")
  173. hash2 := types.Tx(txResult2.Tx).Hash()
  174. b := esdb.NewBatch()
  175. rawBytes, err := proto.Marshal(txResult2)
  176. require.NoError(t, err)
  177. depKey := []byte(fmt.Sprintf("%s/%s/%d/%d",
  178. "sender",
  179. "addr1",
  180. txResult2.Height,
  181. txResult2.Index,
  182. ))
  183. err = b.Set(depKey, hash2)
  184. require.NoError(t, err)
  185. err = b.Set(kvtx.KeyFromHeight(txResult2), hash2)
  186. require.NoError(t, err)
  187. err = b.Set(hash2, rawBytes)
  188. require.NoError(t, err)
  189. err = b.Write()
  190. require.NoError(t, err)
  191. testCases := []struct {
  192. q string
  193. results []*abci.TxResult
  194. }{
  195. // search by hash
  196. {fmt.Sprintf("tx.hash = '%X'", hash1), []*abci.TxResult{txResult1}},
  197. // search by hash
  198. {fmt.Sprintf("tx.hash = '%X'", hash2), []*abci.TxResult{txResult2}},
  199. // search by exact match (one key)
  200. {"account.number = 1", []*abci.TxResult{txResult1}},
  201. {"account.number >= 1 AND account.number <= 5", []*abci.TxResult{txResult1}},
  202. // search by range (lower bound)
  203. {"account.number >= 1", []*abci.TxResult{txResult1}},
  204. // search by range (upper bound)
  205. {"account.number <= 5", []*abci.TxResult{txResult1}},
  206. // search using not allowed key
  207. {"not_allowed = 'boom'", []*abci.TxResult{}},
  208. // search for not existing tx result
  209. {"account.number >= 2 AND account.number <= 5", []*abci.TxResult{}},
  210. // search using not existing key
  211. {"account.date >= TIME 2013-05-03T14:45:00Z", []*abci.TxResult{}},
  212. // search by deprecated key
  213. {"sender = 'addr1'", []*abci.TxResult{txResult2}},
  214. }
  215. ctx := context.Background()
  216. for _, tc := range testCases {
  217. tc := tc
  218. t.Run(tc.q, func(t *testing.T) {
  219. results, err := indexer.SearchTxEvents(ctx, query.MustCompile(tc.q))
  220. require.NoError(t, err)
  221. for _, txr := range results {
  222. for _, tr := range tc.results {
  223. assert.True(t, proto.Equal(tr, txr))
  224. }
  225. }
  226. })
  227. }
  228. }
  229. func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) {
  230. indexer := NewEventSink(dbm.NewMemDB())
  231. txResult := txResultWithEvents([]abci.Event{
  232. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "1", Index: true}}},
  233. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "2", Index: true}}},
  234. })
  235. err := indexer.IndexTxEvents([]*abci.TxResult{txResult})
  236. require.NoError(t, err)
  237. ctx := context.Background()
  238. results, err := indexer.SearchTxEvents(ctx, query.MustCompile(`account.number >= 1`))
  239. assert.NoError(t, err)
  240. assert.Len(t, results, 1)
  241. for _, txr := range results {
  242. assert.True(t, proto.Equal(txResult, txr))
  243. }
  244. }
  245. func TestTxSearchMultipleTxs(t *testing.T) {
  246. indexer := NewEventSink(dbm.NewMemDB())
  247. // indexed first, but bigger height (to test the order of transactions)
  248. txResult := txResultWithEvents([]abci.Event{
  249. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "1", Index: true}}},
  250. })
  251. txResult.Tx = types.Tx("Bob's account")
  252. txResult.Height = 2
  253. txResult.Index = 1
  254. err := indexer.IndexTxEvents([]*abci.TxResult{txResult})
  255. require.NoError(t, err)
  256. // indexed second, but smaller height (to test the order of transactions)
  257. txResult2 := txResultWithEvents([]abci.Event{
  258. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "2", Index: true}}},
  259. })
  260. txResult2.Tx = types.Tx("Alice's account")
  261. txResult2.Height = 1
  262. txResult2.Index = 2
  263. err = indexer.IndexTxEvents([]*abci.TxResult{txResult2})
  264. require.NoError(t, err)
  265. // indexed third (to test the order of transactions)
  266. txResult3 := txResultWithEvents([]abci.Event{
  267. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number", Value: "3", Index: true}}},
  268. })
  269. txResult3.Tx = types.Tx("Jack's account")
  270. txResult3.Height = 1
  271. txResult3.Index = 1
  272. err = indexer.IndexTxEvents([]*abci.TxResult{txResult3})
  273. require.NoError(t, err)
  274. // indexed fourth (to test we don't include txs with similar events)
  275. // https://github.com/tendermint/tendermint/issues/2908
  276. txResult4 := txResultWithEvents([]abci.Event{
  277. {Type: "account", Attributes: []abci.EventAttribute{{Key: "number.id", Value: "1", Index: true}}},
  278. })
  279. txResult4.Tx = types.Tx("Mike's account")
  280. txResult4.Height = 2
  281. txResult4.Index = 2
  282. err = indexer.IndexTxEvents([]*abci.TxResult{txResult4})
  283. require.NoError(t, err)
  284. ctx := context.Background()
  285. results, err := indexer.SearchTxEvents(ctx, query.MustCompile(`account.number >= 1`))
  286. assert.NoError(t, err)
  287. require.Len(t, results, 3)
  288. }
  289. func txResultWithEvents(events []abci.Event) *abci.TxResult {
  290. tx := types.Tx("HELLO WORLD")
  291. return &abci.TxResult{
  292. Height: 1,
  293. Index: 0,
  294. Tx: tx,
  295. Result: abci.ResponseDeliverTx{
  296. Data: []byte{0},
  297. Code: abci.CodeTypeOK,
  298. Log: "",
  299. Events: events,
  300. },
  301. }
  302. }