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.

364 lines
9.8 KiB

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