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.

148 lines
4.3 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. height := r.Height
  33. index := r.Index
  34. var proof types.TxProof
  35. if prove {
  36. block := env.BlockStore.LoadBlock(height)
  37. proof = block.Data.Txs.Proof(int(index)) // XXX: overflow on 32-bit machines
  38. }
  39. return &coretypes.ResultTx{
  40. Hash: hash,
  41. Height: height,
  42. Index: index,
  43. TxResult: r.Result,
  44. Tx: r.Tx,
  45. Proof: proof,
  46. }, nil
  47. }
  48. }
  49. return nil, fmt.Errorf("transaction querying is disabled on this node due to the KV event sink being disabled")
  50. }
  51. // TxSearch allows you to query for multiple transactions results. It returns a
  52. // list of transactions (maximum ?per_page entries) and the total count.
  53. // More: https://docs.tendermint.com/master/rpc/#/Info/tx_search
  54. func (env *Environment) TxSearch(
  55. ctx context.Context,
  56. query string,
  57. prove bool,
  58. pagePtr, perPagePtr *int,
  59. orderBy string,
  60. ) (*coretypes.ResultTxSearch, error) {
  61. if !indexer.KVSinkEnabled(env.EventSinks) {
  62. return nil, fmt.Errorf("transaction searching is disabled due to no kvEventSink")
  63. } else if len(query) > maxQueryLength {
  64. return nil, errors.New("maximum query length exceeded")
  65. }
  66. q, err := tmquery.New(query)
  67. if err != nil {
  68. return nil, err
  69. }
  70. for _, sink := range env.EventSinks {
  71. if sink.Type() == indexer.KV {
  72. results, err := sink.SearchTxEvents(ctx, q)
  73. if err != nil {
  74. return nil, err
  75. }
  76. // sort results (must be done before pagination)
  77. switch orderBy {
  78. case "desc", "":
  79. sort.Slice(results, func(i, j int) bool {
  80. if results[i].Height == results[j].Height {
  81. return results[i].Index > results[j].Index
  82. }
  83. return results[i].Height > results[j].Height
  84. })
  85. case "asc":
  86. sort.Slice(results, func(i, j int) bool {
  87. if results[i].Height == results[j].Height {
  88. return results[i].Index < results[j].Index
  89. }
  90. return results[i].Height < results[j].Height
  91. })
  92. default:
  93. return nil, fmt.Errorf("expected order_by to be either `asc` or `desc` or empty: %w", coretypes.ErrInvalidRequest)
  94. }
  95. // paginate results
  96. totalCount := len(results)
  97. perPage := env.validatePerPage(perPagePtr)
  98. page, err := validatePage(pagePtr, perPage, totalCount)
  99. if err != nil {
  100. return nil, err
  101. }
  102. skipCount := validateSkipCount(page, perPage)
  103. pageSize := tmmath.MinInt(perPage, totalCount-skipCount)
  104. apiResults := make([]*coretypes.ResultTx, 0, pageSize)
  105. for i := skipCount; i < skipCount+pageSize; i++ {
  106. r := results[i]
  107. var proof types.TxProof
  108. if prove {
  109. block := env.BlockStore.LoadBlock(r.Height)
  110. proof = block.Data.Txs.Proof(int(r.Index)) // XXX: overflow on 32-bit machines
  111. }
  112. apiResults = append(apiResults, &coretypes.ResultTx{
  113. Hash: types.Tx(r.Tx).Hash(),
  114. Height: r.Height,
  115. Index: r.Index,
  116. TxResult: r.Result,
  117. Tx: r.Tx,
  118. Proof: proof,
  119. })
  120. }
  121. return &coretypes.ResultTxSearch{Txs: apiResults, TotalCount: totalCount}, nil
  122. }
  123. }
  124. return nil, fmt.Errorf("transaction searching is disabled on this node due to the KV event sink being disabled")
  125. }