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