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.

71 lines
2.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package core
  2. import (
  3. "fmt"
  4. . "github.com/tendermint/tmlibs/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. }
  39. //-----------------------------------------------------------------------------
  40. func Commit(height int) (*ctypes.ResultCommit, error) {
  41. if height == 0 {
  42. return nil, fmt.Errorf("Height must be greater than 0")
  43. }
  44. storeHeight := blockStore.Height()
  45. if height > storeHeight {
  46. return nil, fmt.Errorf("Height must be less than or equal to the current blockchain height")
  47. }
  48. header := blockStore.LoadBlockMeta(height).Header
  49. // If the next block has not been committed yet,
  50. // use a non-canonical commit
  51. if height == storeHeight {
  52. commit := blockStore.LoadSeenCommit(height)
  53. return &ctypes.ResultCommit{header, commit, false}, nil
  54. }
  55. // Return the canonical commit (comes from the block at height+1)
  56. commit := blockStore.LoadBlockCommit(height)
  57. return &ctypes.ResultCommit{header, commit, true}, nil
  58. }