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.

55 lines
1.5 KiB

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