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.

46 lines
1.4 KiB

  1. package core
  2. import (
  3. "fmt"
  4. . "github.com/tendermint/go-common"
  5. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  6. "github.com/tendermint/tendermint/types"
  7. )
  8. //-----------------------------------------------------------------------------
  9. // TODO: limit/permission on (max - min)
  10. func BlockchainInfo(minHeight, maxHeight int) (*ctypes.ResultBlockchainInfo, error) {
  11. if maxHeight == 0 {
  12. maxHeight = blockStore.Height()
  13. } else {
  14. maxHeight = MinInt(blockStore.Height(), maxHeight)
  15. }
  16. if minHeight == 0 {
  17. minHeight = MaxInt(1, maxHeight-20)
  18. }
  19. log.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight)
  20. blockMetas := []*types.BlockMeta{}
  21. for height := maxHeight; height >= minHeight; height-- {
  22. blockMeta := blockStore.LoadBlockMeta(height)
  23. blockMetas = append(blockMetas, blockMeta)
  24. }
  25. return &ctypes.ResultBlockchainInfo{blockStore.Height(), blockMetas}, nil
  26. }
  27. //-----------------------------------------------------------------------------
  28. func Block(height int) (*ctypes.ResultBlock, error) {
  29. if height == 0 {
  30. return nil, fmt.Errorf("Height must be greater than 0")
  31. }
  32. if height > blockStore.Height() {
  33. return nil, fmt.Errorf("Height must be less than the current blockchain height")
  34. }
  35. blockMeta := blockStore.LoadBlockMeta(height)
  36. block := blockStore.LoadBlock(height)
  37. return &ctypes.ResultBlock{blockMeta, block}, nil
  38. }