github.com/516108736/tendermint@v0.36.0/rpc/core/consensus.go (about)

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