forked from althea-net/cosmos-gravity-bridge
-
Notifications
You must be signed in to change notification settings - Fork 2
/
grpc_query.go
390 lines (349 loc) · 14.2 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
package keeper
import (
"context"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/althea-net/cosmos-gravity-bridge/module/x/gravity/types"
)
var _ types.QueryServer = Keeper{
StakingKeeper: nil,
storeKey: nil,
paramSpace: paramstypes.Subspace{},
cdc: nil,
bankKeeper: nil,
SlashingKeeper: nil,
AttestationHandler: nil,
}
const QUERY_ATTESTATIONS_LIMIT uint64 = 1000
// Params queries the params of the gravity module
func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
var params types.Params
k.paramSpace.GetParamSet(sdk.UnwrapSDKContext(c), ¶ms)
return &types.QueryParamsResponse{Params: params}, nil
}
// CurrentValset queries the CurrentValset of the gravity module
func (k Keeper) CurrentValset(
c context.Context,
req *types.QueryCurrentValsetRequest) (*types.QueryCurrentValsetResponse, error) {
return &types.QueryCurrentValsetResponse{Valset: k.GetCurrentValset(sdk.UnwrapSDKContext(c))}, nil
}
// ValsetRequest queries the ValsetRequest of the gravity module
func (k Keeper) ValsetRequest(
c context.Context,
req *types.QueryValsetRequestRequest) (*types.QueryValsetRequestResponse, error) {
return &types.QueryValsetRequestResponse{Valset: k.GetValset(sdk.UnwrapSDKContext(c), req.Nonce)}, nil
}
// ValsetConfirm queries the ValsetConfirm of the gravity module
func (k Keeper) ValsetConfirm(
c context.Context,
req *types.QueryValsetConfirmRequest) (*types.QueryValsetConfirmResponse, error) {
addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "address invalid")
}
return &types.QueryValsetConfirmResponse{Confirm: k.GetValsetConfirm(sdk.UnwrapSDKContext(c), req.Nonce, addr)}, nil
}
// ValsetConfirmsByNonce queries the ValsetConfirmsByNonce of the gravity module
func (k Keeper) ValsetConfirmsByNonce(
c context.Context,
req *types.QueryValsetConfirmsByNonceRequest) (*types.QueryValsetConfirmsByNonceResponse, error) {
var confirms []*types.MsgValsetConfirm
k.IterateValsetConfirmByNonce(sdk.UnwrapSDKContext(c), req.Nonce, func(_ []byte, c types.MsgValsetConfirm) bool {
confirms = append(confirms, &c)
return false
})
return &types.QueryValsetConfirmsByNonceResponse{Confirms: confirms}, nil
}
// LastValsetRequests queries the LastValsetRequests of the gravity module
func (k Keeper) LastValsetRequests(
c context.Context,
req *types.QueryLastValsetRequestsRequest) (*types.QueryLastValsetRequestsResponse, error) {
valReq := k.GetValsets(sdk.UnwrapSDKContext(c))
valReqLen := len(valReq)
retLen := 0
if valReqLen < maxValsetRequestsReturned {
retLen = valReqLen
} else {
retLen = maxValsetRequestsReturned
}
return &types.QueryLastValsetRequestsResponse{Valsets: valReq[0:retLen]}, nil
}
// LastPendingValsetRequestByAddr queries the LastPendingValsetRequestByAddr of the gravity module
func (k Keeper) LastPendingValsetRequestByAddr(
c context.Context,
req *types.QueryLastPendingValsetRequestByAddrRequest) (*types.QueryLastPendingValsetRequestByAddrResponse, error) {
addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "address invalid")
}
var pendingValsetReq []*types.Valset
k.IterateValsets(sdk.UnwrapSDKContext(c), func(_ []byte, val *types.Valset) bool {
// foundConfirm is true if the operatorAddr has signed the valset we are currently looking at
foundConfirm := k.GetValsetConfirm(sdk.UnwrapSDKContext(c), 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
})
return &types.QueryLastPendingValsetRequestByAddrResponse{Valsets: pendingValsetReq}, nil
}
// BatchFees queries the batch fees from unbatched pool
func (k Keeper) BatchFees(
c context.Context,
req *types.QueryBatchFeeRequest) (*types.QueryBatchFeeResponse, error) {
return &types.QueryBatchFeeResponse{BatchFees: k.GetAllBatchFees(sdk.UnwrapSDKContext(c), OutgoingTxBatchSize)}, nil
}
// LastPendingBatchRequestByAddr queries the LastPendingBatchRequestByAddr of the gravity module
func (k Keeper) LastPendingBatchRequestByAddr(
c context.Context,
req *types.QueryLastPendingBatchRequestByAddrRequest) (*types.QueryLastPendingBatchRequestByAddrResponse, error) {
addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "address invalid")
}
var pendingBatchReq *types.InternalOutgoingTxBatch
k.IterateOutgoingTXBatches(sdk.UnwrapSDKContext(c), func(_ []byte, batch *types.InternalOutgoingTxBatch) bool {
foundConfirm := k.GetBatchConfirm(sdk.UnwrapSDKContext(c), batch.BatchNonce, batch.TokenContract, addr) != nil
if !foundConfirm {
pendingBatchReq = batch
return true
}
return false
})
return &types.QueryLastPendingBatchRequestByAddrResponse{Batch: pendingBatchReq.ToExternal()}, nil
}
func (k Keeper) LastPendingLogicCallByAddr(
c context.Context,
req *types.QueryLastPendingLogicCallByAddrRequest) (*types.QueryLastPendingLogicCallByAddrResponse, error) {
addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "address invalid")
}
var pendingLogicReq *types.OutgoingLogicCall
k.IterateOutgoingLogicCalls(sdk.UnwrapSDKContext(c), func(_ []byte, logic *types.OutgoingLogicCall) bool {
foundConfirm := k.GetLogicCallConfirm(sdk.UnwrapSDKContext(c),
logic.InvalidationId, logic.InvalidationNonce, addr) != nil
if !foundConfirm {
pendingLogicReq = logic
return true
}
return false
})
return &types.QueryLastPendingLogicCallByAddrResponse{Call: pendingLogicReq}, nil
}
// OutgoingTxBatches queries the OutgoingTxBatches of the gravity module
func (k Keeper) OutgoingTxBatches(
c context.Context,
req *types.QueryOutgoingTxBatchesRequest) (*types.QueryOutgoingTxBatchesResponse, error) {
var batches []*types.OutgoingTxBatch
k.IterateOutgoingTXBatches(sdk.UnwrapSDKContext(c), func(_ []byte, batch *types.InternalOutgoingTxBatch) bool {
batches = append(batches, batch.ToExternal())
return len(batches) == MaxResults
})
return &types.QueryOutgoingTxBatchesResponse{Batches: batches}, nil
}
// OutgoingLogicCalls queries the OutgoingLogicCalls of the gravity module
func (k Keeper) OutgoingLogicCalls(
c context.Context,
req *types.QueryOutgoingLogicCallsRequest) (*types.QueryOutgoingLogicCallsResponse, error) {
var calls []*types.OutgoingLogicCall
k.IterateOutgoingLogicCalls(sdk.UnwrapSDKContext(c), func(_ []byte, call *types.OutgoingLogicCall) bool {
calls = append(calls, call)
return len(calls) == MaxResults
})
return &types.QueryOutgoingLogicCallsResponse{Calls: calls}, nil
}
// BatchRequestByNonce queries the BatchRequestByNonce of the gravity module
func (k Keeper) BatchRequestByNonce(
c context.Context,
req *types.QueryBatchRequestByNonceRequest) (*types.QueryBatchRequestByNonceResponse, error) {
addr, err := types.NewEthAddress(req.ContractAddress)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, err.Error())
}
foundBatch := k.GetOutgoingTXBatch(sdk.UnwrapSDKContext(c), *addr, req.Nonce)
if foundBatch == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Can not find tx batch")
}
return &types.QueryBatchRequestByNonceResponse{Batch: foundBatch.ToExternal()}, nil
}
// BatchConfirms returns the batch confirmations by nonce and token contract
func (k Keeper) BatchConfirms(
c context.Context,
req *types.QueryBatchConfirmsRequest) (*types.QueryBatchConfirmsResponse, error) {
var confirms []*types.MsgConfirmBatch
contract, err := types.NewEthAddress(req.ContractAddress)
if err != nil {
return nil, sdkerrors.Wrap(err, "invalid contract address in request")
}
k.IterateBatchConfirmByNonceAndTokenContract(sdk.UnwrapSDKContext(c),
req.Nonce, *contract, func(_ []byte, c types.MsgConfirmBatch) bool {
confirms = append(confirms, &c)
return false
})
return &types.QueryBatchConfirmsResponse{Confirms: confirms}, nil
}
// LogicConfirms returns the Logic confirmations by nonce and token contract
func (k Keeper) LogicConfirms(
c context.Context,
req *types.QueryLogicConfirmsRequest) (*types.QueryLogicConfirmsResponse, error) {
var confirms []*types.MsgConfirmLogicCall
k.IterateLogicConfirmByInvalidationIDAndNonce(sdk.UnwrapSDKContext(c), req.InvalidationId,
req.InvalidationNonce, func(_ []byte, c *types.MsgConfirmLogicCall) bool {
confirms = append(confirms, c)
return false
})
return &types.QueryLogicConfirmsResponse{Confirms: confirms}, nil
}
// LastEventNonceByAddr returns the last event nonce for the given validator address,
// this allows eth oracles to figure out where they left off
func (k Keeper) LastEventNonceByAddr(
c context.Context,
req *types.QueryLastEventNonceByAddrRequest) (*types.QueryLastEventNonceByAddrResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
var ret types.QueryLastEventNonceByAddrResponse
addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, req.Address)
}
validator, found := k.GetOrchestratorValidator(ctx, addr)
if !found {
return nil, sdkerrors.Wrap(types.ErrUnknown, "address")
}
lastEventNonce := k.GetLastEventNonceByValidator(ctx, validator.GetOperator())
ret.EventNonce = lastEventNonce
return &ret, nil
}
// DenomToERC20 queries the Cosmos Denom that maps to an Ethereum ERC20
func (k Keeper) DenomToERC20(
c context.Context,
req *types.QueryDenomToERC20Request) (*types.QueryDenomToERC20Response, error) {
ctx := sdk.UnwrapSDKContext(c)
cosmosOriginated, erc20, err := k.DenomToERC20Lookup(ctx, req.Denom)
var ret types.QueryDenomToERC20Response
ret.Erc20 = erc20.GetAddress()
ret.CosmosOriginated = cosmosOriginated
return &ret, err
}
// ERC20ToDenom queries the ERC20 contract that maps to an Ethereum ERC20 if any
func (k Keeper) ERC20ToDenom(
c context.Context,
req *types.QueryERC20ToDenomRequest) (*types.QueryERC20ToDenomResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ethAddr, err := types.NewEthAddress(req.Erc20)
if err != nil {
return nil, sdkerrors.Wrapf(err, "invalid Erc20 in request: %s", req.Erc20)
}
cosmosOriginated, name := k.ERC20ToDenomLookup(ctx, *ethAddr)
var ret types.QueryERC20ToDenomResponse
ret.Denom = name
ret.CosmosOriginated = cosmosOriginated
return &ret, nil
}
// GetAttestations queries the attestation map
func (k Keeper) GetAttestations(
c context.Context,
req *types.QueryAttestationsRequest) (*types.QueryAttestationsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
limit := req.Limit
if limit > QUERY_ATTESTATIONS_LIMIT {
limit = QUERY_ATTESTATIONS_LIMIT
}
attestations := k.GetMostRecentAttestations(ctx, limit)
return &types.QueryAttestationsResponse{Attestations: attestations}, nil
}
func (k Keeper) GetDelegateKeyByValidator(
c context.Context,
req *types.QueryDelegateKeysByValidatorAddress) (*types.QueryDelegateKeysByValidatorAddressResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
keys := k.GetDelegateKeys(ctx)
reqValidator, err := sdk.ValAddressFromBech32(req.ValidatorAddress)
if err != nil {
return nil, err
}
for _, key := range keys {
keyValidator, err := sdk.ValAddressFromBech32(key.Validator)
// this should be impossible due to the validate basic on the set orchestrator message
if err != nil {
panic("Invalid validator addr in store!")
}
if reqValidator.Equals(keyValidator) {
return &types.QueryDelegateKeysByValidatorAddressResponse{EthAddress: key.EthAddress, OrchestratorAddress: key.Orchestrator}, nil
}
}
return nil, sdkerrors.Wrap(types.ErrInvalid, "No validator")
}
func (k Keeper) GetDelegateKeyByOrchestrator(
c context.Context,
req *types.QueryDelegateKeysByOrchestratorAddress) (*types.QueryDelegateKeysByOrchestratorAddressResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
keys := k.GetDelegateKeys(ctx)
reqOrchestrator, err := sdk.AccAddressFromBech32(req.OrchestratorAddress)
if err != nil {
return nil, err
}
for _, key := range keys {
keyOrchestrator, err := sdk.AccAddressFromBech32(key.Orchestrator)
// this should be impossible due to the validate basic on the set orchestrator message
if err != nil {
panic("Invalid orchestrator addr in store!")
}
if reqOrchestrator.Equals(keyOrchestrator) {
return &types.QueryDelegateKeysByOrchestratorAddressResponse{ValidatorAddress: key.Validator, EthAddress: key.EthAddress}, nil
}
}
return nil, sdkerrors.Wrap(types.ErrInvalid, "No validator")
}
func (k Keeper) GetDelegateKeyByEth(
c context.Context,
req *types.QueryDelegateKeysByEthAddress) (*types.QueryDelegateKeysByEthAddressResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
keys := k.GetDelegateKeys(ctx)
if err := types.ValidateEthAddress(req.EthAddress); err != nil {
return nil, sdkerrors.Wrap(err, "invalid eth address")
}
for _, key := range keys {
if strings.EqualFold(req.EthAddress, key.EthAddress) {
return &types.QueryDelegateKeysByEthAddressResponse{
ValidatorAddress: key.Validator,
OrchestratorAddress: key.Orchestrator,
}, nil
}
}
return nil, sdkerrors.Wrap(types.ErrInvalid, "No validator")
}
func (k Keeper) GetPendingSendToEth(
c context.Context,
req *types.QueryPendingSendToEth) (*types.QueryPendingSendToEthResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
batches := k.GetOutgoingTxBatches(ctx)
unbatched_tx := k.GetUnbatchedTransactions(ctx)
sender_address := req.GetSenderAddress()
res := types.QueryPendingSendToEthResponse{
TransfersInBatches: []*types.OutgoingTransferTx{},
UnbatchedTransfers: []*types.OutgoingTransferTx{},
}
for _, batch := range batches {
for _, tx := range batch.Transactions {
if tx.Sender.String() == sender_address {
res.TransfersInBatches = append(res.TransfersInBatches, tx.ToExternal())
}
}
}
for _, tx := range unbatched_tx {
if tx.Sender.String() == sender_address {
res.UnbatchedTransfers = append(res.UnbatchedTransfers, tx.ToExternal())
}
}
return &res, nil
}