-
Notifications
You must be signed in to change notification settings - Fork 1
/
querier.go
544 lines (485 loc) · 18.4 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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package keeper
import (
"encoding/hex"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/onomyprotocol/near-aurora-bridge/module/x/nab/types"
)
const (
// Valsets
// This retrieves a specific validator set by it's nonce
// used to compare what's on Ethereum with what's in Cosmos
// to perform slashing / validation of system consistency
QueryValsetRequest = "valsetRequest"
// Gets all the confirmation signatures for a given validator
// set, used by the relayer to package the validator set and
// it's signatures into an Ethereum transaction
QueryValsetConfirmsByNonce = "valsetConfirms"
// Gets the last N (where N is currently 5) validator sets that
// have been produced by the chain. Useful to see if any recently
// signed requests can be submitted.
QueryLastValsetRequests = "lastValsetRequests"
// Gets a list of unsigned valsets for a given validators delegate
// orchestrator address. Up to 100 are sent at a time
QueryLastPendingValsetRequestByAddr = "lastPendingValsetRequest"
QueryCurrentValset = "currentValset"
// TODO remove this, it's not used, getting one confirm at a time
// is mostly useless
QueryValsetConfirm = "valsetConfirm"
// used by the contract deployer script. GravityID is set in the Genesis
// file, then read by the contract deployer and deployed to Ethereum
// a unique GravityID ensures that even if the same validator set with
// the same keys is running on two chains these chains can have independent
// bridges
QueryGravityID = "gravityID"
// Batches
// note the current logic here constrains batch throughput to one
// batch (of any type) per Cosmos block.
// This retrieves a specific batch by it's nonce and token contract
// or in the case of a Cosmos originated address it's denom
QueryBatch = "batch"
// Get the last unsigned batch (of any denom) for the validators
// orchestrator to sign
QueryLastPendingBatchRequestByAddr = "lastPendingBatchRequest"
// gets the last 100 outgoing batches, regardless of denom, useful
// for a relayer to see what is available to relay
QueryOutgoingTxBatches = "lastBatches"
// Used by the relayer to package a batch with signatures required
// to submit to Ethereum
QueryBatchConfirms = "batchConfirms"
// Used to query all pending SendToEth transactions and fees available for each
// token type, a relayer can then estimate their potential profit when requesting
// a batch
QueryBatchFees = "batchFees"
// Logic calls
// note the current logic here constrains logic call throughput to one
// call (of any type) per Cosmos block.
// This retrieves a specific logic call by it's nonce and token contract
// or in the case of a Cosmos originated address it's denom
QueryLogicCall = "logicCall"
// Get the last unsigned logic call for the validators orchestrator
// to sign
QueryLastPendingLogicCallByAddr = "lastPendingLogicCall"
// gets the last 5 outgoing logic calls, regardless of denom, useful
// for a relayer to see what is available to relay
QueryOutgoingLogicCalls = "lastLogicCalls"
// Used by the relayer to package a logic call with signatures required
// to submit to Ethereum
QueryLogicCallConfirms = "logicCallConfirms"
// Token mapping
// This retrieves the denom which is represented by a given ERC20 contract
QueryERC20ToDenom = "ERC20ToDenom"
// This retrieves the ERC20 contract which represents a given denom
QueryDenomToERC20 = "DenomToERC20"
// Query pending transactions
QueryPendingSendToEth = "PendingSendToEth"
)
// NewQuerier is the module level router for state queries
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
switch path[0] {
// Valsets
case QueryCurrentValset:
return queryCurrentValset(ctx, keeper)
case QueryValsetRequest:
return queryValsetRequest(ctx, path[1:], keeper)
case QueryValsetConfirm:
return queryValsetConfirm(ctx, path[1:], keeper)
case QueryValsetConfirmsByNonce:
return queryAllValsetConfirms(ctx, path[1], keeper)
case QueryLastValsetRequests:
return lastValsetRequests(ctx, keeper)
case QueryLastPendingValsetRequestByAddr:
return lastPendingValsetRequest(ctx, path[1], keeper)
// Batches
case QueryBatch:
return queryBatch(ctx, path[1], path[2], keeper)
case QueryBatchConfirms:
return queryAllBatchConfirms(ctx, path[1], path[2], keeper)
case QueryLastPendingBatchRequestByAddr:
return lastPendingBatchRequest(ctx, path[1], keeper)
case QueryOutgoingTxBatches:
return lastBatchesRequest(ctx, keeper)
case QueryBatchFees:
return queryBatchFees(ctx, keeper)
// Logic calls
case QueryLogicCall:
return queryLogicCall(ctx, path[1], path[2], keeper)
case QueryLogicCallConfirms:
return queryAllLogicCallConfirms(ctx, path[1], path[2], keeper)
case QueryLastPendingLogicCallByAddr:
return lastPendingLogicCallRequest(ctx, path[1], keeper)
case QueryOutgoingLogicCalls:
return lastLogicCallRequests(ctx, keeper)
case QueryGravityID:
return queryGravityID(ctx, keeper)
// Token mappings
case QueryDenomToERC20:
return queryDenomToERC20(ctx, path[1], keeper)
case QueryERC20ToDenom:
return queryERC20ToDenom(ctx, path[1], keeper)
// Pending transactions
case QueryPendingSendToEth:
return queryPendingSendToEth(ctx, path[1], keeper)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
}
}
}
func queryValsetRequest(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
nonce, err := types.UInt64FromString(path[0])
if err != nil {
return nil, err
}
valset := keeper.GetValset(ctx, nonce)
if valset == nil {
return nil, nil
}
// TODO: replace these with the GRPC response types
// TODO: fix the use of module codec here
res, err := codec.MarshalJSONIndent(types.ModuleCdc, valset)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// allValsetConfirmsByNonce returns all the confirm messages for a given nonce
// When nothing found an empty json array is returned. No pagination.
func queryAllValsetConfirms(ctx sdk.Context, nonceStr string, keeper Keeper) ([]byte, error) {
nonce, err := types.UInt64FromString(nonceStr)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
var confirms []*types.MsgValsetConfirm
keeper.IterateValsetConfirmByNonce(ctx, nonce, func(_ []byte, c types.MsgValsetConfirm) bool {
confirms = append(confirms, &c)
return false
})
if len(confirms) == 0 {
return nil, nil
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, confirms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// allBatchConfirms returns all the confirm messages for a given nonce
// When nothing found an empty json array is returned. No pagination.
func queryAllBatchConfirms(ctx sdk.Context, nonceStr string, tokenContract string, keeper Keeper) ([]byte, error) {
nonce, err := types.UInt64FromString(nonceStr)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
var confirms []types.MsgConfirmBatch
keeper.IterateBatchConfirmByNonceAndTokenContract(ctx, nonce, tokenContract, func(_ []byte, c types.MsgConfirmBatch) bool {
confirms = append(confirms, c)
return false
})
if len(confirms) == 0 {
return nil, nil
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, confirms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
const maxValsetRequestsReturned = 5
// lastValsetRequests returns up to maxValsetRequestsReturned valsets from the store
func lastValsetRequests(ctx sdk.Context, keeper Keeper) ([]byte, error) {
var counter int
var valReq []*types.Valset
keeper.IterateValsets(ctx, func(_ []byte, val *types.Valset) bool {
valReq = append(valReq, val)
counter++
return counter >= maxValsetRequestsReturned
})
if len(valReq) == 0 {
return nil, nil
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, valReq)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// lastPendingValsetRequest gets a list of validator sets that this validator has not signed
// limited by 100 sets per request.
func lastPendingValsetRequest(ctx sdk.Context, operatorAddr string, keeper Keeper) ([]byte, error) {
addr, err := sdk.AccAddressFromBech32(operatorAddr)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "address invalid")
}
var pendingValsetReq []*types.Valset
keeper.IterateValsets(ctx, func(_ []byte, val *types.Valset) bool {
// foundConfirm is true if the operatorAddr has signed the valset we are currently looking at
foundConfirm := keeper.GetValsetConfirm(ctx, val.Nonce, addr) != nil
// if this valset has NOT been signed by operatorAddr, store it in pendingValsetReq
// and exit the loop
if !foundConfirm {
pendingValsetReq = append(pendingValsetReq, val)
}
// if we have more than 100 unconfirmed requests in
// our array we should exit, TODO pagination
if len(pendingValsetReq) > 100 {
return true
}
// return false to continue the loop
return false
})
if len(pendingValsetReq) == 0 {
return nil, nil
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, pendingValsetReq)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func queryCurrentValset(ctx sdk.Context, keeper Keeper) ([]byte, error) {
valset := keeper.GetCurrentValset(ctx)
res, err := codec.MarshalJSONIndent(types.ModuleCdc, valset)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryValsetConfirm returns the confirm msg for single orchestrator address and nonce
// When nothing found a nil value is returned
func queryValsetConfirm(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
nonce, err := types.UInt64FromString(path[0])
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
accAddress, err := sdk.AccAddressFromBech32(path[1])
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
valset := keeper.GetValsetConfirm(ctx, nonce, accAddress)
if valset == nil {
return nil, nil
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, *valset)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
type MultiSigUpdateResponse struct {
Valset types.Valset `json:"valset"`
Signatures [][]byte `json:"signatures,omitempty"`
}
// lastPendingBatchRequest gets the latest batch that has NOT been signed by operatorAddr
func lastPendingBatchRequest(ctx sdk.Context, operatorAddr string, keeper Keeper) ([]byte, error) {
addr, err := sdk.AccAddressFromBech32(operatorAddr)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "address invalid")
}
var pendingBatchReq *types.OutgoingTxBatch
keeper.IterateOutgoingTXBatches(ctx, func(_ []byte, batch *types.OutgoingTxBatch) bool {
foundConfirm := keeper.GetBatchConfirm(ctx, batch.BatchNonce, batch.TokenContract, addr) != nil
if !foundConfirm {
pendingBatchReq = batch
return true
}
return false
})
if pendingBatchReq == nil {
return nil, nil
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, pendingBatchReq)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// Gets MaxResults batches from store. Does not select by token type or anything
func lastBatchesRequest(ctx sdk.Context, keeper Keeper) ([]byte, error) {
var batches []*types.OutgoingTxBatch
keeper.IterateOutgoingTXBatches(ctx, func(_ []byte, batch *types.OutgoingTxBatch) bool {
batches = append(batches, batch)
return len(batches) == OutgoingTxBatchSize
})
if len(batches) == 0 {
return nil, nil
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, batches)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func queryBatchFees(ctx sdk.Context, keeper Keeper) ([]byte, error) {
val := types.QueryBatchFeeResponse{BatchFees: keeper.GetAllBatchFees(ctx, OutgoingTxBatchSize)}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, val)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// Gets MaxResults logic calls from store.
func lastLogicCallRequests(ctx sdk.Context, keeper Keeper) ([]byte, error) {
var calls []*types.OutgoingLogicCall
keeper.IterateOutgoingLogicCalls(ctx, func(_ []byte, call *types.OutgoingLogicCall) bool {
calls = append(calls, call)
return len(calls) == OutgoingTxBatchSize
})
if len(calls) == 0 {
return nil, nil
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, calls)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryBatch gets a batch by tokenContract and nonce
func queryBatch(ctx sdk.Context, nonce string, tokenContract string, keeper Keeper) ([]byte, error) {
parsedNonce, err := types.UInt64FromString(nonce)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, err.Error())
}
if types.ValidateEthAddress(tokenContract) != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, err.Error())
}
foundBatch := keeper.GetOutgoingTXBatch(ctx, tokenContract, parsedNonce)
if foundBatch == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Can not find tx batch")
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, foundBatch)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, err.Error())
}
return res, nil
}
// lastPendingLogicCallRequest gets the latest call that has NOT been signed by operatorAddr
func lastPendingLogicCallRequest(ctx sdk.Context, operatorAddr string, keeper Keeper) ([]byte, error) {
addr, err := sdk.AccAddressFromBech32(operatorAddr)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "address invalid")
}
var pendingLogicCalls *types.OutgoingLogicCall
keeper.IterateOutgoingLogicCalls(ctx, func(_ []byte, call *types.OutgoingLogicCall) bool {
foundConfirm := keeper.GetLogicCallConfirm(ctx, call.InvalidationId, call.InvalidationNonce, addr) != nil
if !foundConfirm {
pendingLogicCalls = call
return true
}
return false
})
if pendingLogicCalls == nil {
return nil, nil
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, pendingLogicCalls)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryLogicCall gets a logic call by nonce and invalidation id
func queryLogicCall(ctx sdk.Context, invalidationId string, invalidationNonce string, keeper Keeper) ([]byte, error) {
nonce, err := types.UInt64FromString(invalidationNonce)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
foundCall := keeper.GetOutgoingLogicCall(ctx, []byte(invalidationId), nonce)
if foundCall == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Can not find logic call")
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, foundCall)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, err.Error())
}
return res, nil
}
// allLogicCallConfirms returns all the confirm messages for a given nonce
// When nothing found an empty json array is returned. No pagination.
func queryAllLogicCallConfirms(ctx sdk.Context, invalidationId string, invalidationNonce string, keeper Keeper) ([]byte, error) {
nonce, err := types.UInt64FromString(invalidationNonce)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
invalidationIdBytes, err := hex.DecodeString(invalidationId)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
var confirms []*types.MsgConfirmLogicCall
keeper.IterateLogicConfirmByInvalidationIDAndNonce(ctx, invalidationIdBytes, nonce, func(_ []byte, c *types.MsgConfirmLogicCall) bool {
confirms = append(confirms, c)
return false
})
if len(confirms) == 0 {
return nil, nil
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, confirms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func queryGravityID(ctx sdk.Context, keeper Keeper) ([]byte, error) {
gravityID := keeper.GetGravityID(ctx)
res, err := codec.MarshalJSONIndent(types.ModuleCdc, gravityID)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
} else {
return res, nil
}
}
func queryDenomToERC20(ctx sdk.Context, denom string, keeper Keeper) ([]byte, error) {
cosmos_originated, erc20, err := keeper.DenomToERC20Lookup(ctx, denom)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
var response types.QueryDenomToERC20Response
response.CosmosOriginated = cosmos_originated
response.Erc20 = erc20
bytes, err := codec.MarshalJSONIndent(types.ModuleCdc, response)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
} else {
return bytes, nil
}
}
func queryERC20ToDenom(ctx sdk.Context, ERC20 string, keeper Keeper) ([]byte, error) {
cosmos_originated, denom := keeper.ERC20ToDenomLookup(ctx, ERC20)
var response types.QueryERC20ToDenomResponse
response.CosmosOriginated = cosmos_originated
response.Denom = denom
bytes, err := codec.MarshalJSONIndent(types.ModuleCdc, response)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
} else {
return bytes, nil
}
}
func queryPendingSendToEth(ctx sdk.Context, senderAddr string, k Keeper) ([]byte, error) {
batches := k.GetOutgoingTxBatches(ctx)
unbatched_tx := k.GetUnbatchedTransactions(ctx)
sender_address := senderAddr
res := types.QueryPendingSendToEthResponse{
TransfersInBatches: []*types.OutgoingTransferTx{},
UnbatchedTransfers: []*types.OutgoingTransferTx{},
}
for _, batch := range batches {
for _, tx := range batch.Transactions {
if tx.Sender == sender_address {
res.TransfersInBatches = append(res.TransfersInBatches, tx)
}
}
}
for _, tx := range unbatched_tx {
if tx.Sender == sender_address {
res.UnbatchedTransfers = append(res.UnbatchedTransfers, tx)
}
}
bytes, err := codec.MarshalJSONIndent(types.ModuleCdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
} else {
return bytes, nil
}
}