Some checks are pending
docker-build-cometbft / vars (push) Waiting to run
docker-build-cometbft / build-images (amd64, ubuntu-24.04) (push) Blocked by required conditions
docker-build-cometbft / build-images (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
docker-build-cometbft / merge-images (push) Blocked by required conditions
docker-build-e2e-node / vars (push) Waiting to run
docker-build-e2e-node / build-images (amd64, ubuntu-24.04) (push) Blocked by required conditions
docker-build-e2e-node / build-images (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
docker-build-e2e-node / merge-images (push) Blocked by required conditions
118 lines
3.7 KiB
Go
118 lines
3.7 KiB
Go
package core
|
|
|
|
import (
|
|
cm "git.cw.tr/mukan-network/mukan-consensus/consensus"
|
|
cmtmath "git.cw.tr/mukan-network/mukan-consensus/libs/math"
|
|
ctypes "git.cw.tr/mukan-network/mukan-consensus/rpc/core/types"
|
|
rpctypes "git.cw.tr/mukan-network/mukan-consensus/rpc/jsonrpc/types"
|
|
"git.cw.tr/mukan-network/mukan-consensus/types"
|
|
)
|
|
|
|
// Validators gets the validator set at the given block height.
|
|
//
|
|
// If no height is provided, it will fetch the latest validator set. Note the
|
|
// validators are sorted by their voting power - this is the canonical order
|
|
// for the validators in the set as used in computing their Merkle root.
|
|
//
|
|
// More: https://docs.cometbft.com/v0.38.x/rpc/#/Info/validators
|
|
func (env *Environment) Validators(
|
|
_ *rpctypes.Context,
|
|
heightPtr *int64,
|
|
pagePtr, perPagePtr *int,
|
|
) (*ctypes.ResultValidators, error) {
|
|
// The latest validator that we know is the NextValidator of the last block.
|
|
height, err := env.getHeight(env.latestUncommittedHeight(), heightPtr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
validators, err := env.StateStore.LoadValidators(height)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
totalCount := len(validators.Validators)
|
|
perPage := env.validatePerPage(perPagePtr)
|
|
page, err := validatePage(pagePtr, perPage, totalCount)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
skipCount := validateSkipCount(page, perPage)
|
|
|
|
v := validators.Validators[skipCount : skipCount+cmtmath.MinInt(perPage, totalCount-skipCount)]
|
|
|
|
return &ctypes.ResultValidators{
|
|
BlockHeight: height,
|
|
Validators: v,
|
|
Count: len(v),
|
|
Total: totalCount,
|
|
}, nil
|
|
}
|
|
|
|
// DumpConsensusState dumps consensus state.
|
|
// UNSTABLE
|
|
// More: https://docs.cometbft.com/v0.38.x/rpc/#/Info/dump_consensus_state
|
|
func (env *Environment) DumpConsensusState(*rpctypes.Context) (*ctypes.ResultDumpConsensusState, error) {
|
|
// Get Peer consensus states.
|
|
peers := env.P2PPeers.Peers().List()
|
|
peerStates := make([]ctypes.PeerStateInfo, len(peers))
|
|
for i, peer := range peers {
|
|
peerState, ok := peer.Get(types.PeerStateKey).(*cm.PeerState)
|
|
if !ok { // peer does not have a state yet
|
|
continue
|
|
}
|
|
peerStateJSON, err := peerState.MarshalJSON()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
peerStates[i] = ctypes.PeerStateInfo{
|
|
// Peer basic info.
|
|
NodeAddress: peer.SocketAddr().String(),
|
|
// Peer consensus state.
|
|
PeerState: peerStateJSON,
|
|
}
|
|
}
|
|
// Get self round state.
|
|
roundState, err := env.ConsensusState.GetRoundStateJSON()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ctypes.ResultDumpConsensusState{
|
|
RoundState: roundState,
|
|
Peers: peerStates,
|
|
}, nil
|
|
}
|
|
|
|
// ConsensusState returns a concise summary of the consensus state.
|
|
// UNSTABLE
|
|
// More: https://docs.cometbft.com/v0.38.x/rpc/#/Info/consensus_state
|
|
func (env *Environment) GetConsensusState(*rpctypes.Context) (*ctypes.ResultConsensusState, error) {
|
|
// Get self round state.
|
|
bz, err := env.ConsensusState.GetRoundStateSimpleJSON()
|
|
return &ctypes.ResultConsensusState{RoundState: bz}, err
|
|
}
|
|
|
|
// ConsensusParams gets the consensus parameters at the given block height.
|
|
// If no height is provided, it will fetch the latest consensus params.
|
|
// More: https://docs.cometbft.com/v0.38.x/rpc/#/Info/consensus_params
|
|
func (env *Environment) ConsensusParams(
|
|
_ *rpctypes.Context,
|
|
heightPtr *int64,
|
|
) (*ctypes.ResultConsensusParams, error) {
|
|
// The latest consensus params that we know is the consensus params after the
|
|
// last block.
|
|
height, err := env.getHeight(env.latestUncommittedHeight(), heightPtr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
consensusParams, err := env.StateStore.LoadConsensusParams(height)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ctypes.ResultConsensusParams{
|
|
BlockHeight: height,
|
|
ConsensusParams: consensusParams,
|
|
}, nil
|
|
}
|