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.

57 lines
1.3 KiB

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 = MaxUint(1, maxHeight-20)
  21. }
  22. blockMetas := []*BlockMeta{}
  23. for height := minHeight; height <= maxHeight; height++ {
  24. blockMetas = append(blockMetas, blockStore.LoadBlockMeta(height))
  25. }
  26. res := BlockchainInfoResponse{
  27. LastHeight: blockStore.Height(),
  28. BlockMetas: blockMetas,
  29. }
  30. WriteAPIResponse(w, API_OK, res)
  31. return
  32. }
  33. //-----------------------------------------------------------------------------
  34. func BlockHandler(w http.ResponseWriter, r *http.Request) {
  35. height, _ := GetParamUint(r, "height")
  36. if height == 0 {
  37. WriteAPIResponse(w, API_INVALID_PARAM, "height must be greater than 1")
  38. return
  39. }
  40. if height > blockStore.Height() {
  41. WriteAPIResponse(w, API_INVALID_PARAM, "height must be less than the current blockchain height")
  42. return
  43. }
  44. block := blockStore.LoadBlock(height)
  45. WriteAPIResponse(w, API_OK, block)
  46. return
  47. }