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.

289 lines
8.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package core
  2. import (
  3. "fmt"
  4. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  5. "github.com/tendermint/tendermint/types"
  6. . "github.com/tendermint/tmlibs/common"
  7. )
  8. // Get block headers for minHeight <= height <= maxHeight.
  9. //
  10. // ```shell
  11. // curl 'localhost:46657/blockchain?minHeight=10&maxHeight=10'
  12. // ```
  13. //
  14. // ```go
  15. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  16. // info, err := client.BlockchainInfo(10, 10)
  17. // ```
  18. //
  19. // > The above command returns JSON structured like this:
  20. //
  21. // ```json
  22. // {
  23. // "error": "",
  24. // "result": {
  25. // "block_metas": [
  26. // {
  27. // "header": {
  28. // "app_hash": "",
  29. // "chain_id": "test-chain-6UTNIN",
  30. // "height": 10,
  31. // "time": "2017-05-29T15:05:53.877Z",
  32. // "num_txs": 0,
  33. // "last_block_id": {
  34. // "parts": {
  35. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  36. // "total": 1
  37. // },
  38. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  39. // },
  40. // "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
  41. // "data_hash": "",
  42. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  43. // },
  44. // "block_id": {
  45. // "parts": {
  46. // "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
  47. // "total": 1
  48. // },
  49. // "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
  50. // }
  51. // }
  52. // ],
  53. // "last_height": 5493
  54. // },
  55. // "id": "",
  56. // "jsonrpc": "2.0"
  57. // }
  58. // ```
  59. //
  60. // <aside class="notice">Returns at most 20 items.</aside>
  61. func BlockchainInfo(minHeight, maxHeight int) (*ctypes.ResultBlockchainInfo, error) {
  62. if maxHeight == 0 {
  63. maxHeight = blockStore.Height()
  64. } else {
  65. maxHeight = MinInt(blockStore.Height(), maxHeight)
  66. }
  67. if minHeight == 0 {
  68. minHeight = MaxInt(1, maxHeight-20)
  69. } else {
  70. minHeight = MaxInt(minHeight, maxHeight-20)
  71. }
  72. logger.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight)
  73. blockMetas := []*types.BlockMeta{}
  74. for height := maxHeight; height >= minHeight; height-- {
  75. blockMeta := blockStore.LoadBlockMeta(height)
  76. blockMetas = append(blockMetas, blockMeta)
  77. }
  78. return &ctypes.ResultBlockchainInfo{blockStore.Height(), blockMetas}, nil
  79. }
  80. // Get block at a given height.
  81. //
  82. // ```shell
  83. // curl 'localhost:46657/block?height=10'
  84. // ```
  85. //
  86. // ```go
  87. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  88. // info, err := client.Block(10)
  89. // ```
  90. //
  91. // > The above command returns JSON structured like this:
  92. //
  93. // ```json
  94. // {
  95. // "error": "",
  96. // "result": {
  97. // "block": {
  98. // "last_commit": {
  99. // "precommits": [
  100. // {
  101. // "signature": {
  102. // "data": "12C0D8893B8A38224488DC1DE6270DF76BB1A5E9DB1C68577706A6A97C6EC34FFD12339183D5CA8BC2F46148773823DE905B7F6F5862FD564038BB7AE03BF50D",
  103. // "type": "ed25519"
  104. // },
  105. // "block_id": {
  106. // "parts": {
  107. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  108. // "total": 1
  109. // },
  110. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  111. // },
  112. // "type": 2,
  113. // "round": 0,
  114. // "height": 9,
  115. // "validator_index": 0,
  116. // "validator_address": "E89A51D60F68385E09E716D353373B11F8FACD62"
  117. // }
  118. // ],
  119. // "blockID": {
  120. // "parts": {
  121. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  122. // "total": 1
  123. // },
  124. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  125. // }
  126. // },
  127. // "data": {
  128. // "txs": []
  129. // },
  130. // "header": {
  131. // "app_hash": "",
  132. // "chain_id": "test-chain-6UTNIN",
  133. // "height": 10,
  134. // "time": "2017-05-29T15:05:53.877Z",
  135. // "num_txs": 0,
  136. // "last_block_id": {
  137. // "parts": {
  138. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  139. // "total": 1
  140. // },
  141. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  142. // },
  143. // "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
  144. // "data_hash": "",
  145. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  146. // }
  147. // },
  148. // "block_meta": {
  149. // "header": {
  150. // "app_hash": "",
  151. // "chain_id": "test-chain-6UTNIN",
  152. // "height": 10,
  153. // "time": "2017-05-29T15:05:53.877Z",
  154. // "num_txs": 0,
  155. // "last_block_id": {
  156. // "parts": {
  157. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  158. // "total": 1
  159. // },
  160. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  161. // },
  162. // "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
  163. // "data_hash": "",
  164. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  165. // },
  166. // "block_id": {
  167. // "parts": {
  168. // "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
  169. // "total": 1
  170. // },
  171. // "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
  172. // }
  173. // }
  174. // },
  175. // "id": "",
  176. // "jsonrpc": "2.0"
  177. // }
  178. // ```
  179. func Block(height int) (*ctypes.ResultBlock, error) {
  180. if height == 0 {
  181. return nil, fmt.Errorf("Height must be greater than 0")
  182. }
  183. if height > blockStore.Height() {
  184. return nil, fmt.Errorf("Height must be less than the current blockchain height")
  185. }
  186. blockMeta := blockStore.LoadBlockMeta(height)
  187. block := blockStore.LoadBlock(height)
  188. return &ctypes.ResultBlock{blockMeta, block}, nil
  189. }
  190. // Get block commit at a given height.
  191. //
  192. // ```shell
  193. // curl 'localhost:46657/commit?height=11'
  194. // ```
  195. //
  196. // ```go
  197. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  198. // info, err := client.Commit(11)
  199. // ```
  200. //
  201. // > The above command returns JSON structured like this:
  202. //
  203. // ```json
  204. // {
  205. // "error": "",
  206. // "result": {
  207. // "canonical": true,
  208. // "commit": {
  209. // "precommits": [
  210. // {
  211. // "signature": {
  212. // "data": "00970429FEC652E9E21D106A90AE8C5413759A7488775CEF4A3F44DC46C7F9D941070E4FBE9ED54DF247FA3983359A0C3A238D61DE55C75C9116D72ABC9CF50F",
  213. // "type": "ed25519"
  214. // },
  215. // "block_id": {
  216. // "parts": {
  217. // "hash": "9E37CBF266BC044A779E09D81C456E653B89E006",
  218. // "total": 1
  219. // },
  220. // "hash": "CC6E861E31CA4334E9888381B4A9137D1458AB6A"
  221. // },
  222. // "type": 2,
  223. // "round": 0,
  224. // "height": 11,
  225. // "validator_index": 0,
  226. // "validator_address": "E89A51D60F68385E09E716D353373B11F8FACD62"
  227. // }
  228. // ],
  229. // "blockID": {
  230. // "parts": {
  231. // "hash": "9E37CBF266BC044A779E09D81C456E653B89E006",
  232. // "total": 1
  233. // },
  234. // "hash": "CC6E861E31CA4334E9888381B4A9137D1458AB6A"
  235. // }
  236. // },
  237. // "header": {
  238. // "app_hash": "",
  239. // "chain_id": "test-chain-6UTNIN",
  240. // "height": 11,
  241. // "time": "2017-05-29T15:05:54.893Z",
  242. // "num_txs": 0,
  243. // "last_block_id": {
  244. // "parts": {
  245. // "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
  246. // "total": 1
  247. // },
  248. // "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
  249. // },
  250. // "last_commit_hash": "3CE0C9727CE524BA9CB7C91E28F08E2B94001087",
  251. // "data_hash": "",
  252. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  253. // }
  254. // },
  255. // "id": "",
  256. // "jsonrpc": "2.0"
  257. // }
  258. // ```
  259. func Commit(height int) (*ctypes.ResultCommit, error) {
  260. if height == 0 {
  261. return nil, fmt.Errorf("Height must be greater than 0")
  262. }
  263. storeHeight := blockStore.Height()
  264. if height > storeHeight {
  265. return nil, fmt.Errorf("Height must be less than or equal to the current blockchain height")
  266. }
  267. header := blockStore.LoadBlockMeta(height).Header
  268. // If the next block has not been committed yet,
  269. // use a non-canonical commit
  270. if height == storeHeight {
  271. commit := blockStore.LoadSeenCommit(height)
  272. return &ctypes.ResultCommit{header, commit, false}, nil
  273. }
  274. // Return the canonical commit (comes from the block at height+1)
  275. commit := blockStore.LoadBlockCommit(height)
  276. return &ctypes.ResultCommit{header, commit, true}, nil
  277. }