github.com/number571/tendermint@v0.34.11-gost/rpc/core/consensus.go (about)

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