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.

235 lines
7.1 KiB

8 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
add support for block pruning via ABCI Commit response (#4588) * Added BlockStore.DeleteBlock() * Added initial block pruner prototype * wip * Added BlockStore.PruneBlocks() * Added consensus setting for block pruning * Added BlockStore base * Error on replay if base does not have blocks * Handle missing blocks when sending VoteSetMaj23Message * Error message tweak * Properly update blockstore state * Error message fix again * blockchain: ignore peer missing blocks * Added FIXME * Added test for block replay with truncated history * Handle peer base in blockchain reactor * Improved replay error handling * Added tests for Store.PruneBlocks() * Fix non-RPC handling of truncated block history * Panic on missing block meta in needProofBlock() * Updated changelog * Handle truncated block history in RPC layer * Added info about earliest block in /status RPC * Reorder height and base in blockchain reactor messages * Updated changelog * Fix tests * Appease linter * Minor review fixes * Non-empty BlockStores should always have base > 0 * Update code to assume base > 0 invariant * Added blockstore tests for pruning to 0 * Make sure we don't prune below the current base * Added BlockStore.Size() * config: added retain_blocks recommendations * Update v1 blockchain reactor to handle blockstore base * Added state database pruning * Propagate errors on missing validator sets * Comment tweaks * Improved error message Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * use ABCI field ResponseCommit.retain_height instead of retain-blocks config option * remove State.RetainHeight, return value instead * fix minor issues * rename pruneHeights() to pruneBlocks() * noop to fix GitHub borkage Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
5 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
8 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
limit number of /subscribe clients and queries per client (#3269) * limit number of /subscribe clients and queries per client Add the following config variables (under [rpc] section): * max_subscription_clients * max_subscriptions_per_client * timeout_broadcast_tx_commit Fixes #2826 new HTTPClient interface for subscriptions finalize HTTPClient events interface remove EventSubscriber fix data race ``` WARNING: DATA RACE Read at 0x00c000a36060 by goroutine 129: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe.func1() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:168 +0x1f0 Previous write at 0x00c000a36060 by goroutine 132: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:191 +0x4e0 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 129 (running) created at: github.com/tendermint/tendermint/rpc/client.(*Local).Subscribe() /go/src/github.com/tendermint/tendermint/rpc/client/localclient.go:164 +0x4b7 github.com/tendermint/tendermint/rpc/client.WaitForOneEvent() /go/src/github.com/tendermint/tendermint/rpc/client/helpers.go:64 +0x178 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync.func1() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:139 +0x298 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 Goroutine 132 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:878 +0x659 github.com/tendermint/tendermint/rpc/client_test.TestTxEventsSentWithBroadcastTxSync() /go/src/github.com/tendermint/tendermint/rpc/client/event_test.go:119 +0x186 testing.tRunner() /usr/local/go/src/testing/testing.go:827 +0x162 ================== ``` lite client works (tested manually) godoc comments httpclient: do not close the out channel use TimeoutBroadcastTxCommit no timeout for unsubscribe but 1s Local (5s HTTP) timeout for resubscribe format code change Subscribe#out cap to 1 and replace config vars with RPCConfig TimeoutBroadcastTxCommit can't be greater than rpcserver.WriteTimeout rpc: Context as first parameter to all functions reformat code fixes after my own review fixes after Ethan's review add test stubs fix config.toml * fixes after manual testing - rpc: do not recommend to use BroadcastTxCommit because it's slow and wastes Tendermint resources (pubsub) - rpc: better error in Subscribe and BroadcastTxCommit - HTTPClient: do not resubscribe if err = ErrAlreadySubscribed * fixes after Ismail's review * Update rpc/grpc/grpc_test.go Co-Authored-By: melekes <anton.kalyaev@gmail.com>
6 years ago
  1. package core
  2. import (
  3. "errors"
  4. "fmt"
  5. "sort"
  6. tmmath "github.com/tendermint/tendermint/libs/math"
  7. tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
  8. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  9. rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  10. blockidxnull "github.com/tendermint/tendermint/state/indexer/block/null"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. // BlockchainInfo gets block headers for minHeight <= height <= maxHeight.
  14. //
  15. // If maxHeight does not yet exist, blocks up to the current height will be
  16. // returned. If minHeight does not exist (due to pruning), earliest existing
  17. // height will be used.
  18. //
  19. // At most 20 items will be returned. Block headers are returned in descending
  20. // order (highest first).
  21. //
  22. // More: https://docs.tendermint.com/master/rpc/#/Info/blockchain
  23. func BlockchainInfo(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
  24. const limit int64 = 20
  25. var err error
  26. minHeight, maxHeight, err = filterMinMax(
  27. env.BlockStore.Base(),
  28. env.BlockStore.Height(),
  29. minHeight,
  30. maxHeight,
  31. limit)
  32. if err != nil {
  33. return nil, err
  34. }
  35. env.Logger.Debug("BlockchainInfo", "maxHeight", maxHeight, "minHeight", minHeight)
  36. blockMetas := make([]*types.BlockMeta, 0, maxHeight-minHeight+1)
  37. for height := maxHeight; height >= minHeight; height-- {
  38. blockMeta := env.BlockStore.LoadBlockMeta(height)
  39. if blockMeta != nil {
  40. blockMetas = append(blockMetas, blockMeta)
  41. }
  42. }
  43. return &ctypes.ResultBlockchainInfo{
  44. LastHeight: env.BlockStore.Height(),
  45. BlockMetas: blockMetas}, nil
  46. }
  47. // error if either min or max are negative or min > max
  48. // if 0, use blockstore base for min, latest block height for max
  49. // enforce limit.
  50. func filterMinMax(base, height, min, max, limit int64) (int64, int64, error) {
  51. // filter negatives
  52. if min < 0 || max < 0 {
  53. return min, max, ctypes.ErrZeroOrNegativeHeight
  54. }
  55. // adjust for default values
  56. if min == 0 {
  57. min = 1
  58. }
  59. if max == 0 {
  60. max = height
  61. }
  62. // limit max to the height
  63. max = tmmath.MinInt64(height, max)
  64. // limit min to the base
  65. min = tmmath.MaxInt64(base, min)
  66. // limit min to within `limit` of max
  67. // so the total number of blocks returned will be `limit`
  68. min = tmmath.MaxInt64(min, max-limit+1)
  69. if min > max {
  70. return min, max, fmt.Errorf("%w: min height %d can't be greater than max height %d",
  71. ctypes.ErrInvalidRequest, min, max)
  72. }
  73. return min, max, nil
  74. }
  75. // Block gets block at a given height.
  76. // If no height is provided, it will fetch the latest block.
  77. // More: https://docs.tendermint.com/master/rpc/#/Info/block
  78. func Block(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlock, error) {
  79. height, err := getHeight(env.BlockStore.Height(), heightPtr)
  80. if err != nil {
  81. return nil, err
  82. }
  83. block := env.BlockStore.LoadBlock(height)
  84. blockMeta := env.BlockStore.LoadBlockMeta(height)
  85. if blockMeta == nil {
  86. return &ctypes.ResultBlock{BlockID: types.BlockID{}, Block: block}, nil
  87. }
  88. return &ctypes.ResultBlock{BlockID: blockMeta.BlockID, Block: block}, nil
  89. }
  90. // BlockByHash gets block by hash.
  91. // More: https://docs.tendermint.com/master/rpc/#/Info/block_by_hash
  92. func BlockByHash(ctx *rpctypes.Context, hash []byte) (*ctypes.ResultBlock, error) {
  93. block := env.BlockStore.LoadBlockByHash(hash)
  94. if block == nil {
  95. return &ctypes.ResultBlock{BlockID: types.BlockID{}, Block: nil}, nil
  96. }
  97. // If block is not nil, then blockMeta can't be nil.
  98. blockMeta := env.BlockStore.LoadBlockMeta(block.Height)
  99. return &ctypes.ResultBlock{BlockID: blockMeta.BlockID, Block: block}, nil
  100. }
  101. // Commit gets block commit at a given height.
  102. // If no height is provided, it will fetch the commit for the latest block.
  103. // More: https://docs.tendermint.com/master/rpc/#/Info/commit
  104. func Commit(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultCommit, error) {
  105. height, err := getHeight(env.BlockStore.Height(), heightPtr)
  106. if err != nil {
  107. return nil, err
  108. }
  109. blockMeta := env.BlockStore.LoadBlockMeta(height)
  110. if blockMeta == nil {
  111. return nil, nil
  112. }
  113. header := blockMeta.Header
  114. // If the next block has not been committed yet,
  115. // use a non-canonical commit
  116. if height == env.BlockStore.Height() {
  117. commit := env.BlockStore.LoadSeenCommit(height)
  118. return ctypes.NewResultCommit(&header, commit, false), nil
  119. }
  120. // Return the canonical commit (comes from the block at height+1)
  121. commit := env.BlockStore.LoadBlockCommit(height)
  122. return ctypes.NewResultCommit(&header, commit, true), nil
  123. }
  124. // BlockResults gets ABCIResults at a given height.
  125. // If no height is provided, it will fetch results for the latest block.
  126. //
  127. // Results are for the height of the block containing the txs.
  128. // Thus response.results.deliver_tx[5] is the results of executing
  129. // getBlock(h).Txs[5]
  130. // More: https://docs.tendermint.com/master/rpc/#/Info/block_results
  131. func BlockResults(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlockResults, error) {
  132. height, err := getHeight(env.BlockStore.Height(), heightPtr)
  133. if err != nil {
  134. return nil, err
  135. }
  136. results, err := env.StateStore.LoadABCIResponses(height)
  137. if err != nil {
  138. return nil, err
  139. }
  140. return &ctypes.ResultBlockResults{
  141. Height: height,
  142. TxsResults: results.DeliverTxs,
  143. BeginBlockEvents: results.BeginBlock.Events,
  144. EndBlockEvents: results.EndBlock.Events,
  145. ValidatorUpdates: results.EndBlock.ValidatorUpdates,
  146. ConsensusParamUpdates: results.EndBlock.ConsensusParamUpdates,
  147. }, nil
  148. }
  149. // BlockSearch searches for a paginated set of blocks matching BeginBlock and
  150. // EndBlock event search criteria.
  151. func BlockSearch(
  152. ctx *rpctypes.Context,
  153. query string,
  154. pagePtr, perPagePtr *int,
  155. orderBy string,
  156. ) (*ctypes.ResultBlockSearch, error) {
  157. // skip if block indexing is disabled
  158. if _, ok := env.BlockIndexer.(*blockidxnull.BlockerIndexer); ok {
  159. return nil, errors.New("block indexing is disabled")
  160. }
  161. q, err := tmquery.New(query)
  162. if err != nil {
  163. return nil, err
  164. }
  165. results, err := env.BlockIndexer.Search(ctx.Context(), q)
  166. if err != nil {
  167. return nil, err
  168. }
  169. // sort results (must be done before pagination)
  170. switch orderBy {
  171. case "desc", "":
  172. sort.Slice(results, func(i, j int) bool { return results[i] > results[j] })
  173. case "asc":
  174. sort.Slice(results, func(i, j int) bool { return results[i] < results[j] })
  175. default:
  176. return nil, fmt.Errorf("%w: expected order_by to be either `asc` or `desc` or empty", ctypes.ErrInvalidRequest)
  177. }
  178. // paginate results
  179. totalCount := len(results)
  180. perPage := validatePerPage(perPagePtr)
  181. page, err := validatePage(pagePtr, perPage, totalCount)
  182. if err != nil {
  183. return nil, err
  184. }
  185. skipCount := validateSkipCount(page, perPage)
  186. pageSize := tmmath.MinInt(perPage, totalCount-skipCount)
  187. apiResults := make([]*ctypes.ResultBlock, 0, pageSize)
  188. for i := skipCount; i < skipCount+pageSize; i++ {
  189. block := env.BlockStore.LoadBlock(results[i])
  190. if block != nil {
  191. blockMeta := env.BlockStore.LoadBlockMeta(block.Height)
  192. if blockMeta != nil {
  193. apiResults = append(apiResults, &ctypes.ResultBlock{
  194. Block: block,
  195. BlockID: blockMeta.BlockID,
  196. })
  197. }
  198. }
  199. }
  200. return &ctypes.ResultBlockSearch{Blocks: apiResults, TotalCount: totalCount}, nil
  201. }