/
grpc_query.go
464 lines (423 loc) · 14.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package keeper
import (
"context"
"encoding/hex"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/tendermint/tendermint/crypto/secp256k1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/bandprotocol/chain/v2/x/oracle/types"
)
// 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{}
// Counts queries the number of data sources, oracle scripts, and requests.
func (k Querier) Counts(c context.Context, req *types.QueryCountsRequest) (*types.QueryCountsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
return &types.QueryCountsResponse{
DataSourceCount: k.GetDataSourceCount(ctx),
OracleScriptCount: k.GetOracleScriptCount(ctx),
RequestCount: k.GetRequestCount(ctx)},
nil
}
// Data queries the data source or oracle script script for given file hash.
func (k Querier) Data(c context.Context, req *types.QueryDataRequest) (*types.QueryDataResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
data, err := k.fileCache.GetFile(req.DataHash)
if err != nil {
return nil, err
}
return &types.QueryDataResponse{Data: data}, nil
}
// DataSource queries data source info for given data source id.
func (k Querier) DataSource(
c context.Context,
req *types.QueryDataSourceRequest,
) (*types.QueryDataSourceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
ds, err := k.GetDataSource(ctx, types.DataSourceID(req.DataSourceId))
if err != nil {
return nil, err
}
return &types.QueryDataSourceResponse{DataSource: &ds}, nil
}
// OracleScript queries oracle script info for given oracle script id.
func (k Querier) OracleScript(
c context.Context,
req *types.QueryOracleScriptRequest,
) (*types.QueryOracleScriptResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
os, err := k.GetOracleScript(ctx, types.OracleScriptID(req.OracleScriptId))
if err != nil {
return nil, err
}
return &types.QueryOracleScriptResponse{OracleScript: &os}, nil
}
// Request queries request info for given request id.
func (k Querier) Request(c context.Context, req *types.QueryRequestRequest) (*types.QueryRequestResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
rid := types.RequestID(req.RequestId)
request, err := k.GetRequest(ctx, rid)
if err != nil {
lastExpired := k.GetRequestLastExpired(ctx)
if rid > lastExpired {
return nil, status.Error(
codes.NotFound,
fmt.Sprintf(
"unable to get request from chain: request id (%d) > latest expired request id (%d)",
rid,
lastExpired,
),
)
}
result := k.MustGetResult(ctx, rid)
return &types.QueryRequestResponse{Request: nil, Reports: nil, Result: &result}, nil
}
reports := k.GetReports(ctx, rid)
if !k.HasResult(ctx, rid) {
return &types.QueryRequestResponse{Request: &request, Reports: reports, Result: nil}, nil
}
result := k.MustGetResult(ctx, rid)
return &types.QueryRequestResponse{Request: &request, Reports: reports, Result: &result}, nil
}
func (k Querier) PendingRequests(
c context.Context,
req *types.QueryPendingRequestsRequest,
) (*types.QueryPendingRequestsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
valAddress, err := sdk.ValAddressFromBech32(req.ValidatorAddress)
if err != nil {
return nil, status.Error(
codes.InvalidArgument,
fmt.Sprintf("unable to parse given validator address: %v", err),
)
}
lastExpired := k.GetRequestLastExpired(ctx)
requestCount := k.GetRequestCount(ctx)
var pendingIDs []uint64
for id := lastExpired + 1; uint64(id) <= requestCount; id++ {
oracleReq := k.MustGetRequest(ctx, id)
// If all validators reported on this request, then skip it.
reports := k.GetReports(ctx, id)
if len(reports) == len(oracleReq.RequestedValidators) {
continue
}
// Skip if validator hasn't been assigned or has been reported.
// If the validator isn't in requested validators set, then skip it.
isInValidatorSet := false
for _, v := range oracleReq.RequestedValidators {
val, err := sdk.ValAddressFromBech32(v)
if err != nil {
return nil, status.Error(
codes.Internal,
fmt.Sprintf("unable to parse validator address in requested validators %v: %v", v, err),
)
}
if valAddress.Equals(val) {
isInValidatorSet = true
break
}
}
if !isInValidatorSet {
continue
}
// If the validator has reported, then skip it.
reported := false
for _, r := range reports {
val, err := sdk.ValAddressFromBech32(r.Validator)
if err != nil {
return nil, status.Error(
codes.Internal,
fmt.Sprintf("unable to parse validator address in requested validators %v: %v", r.Validator, err),
)
}
if valAddress.Equals(val) {
reported = true
break
}
}
if reported {
continue
}
pendingIDs = append(pendingIDs, uint64(id))
}
return &types.QueryPendingRequestsResponse{RequestIDs: pendingIDs}, nil
}
// Validator queries oracle info of validator for given validator
// address.
func (k Querier) Validator(
c context.Context,
req *types.QueryValidatorRequest,
) (*types.QueryValidatorResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
val, err := sdk.ValAddressFromBech32(req.ValidatorAddress)
if err != nil {
return nil, err
}
status := k.GetValidatorStatus(ctx, val)
if err != nil {
return nil, err
}
return &types.QueryValidatorResponse{Status: &status}, nil
}
// IsReporter queries grant of account on this validator
func (k Querier) IsReporter(
c context.Context,
req *types.QueryIsReporterRequest,
) (*types.QueryIsReporterResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
val, err := sdk.ValAddressFromBech32(req.ValidatorAddress)
if err != nil {
return nil, err
}
rep, err := sdk.AccAddressFromBech32(req.ReporterAddress)
if err != nil {
return nil, err
}
return &types.QueryIsReporterResponse{IsReporter: k.Keeper.IsReporter(ctx, val, rep)}, nil
}
// Reporters queries 100 gratees of a given validator address and filter for reporter.
func (k Querier) Reporters(
c context.Context,
req *types.QueryReportersRequest,
) (*types.QueryReportersResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
val, err := sdk.ValAddressFromBech32(req.ValidatorAddress)
if err != nil {
return nil, err
}
granter := sdk.AccAddress(val).String()
granterGrantsRequest := &authz.QueryGranterGrantsRequest{
Granter: granter,
}
granterGrantsRes, err := k.authzKeeper.GranterGrants(c, granterGrantsRequest)
if err != nil {
return nil, err
}
reporters := make([]string, 0)
for _, rep := range granterGrantsRes.Grants {
if rep.Authorization.GetCachedValue().(authz.Authorization).MsgTypeURL() == sdk.MsgTypeURL(
&types.MsgReportData{},
) &&
rep.Expiration.After(ctx.BlockTime()) {
reporters = append(reporters, rep.Grantee)
}
}
return &types.QueryReportersResponse{Reporter: reporters}, nil
}
// ActiveValidators queries all active oracle validators.
func (k Querier) ActiveValidators(
c context.Context,
req *types.QueryActiveValidatorsRequest,
) (*types.QueryActiveValidatorsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
result := types.QueryActiveValidatorsResponse{}
k.stakingKeeper.IterateBondedValidatorsByPower(ctx,
func(idx int64, val stakingtypes.ValidatorI) (stop bool) {
if k.GetValidatorStatus(ctx, val.GetOperator()).IsActive {
result.Validators = append(result.Validators, &types.ActiveValidator{
Address: val.GetOperator().String(),
Power: val.GetTokens().Uint64(),
})
}
return false
})
return &result, nil
}
// Params queries the oracle parameters.
func (k Querier) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}
// RequestSearch queries the latest request that match the given input.
func (k Querier) RequestSearch(
c context.Context,
req *types.QueryRequestSearchRequest,
) (*types.QueryRequestSearchResponse, error) {
return nil, status.Error(codes.Unimplemented, "This feature can be taken from extra/rest branch")
}
// RequestPrice queries the latest price on standard price reference oracle
// script.
func (k Querier) RequestPrice(
c context.Context,
req *types.QueryRequestPriceRequest,
) (*types.QueryRequestPriceResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "This feature can be taken from extra/rest branch")
}
// RequestVerification verifies oracle request for validation before executing data sources
func (k Querier) RequestVerification(
c context.Context,
req *types.QueryRequestVerificationRequest,
) (*types.QueryRequestVerificationResponse, error) {
// Request should not be empty
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
// Provided chain ID should match current chain ID
if ctx.ChainID() != req.ChainId {
return nil, status.Error(
codes.FailedPrecondition,
fmt.Sprintf(
"provided chain ID does not match the validator's chain ID; expected %s, got %s",
ctx.ChainID(),
req.ChainId,
),
)
}
// Provided validator's address should be valid
validator, err := sdk.ValAddressFromBech32(req.Validator)
if err != nil {
return nil, status.Error(
codes.InvalidArgument,
fmt.Sprintf("unable to parse validator address: %s", err.Error()),
)
}
// Provided signature should be valid, which means this query request should be signed by the provided reporter
pk, err := hex.DecodeString(req.Reporter)
if err != nil || len(pk) != secp256k1.PubKeySize {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("unable to get reporter's public key"))
}
reporterPubKey := secp256k1.PubKey(pk[:])
requestVerificationContent := types.NewRequestVerification(
req.ChainId,
validator,
types.RequestID(req.RequestId),
types.ExternalID(req.ExternalId),
types.DataSourceID(req.DataSourceId),
)
signByte := requestVerificationContent.GetSignBytes()
if !reporterPubKey.VerifySignature(signByte, req.Signature) {
return nil, status.Error(codes.Unauthenticated, "invalid reporter's signature")
}
// Provided reporter should be authorized by the provided validator
reporter := sdk.AccAddress(reporterPubKey.Address().Bytes())
if !k.Keeper.IsReporter(ctx, validator, reporter) {
return nil, status.Error(
codes.PermissionDenied,
fmt.Sprintf("%s is not an authorized reporter of %s", reporter, req.Validator),
)
}
// Provided request should exist on chain
request, err := k.GetRequest(ctx, types.RequestID(req.RequestId))
if err != nil {
// return uncertain result if request id is in range of max delay
if req.RequestId-k.GetRequestCount(ctx) > 0 && req.RequestId-k.GetRequestCount(ctx) <= req.MaxDelay {
return &types.QueryRequestVerificationResponse{
ChainId: req.ChainId,
Validator: req.Validator,
RequestId: req.RequestId,
ExternalId: req.ExternalId,
DataSourceId: req.DataSourceId,
IsDelay: true,
}, nil
}
return nil, status.Error(codes.NotFound, fmt.Sprintf("unable to get request from chain: %s", err.Error()))
}
// Provided validator should be assigned to response to the request
isValidatorAssigned := false
for _, requestedValidator := range request.RequestedValidators {
v, _ := sdk.ValAddressFromBech32(requestedValidator)
if validator.Equals(v) {
isValidatorAssigned = true
break
}
}
if !isValidatorAssigned {
return nil, status.Error(
codes.PermissionDenied,
fmt.Sprintf("%s is not assigned for request ID %d", validator, req.RequestId),
)
}
// Provided external ID should be required by the request determined by oracle script
var dataSourceID *types.DataSourceID
for _, rawRequest := range request.RawRequests {
if rawRequest.ExternalID == types.ExternalID(req.ExternalId) {
dataSourceID = &rawRequest.DataSourceID
break
}
}
if dataSourceID == nil {
return nil, status.Error(
codes.InvalidArgument,
fmt.Sprintf(
"no data source required by the request %d found which relates to the external data source with ID %d.",
req.RequestId,
req.ExternalId,
),
)
}
if *dataSourceID != types.DataSourceID(req.DataSourceId) {
return nil, status.Error(
codes.InvalidArgument,
fmt.Sprintf(
"data source required by the request %d which relates to the external data source with ID %d is not match with data source id provided in request.",
req.RequestId,
req.ExternalId,
),
)
}
// Provided validator should not have reported data for the request
reports := k.GetReports(ctx, types.RequestID(req.RequestId))
isValidatorReported := false
for _, report := range reports {
reportVal, _ := sdk.ValAddressFromBech32(report.Validator)
if reportVal.Equals(validator) {
isValidatorReported = true
break
}
}
if isValidatorReported {
return nil, status.Error(
codes.AlreadyExists,
fmt.Sprintf("validator %s already submitted data report for this request", validator),
)
}
// The request should not be expired
if request.RequestHeight+int64(k.ExpirationBlockCount(ctx)) < ctx.BlockHeader().Height {
return nil, status.Error(
codes.DeadlineExceeded,
fmt.Sprintf("Request with ID %d is already expired", req.RequestId),
)
}
return &types.QueryRequestVerificationResponse{
ChainId: req.ChainId,
Validator: req.Validator,
RequestId: req.RequestId,
ExternalId: req.ExternalId,
DataSourceId: uint64(*dataSourceID),
IsDelay: false,
}, nil
}