-
Notifications
You must be signed in to change notification settings - Fork 13
/
grpc_query.go
307 lines (273 loc) · 13.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
package keeper
import (
"context"
"sort"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/functionx/fx-core/x/gravity/types"
)
var _ types.QueryServer = Keeper{}
// 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, 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))}, 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.OutgoingTxBatch
k.IterateOutgoingTXBatches(sdk.UnwrapSDKContext(c), func(_ []byte, batch *types.OutgoingTxBatch) 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}, 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.OutgoingTxBatch) bool {
batches = append(batches, batch)
return len(batches) == MaxResults
})
sort.Slice(batches, func(i, j int) bool {
return batches[i].BatchTimeout < batches[j].BatchTimeout
})
return &types.QueryOutgoingTxBatchesResponse{Batches: batches}, nil
}
// BatchRequestByNonce queries the BatchRequestByNonce of the gravity module
func (k Keeper) BatchRequestByNonce(c context.Context, req *types.QueryBatchRequestByNonceRequest) (*types.QueryBatchRequestByNonceResponse, error) {
if err := types.ValidateEthAddressAndValidateChecksum(req.ContractAddress); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, err.Error())
}
foundBatch := k.GetOutgoingTXBatch(sdk.UnwrapSDKContext(c), req.ContractAddress, req.Nonce)
if foundBatch == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Can not find tx batch")
}
return &types.QueryBatchRequestByNonceResponse{Batch: foundBatch}, nil
}
func (k Keeper) BatchConfirm(ctx context.Context, req *types.QueryBatchConfirmRequest) (*types.QueryBatchConfirmResponse, error) {
orchestrator, err := sdk.AccAddressFromBech32(req.GetAddress())
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
confirm := k.GetBatchConfirm(sdk.UnwrapSDKContext(ctx), req.GetNonce(), req.GetContractAddress(), orchestrator)
return &types.QueryBatchConfirmResponse{Confirm: confirm}, 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
k.IterateBatchConfirmByNonceAndTokenContract(sdk.UnwrapSDKContext(c), req.Nonce, req.ContractAddress, func(_ []byte, c types.MsgConfirmBatch) bool {
confirms = append(confirms, &c)
return false
})
return &types.QueryBatchConfirmsResponse{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)
}
valAddr, found := k.GetOrchestratorValidator(ctx, addr)
if !found {
return nil, sdkerrors.Wrap(types.ErrUnknown, "address")
}
lastEventNonce := k.GetLastEventNonceByValidator(ctx, valAddr)
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
ret.FxOriginated = 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)
cosmosOriginated, name := k.ERC20ToDenomLookup(ctx, req.Erc20)
var ret types.QueryERC20ToDenomResponse
ret.Denom = name
ret.FxOriginated = cosmosOriginated
return &ret, nil
}
func (k Keeper) GetDelegateKeyByValidator(c context.Context, req *types.QueryDelegateKeyByValidatorRequest) (*types.QueryDelegateKeyByValidatorResponse, 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.QueryDelegateKeyByValidatorResponse{EthAddress: key.EthAddress, OrchestratorAddress: key.Orchestrator}, nil
}
}
return nil, sdkerrors.Wrap(types.ErrInvalid, "No validator")
}
func (k Keeper) GetDelegateKeyByOrchestrator(c context.Context, req *types.QueryDelegateKeyByOrchestratorRequest) (*types.QueryDelegateKeyByOrchestratorResponse, 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.QueryDelegateKeyByOrchestratorResponse{ValidatorAddress: key.Validator, EthAddress: key.EthAddress}, nil
}
}
return nil, sdkerrors.Wrap(types.ErrInvalid, "No validator")
}
func (k Keeper) GetDelegateKeyByEth(c context.Context, req *types.QueryDelegateKeyByEthRequest) (*types.QueryDelegateKeyByEthResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
keys := k.GetDelegateKeys(ctx)
if err := types.ValidateEthAddressAndValidateChecksum(req.EthAddress); err != nil {
return nil, sdkerrors.Wrap(err, "invalid eth address")
}
for _, key := range keys {
if req.EthAddress == key.EthAddress {
return &types.QueryDelegateKeyByEthResponse{ValidatorAddress: key.Validator, OrchestratorAddress: key.Orchestrator}, nil
}
}
return nil, sdkerrors.Wrap(types.ErrInvalid, "No validator")
}
func (k Keeper) GetPendingSendToEth(c context.Context, req *types.QueryPendingSendToEthRequest) (*types.QueryPendingSendToEthResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
batches := k.GetOutgoingTxBatches(ctx)
unbatchedTx := k.GetPoolTransactions(ctx)
senderAddress := req.SenderAddress
var res = &types.QueryPendingSendToEthResponse{
TransfersInBatches: make([]*types.OutgoingTransferTx, 0),
UnbatchedTransfers: make([]*types.OutgoingTransferTx, 0),
}
for _, batch := range batches {
for _, tx := range batch.Transactions {
if tx.Sender == senderAddress {
res.TransfersInBatches = append(res.TransfersInBatches, tx)
}
}
}
for _, tx := range unbatchedTx {
if tx.Sender == senderAddress {
res.UnbatchedTransfers = append(res.UnbatchedTransfers, tx)
}
}
return res, nil
}
func (k Keeper) GetIbcSequenceHeightByChannel(c context.Context, req *types.QueryIbcSequenceHeightRequest) (*types.QueryIbcSequenceHeightResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
height, found := k.GetIbcSequenceHeight(ctx, req.GetSourcePort(), req.GetSourceChannel(), req.GetSequence())
return &types.QueryIbcSequenceHeightResponse{
Found: found,
Height: height,
}, nil
}
func (k Keeper) LastObservedEthBlockHeight(c context.Context, req *types.QueryLastObservedEthBlockHeightRequest) (*types.QueryLastObservedEthBlockHeightResponse, error) {
blockHeight := k.GetLastObservedEthBlockHeight(sdk.UnwrapSDKContext(c))
return &types.QueryLastObservedEthBlockHeightResponse{BlockHeight: blockHeight.EthBlockHeight}, nil
}
func (k Keeper) LastEventBlockHeightByAddr(c context.Context, req *types.QueryLastEventBlockHeightByAddrRequest) (*types.QueryLastEventBlockHeightByAddrResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
var res types.QueryLastEventBlockHeightByAddrResponse
addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, req.Address)
}
valAddr, found := k.GetOrchestratorValidator(ctx, addr)
if !found {
return nil, sdkerrors.Wrap(types.ErrUnknown, "address")
}
lastEventBlockHeight := k.getLastEventBlockHeightByValidator(ctx, valAddr)
res.BlockHeight = lastEventBlockHeight
return &res, nil
}