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.

58 lines
1.4 KiB

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