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.

307 lines
8.7 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
  1. package core
  2. import (
  3. "fmt"
  4. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  5. "github.com/tendermint/tendermint/types"
  6. cmn "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 = cmn.MinInt(blockStore.Height(), maxHeight)
  66. }
  67. if minHeight == 0 {
  68. minHeight = cmn.MaxInt(1, maxHeight-20)
  69. } else {
  70. minHeight = cmn.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. // If no height is provided, it will fetch the latest block.
  82. //
  83. // ```shell
  84. // curl 'localhost:46657/block?height=10'
  85. // ```
  86. //
  87. // ```go
  88. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  89. // info, err := client.Block(10)
  90. // ```
  91. //
  92. // > The above command returns JSON structured like this:
  93. //
  94. // ```json
  95. // {
  96. // "error": "",
  97. // "result": {
  98. // "block": {
  99. // "last_commit": {
  100. // "precommits": [
  101. // {
  102. // "signature": {
  103. // "data": "12C0D8893B8A38224488DC1DE6270DF76BB1A5E9DB1C68577706A6A97C6EC34FFD12339183D5CA8BC2F46148773823DE905B7F6F5862FD564038BB7AE03BF50D",
  104. // "type": "ed25519"
  105. // },
  106. // "block_id": {
  107. // "parts": {
  108. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  109. // "total": 1
  110. // },
  111. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  112. // },
  113. // "type": 2,
  114. // "round": 0,
  115. // "height": 9,
  116. // "validator_index": 0,
  117. // "validator_address": "E89A51D60F68385E09E716D353373B11F8FACD62"
  118. // }
  119. // ],
  120. // "blockID": {
  121. // "parts": {
  122. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  123. // "total": 1
  124. // },
  125. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  126. // }
  127. // },
  128. // "data": {
  129. // "txs": []
  130. // },
  131. // "header": {
  132. // "app_hash": "",
  133. // "chain_id": "test-chain-6UTNIN",
  134. // "height": 10,
  135. // "time": "2017-05-29T15:05:53.877Z",
  136. // "num_txs": 0,
  137. // "last_block_id": {
  138. // "parts": {
  139. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  140. // "total": 1
  141. // },
  142. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  143. // },
  144. // "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
  145. // "data_hash": "",
  146. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  147. // }
  148. // },
  149. // "block_meta": {
  150. // "header": {
  151. // "app_hash": "",
  152. // "chain_id": "test-chain-6UTNIN",
  153. // "height": 10,
  154. // "time": "2017-05-29T15:05:53.877Z",
  155. // "num_txs": 0,
  156. // "last_block_id": {
  157. // "parts": {
  158. // "hash": "3C78F00658E06744A88F24FF97A0A5011139F34A",
  159. // "total": 1
  160. // },
  161. // "hash": "F70588DAB36BDA5A953D548A16F7D48C6C2DFD78"
  162. // },
  163. // "last_commit_hash": "F31CC4282E50B3F2A58D763D233D76F26D26CABE",
  164. // "data_hash": "",
  165. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  166. // },
  167. // "block_id": {
  168. // "parts": {
  169. // "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
  170. // "total": 1
  171. // },
  172. // "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
  173. // }
  174. // }
  175. // },
  176. // "id": "",
  177. // "jsonrpc": "2.0"
  178. // }
  179. // ```
  180. func Block(heightPtr *int) (*ctypes.ResultBlock, error) {
  181. if heightPtr == nil {
  182. height := blockStore.Height()
  183. blockMeta := blockStore.LoadBlockMeta(height)
  184. block := blockStore.LoadBlock(height)
  185. return &ctypes.ResultBlock{blockMeta, block}, nil
  186. }
  187. height := *heightPtr
  188. if height <= 0 {
  189. return nil, fmt.Errorf("Height must be greater than 0")
  190. }
  191. if height > blockStore.Height() {
  192. return nil, fmt.Errorf("Height must be less than the current blockchain height")
  193. }
  194. blockMeta := blockStore.LoadBlockMeta(height)
  195. block := blockStore.LoadBlock(height)
  196. return &ctypes.ResultBlock{blockMeta, block}, nil
  197. }
  198. // Get block commit at a given height.
  199. // If no height is provided, it will fetch the commit for the latest block.
  200. //
  201. // ```shell
  202. // curl 'localhost:46657/commit?height=11'
  203. // ```
  204. //
  205. // ```go
  206. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  207. // info, err := client.Commit(11)
  208. // ```
  209. //
  210. // > The above command returns JSON structured like this:
  211. //
  212. // ```json
  213. // {
  214. // "error": "",
  215. // "result": {
  216. // "canonical": true,
  217. // "commit": {
  218. // "precommits": [
  219. // {
  220. // "signature": {
  221. // "data": "00970429FEC652E9E21D106A90AE8C5413759A7488775CEF4A3F44DC46C7F9D941070E4FBE9ED54DF247FA3983359A0C3A238D61DE55C75C9116D72ABC9CF50F",
  222. // "type": "ed25519"
  223. // },
  224. // "block_id": {
  225. // "parts": {
  226. // "hash": "9E37CBF266BC044A779E09D81C456E653B89E006",
  227. // "total": 1
  228. // },
  229. // "hash": "CC6E861E31CA4334E9888381B4A9137D1458AB6A"
  230. // },
  231. // "type": 2,
  232. // "round": 0,
  233. // "height": 11,
  234. // "validator_index": 0,
  235. // "validator_address": "E89A51D60F68385E09E716D353373B11F8FACD62"
  236. // }
  237. // ],
  238. // "blockID": {
  239. // "parts": {
  240. // "hash": "9E37CBF266BC044A779E09D81C456E653B89E006",
  241. // "total": 1
  242. // },
  243. // "hash": "CC6E861E31CA4334E9888381B4A9137D1458AB6A"
  244. // }
  245. // },
  246. // "header": {
  247. // "app_hash": "",
  248. // "chain_id": "test-chain-6UTNIN",
  249. // "height": 11,
  250. // "time": "2017-05-29T15:05:54.893Z",
  251. // "num_txs": 0,
  252. // "last_block_id": {
  253. // "parts": {
  254. // "hash": "277A4DBEF91483A18B85F2F5677ABF9694DFA40F",
  255. // "total": 1
  256. // },
  257. // "hash": "96B1D2F2D201BA4BC383EB8224139DB1294944E5"
  258. // },
  259. // "last_commit_hash": "3CE0C9727CE524BA9CB7C91E28F08E2B94001087",
  260. // "data_hash": "",
  261. // "validators_hash": "9365FC80F234C967BD233F5A3E2AB2F1E4B0E5AA"
  262. // }
  263. // },
  264. // "id": "",
  265. // "jsonrpc": "2.0"
  266. // }
  267. // ```
  268. func Commit(heightPtr *int) (*ctypes.ResultCommit, error) {
  269. if heightPtr == nil {
  270. height := blockStore.Height()
  271. header := blockStore.LoadBlockMeta(height).Header
  272. commit := blockStore.LoadSeenCommit(height)
  273. return &ctypes.ResultCommit{header, commit, false}, nil
  274. }
  275. height := *heightPtr
  276. if height <= 0 {
  277. return nil, fmt.Errorf("Height must be greater than 0")
  278. }
  279. storeHeight := blockStore.Height()
  280. if height > storeHeight {
  281. return nil, fmt.Errorf("Height must be less than or equal to the current blockchain height")
  282. }
  283. header := blockStore.LoadBlockMeta(height).Header
  284. // If the next block has not been committed yet,
  285. // use a non-canonical commit
  286. if height == storeHeight {
  287. commit := blockStore.LoadSeenCommit(height)
  288. return &ctypes.ResultCommit{header, commit, false}, nil
  289. }
  290. // Return the canonical commit (comes from the block at height+1)
  291. commit := blockStore.LoadBlockCommit(height)
  292. return &ctypes.ResultCommit{header, commit, true}, nil
  293. }