github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/internal/rpc/core/consensus.go (about)

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