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.

75 lines
2.2 KiB

8 years ago
8 years ago
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. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  5. "github.com/tendermint/tendermint/types"
  6. . "github.com/tendermint/tmlibs/common"
  7. )
  8. //-----------------------------------------------------------------------------
  9. // Returns at most 20 blocks
  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. } else {
  19. minHeight = MaxInt(minHeight, maxHeight-20)
  20. }
  21. logger.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight)
  22. blockMetas := []*types.BlockMeta{}
  23. for height := maxHeight; height >= minHeight; height-- {
  24. blockMeta := blockStore.LoadBlockMeta(height)
  25. blockMetas = append(blockMetas, blockMeta)
  26. }
  27. return &ctypes.ResultBlockchainInfo{blockStore.Height(), blockMetas}, nil
  28. }
  29. //-----------------------------------------------------------------------------
  30. func Block(height int) (*ctypes.ResultBlock, error) {
  31. if height == 0 {
  32. return nil, fmt.Errorf("Height must be greater than 0")
  33. }
  34. if height > blockStore.Height() {
  35. return nil, fmt.Errorf("Height must be less than the current blockchain height")
  36. }
  37. blockMeta := blockStore.LoadBlockMeta(height)
  38. block := blockStore.LoadBlock(height)
  39. return &ctypes.ResultBlock{blockMeta, block}, nil
  40. }
  41. //-----------------------------------------------------------------------------
  42. func Commit(height int) (*ctypes.ResultCommit, error) {
  43. if height == 0 {
  44. return nil, fmt.Errorf("Height must be greater than 0")
  45. }
  46. storeHeight := blockStore.Height()
  47. if height > storeHeight {
  48. return nil, fmt.Errorf("Height must be less than or equal to the current blockchain height")
  49. }
  50. header := blockStore.LoadBlockMeta(height).Header
  51. // If the next block has not been committed yet,
  52. // use a non-canonical commit
  53. if height == storeHeight {
  54. commit := blockStore.LoadSeenCommit(height)
  55. return &ctypes.ResultCommit{header, commit, false}, nil
  56. }
  57. // Return the canonical commit (comes from the block at height+1)
  58. commit := blockStore.LoadBlockCommit(height)
  59. return &ctypes.ResultCommit{header, commit, true}, nil
  60. }