github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/rpc/core/consensus.go (about)

     1  package core
     2  
     3  import (
     4  	cm "github.com/badrootd/nibiru-cometbft/consensus"
     5  	cmtmath "github.com/badrootd/nibiru-cometbft/libs/math"
     6  	ctypes "github.com/badrootd/nibiru-cometbft/rpc/core/types"
     7  	rpctypes "github.com/badrootd/nibiru-cometbft/rpc/jsonrpc/types"
     8  	"github.com/badrootd/nibiru-cometbft/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.cometbft.com/v0.37/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+cmtmath.MinInt(perPage, totalCount-skipCount)]
    40  
    41  	return &ctypes.ResultValidators{
    42  		BlockHeight: height,
    43  		Validators:  v,
    44  		Count:       len(v),
    45  		Total:       totalCount,
    46  	}, nil
    47  }
    48  
    49  // DumpConsensusState dumps consensus state.
    50  // UNSTABLE
    51  // More: https://docs.cometbft.com/v0.37/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.MarshalJSON()
    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,
    80  	}, nil
    81  }
    82  
    83  // ConsensusState returns a concise summary of the consensus state.
    84  // UNSTABLE
    85  // More: https://docs.cometbft.com/v0.37/rpc/#/Info/consensus_state
    86  func ConsensusState(ctx *rpctypes.Context) (*ctypes.ResultConsensusState, error) {
    87  	// Get self round state.
    88  	bz, err := env.ConsensusState.GetRoundStateSimpleJSON()
    89  	return &ctypes.ResultConsensusState{RoundState: bz}, err
    90  }
    91  
    92  // ConsensusParams gets the consensus parameters at the given block height.
    93  // If no height is provided, it will fetch the latest consensus params.
    94  // More: https://docs.cometbft.com/v0.37/rpc/#/Info/consensus_params
    95  func ConsensusParams(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultConsensusParams, error) {
    96  	// The latest consensus params that we know is the consensus params after the
    97  	// last block.
    98  	height, err := getHeight(latestUncommittedHeight(), heightPtr)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	consensusParams, err := env.StateStore.LoadConsensusParams(height)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	return &ctypes.ResultConsensusParams{
   108  		BlockHeight:     height,
   109  		ConsensusParams: consensusParams,
   110  	}, nil
   111  }