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.

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