-
Notifications
You must be signed in to change notification settings - Fork 2
/
querier.go
89 lines (70 loc) · 2.48 KB
/
querier.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
package slashing
import (
"fmt"
abci "github.com/hdac-io/tendermint/abci/types"
"github.com/hdac-io/friday/client"
"github.com/hdac-io/friday/codec"
sdk "github.com/hdac-io/friday/types"
)
// NewQuerier creates a new querier for slashing clients.
func NewQuerier(k Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, sdk.Error) {
switch path[0] {
case QueryParameters:
return queryParams(ctx, k)
case QuerySigningInfo:
return querySigningInfo(ctx, req, k)
case QuerySigningInfos:
return querySigningInfos(ctx, req, k)
default:
return nil, sdk.ErrUnknownRequest("unknown staking query endpoint")
}
}
}
func queryParams(ctx sdk.Context, k Keeper) ([]byte, sdk.Error) {
params := k.GetParams(ctx)
res, err := codec.MarshalJSONIndent(ModuleCdc, params)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal JSON", err.Error()))
}
return res, nil
}
func querySigningInfo(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
var params QuerySigningInfoParams
err := ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
}
signingInfo, found := k.getValidatorSigningInfo(ctx, params.ConsAddress)
if !found {
return nil, ErrNoSigningInfoFound(DefaultCodespace, params.ConsAddress)
}
res, err := codec.MarshalJSONIndent(ModuleCdc, signingInfo)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", err.Error()))
}
return res, nil
}
func querySigningInfos(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
var params QuerySigningInfosParams
err := ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
}
var signingInfos []ValidatorSigningInfo
k.IterateValidatorSigningInfos(ctx, func(consAddr sdk.ConsAddress, info ValidatorSigningInfo) (stop bool) {
signingInfos = append(signingInfos, info)
return false
})
start, end := client.Paginate(len(signingInfos), params.Page, params.Limit, int(k.sk.MaxValidators(ctx)))
if start < 0 || end < 0 {
signingInfos = []ValidatorSigningInfo{}
} else {
signingInfos = signingInfos[start:end]
}
res, err := codec.MarshalJSONIndent(ModuleCdc, signingInfos)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", err.Error()))
}
return res, nil
}