-
Notifications
You must be signed in to change notification settings - Fork 170
/
grpc_query.go
246 lines (214 loc) · 8.1 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
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
package keeper
import (
"context"
"errors"
"cosmossdk.io/math"
errorsmod "cosmossdk.io/errors"
"github.com/babylonchain/babylon/x/epoching/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Querier is used as Keeper will have duplicate methods if used directly, and gRPC names take precedence over keeper
type Querier struct {
Keeper
}
var _ types.QueryServer = Querier{}
func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil
}
// CurrentEpoch handles the QueryCurrentEpochRequest query
func (k Keeper) CurrentEpoch(c context.Context, req *types.QueryCurrentEpochRequest) (*types.QueryCurrentEpochResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
epoch := k.GetEpoch(ctx)
resp := &types.QueryCurrentEpochResponse{
CurrentEpoch: epoch.EpochNumber,
EpochBoundary: epoch.GetLastBlockHeight(),
}
return resp, nil
}
// EpochInfo handles the QueryEpochInfoRequest query
func (k Keeper) EpochInfo(c context.Context, req *types.QueryEpochInfoRequest) (*types.QueryEpochInfoResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
epoch, err := k.GetHistoricalEpoch(ctx, req.EpochNum)
if err != nil {
return nil, err
}
return &types.QueryEpochInfoResponse{
Epoch: epoch.ToResponse(),
}, nil
}
// EpochsInfo handles the QueryEpochsInfoRequest query
func (k Keeper) EpochsInfo(c context.Context, req *types.QueryEpochsInfoRequest) (*types.QueryEpochsInfoResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
epochInfoStore := k.epochInfoStore(ctx)
epochs := []*types.EpochResponse{}
pageRes, err := query.Paginate(epochInfoStore, req.Pagination, func(key, value []byte) error {
// unmarshal to epoch metadata
var epoch types.Epoch
if err := k.cdc.Unmarshal(value, &epoch); err != nil {
return err
}
// append to epochs list
epochs = append(epochs, epoch.ToResponse())
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryEpochsInfoResponse{
Epochs: epochs,
Pagination: pageRes,
}, nil
}
// EpochMsgs handles the QueryEpochMsgsRequest query
func (k Keeper) EpochMsgs(c context.Context, req *types.QueryEpochMsgsRequest) (*types.QueryEpochMsgsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
epoch := k.GetEpoch(ctx)
if epoch.EpochNumber < req.EpochNum {
return nil, types.ErrUnknownEpochNumber
}
var msgs []*types.QueuedMessageResponse
epochMsgsStore := k.msgQueueStore(ctx, req.EpochNum)
// handle pagination
// TODO (non-urgent): the epoch might end between pagination requests, leading inconsistent results by the time someone gets to the end. Possible fixes:
// - We could add the epoch number to the query, and return nothing if the current epoch number is different. But it's a bit of pain to have to set it and not know why there's no result.
// - We could not reset the key to 0 when the queue is cleared, and just keep incrementing the ID forever. That way when the next query comes, it might skip some records that have been deleted, then resume from the next available record which has a higher key than the value in the pagination data structure.
// - We can do nothing, in which case some records that have been inserted after the delete might be skipped because their keys are lower than the pagionation state.
pageRes, err := query.Paginate(epochMsgsStore, req.Pagination, func(key, value []byte) error {
// unmarshal to queuedMsg
var sdkMsg sdk.Msg
if err := k.cdc.UnmarshalInterface(value, &sdkMsg); err != nil {
return err
}
queuedMsg, ok := sdkMsg.(*types.QueuedMessage)
if !ok {
return errors.New("invalid queue message")
}
// append to msgs
msgs = append(msgs, queuedMsg.ToResponse())
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryEpochMsgsResponse{
Msgs: msgs,
Pagination: pageRes,
}, nil
}
// LatestEpochMsgs handles the QueryLatestEpochMsgsRequest query
// TODO: test this API
func (k Keeper) LatestEpochMsgs(c context.Context, req *types.QueryLatestEpochMsgsRequest) (*types.QueryLatestEpochMsgsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
if req.EpochCount == 0 {
return nil, errorsmod.Wrapf(
sdkerrors.ErrInvalidRequest, "epoch_count should be specified and be larger than zero",
)
}
// the API will return epoch msgs between [max(1, end_epoch-epoch_count+1), end_epoch].
// NOTE: epoch 0 does not have any queued msg
endEpoch := req.EndEpoch
// If not specified, endEpoch will be the current epoch
if endEpoch == 0 {
endEpoch = k.GetEpoch(ctx).EpochNumber
}
beginEpoch := uint64(1)
if req.EpochCount < endEpoch { // i.e., 1 < endEpoch - req.EpochCount + 1
beginEpoch = endEpoch - req.EpochCount + 1
}
latestEpochMsgs := []*types.QueuedMessageList{}
// iterate over queueLenStore since we only need to iterate over the epoch number
queueLenStore := k.msgQueueLengthStore(ctx)
pageRes, err := query.FilteredPaginate(queueLenStore, req.Pagination, func(key []byte, _ []byte, accumulate bool) (bool, error) {
// unmarshal to epoch number
epochNumber := sdk.BigEndianToUint64(key)
// only return queued msgs within [beginEpoch, endEpoch]
if epochNumber < beginEpoch || endEpoch < epochNumber {
return false, nil
}
if accumulate {
msgs := k.GetEpochMsgs(ctx, epochNumber)
msgList := &types.QueuedMessageList{
EpochNumber: epochNumber,
Msgs: types.NewQueuedMessagesResponse(msgs),
}
latestEpochMsgs = append(latestEpochMsgs, msgList)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
resp := &types.QueryLatestEpochMsgsResponse{
LatestEpochMsgs: latestEpochMsgs,
Pagination: pageRes,
}
return resp, nil
}
// ValidatorLifecycle handles the QueryValidatorLifecycleRequest query
// TODO: test this API
func (k Keeper) ValidatorLifecycle(c context.Context, req *types.QueryValidatorLifecycleRequest) (*types.QueryValidatorLifecycleResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
valAddr, err := sdk.ValAddressFromBech32(req.ValAddr)
if err != nil {
return nil, err
}
lc := k.GetValLifecycle(ctx, valAddr)
return &types.QueryValidatorLifecycleResponse{
ValAddr: lc.ValAddr,
ValLife: types.NewValsetUpdateResponses(lc.ValLife),
}, nil
}
// DelegationLifecycle handles the QueryDelegationLifecycleRequest query
// TODO: test this API
func (k Keeper) DelegationLifecycle(c context.Context, req *types.QueryDelegationLifecycleRequest) (*types.QueryDelegationLifecycleResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
delAddr, err := sdk.AccAddressFromBech32(req.DelAddr)
if err != nil {
return nil, err
}
lc := k.GetDelegationLifecycle(ctx, delAddr)
return &types.QueryDelegationLifecycleResponse{
DelLife: lc,
}, nil
}
func (k Keeper) EpochValSet(c context.Context, req *types.QueryEpochValSetRequest) (*types.QueryEpochValSetResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
epoch := k.GetEpoch(ctx)
if epoch.EpochNumber < req.EpochNum {
return nil, types.ErrUnknownEpochNumber
}
totalVotingPower := k.GetTotalVotingPower(ctx, epoch.EpochNumber)
vals := []*types.Validator{}
epochValSetStore := k.valSetStore(ctx, epoch.EpochNumber)
pageRes, err := query.Paginate(epochValSetStore, req.Pagination, func(key, value []byte) error {
// Here key is the validator's ValAddress, and value is the voting power
var power math.Int
if err := power.Unmarshal(value); err != nil {
panic(errorsmod.Wrap(types.ErrUnmarshal, err.Error())) // this only happens upon a programming error
}
val := types.Validator{
Addr: key,
Power: power.Int64(),
}
// append to msgs
vals = append(vals, &val)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
resp := &types.QueryEpochValSetResponse{
Validators: vals,
TotalVotingPower: totalVotingPower,
Pagination: pageRes,
}
return resp, nil
}