/
utils_validators.go
238 lines (193 loc) · 7.3 KB
/
utils_validators.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package staking
import (
"fmt"
juno "github.com/forbole/juno/v4/types"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/rarimo/bdjuno/modules/staking/keybase"
"github.com/rarimo/bdjuno/types"
"github.com/rs/zerolog/log"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
// getValidatorConsPubKey returns the consensus public key of the given validator
func (m *Module) getValidatorConsPubKey(validator stakingtypes.Validator) (cryptotypes.PubKey, error) {
var pubKey cryptotypes.PubKey
err := m.cdc.UnpackAny(validator.ConsensusPubkey, &pubKey)
return pubKey, err
}
// getValidatorConsAddr returns the consensus address of the given validator
func (m *Module) getValidatorConsAddr(validator stakingtypes.Validator) (sdk.ConsAddress, error) {
pubKey, err := m.getValidatorConsPubKey(validator)
if err != nil {
return nil, fmt.Errorf("error while getting validator consensus pub key: %s", err)
}
return sdk.ConsAddress(pubKey.Address()), err
}
// ---------------------------------------------------------------------------------------------------------------------
// ConvertValidator converts the given staking validator into a BDJuno validator
func (m *Module) convertValidator(height int64, validator stakingtypes.Validator) (types.Validator, error) {
consAddr, err := m.getValidatorConsAddr(validator)
if err != nil {
return nil, fmt.Errorf("error while getting validator consensus address: %s", err)
}
consPubKey, err := m.getValidatorConsPubKey(validator)
if err != nil {
return nil, fmt.Errorf("error while getting validator consensus pub key: %s", err)
}
return types.NewValidator(
consAddr.String(),
validator.OperatorAddress,
consPubKey.String(),
sdk.AccAddress(validator.GetOperator()).String(),
&validator.Commission.MaxChangeRate,
&validator.Commission.MaxRate,
height,
), nil
}
// convertValidatorDescription returns a new types.ValidatorDescription object by fetching the avatar URL
// using the Keybase APIs
func (m *Module) convertValidatorDescription(
height int64, opAddr string, description stakingtypes.Description,
) types.ValidatorDescription {
var avatarURL string
if description.Identity == stakingtypes.DoNotModifyDesc {
avatarURL = stakingtypes.DoNotModifyDesc
} else {
url, err := keybase.GetAvatarURL(description.Identity)
if err != nil {
url = ""
}
avatarURL = url
}
return types.NewValidatorDescription(opAddr, description, avatarURL, height)
}
// --------------------------------------------------------------------------------------------------------------------
// RefreshAllValidatorInfos refreshes the info of all the validators at the given height
func (m *Module) RefreshAllValidatorInfos(height int64) error {
// Get all validators
validators, err := m.source.GetValidatorsWithStatus(height, "")
if err != nil {
return fmt.Errorf("error while getting validators: %s", err)
}
// Refresh each validator
for _, validator := range validators {
err = m.RefreshValidatorInfos(height, validator.OperatorAddress)
if err != nil {
return fmt.Errorf("error while refreshing validator: %s", err)
}
}
return nil
}
// RefreshValidatorInfos refreshes the info for the validator with the given operator address at the provided height
func (m *Module) RefreshValidatorInfos(height int64, valOper string) error {
stakingValidator, err := m.source.GetValidator(height, valOper)
if err != nil {
return err
}
validator, err := m.convertValidator(height, stakingValidator)
if err != nil {
return fmt.Errorf("error while converting validator: %s", err)
}
desc := m.convertValidatorDescription(height, stakingValidator.OperatorAddress, stakingValidator.Description)
// Save the validator
err = m.db.SaveValidatorsData([]types.Validator{validator})
if err != nil {
return err
}
// Save the description
err = m.db.SaveValidatorDescription(desc)
if err != nil {
return err
}
// Save the commission
return m.db.SaveValidatorCommission(types.NewValidatorCommission(
stakingValidator.OperatorAddress,
&stakingValidator.Commission.Rate,
&stakingValidator.MinSelfDelegation,
height,
))
}
// GetValidatorsWithStatus returns the list of all the validators having the given status at the given height
func (m *Module) GetValidatorsWithStatus(height int64, status string) ([]stakingtypes.Validator, []types.Validator, error) {
validators, err := m.source.GetValidatorsWithStatus(height, status)
if err != nil {
return nil, nil, err
}
var vals = make([]types.Validator, len(validators))
for index, val := range validators {
validator, err := m.convertValidator(height, val)
if err != nil {
return nil, nil, fmt.Errorf("error while converting validator: %s", err)
}
vals[index] = validator
}
return validators, vals, nil
}
// getValidators returns the validators list at the given height
func (m *Module) getValidators(height int64) ([]stakingtypes.Validator, []types.Validator, error) {
return m.GetValidatorsWithStatus(height, "")
}
// updateValidators updates the list of validators that are present at the given height
func (m *Module) updateValidators(height int64) ([]stakingtypes.Validator, error) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating validators")
vals, validators, err := m.getValidators(height)
if err != nil {
return nil, fmt.Errorf("error while getting validator: %s", err)
}
err = m.db.SaveValidatorsData(validators)
if err != nil {
return nil, err
}
return vals, err
}
// --------------------------------------------------------------------------------------------------------------------
func (m *Module) GetValidatorsStatuses(height int64, validators []stakingtypes.Validator) ([]types.ValidatorStatus, error) {
statuses := make([]types.ValidatorStatus, len(validators))
for index, validator := range validators {
consAddr, err := m.getValidatorConsAddr(validator)
if err != nil {
return nil, fmt.Errorf("error while getting validator consensus address: %s", err)
}
consPubKey, err := m.getValidatorConsPubKey(validator)
if err != nil {
return nil, fmt.Errorf("error while getting validator consensus public key: %s", err)
}
statuses[index] = types.NewValidatorStatus(
consAddr.String(),
consPubKey.String(),
int(validator.GetStatus()),
validator.IsJailed(),
height,
)
}
return statuses, nil
}
func (m *Module) GetValidatorsVotingPowers(height int64, vals *tmctypes.ResultValidators) ([]types.ValidatorVotingPower, error) {
stakingVals, _, err := m.getValidators(height)
if err != nil {
return nil, err
}
votingPowers := make([]types.ValidatorVotingPower, len(stakingVals))
for index, validator := range stakingVals {
// Get the validator consensus address
consAddr, err := validator.GetConsAddr()
if err != nil {
return nil, err
}
// Find the voting power of this validator
var votingPower int64 = 0
for _, blockVal := range vals.Validators {
blockValConsAddr := juno.ConvertValidatorAddressToBech32String(blockVal.Address)
if blockValConsAddr == consAddr.String() {
votingPower = blockVal.VotingPower
}
}
if found, _ := m.db.HasValidator(consAddr.String()); !found {
continue
}
votingPowers[index] = types.NewValidatorVotingPower(consAddr.String(), votingPower, height)
}
return votingPowers, nil
}