-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
202 lines (186 loc) · 6.29 KB
/
validator.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
package main
import (
"context"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/types/bech32"
slashing "github.com/cosmos/cosmos-sdk/x/slashing/types"
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)
// ValInfo holds most of the stats/info used for secondary alarms. It is refreshed roughly every minute.
type ValInfo struct {
Moniker string `json:"moniker"`
Bonded bool `json:"bonded"`
Jailed bool `json:"jailed"`
Tombstoned bool `json:"tombstoned"`
Missed int64 `json:"missed"`
Window int64 `json:"window"`
Conspub []byte `json:"conspub"`
Valcons string `json:"valcons"`
}
// GetValInfo the first bool is used to determine if extra information about the validator should be printed.
func (c *Config) GetValInfo(first bool) (err error) {
if c.client == nil {
return errors.New("nil rpc client")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if c.valInfo == nil {
c.valInfo = &ValInfo{}
}
// Fetch info from /cosmos.staking.v1beta1.Query/Validator
// it's easier to ask people to provide valoper since it's readily available on
// explorers, so make it easy and lookup the consensus key for them.
c.valInfo.Conspub, c.valInfo.Moniker, c.valInfo.Jailed, c.valInfo.Bonded, err = getVal(ctx, c.rpcClient, c.ValAddress)
if err != nil {
return
}
if first && c.valInfo.Bonded {
c.logger.Infof("⚙️ found %s (%s) in validator set", c.ValAddress, c.valInfo.Moniker)
} else if first && !c.valInfo.Bonded {
c.logger.Warningf("❌ %s (%s) is INACTIVE", c.ValAddress, c.valInfo.Moniker)
}
if strings.Contains(c.ValAddress, "valcons") {
// no need to change prefix for signing info query
c.valInfo.Valcons = c.ValAddress
} else {
// need to know the prefix for when we serialize the slashing info query, this is too fragile.
// for now, we perform specific chain overrides based on known values because the valoper is used
// in so many places.
var prefix string
split := strings.Split(c.ValAddress, "valoper")
if len(split) != 2 {
if pre, ok := altValopers.getAltPrefix(c.ValAddress); ok {
c.valInfo.Valcons, err = bech32.ConvertAndEncode(pre, c.valInfo.Conspub[:20])
if err != nil {
return
}
} else {
err = errors.New("❓ could not determine bech32 prefix from valoper address: " + c.ValAddress)
return
}
} else {
prefix = split[0] + "valcons"
c.valInfo.Valcons, err = bech32.ConvertAndEncode(prefix, c.valInfo.Conspub[:20])
if err != nil {
return
}
}
if first {
c.logger.Info("⚙️", c.ValAddress[:20], "... is using consensus key:", c.valInfo.Valcons)
}
}
// get current signing information (tombstoned, missed block count)
qSigning := slashing.QuerySigningInfoRequest{ConsAddress: c.valInfo.Valcons}
b, err := qSigning.Marshal()
if err != nil {
return
}
resp, err := c.rpcClient.ABCIQuery(ctx, "/cosmos.slashing.v1beta1.Query/SigningInfo", b)
if resp == nil || resp.Response.Value == nil {
err = errors.New("could not query validator slashing status, got empty response")
return
}
slash := &slashing.QuerySigningInfoResponse{}
err = slash.Unmarshal(resp.Response.Value)
if err != nil {
return
}
c.valInfo.Tombstoned = slash.ValSigningInfo.Tombstoned
if c.valInfo.Tombstoned {
c.logger.Errorf("❗️☠️ %s (%s) is tombstoned 🪦❗️", c.ValAddress, c.valInfo.Moniker)
}
c.valInfo.Missed = slash.ValSigningInfo.MissedBlocksCounter
c.statsChan <- c.mkUpdate(metricWindowMissed, float64(c.valInfo.Missed), "")
// finally get the signed blocks window
if c.valInfo.Window == 0 {
qParams := &slashing.QueryParamsRequest{}
b, err = qParams.Marshal()
if err != nil {
return
}
resp, err = c.rpcClient.ABCIQuery(ctx, "/cosmos.slashing.v1beta1.Query/Params", b)
if err != nil {
return
}
if resp.Response.Value == nil {
err = errors.New("🛑 could not query slashing params, got empty response")
return
}
params := &slashing.QueryParamsResponse{}
err = params.Unmarshal(resp.Response.Value)
if err != nil {
return
}
if first {
c.statsChan <- c.mkUpdate(metricWindowSize, float64(params.Params.SignedBlocksWindow), "")
c.statsChan <- c.mkUpdate(metricTotalNodes, float64(len(c.Nodes)), "")
}
c.valInfo.Window = params.Params.SignedBlocksWindow
}
return
}
// getVal returns the public key, moniker, and if the validator is jailed.
func getVal(ctx context.Context, client *rpchttp.HTTP, valoper string) (pub []byte, moniker string, jailed, bonded bool, err error) {
if strings.Contains(valoper, "valcons") {
_, bz, err := bech32.DecodeAndConvert(valoper)
if err != nil {
return nil, "", false, false, errors.New("could not decode and convert your address" + valoper)
}
hexAddress := fmt.Sprintf("%X", bz)
return ToBytes(hexAddress), valoper, false, true, nil
}
q := staking.QueryValidatorRequest{
ValidatorAddr: valoper,
}
b, err := q.Marshal()
if err != nil {
return
}
resp, err := client.ABCIQuery(ctx, "/cosmos.staking.v1beta1.Query/Validator", b)
if err != nil {
return
}
if resp.Response.Value == nil {
return nil, "", false, false, errors.New("could not find validator " + valoper)
}
val := &staking.QueryValidatorResponse{}
err = val.Unmarshal(resp.Response.Value)
if err != nil {
return
}
if val.Validator.ConsensusPubkey == nil {
return nil, "", false, false, errors.New("got invalid consensus pubkey for " + valoper)
}
pubBytes := make([]byte, 0)
switch val.Validator.ConsensusPubkey.TypeUrl {
case "/cosmos.crypto.ed25519.PubKey":
pk := ed25519.PubKey{}
err = pk.Unmarshal(val.Validator.ConsensusPubkey.Value)
if err != nil {
return
}
pubBytes = pk.Address().Bytes()
case "/cosmos.crypto.secp256k1.PubKey":
pk := secp256k1.PubKey{}
err = pk.Unmarshal(val.Validator.ConsensusPubkey.Value)
if err != nil {
return
}
pubBytes = pk.Address().Bytes()
}
if len(pubBytes) == 0 {
return nil, "", false, false, errors.New("could not get pubkey for" + valoper)
}
return pubBytes, val.Validator.GetMoniker(), val.Validator.Jailed, val.Validator.Status == 3, nil
}
func ToBytes(address string) []byte {
bz, _ := hex.DecodeString(strings.ToLower(address))
return bz
}