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.

69 lines
1.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package rpc
  2. import (
  3. "net/http"
  4. . "github.com/tendermint/tendermint/block"
  5. . "github.com/tendermint/tendermint/common"
  6. )
  7. type BlockchainInfoResponse struct {
  8. LastHeight uint
  9. BlockMetas []*BlockMeta
  10. }
  11. func BlockchainInfoHandler(w http.ResponseWriter, r *http.Request) {
  12. minHeight, _ := GetParamUint(r, "min_height")
  13. maxHeight, _ := GetParamUint(r, "max_height")
  14. if maxHeight == 0 {
  15. maxHeight = blockStore.Height()
  16. } else {
  17. maxHeight = MinUint(blockStore.Height(), maxHeight)
  18. }
  19. if minHeight == 0 {
  20. minHeight = uint(MaxInt(1, int(maxHeight)-20))
  21. }
  22. log.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight)
  23. blockMetas := []*BlockMeta{}
  24. for height := maxHeight; height >= minHeight; height-- {
  25. blockMeta := blockStore.LoadBlockMeta(height)
  26. blockMetas = append(blockMetas, blockMeta)
  27. }
  28. res := BlockchainInfoResponse{
  29. LastHeight: blockStore.Height(),
  30. BlockMetas: blockMetas,
  31. }
  32. WriteAPIResponse(w, API_OK, res)
  33. return
  34. }
  35. //-----------------------------------------------------------------------------
  36. type BlockResponse struct {
  37. BlockMeta *BlockMeta
  38. Block *Block
  39. }
  40. func BlockHandler(w http.ResponseWriter, r *http.Request) {
  41. height, _ := GetParamUint(r, "height")
  42. if height == 0 {
  43. WriteAPIResponse(w, API_INVALID_PARAM, "height must be greater than 1")
  44. return
  45. }
  46. if height > blockStore.Height() {
  47. WriteAPIResponse(w, API_INVALID_PARAM, "height must be less than the current blockchain height")
  48. return
  49. }
  50. blockMeta := blockStore.LoadBlockMeta(height)
  51. block := blockStore.LoadBlock(height)
  52. res := BlockResponse{
  53. BlockMeta: blockMeta,
  54. Block: block,
  55. }
  56. WriteAPIResponse(w, API_OK, res)
  57. return
  58. }