-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathconsensus.go
158 lines (138 loc) · 4.97 KB
/
consensus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package core
import (
cm "github.com/line/ostracon/consensus"
tmmath "github.com/line/ostracon/libs/math"
ctypes "github.com/line/ostracon/rpc/core/types"
rpctypes "github.com/line/ostracon/rpc/jsonrpc/types"
"github.com/line/ostracon/types"
)
// Validators gets the validators set at the given block height.
//
// If no height is provided, it will fetch the latest validator set. Note the
// voters are sorted by their voting power - this is the canonical order
// for the voters in the set as used in computing their Merkle root.
//
// More: https://docs.tendermint.com/master/rpc/#/Info/validators
func Validators(ctx *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 := getHeight(latestUncommittedHeight(), heightPtr)
if err != nil {
return nil, err
}
vals, voters, _, _, err := env.StateStore.LoadVoters(height, nil)
if err != nil {
return nil, err
}
totalCount := len(vals.Validators)
perPage := validatePerPage(perPagePtr)
page, err := validatePage(pagePtr, perPage, totalCount)
if err != nil {
return nil, err
}
skipCount := validateSkipCount(page, perPage)
v := vals.Validators[skipCount : skipCount+tmmath.MinInt(perPage, totalCount-skipCount)]
// Retrieve to the indices where selected as Voters in Validators.
votersIndices := make([]int32, 0, len(v))
for i := range v {
idx, voter := voters.GetByAddress(v[i].Address)
if idx >= 0 {
votersIndices = append(votersIndices, int32(i))
v[i] = voter // replace to preserve its VotingWeight
}
}
return &ctypes.ResultValidators{
BlockHeight: height,
Validators: v,
VoterIndices: votersIndices,
Count: len(v),
Total: totalCount}, nil
}
// Voters gets the voters set at the given block height.
//
// If no height is provided, it will fetch the latest validator set. Note the
// voters are sorted by their voting power - this is the canonical order
// for the voters in the set as used in computing their Merkle root.
//
// More: https://docs.tendermint.com/master/rpc/#/Info/validators
func Voters(ctx *rpctypes.Context, heightPtr *int64, pagePtr, perPagePtr *int) (*ctypes.ResultVoters, error) {
// The latest validator that we know is the NextValidator of the last block.
height, err := getHeight(latestUncommittedHeight(), heightPtr)
if err != nil {
return nil, err
}
_, voters, _, _, err := env.StateStore.LoadVoters(height, nil)
if err != nil {
return nil, err
}
totalCount := len(voters.Voters)
perPage := validatePerPage(perPagePtr)
page, err := validatePage(pagePtr, perPage, totalCount)
if err != nil {
return nil, err
}
skipCount := validateSkipCount(page, perPage)
v := voters.Voters[skipCount : skipCount+tmmath.MinInt(perPage, totalCount-skipCount)]
return &ctypes.ResultVoters{
BlockHeight: height,
Voters: v,
Count: len(v),
Total: totalCount}, nil
}
// DumpConsensusState dumps consensus state.
// UNSTABLE
// More: https://docs.tendermint.com/master/rpc/#/Info/dump_consensus_state
func DumpConsensusState(ctx *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.ToJSON()
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.tendermint.com/master/rpc/#/Info/consensus_state
func ConsensusState(ctx *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.tendermint.com/master/rpc/#/Info/consensus_params
func ConsensusParams(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultConsensusParams, error) {
// The latest consensus params that we know is the consensus params after the
// last block.
height, err := getHeight(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
}