-
Notifications
You must be signed in to change notification settings - Fork 132
/
grpc_query.go
81 lines (66 loc) · 2.04 KB
/
grpc_query.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
package keeper
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/palomachain/paloma/x/metrix/types"
)
var _ types.QueryServer = Keeper{}
// Validator implements types.QueryServer.
func (k Keeper) Validator(goCtx context.Context, req *types.QueryValidatorRequest) (*types.QueryValidatorResponse, error) {
if req == nil {
return nil, fmt.Errorf("invalid request")
}
valAddr, err := sdk.ValAddressFromBech32(req.ValAddress)
if err != nil {
return nil, fmt.Errorf("query validator: %w", err)
}
ctx := sdk.UnwrapSDKContext(goCtx)
metrics, err := k.GetValidatorMetrics(ctx, valAddr)
if err != nil {
return nil, fmt.Errorf("query metrics: %w", err)
}
if metrics == nil {
return nil, fmt.Errorf("no metrics found")
}
return &types.QueryValidatorResponse{
ValMetrics: *metrics,
}, nil
}
// Validators implements types.QueryServer.
func (k Keeper) Validators(goCtx context.Context, _ *types.Empty) (*types.QueryValidatorsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
metrics := make([]types.ValidatorMetrics, 0, 200)
if err := k.metrics.Iterate(ctx, func(_ []byte, m *types.ValidatorMetrics) bool {
if m != nil {
metrics = append(metrics, *m)
}
return true
}); err != nil {
return nil, fmt.Errorf("iterate metrics: %w", err)
}
return &types.QueryValidatorsResponse{
ValMetrics: metrics,
}, nil
}
// HistoricRelayData implements types.QueryServer.
func (k Keeper) HistoricRelayData(goCtx context.Context, req *types.QueryHistoricRelayDataRequest) (*types.QueryHistoricRelayDataResponse, error) {
if req == nil {
return nil, fmt.Errorf("invalid request")
}
valAddr, err := sdk.ValAddressFromBech32(req.ValAddress)
if err != nil {
return nil, fmt.Errorf("query validator: %w", err)
}
ctx := sdk.UnwrapSDKContext(goCtx)
history, err := k.GetValidatorHistory(ctx, valAddr)
if err != nil {
return nil, fmt.Errorf("query history: %w", err)
}
if history == nil {
return nil, fmt.Errorf("no history found")
}
return &types.QueryHistoricRelayDataResponse{
History: *history,
}, nil
}