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.

418 lines
11 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
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
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
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. "fmt"
  4. cmn "github.com/tendermint/tendermint/libs/common"
  5. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  6. rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
  7. sm "github.com/tendermint/tendermint/state"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. // Get block headers for minHeight <= height <= maxHeight.
  11. // Block headers are returned in descending order (highest first).
  12. //
  13. // ```shell
  14. // curl 'localhost:26657/blockchain?minHeight=10&maxHeight=10'
  15. // ```
  16. //
  17. // ```go
  18. // client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
  19. // err := client.Start()
  20. // if err != nil {
  21. // // handle error
  22. // }
  23. // defer client.Stop()
  24. // info, err := client.BlockchainInfo(10, 10)
  25. // ```
  26. //
  27. // > The above command returns JSON structured like this:
  28. //
  29. // ```json
  30. // {
  31. // "error": "",
  32. // "result": {
  33. // "block_metas": [
  34. // {
  35. // "header": {
  36. // "app_hash": "",
  37. // "chain_id": "test-chain-6UTNIN",
  38. // "height": "10",
  39. // "time": "2017-05-29T15:05:53.877Z",
  40. // "num_txs": "0",
  41. // "last_block_id": {
  42. // "parts": {
  43. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  44. // "total": "1"
  45. // },
  46. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  47. // },
  48. // "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
  49. // "data_hash": "",
  50. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  51. // },
  52. // "block_id": {
  53. // "parts": {
  54. // "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
  55. // "total": "1"
  56. // },
  57. // "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
  58. // }
  59. // }
  60. // ],
  61. // "last_height": "5493"
  62. // },
  63. // "id": "",
  64. // "jsonrpc": "2.0"
  65. // }
  66. // ```
  67. //
  68. // <aside class="notice">Returns at most 20 items.</aside>
  69. func BlockchainInfo(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
  70. // maximum 20 block metas
  71. const limit int64 = 20
  72. var err error
  73. minHeight, maxHeight, err = filterMinMax(blockStore.Height(), minHeight, maxHeight, limit)
  74. if err != nil {
  75. return nil, err
  76. }
  77. logger.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight)
  78. blockMetas := []*types.BlockMeta{}
  79. for height := maxHeight; height >= minHeight; height-- {
  80. blockMeta := blockStore.LoadBlockMeta(height)
  81. blockMetas = append(blockMetas, blockMeta)
  82. }
  83. return &ctypes.ResultBlockchainInfo{
  84. LastHeight: blockStore.Height(),
  85. BlockMetas: blockMetas}, nil
  86. }
  87. // error if either min or max are negative or min < max
  88. // if 0, use 1 for min, latest block height for max
  89. // enforce limit.
  90. // error if min > max
  91. func filterMinMax(height, min, max, limit int64) (int64, int64, error) {
  92. // filter negatives
  93. if min < 0 || max < 0 {
  94. return min, max, fmt.Errorf("heights must be non-negative")
  95. }
  96. // adjust for default values
  97. if min == 0 {
  98. min = 1
  99. }
  100. if max == 0 {
  101. max = height
  102. }
  103. // limit max to the height
  104. max = cmn.MinInt64(height, max)
  105. // limit min to within `limit` of max
  106. // so the total number of blocks returned will be `limit`
  107. min = cmn.MaxInt64(min, max-limit+1)
  108. if min > max {
  109. return min, max, fmt.Errorf("min height %d can't be greater than max height %d", min, max)
  110. }
  111. return min, max, nil
  112. }
  113. // Get block at a given height.
  114. // If no height is provided, it will fetch the latest block.
  115. //
  116. // ```shell
  117. // curl 'localhost:26657/block?height=10'
  118. // ```
  119. //
  120. // ```go
  121. // client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
  122. // err := client.Start()
  123. // if err != nil {
  124. // // handle error
  125. // }
  126. // defer client.Stop()
  127. // info, err := client.Block(10)
  128. // ```
  129. //
  130. // > The above command returns JSON structured like this:
  131. //
  132. // ```json
  133. // {
  134. // "error": "",
  135. // "result": {
  136. // "block": {
  137. // "last_commit": {
  138. // "precommits": [
  139. // {
  140. // "signature": {
  141. // "data": "12C0D8893B8A38224488DC1DE6270DF76BB1A5E9DB1C68577706A6A97C6EC34FFD12339183D5CA8BC2F46148773823DE905B7F6F5862FD564038BB7AE03BF50D",
  142. // "type": "ed25519"
  143. // },
  144. // "block_id": {
  145. // "parts": {
  146. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  147. // "total": "1"
  148. // },
  149. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  150. // },
  151. // "type": "2",
  152. // "round": "0",
  153. // "height": "9",
  154. // "validator_index": "0",
  155. // "validator_address": "E89A51D60F68385E09E716D353373B11F8FACD62"
  156. // }
  157. // ],
  158. // "blockID": {
  159. // "parts": {
  160. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  161. // "total": "1"
  162. // },
  163. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  164. // }
  165. // },
  166. // "data": {
  167. // "txs": []
  168. // },
  169. // "header": {
  170. // "app_hash": "",
  171. // "chain_id": "test-chain-6UTNIN",
  172. // "height": "10",
  173. // "time": "2017-05-29T15:05:53.877Z",
  174. // "num_txs": "0",
  175. // "last_block_id": {
  176. // "parts": {
  177. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  178. // "total": "1"
  179. // },
  180. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  181. // },
  182. // "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
  183. // "data_hash": "",
  184. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  185. // }
  186. // },
  187. // "block_meta": {
  188. // "header": {
  189. // "app_hash": "",
  190. // "chain_id": "test-chain-6UTNIN",
  191. // "height": "10",
  192. // "time": "2017-05-29T15:05:53.877Z",
  193. // "num_txs": "0",
  194. // "last_block_id": {
  195. // "parts": {
  196. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  197. // "total": "1"
  198. // },
  199. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  200. // },
  201. // "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
  202. // "data_hash": "",
  203. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  204. // },
  205. // "block_id": {
  206. // "parts": {
  207. // "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
  208. // "total": "1"
  209. // },
  210. // "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
  211. // }
  212. // }
  213. // },
  214. // "id": "",
  215. // "jsonrpc": "2.0"
  216. // }
  217. // ```
  218. func Block(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlock, error) {
  219. storeHeight := blockStore.Height()
  220. height, err := getHeight(storeHeight, heightPtr)
  221. if err != nil {
  222. return nil, err
  223. }
  224. blockMeta := blockStore.LoadBlockMeta(height)
  225. block := blockStore.LoadBlock(height)
  226. return &ctypes.ResultBlock{BlockMeta: blockMeta, Block: block}, nil
  227. }
  228. // Get block commit at a given height.
  229. // If no height is provided, it will fetch the commit for the latest block.
  230. //
  231. // ```shell
  232. // curl 'localhost:26657/commit?height=11'
  233. // ```
  234. //
  235. // ```go
  236. // client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
  237. // err := client.Start()
  238. // if err != nil {
  239. // // handle error
  240. // }
  241. // defer client.Stop()
  242. // info, err := client.Commit(11)
  243. // ```
  244. //
  245. // > The above command returns JSON structured like this:
  246. //
  247. // ```json
  248. // {
  249. // "error": "",
  250. // "result": {
  251. // "canonical": true,
  252. // "commit": {
  253. // "precommits": [
  254. // {
  255. // "signature": {
  256. // "data": "00970429FEC652E9E21D106A90AE8C5413759A7488775CEF4A3F44DC46C7F9D941070E4FBE9ED54DF247FA3983359A0C3A238D61DE55C75C9116D72ABC9CF50F",
  257. // "type": "ed25519"
  258. // },
  259. // "block_id": {
  260. // "parts": {
  261. // "hash": "9E37CBF266BC044A779E09D81C456E653B89E006",
  262. // "total": "1"
  263. // },
  264. // "hash": "CC6E861E31CA4334E9888381B4A9137D1458AB6A"
  265. // },
  266. // "type": "2",
  267. // "round": "0",
  268. // "height": "11",
  269. // "validator_index": "0",
  270. // "validator_address": "E89A51D60F68385E09E716D353373B11F8FACD62"
  271. // }
  272. // ],
  273. // "blockID": {
  274. // "parts": {
  275. // "hash": "9E37CBF266BC044A779E09D81C456E653B89E006",
  276. // "total": "1"
  277. // },
  278. // "hash": "CC6E861E31CA4334E9888381B4A9137D1458AB6A"
  279. // }
  280. // },
  281. // "header": {
  282. // "app_hash": "",
  283. // "chain_id": "test-chain-6UTNIN",
  284. // "height": "11",
  285. // "time": "2017-05-29T15:05:54.893Z",
  286. // "num_txs": "0",
  287. // "last_block_id": {
  288. // "parts": {
  289. // "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
  290. // "total": "1"
  291. // },
  292. // "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
  293. // },
  294. // "last_commit_hash": "3CE0C9727CE524BA9CB7C91E28F08E2B94001087",
  295. // "data_hash": "",
  296. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  297. // }
  298. // },
  299. // "id": "",
  300. // "jsonrpc": "2.0"
  301. // }
  302. // ```
  303. func Commit(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultCommit, error) {
  304. storeHeight := blockStore.Height()
  305. height, err := getHeight(storeHeight, heightPtr)
  306. if err != nil {
  307. return nil, err
  308. }
  309. header := blockStore.LoadBlockMeta(height).Header
  310. // If the next block has not been committed yet,
  311. // use a non-canonical commit
  312. if height == storeHeight {
  313. commit := blockStore.LoadSeenCommit(height)
  314. return ctypes.NewResultCommit(&header, commit, false), nil
  315. }
  316. // Return the canonical commit (comes from the block at height+1)
  317. commit := blockStore.LoadBlockCommit(height)
  318. return ctypes.NewResultCommit(&header, commit, true), nil
  319. }
  320. // BlockResults gets ABCIResults at a given height.
  321. // If no height is provided, it will fetch results for the latest block.
  322. //
  323. // Results are for the height of the block containing the txs.
  324. // Thus response.results.deliver_tx[5] is the results of executing
  325. // getBlock(h).Txs[5]
  326. //
  327. // ```shell
  328. // curl 'localhost:26657/block_results?height=10'
  329. // ```
  330. //
  331. // ```go
  332. // client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
  333. // err := client.Start()
  334. // if err != nil {
  335. // // handle error
  336. // }
  337. // defer client.Stop()
  338. // info, err := client.BlockResults(10)
  339. // ```
  340. //
  341. //
  342. // > The above command returns JSON structured like this:
  343. //
  344. // ```json
  345. // {
  346. // "jsonrpc": "2.0",
  347. // "id": "",
  348. // "result": {
  349. // "height": "39",
  350. // "results": {
  351. // "deliver_tx": [
  352. // {
  353. // "tags": [
  354. // {
  355. // "key": "YXBwLmNyZWF0b3I=",
  356. // "value": "Q29zbW9zaGkgTmV0b3dva28="
  357. // }
  358. // ]
  359. // }
  360. // ],
  361. // "end_block": {
  362. // "validator_updates": null
  363. // },
  364. // "begin_block": {}
  365. // }
  366. // }
  367. // }
  368. // ```
  369. func BlockResults(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlockResults, error) {
  370. storeHeight := blockStore.Height()
  371. height, err := getHeight(storeHeight, heightPtr)
  372. if err != nil {
  373. return nil, err
  374. }
  375. results, err := sm.LoadABCIResponses(stateDB, height)
  376. if err != nil {
  377. return nil, err
  378. }
  379. res := &ctypes.ResultBlockResults{
  380. Height: height,
  381. Results: results,
  382. }
  383. return res, nil
  384. }
  385. func getHeight(currentHeight int64, heightPtr *int64) (int64, error) {
  386. if heightPtr != nil {
  387. height := *heightPtr
  388. if height <= 0 {
  389. return 0, fmt.Errorf("Height must be greater than 0")
  390. }
  391. if height > currentHeight {
  392. return 0, fmt.Errorf("Height must be less than or equal to the current blockchain height")
  393. }
  394. return height, nil
  395. }
  396. return currentHeight, nil
  397. }