/
validators.go
136 lines (115 loc) · 4.12 KB
/
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
package staking
import (
"context"
"fmt"
cometbftcoretypes "github.com/cometbft/cometbft/rpc/core/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/bro-n-bro/spacebox-crawler/modules/utils"
"github.com/bro-n-bro/spacebox-crawler/types"
"github.com/bro-n-bro/spacebox/broker/model"
)
const (
defaultLimit = 150
)
// HandleValidators handles validators for each block height.
func (m *Module) HandleValidators(ctx context.Context, tmValidators *cometbftcoretypes.ResultValidators) error {
vals, validators, err := GetValidators(ctx, tmValidators.BlockHeight, m.client.StakingQueryClient, m.cdc)
if err != nil {
return err
}
if err = m.PublishValidatorsData(ctx, validators); err != nil {
return err
}
if err = m.publishValidatorDescriptions(ctx, vals, tmValidators.BlockHeight); err != nil {
return err
}
for _, val := range vals {
consAddr, err := getValidatorConsAddr(m.cdc, val)
if err != nil {
return fmt.Errorf("error while getting validator consensus address: %w", err)
}
if err = m.broker.PublishValidatorStatus(ctx, model.ValidatorStatus{
Height: tmValidators.BlockHeight,
ConsensusAddress: consAddr.String(),
Status: int64(val.GetStatus()),
Jailed: val.IsJailed(),
}); err != nil {
return err
}
if err := m.broker.PublishValidatorCommission(ctx, model.ValidatorCommission{
Height: tmValidators.BlockHeight,
OperatorAddress: val.OperatorAddress,
Commission: val.Commission.Rate.MustFloat64(),
MaxChangeRate: val.Commission.MaxChangeRate.MustFloat64(),
MaxRate: val.Commission.MaxRate.MustFloat64(),
}); err != nil {
return err
}
}
// FIXME: does it needed?
// Update the voting powers
// go updateValidatorVotingPower(block.Height, vals)
return nil
}
// PublishValidatorsData produces a message about validator, account and validator info to the broker.
func (m *Module) PublishValidatorsData(ctx context.Context, sVals []types.StakingValidator) error {
for _, val := range sVals {
if err := m.broker.PublishValidator(ctx, model.Validator{
ConsensusAddress: val.GetConsAddr(),
ConsensusPubkey: val.GetConsPubKey(),
OperatorAddress: val.GetOperator(),
Height: val.GetHeight(),
}); err != nil {
return err
}
if err := utils.GetAndPublishAccount(ctx, val.GetSelfDelegateAddress(), val.GetHeight(), m.accCache,
m.broker, m.client.AuthQueryClient); err != nil {
return err
}
if err := m.broker.PublishValidatorInfo(ctx, model.ValidatorInfo{
ConsensusAddress: val.GetConsAddr(),
OperatorAddress: val.GetOperator(),
SelfDelegateAddress: val.GetSelfDelegateAddress(),
MinSelfDelegation: val.GetMinSelfDelegation(),
Height: val.GetHeight(),
}); err != nil {
return err
}
}
return nil
}
// asyncPublishValidatorDescriptions process validator descriptions and publish them to the broker.
func (m *Module) publishValidatorDescriptions(ctx context.Context, vals stakingtypes.Validators, height int64) error {
for _, val := range vals {
if err := m.publishValidatorDescription(ctx, val, height); err != nil {
return err
}
}
return nil
}
// publishValidatorDescription process validator description and publish it to the broker.
// It also gets avatar url from the keybase.
// Contains the cache for validator identity to skip the keybase API call as it might be stopped due to rate limits.
func (m *Module) publishValidatorDescription(
ctx context.Context,
val stakingtypes.Validator,
height int64,
) error {
if err := m.broker.PublishValidatorDescription(ctx, model.ValidatorDescription{
OperatorAddress: val.OperatorAddress,
Moniker: val.Description.Moniker,
Identity: val.Description.Identity,
Website: val.Description.Website,
SecurityContact: val.Description.SecurityContact,
Details: val.Description.Details,
Height: height,
}); err != nil {
m.log.Error().Err(err).
Str("operator_address", val.OperatorAddress).
Str("identity", val.Description.Identity).
Int64("height", height).
Msg("failed to publish validator description")
return err
}
return nil
}