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.

145 lines
4.2 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
7 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 core
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "sort"
  7. tmquery "github.com/tendermint/tendermint/internal/pubsub/query"
  8. "github.com/tendermint/tendermint/internal/state/indexer"
  9. "github.com/tendermint/tendermint/libs/bytes"
  10. tmmath "github.com/tendermint/tendermint/libs/math"
  11. "github.com/tendermint/tendermint/rpc/coretypes"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. // Tx allows you to query the transaction results. `nil` could mean the
  15. // transaction is in the mempool, invalidated, or was not sent in the first
  16. // place.
  17. // More: https://docs.tendermint.com/master/rpc/#/Info/tx
  18. func (env *Environment) Tx(ctx context.Context, hash bytes.HexBytes, prove bool) (*coretypes.ResultTx, error) {
  19. // if index is disabled, return error
  20. // N.B. The hash parameter is HexBytes so that the reflective parameter
  21. // decoding logic in the HTTP service will correctly translate from JSON.
  22. // See https://github.com/tendermint/tendermint/issues/6802 for context.
  23. if !indexer.KVSinkEnabled(env.EventSinks) {
  24. return nil, errors.New("transaction querying is disabled due to no kvEventSink")
  25. }
  26. for _, sink := range env.EventSinks {
  27. if sink.Type() == indexer.KV {
  28. r, err := sink.GetTxByHash(hash)
  29. if r == nil {
  30. return nil, fmt.Errorf("tx (%X) not found, err: %w", hash, err)
  31. }
  32. var proof types.TxProof
  33. if prove {
  34. block := env.BlockStore.LoadBlock(r.Height)
  35. proof = block.Data.Txs.Proof(int(r.Index))
  36. }
  37. return &coretypes.ResultTx{
  38. Hash: hash,
  39. Height: r.Height,
  40. Index: r.Index,
  41. TxResult: r.Result,
  42. Tx: r.Tx,
  43. Proof: proof,
  44. }, nil
  45. }
  46. }
  47. return nil, fmt.Errorf("transaction querying is disabled on this node due to the KV event sink being disabled")
  48. }
  49. // TxSearch allows you to query for multiple transactions results. It returns a
  50. // list of transactions (maximum ?per_page entries) and the total count.
  51. // More: https://docs.tendermint.com/master/rpc/#/Info/tx_search
  52. func (env *Environment) TxSearch(
  53. ctx context.Context,
  54. query string,
  55. prove bool,
  56. pagePtr, perPagePtr *int,
  57. orderBy string,
  58. ) (*coretypes.ResultTxSearch, error) {
  59. if !indexer.KVSinkEnabled(env.EventSinks) {
  60. return nil, fmt.Errorf("transaction searching is disabled due to no kvEventSink")
  61. } else if len(query) > maxQueryLength {
  62. return nil, errors.New("maximum query length exceeded")
  63. }
  64. q, err := tmquery.New(query)
  65. if err != nil {
  66. return nil, err
  67. }
  68. for _, sink := range env.EventSinks {
  69. if sink.Type() == indexer.KV {
  70. results, err := sink.SearchTxEvents(ctx, q)
  71. if err != nil {
  72. return nil, err
  73. }
  74. // sort results (must be done before pagination)
  75. switch orderBy {
  76. case "desc", "":
  77. sort.Slice(results, func(i, j int) bool {
  78. if results[i].Height == results[j].Height {
  79. return results[i].Index > results[j].Index
  80. }
  81. return results[i].Height > results[j].Height
  82. })
  83. case "asc":
  84. sort.Slice(results, func(i, j int) bool {
  85. if results[i].Height == results[j].Height {
  86. return results[i].Index < results[j].Index
  87. }
  88. return results[i].Height < results[j].Height
  89. })
  90. default:
  91. return nil, fmt.Errorf("expected order_by to be either `asc` or `desc` or empty: %w", coretypes.ErrInvalidRequest)
  92. }
  93. // paginate results
  94. totalCount := len(results)
  95. perPage := env.validatePerPage(perPagePtr)
  96. page, err := validatePage(pagePtr, perPage, totalCount)
  97. if err != nil {
  98. return nil, err
  99. }
  100. skipCount := validateSkipCount(page, perPage)
  101. pageSize := tmmath.MinInt(perPage, totalCount-skipCount)
  102. apiResults := make([]*coretypes.ResultTx, 0, pageSize)
  103. for i := skipCount; i < skipCount+pageSize; i++ {
  104. r := results[i]
  105. var proof types.TxProof
  106. if prove {
  107. block := env.BlockStore.LoadBlock(r.Height)
  108. proof = block.Data.Txs.Proof(int(r.Index))
  109. }
  110. apiResults = append(apiResults, &coretypes.ResultTx{
  111. Hash: types.Tx(r.Tx).Hash(),
  112. Height: r.Height,
  113. Index: r.Index,
  114. TxResult: r.Result,
  115. Tx: r.Tx,
  116. Proof: proof,
  117. })
  118. }
  119. return &coretypes.ResultTxSearch{Txs: apiResults, TotalCount: totalCount}, nil
  120. }
  121. }
  122. return nil, fmt.Errorf("transaction searching is disabled on this node due to the KV event sink being disabled")
  123. }