Skip to content

Commit 915fefc

Browse files
feat: allow querying each signing info (#17)
* feat: allow querying each signing info * chore: gofumpt * chore: return also validators count
1 parent 60b7b99 commit 915fefc

File tree

5 files changed

+76
-12
lines changed

5 files changed

+76
-12
lines changed

config.example.toml

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ include-validators = ["cosmosvaloperxxx"]
1616
# List of validators to exclude from monitoring, with it specified, all validators except mentioned
1717
# will be monitored. Cannot be used together with include-validators.
1818
exclude-validators = ["cosmosvaloperyyy"]
19+
# Some chains, likely cosmos-sdk, return signing-info without an address, making it impossible
20+
# to match some validators with their signing info, as a result, the validators list returned
21+
# by Telegram bot and the list of monitored validators isn't full. This flag, instead of querying
22+
# all signing infos with a single request, does a query for each validator asking for its signing info.
23+
# This is super ugly and resource consuming, but probably is the only solution. If you face
24+
# the issue of not validators appearing in the list, try switching it to true.
25+
# Defaults to false.
26+
query-each-signing-info = false
1927

2028
# Node config.
2129
[node]

config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ type AppConfig struct {
5555
ChainInfoConfig ChainInfoConfig `toml:"chain-info"`
5656
NodeConfig NodeConfig `toml:"node"`
5757

58-
Interval int `toml:"interval" default:"120"`
58+
QueryEachSigningInfo bool `toml:"query-each-signing-info"`
59+
Interval int `toml:"interval" default:"120"`
5960

6061
Prefix string `toml:"bech-prefix"`
6162
ValidatorPrefix string `toml:"bech-validator-prefix"`

grpc.go

+64-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ import (
1414
)
1515

1616
type TendermintGRPC struct {
17-
NodeConfig NodeConfig
18-
Limit uint64
19-
Client *grpc.ClientConn
20-
Logger zerolog.Logger
21-
Registry codectypes.InterfaceRegistry
17+
NodeConfig NodeConfig
18+
Limit uint64
19+
Client *grpc.ClientConn
20+
Logger zerolog.Logger
21+
Registry codectypes.InterfaceRegistry
22+
QueryEachSigningInfo bool
2223
}
2324

2425
func NewTendermintGRPC(
2526
nodeConfig NodeConfig,
2627
registry codectypes.InterfaceRegistry,
28+
queryEachSigningInfo bool,
2729
logger *zerolog.Logger,
2830
) *TendermintGRPC {
2931
grpcConn, err := grpc.Dial(
@@ -35,11 +37,12 @@ func NewTendermintGRPC(
3537
}
3638

3739
return &TendermintGRPC{
38-
NodeConfig: nodeConfig,
39-
Limit: 1000,
40-
Logger: logger.With().Str("component", "grpc").Logger(),
41-
Client: grpcConn,
42-
Registry: registry,
40+
NodeConfig: nodeConfig,
41+
Limit: 1000,
42+
Logger: logger.With().Str("component", "grpc").Logger(),
43+
Client: grpcConn,
44+
Registry: registry,
45+
QueryEachSigningInfo: queryEachSigningInfo,
4346
}
4447
}
4548

@@ -75,6 +78,10 @@ func (grpc *TendermintGRPC) GetSlashingParams() SlashingParams {
7578
}
7679

7780
func (grpc *TendermintGRPC) GetValidatorsState() (ValidatorsState, error) {
81+
if grpc.QueryEachSigningInfo {
82+
return grpc.GetValidatorsStateWithEachSigningInfo()
83+
}
84+
7885
slashingClient := slashingtypes.NewQueryClient(grpc.Client)
7986
signingInfos, err := slashingClient.SigningInfos(
8087
context.Background(),
@@ -135,6 +142,53 @@ func (grpc *TendermintGRPC) GetValidatorsState() (ValidatorsState, error) {
135142
return newState, nil
136143
}
137144

145+
func (grpc *TendermintGRPC) GetValidatorsStateWithEachSigningInfo() (ValidatorsState, error) {
146+
slashingClient := slashingtypes.NewQueryClient(grpc.Client)
147+
stakingClient := stakingtypes.NewQueryClient(grpc.Client)
148+
validatorsResult, err := stakingClient.Validators(
149+
context.Background(),
150+
&stakingtypes.QueryValidatorsRequest{
151+
Pagination: &querytypes.PageRequest{
152+
Limit: grpc.Limit,
153+
},
154+
},
155+
)
156+
if err != nil {
157+
grpc.Logger.Error().Err(err).Msg("Could not query for validators")
158+
return nil, err
159+
}
160+
161+
newState := make(ValidatorsState, len(validatorsResult.Validators))
162+
for _, validator := range validatorsResult.Validators {
163+
err := validator.UnpackInterfaces(grpc.Registry)
164+
if err != nil {
165+
grpc.Logger.Error().Err(err).Msg("Could not unpack interface")
166+
return nil, err
167+
}
168+
169+
pubKey, err := validator.GetConsAddr()
170+
if err != nil {
171+
grpc.Logger.Error().Err(err).Msg("Could not get cons addr")
172+
return nil, err
173+
}
174+
175+
info, err := slashingClient.SigningInfo(
176+
context.Background(),
177+
&slashingtypes.QuerySigningInfoRequest{
178+
ConsAddress: pubKey.String(),
179+
},
180+
)
181+
if err != nil {
182+
grpc.Logger.Error().Err(err).Msg("Could not query for signing info")
183+
continue
184+
}
185+
186+
newState[pubKey.String()] = NewValidatorState(validator, info.ValSigningInfo)
187+
}
188+
189+
return newState, nil
190+
}
191+
138192
func (grpc *TendermintGRPC) GetValidator(address string) (stakingtypes.Validator, error) {
139193
stakingClient := stakingtypes.NewQueryClient(grpc.Client)
140194

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Execute(configPath string) {
4343
interfaceRegistry := encCfg.InterfaceRegistry
4444

4545
rpc := NewTendermintRPC(appConfig.NodeConfig, log)
46-
grpc := NewTendermintGRPC(appConfig.NodeConfig, interfaceRegistry, log)
46+
grpc := NewTendermintGRPC(appConfig.NodeConfig, interfaceRegistry, appConfig.QueryEachSigningInfo, log)
4747
slashingParams := grpc.GetSlashingParams()
4848

4949
params := Params{

telegram.go

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ func (r *TelegramReporter) getValidatorWithMissedBlocksSerialized(state Validato
407407

408408
func (r *TelegramReporter) getValidatorsWithMissedBlocksSerialized(state []ValidatorState) (string, error) {
409409
var sb strings.Builder
410+
sb.WriteString(fmt.Sprintf("<strong>Total validators:</strong> %d\n", len(state)))
410411

411412
for _, validator := range state {
412413
group, err := r.AppConfig.MissedBlocksGroups.GetGroup(validator.MissedBlocks)

0 commit comments

Comments
 (0)