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.

150 lines
4.4 KiB

  1. package core
  2. import (
  3. "errors"
  4. cm "github.com/tendermint/tendermint/internal/consensus"
  5. tmmath "github.com/tendermint/tendermint/libs/math"
  6. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  7. rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. // Validators gets the validator set at the given block height.
  11. //
  12. // If no height is provided, it will fetch the latest validator set. Note the
  13. // validators are sorted by their voting power - this is the canonical order
  14. // for the validators in the set as used in computing their Merkle root.
  15. //
  16. // More: https://docs.tendermint.com/master/rpc/#/Info/validators
  17. func (env *Environment) Validators(
  18. ctx *rpctypes.Context,
  19. heightPtr *int64,
  20. pagePtr, perPagePtr *int) (*ctypes.ResultValidators, error) {
  21. // The latest validator that we know is the NextValidator of the last block.
  22. height, err := env.getHeight(env.latestUncommittedHeight(), heightPtr)
  23. if err != nil {
  24. return nil, err
  25. }
  26. validators, err := env.StateStore.LoadValidators(height)
  27. if err != nil {
  28. return nil, err
  29. }
  30. totalCount := len(validators.Validators)
  31. perPage := env.validatePerPage(perPagePtr)
  32. page, err := validatePage(pagePtr, perPage, totalCount)
  33. if err != nil {
  34. return nil, err
  35. }
  36. skipCount := validateSkipCount(page, perPage)
  37. v := validators.Validators[skipCount : skipCount+tmmath.MinInt(perPage, totalCount-skipCount)]
  38. return &ctypes.ResultValidators{
  39. BlockHeight: height,
  40. Validators: v,
  41. Count: len(v),
  42. Total: totalCount}, nil
  43. }
  44. // DumpConsensusState dumps consensus state.
  45. // UNSTABLE
  46. // More: https://docs.tendermint.com/master/rpc/#/Info/dump_consensus_state
  47. func (env *Environment) DumpConsensusState(ctx *rpctypes.Context) (*ctypes.ResultDumpConsensusState, error) {
  48. // Get Peer consensus states.
  49. var peerStates []ctypes.PeerStateInfo
  50. switch {
  51. case env.P2PPeers != nil:
  52. peers := env.P2PPeers.Peers().List()
  53. peerStates = make([]ctypes.PeerStateInfo, 0, len(peers))
  54. for _, peer := range peers {
  55. peerState, ok := peer.Get(types.PeerStateKey).(*cm.PeerState)
  56. if !ok { // peer does not have a state yet
  57. continue
  58. }
  59. peerStateJSON, err := peerState.ToJSON()
  60. if err != nil {
  61. return nil, err
  62. }
  63. peerStates = append(peerStates, ctypes.PeerStateInfo{
  64. // Peer basic info.
  65. NodeAddress: peer.SocketAddr().String(),
  66. // Peer consensus state.
  67. PeerState: peerStateJSON,
  68. })
  69. }
  70. case env.PeerManager != nil:
  71. peers := env.PeerManager.Peers()
  72. peerStates = make([]ctypes.PeerStateInfo, 0, len(peers))
  73. for _, pid := range peers {
  74. peerState, ok := env.ConsensusReactor.GetPeerState(pid)
  75. if !ok {
  76. continue
  77. }
  78. peerStateJSON, err := peerState.ToJSON()
  79. if err != nil {
  80. return nil, err
  81. }
  82. addr := env.PeerManager.Addresses(pid)
  83. if len(addr) >= 1 {
  84. peerStates = append(peerStates, ctypes.PeerStateInfo{
  85. // Peer basic info.
  86. NodeAddress: addr[0].String(),
  87. // Peer consensus state.
  88. PeerState: peerStateJSON,
  89. })
  90. }
  91. }
  92. default:
  93. return nil, errors.New("no peer system configured")
  94. }
  95. // Get self round state.
  96. roundState, err := env.ConsensusState.GetRoundStateJSON()
  97. if err != nil {
  98. return nil, err
  99. }
  100. return &ctypes.ResultDumpConsensusState{
  101. RoundState: roundState,
  102. Peers: peerStates}, nil
  103. }
  104. // ConsensusState returns a concise summary of the consensus state.
  105. // UNSTABLE
  106. // More: https://docs.tendermint.com/master/rpc/#/Info/consensus_state
  107. func (env *Environment) GetConsensusState(ctx *rpctypes.Context) (*ctypes.ResultConsensusState, error) {
  108. // Get self round state.
  109. bz, err := env.ConsensusState.GetRoundStateSimpleJSON()
  110. return &ctypes.ResultConsensusState{RoundState: bz}, err
  111. }
  112. // ConsensusParams gets the consensus parameters at the given block height.
  113. // If no height is provided, it will fetch the latest consensus params.
  114. // More: https://docs.tendermint.com/master/rpc/#/Info/consensus_params
  115. func (env *Environment) ConsensusParams(
  116. ctx *rpctypes.Context,
  117. heightPtr *int64) (*ctypes.ResultConsensusParams, error) {
  118. // The latest consensus params that we know is the consensus params after the
  119. // last block.
  120. height, err := env.getHeight(env.latestUncommittedHeight(), heightPtr)
  121. if err != nil {
  122. return nil, err
  123. }
  124. consensusParams, err := env.StateStore.LoadConsensusParams(height)
  125. if err != nil {
  126. return nil, err
  127. }
  128. return &ctypes.ResultConsensusParams{
  129. BlockHeight: height,
  130. ConsensusParams: consensusParams}, nil
  131. }