forked from solarlabsteam/cosmos-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.go
353 lines (305 loc) · 10.1 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"context"
"net/http"
"sort"
"strconv"
"sync"
"time"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"github.com/cosmos/cosmos-sdk/simapp"
querytypes "github.com/cosmos/cosmos-sdk/types/query"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
func ValidatorsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn) {
encCfg := simapp.MakeTestEncodingConfig()
interfaceRegistry := encCfg.InterfaceRegistry
requestStart := time.Now()
sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()
validatorsCommissionGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_commission",
Help: "Commission of the Cosmos-based blockchain validator",
ConstLabels: ConstLabels,
},
[]string{"address", "moniker"},
)
validatorsStatusGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_status",
Help: "Status of the Cosmos-based blockchain validator",
ConstLabels: ConstLabels,
},
[]string{"address", "moniker"},
)
validatorsJailedGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_jailed",
Help: "Jailed status of the Cosmos-based blockchain validator",
ConstLabels: ConstLabels,
},
[]string{"address", "moniker"},
)
validatorsTokensGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_tokens",
Help: "Tokens of the Cosmos-based blockchain validator",
ConstLabels: ConstLabels,
},
[]string{"address", "moniker", "denom"},
)
validatorsDelegatorSharesGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_delegator_shares",
Help: "Delegator shares of the Cosmos-based blockchain validator",
ConstLabels: ConstLabels,
},
[]string{"address", "moniker", "denom"},
)
validatorsMinSelfDelegationGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_min_self_delegation",
Help: "Self declared minimum self delegation shares of the Cosmos-based blockchain validator",
ConstLabels: ConstLabels,
},
[]string{"address", "moniker", "denom"},
)
validatorsMissedBlocksGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_missed_blocks",
Help: "Missed blocks of the Cosmos-based blockchain validator",
ConstLabels: ConstLabels,
},
[]string{"address", "moniker"},
)
validatorsRankGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_rank",
Help: "Rank of the Cosmos-based blockchain validator",
ConstLabels: ConstLabels,
},
[]string{"address", "moniker"},
)
validatorsIsActiveGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_active",
Help: "1 if the Cosmos-based blockchain validator is in active set, 0 if no",
ConstLabels: ConstLabels,
},
[]string{"address", "moniker"},
)
registry := prometheus.NewRegistry()
registry.MustRegister(validatorsCommissionGauge)
registry.MustRegister(validatorsStatusGauge)
registry.MustRegister(validatorsJailedGauge)
registry.MustRegister(validatorsTokensGauge)
registry.MustRegister(validatorsDelegatorSharesGauge)
registry.MustRegister(validatorsMinSelfDelegationGauge)
registry.MustRegister(validatorsMissedBlocksGauge)
registry.MustRegister(validatorsRankGauge)
registry.MustRegister(validatorsIsActiveGauge)
var validators []stakingtypes.Validator
var signingInfos []slashingtypes.ValidatorSigningInfo
var validatorSetLength uint32
var wg sync.WaitGroup
go func() {
defer wg.Done()
sublogger.Debug().Msg("Started querying validators")
queryStart := time.Now()
stakingClient := stakingtypes.NewQueryClient(grpcConn)
validatorsResponse, err := stakingClient.Validators(
context.Background(),
&stakingtypes.QueryValidatorsRequest{
Pagination: &querytypes.PageRequest{
Limit: Limit,
},
},
)
if err != nil {
sublogger.Error().Err(err).Msg("Could not get validators")
return
}
sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying validators")
validators = validatorsResponse.Validators
// sorting by delegator shares to display rankings
sort.Slice(validators[:], func(i, j int) bool {
return validators[i].DelegatorShares.RoundInt64() > validators[j].DelegatorShares.RoundInt64()
})
}()
wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().Msg("Started querying validators signing infos")
queryStart := time.Now()
slashingClient := slashingtypes.NewQueryClient(grpcConn)
signingInfosResponse, err := slashingClient.SigningInfos(
context.Background(),
&slashingtypes.QuerySigningInfosRequest{
Pagination: &querytypes.PageRequest{
Limit: Limit,
},
},
)
if err != nil {
sublogger.Error().
Err(err).
Msg("Could not get validators signing infos")
return
}
sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying validator signing infos")
signingInfos = signingInfosResponse.Info
}()
wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().Msg("Started querying staking params")
queryStart := time.Now()
stakingClient := stakingtypes.NewQueryClient(grpcConn)
paramsResponse, err := stakingClient.Params(
context.Background(),
&stakingtypes.QueryParamsRequest{},
)
if err != nil {
sublogger.Error().
Err(err).
Msg("Could not get staking params")
return
}
sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying staking params")
validatorSetLength = paramsResponse.Params.MaxValidators
}()
wg.Add(1)
wg.Wait()
sublogger.Debug().
Int("signingLength", len(signingInfos)).
Int("validatorsLength", len(validators)).
Msg("Validators info")
for index, validator := range validators {
// because cosmos's dec doesn't have .toFloat64() method or whatever and returns everything as int
rate, err := strconv.ParseFloat(validator.Commission.CommissionRates.Rate.String(), 64)
if err != nil {
log.Error().
Err(err).
Str("address", validator.OperatorAddress).
Msg("Could not get commission")
} else {
validatorsCommissionGauge.With(prometheus.Labels{
"address": validator.OperatorAddress,
"moniker": validator.Description.Moniker,
}).Set(rate)
}
validatorsStatusGauge.With(prometheus.Labels{
"address": validator.OperatorAddress,
"moniker": validator.Description.Moniker,
}).Set(float64(validator.Status))
// golang doesn't have a ternary operator, so we have to stick with this ugly solution
var jailed float64
if validator.Jailed {
jailed = 1
} else {
jailed = 0
}
validatorsJailedGauge.With(prometheus.Labels{
"address": validator.OperatorAddress,
"moniker": validator.Description.Moniker,
}).Set(float64(jailed))
validatorsTokensGauge.With(prometheus.Labels{
"address": validator.OperatorAddress,
"moniker": validator.Description.Moniker,
"denom": Denom,
}).Set(float64(validator.Tokens.Int64()) / DenomCoefficient)
// because cosmos's dec doesn't have .toFloat64() method or whatever and returns everything as int
if value, err := strconv.ParseFloat(validator.DelegatorShares.String(), 64); err != nil {
sublogger.Error().
Str("address", validator.OperatorAddress).
Err(err).
Msg("Could not parse delegator shares")
} else {
validatorsDelegatorSharesGauge.With(prometheus.Labels{
"address": validator.OperatorAddress,
"moniker": validator.Description.Moniker,
"denom": Denom,
}).Set(value / DenomCoefficient)
}
validatorsMinSelfDelegationGauge.With(prometheus.Labels{
"address": validator.OperatorAddress,
"moniker": validator.Description.Moniker,
"denom": Denom,
}).Set(float64(validator.MinSelfDelegation.Int64()) / DenomCoefficient)
err = validator.UnpackInterfaces(interfaceRegistry) // Unpack interfaces, to populate the Anys' cached values
if err != nil {
sublogger.Error().
Str("address", validator.OperatorAddress).
Err(err).
Msg("Could not get unpack validator inferfaces")
}
pubKey, err := validator.GetConsAddr()
if err != nil {
sublogger.Error().
Str("address", validator.OperatorAddress).
Err(err).
Msg("Could not get validator pubkey")
}
var signingInfo slashingtypes.ValidatorSigningInfo
found := false
for _, signingInfoIterated := range signingInfos {
if pubKey.String() == signingInfoIterated.Address {
found = true
signingInfo = signingInfoIterated
break
}
}
if !found {
sublogger.Debug().
Str("address", validator.OperatorAddress).
Msg("Could not get signing info for validator")
continue
}
if validator.Status == stakingtypes.Bonded {
validatorsMissedBlocksGauge.With(prometheus.Labels{
"address": validator.OperatorAddress,
"moniker": validator.Description.Moniker,
}).Set(float64(signingInfo.MissedBlocksCounter))
} else {
sublogger.Trace().
Str("address", validator.OperatorAddress).
Msg("Validator is not active, not returning missed blocks amount.")
}
validatorsRankGauge.With(prometheus.Labels{
"address": validator.OperatorAddress,
"moniker": validator.Description.Moniker,
}).Set(float64(index + 1))
if validatorSetLength != 0 {
// golang doesn't have a ternary operator, so we have to stick with this ugly solution
var active float64
if index+1 <= int(validatorSetLength) {
active = 1
} else {
active = 0
}
validatorsIsActiveGauge.With(prometheus.Labels{
"address": validator.OperatorAddress,
"moniker": validator.Description.Moniker,
}).Set(active)
}
}
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
sublogger.Info().
Str("method", "GET").
Str("endpoint", "/metrics/validators").
Float64("request-time", time.Since(requestStart).Seconds()).
Msg("Request processed")
}