-
Notifications
You must be signed in to change notification settings - Fork 29
/
query.go
472 lines (407 loc) · 16 KB
/
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
465
466
467
468
469
470
471
472
package tendermint
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"time"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
querytypes "github.com/cosmos/cosmos-sdk/types/query"
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clientutils "github.com/cosmos/ibc-go/v7/modules/core/02-client/client/utils"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
connutils "github.com/cosmos/ibc-go/v7/modules/core/03-connection/client/utils"
conntypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
chanutils "github.com/cosmos/ibc-go/v7/modules/core/04-channel/client/utils"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
committypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/hyperledger-labs/yui-relayer/core"
)
// QueryClientState retrevies the latest consensus state for a client in state at a given height
func (c *Chain) QueryClientState(ctx core.QueryContext) (*clienttypes.QueryClientStateResponse, error) {
return c.queryClientState(int64(ctx.Height().GetRevisionHeight()), false)
}
func (c *Chain) queryClientState(height int64, _ bool) (*clienttypes.QueryClientStateResponse, error) {
return clientutils.QueryClientStateABCI(c.CLIContext(height), c.PathEnd.ClientID)
}
var emptyConnRes = conntypes.NewQueryConnectionResponse(
conntypes.NewConnectionEnd(
conntypes.UNINITIALIZED,
"client",
conntypes.NewCounterparty(
"client",
"connection",
committypes.NewMerklePrefix([]byte{}),
),
[]*conntypes.Version{},
0,
),
[]byte{},
clienttypes.NewHeight(0, 0),
)
// QueryConnection returns the remote end of a given connection
func (c *Chain) QueryConnection(ctx core.QueryContext) (*conntypes.QueryConnectionResponse, error) {
return c.queryConnection(int64(ctx.Height().GetRevisionHeight()), false)
}
func (c *Chain) queryConnection(height int64, prove bool) (*conntypes.QueryConnectionResponse, error) {
res, err := connutils.QueryConnection(c.CLIContext(height), c.PathEnd.ConnectionID, prove)
if err != nil && strings.Contains(err.Error(), "not found") {
return emptyConnRes, nil
} else if err != nil {
return nil, err
}
return res, nil
}
var emptyChannelRes = chantypes.NewQueryChannelResponse(
chantypes.NewChannel(
chantypes.UNINITIALIZED,
chantypes.UNORDERED,
chantypes.NewCounterparty(
"port",
"channel",
),
[]string{},
"version",
),
[]byte{},
clienttypes.NewHeight(0, 0),
)
// QueryChannel returns the channel associated with a channelID
func (c *Chain) QueryChannel(ctx core.QueryContext) (chanRes *chantypes.QueryChannelResponse, err error) {
return c.queryChannel(int64(ctx.Height().GetRevisionHeight()), false)
}
func (c *Chain) queryChannel(height int64, prove bool) (chanRes *chantypes.QueryChannelResponse, err error) {
res, err := chanutils.QueryChannel(c.CLIContext(height), c.PathEnd.PortID, c.PathEnd.ChannelID, prove)
if err != nil && strings.Contains(err.Error(), "not found") {
return emptyChannelRes, nil
} else if err != nil {
return nil, err
}
return res, nil
}
// QueryClientConsensusState retrevies the latest consensus state for a client in state at a given height
func (c *Chain) QueryClientConsensusState(
ctx core.QueryContext, dstClientConsHeight ibcexported.Height) (*clienttypes.QueryConsensusStateResponse, error) {
return c.queryClientConsensusState(int64(ctx.Height().GetRevisionHeight()), dstClientConsHeight, false)
}
func (c *Chain) queryClientConsensusState(
height int64, dstClientConsHeight ibcexported.Height, _ bool) (*clienttypes.QueryConsensusStateResponse, error) {
return clientutils.QueryConsensusStateABCI(
c.CLIContext(height),
c.PathEnd.ClientID,
dstClientConsHeight,
)
}
// QueryBalance returns the amount of coins in the relayer account
func (c *Chain) QueryBalance(ctx core.QueryContext, addr sdk.AccAddress) (sdk.Coins, error) {
params := bankTypes.NewQueryAllBalancesRequest(addr, &querytypes.PageRequest{
Key: []byte(""),
Offset: 0,
Limit: 1000,
CountTotal: true,
})
queryClient := bankTypes.NewQueryClient(c.CLIContext(0))
res, err := queryClient.AllBalances(context.Background(), params)
if err != nil {
return nil, err
}
return res.Balances, nil
}
// QueryDenomTraces returns all the denom traces from a given chain
func (c *Chain) QueryDenomTraces(ctx core.QueryContext, offset, limit uint64) (*transfertypes.QueryDenomTracesResponse, error) {
return transfertypes.NewQueryClient(c.CLIContext(int64(ctx.Height().GetRevisionHeight()))).DenomTraces(context.Background(), &transfertypes.QueryDenomTracesRequest{
Pagination: &querytypes.PageRequest{
Key: []byte(""),
Offset: offset,
Limit: limit,
CountTotal: true,
},
})
}
// queryPacketCommitments returns an array of packet commitments
func (c *Chain) queryPacketCommitments(
ctx core.QueryContext,
offset, limit uint64) (comRes *chantypes.QueryPacketCommitmentsResponse, err error) {
qc := chantypes.NewQueryClient(c.CLIContext(int64(ctx.Height().GetRevisionHeight())))
return qc.PacketCommitments(context.Background(), &chantypes.QueryPacketCommitmentsRequest{
PortId: c.PathEnd.PortID,
ChannelId: c.PathEnd.ChannelID,
Pagination: &querytypes.PageRequest{
Offset: offset,
Limit: limit,
CountTotal: true,
},
})
}
// queryPacketAcknowledgementCommitments returns an array of packet acks
func (c *Chain) queryPacketAcknowledgementCommitments(ctx core.QueryContext, offset, limit uint64) (comRes *chantypes.QueryPacketAcknowledgementsResponse, err error) {
qc := chantypes.NewQueryClient(c.CLIContext(int64(ctx.Height().GetRevisionHeight())))
return qc.PacketAcknowledgements(context.Background(), &chantypes.QueryPacketAcknowledgementsRequest{
PortId: c.PathEnd.PortID,
ChannelId: c.PathEnd.ChannelID,
Pagination: &querytypes.PageRequest{
Offset: offset,
Limit: limit,
CountTotal: true,
},
})
}
// QueryUnreceivedPackets returns a list of unrelayed packet commitments
func (c *Chain) QueryUnreceivedPackets(ctx core.QueryContext, seqs []uint64) ([]uint64, error) {
qc := chantypes.NewQueryClient(c.CLIContext(int64(ctx.Height().GetRevisionHeight())))
res, err := qc.UnreceivedPackets(context.Background(), &chantypes.QueryUnreceivedPacketsRequest{
PortId: c.PathEnd.PortID,
ChannelId: c.PathEnd.ChannelID,
PacketCommitmentSequences: seqs,
})
if err != nil {
return nil, err
}
return res.Sequences, nil
}
func (c *Chain) QueryUnfinalizedRelayPackets(ctx core.QueryContext, counterparty core.LightClientICS04Querier) (core.PacketInfoList, error) {
res, err := c.queryPacketCommitments(ctx, 0, 1000)
if err != nil {
return nil, err
}
var packets core.PacketInfoList
for _, ps := range res.Commitments {
packet, height, err := c.querySentPacket(ctx, ps.Sequence)
if err != nil {
return nil, err
}
packets = append(packets, &core.PacketInfo{
Packet: *packet,
Acknowledgement: nil,
EventHeight: height,
})
}
var counterpartyCtx core.QueryContext
if counterpartyH, err := counterparty.GetLatestFinalizedHeader(); err != nil {
return nil, err
} else {
counterpartyCtx = core.NewQueryContext(context.TODO(), counterpartyH.GetHeight())
}
seqs, err := counterparty.QueryUnreceivedPackets(counterpartyCtx, packets.ExtractSequenceList())
if err != nil {
return nil, err
}
packets = packets.Filter(seqs)
return packets, nil
}
// QueryUnreceivedAcknowledgements returns a list of unrelayed packet acks
func (c *Chain) QueryUnreceivedAcknowledgements(ctx core.QueryContext, seqs []uint64) ([]uint64, error) {
qc := chantypes.NewQueryClient(c.CLIContext(int64(ctx.Height().GetRevisionHeight())))
res, err := qc.UnreceivedAcks(context.Background(), &chantypes.QueryUnreceivedAcksRequest{
PortId: c.PathEnd.PortID,
ChannelId: c.PathEnd.ChannelID,
PacketAckSequences: seqs,
})
if err != nil {
return nil, err
}
return res.Sequences, nil
}
func (c *Chain) QueryUnfinalizedRelayAcknowledgements(ctx core.QueryContext, counterparty core.LightClientICS04Querier) (core.PacketInfoList, error) {
res, err := c.queryPacketAcknowledgementCommitments(ctx, 0, 1000)
if err != nil {
return nil, err
}
var packets core.PacketInfoList
for _, ps := range res.Acknowledgements {
packet, rpHeight, err := c.queryReceivedPacket(ctx, ps.Sequence)
if err != nil {
return nil, err
}
ack, _, err := c.queryWrittenAcknowledgement(ctx, ps.Sequence)
if err != nil {
return nil, err
}
packets = append(packets, &core.PacketInfo{
Packet: *packet,
Acknowledgement: ack,
EventHeight: rpHeight,
})
}
var counterpartyCtx core.QueryContext
if counterpartyH, err := counterparty.GetLatestFinalizedHeader(); err != nil {
return nil, err
} else {
counterpartyCtx = core.NewQueryContext(context.TODO(), counterpartyH.GetHeight())
}
seqs, err := counterparty.QueryUnreceivedAcknowledgements(counterpartyCtx, packets.ExtractSequenceList())
if err != nil {
return nil, err
}
packets = packets.Filter(seqs)
return packets, nil
}
// querySentPacket finds a SendPacket event corresponding to `seq` and returns the packet in it
func (c *Chain) querySentPacket(ctx core.QueryContext, seq uint64) (*chantypes.Packet, clienttypes.Height, error) {
txs, err := c.QueryTxs(int64(ctx.Height().GetRevisionHeight()), 1, 1000, sendPacketQuery(c.Path().ChannelID, int(seq)))
switch {
case err != nil:
return nil, clienttypes.Height{}, err
case len(txs) == 0:
return nil, clienttypes.Height{}, fmt.Errorf("no transactions returned with query")
case len(txs) > 1:
return nil, clienttypes.Height{}, fmt.Errorf("more than one transaction returned with query")
}
packet, err := core.FindPacketFromEventsBySequence(txs[0].TxResult.Events, chantypes.EventTypeSendPacket, seq)
if err != nil {
return nil, clienttypes.Height{}, err
}
if packet == nil {
return nil, clienttypes.Height{}, fmt.Errorf("can't find the packet from events")
}
height := clienttypes.NewHeight(clienttypes.ParseChainID(c.ChainID()), uint64(txs[0].Height))
return packet, height, nil
}
// queryReceivedPacket finds a RecvPacket event corresponding to `seq` and return the packet in it
func (c *Chain) queryReceivedPacket(ctx core.QueryContext, seq uint64) (*chantypes.Packet, clienttypes.Height, error) {
txs, err := c.QueryTxs(int64(ctx.Height().GetRevisionHeight()), 1, 1000, recvPacketQuery(c.Path().ChannelID, int(seq)))
switch {
case err != nil:
return nil, clienttypes.Height{}, err
case len(txs) == 0:
return nil, clienttypes.Height{}, fmt.Errorf("no transactions returned with query")
case len(txs) > 1:
return nil, clienttypes.Height{}, fmt.Errorf("more than one transaction returned with query")
}
packet, err := core.FindPacketFromEventsBySequence(txs[0].TxResult.Events, chantypes.EventTypeRecvPacket, seq)
if err != nil {
return nil, clienttypes.Height{}, err
}
if packet == nil {
return nil, clienttypes.Height{}, fmt.Errorf("can't find the packet from events")
}
height := clienttypes.NewHeight(clienttypes.ParseChainID(c.ChainID()), uint64(txs[0].Height))
return packet, height, nil
}
// queryWrittenAcknowledgement finds a WriteAcknowledgement event corresponding to `seq` and returns the acknowledgement in it
func (c *Chain) queryWrittenAcknowledgement(ctx core.QueryContext, seq uint64) ([]byte, clienttypes.Height, error) {
txs, err := c.QueryTxs(int64(ctx.Height().GetRevisionHeight()), 1, 1000, writeAckQuery(c.Path().ChannelID, int(seq)))
switch {
case err != nil:
return nil, clienttypes.Height{}, err
case len(txs) == 0:
return nil, clienttypes.Height{}, fmt.Errorf("no transactions returned with query")
case len(txs) > 1:
return nil, clienttypes.Height{}, fmt.Errorf("more than one transaction returned with query")
}
ack, err := core.FindPacketAcknowledgementFromEventsBySequence(txs[0].TxResult.Events, seq)
if err != nil {
return nil, clienttypes.Height{}, err
}
if ack == nil {
return nil, clienttypes.Height{}, fmt.Errorf("can't find the packet from events")
}
height := clienttypes.NewHeight(clienttypes.ParseChainID(c.ChainID()), uint64(txs[0].Height))
return ack.Data(), height, nil
}
// QueryTxs returns an array of transactions given a tag
func (c *Chain) QueryTxs(height int64, page, limit int, events []string) ([]*ctypes.ResultTx, error) {
if len(events) == 0 {
return nil, errors.New("must declare at least one event to search")
}
if page <= 0 {
return nil, errors.New("page must greater than 0")
}
if limit <= 0 {
return nil, errors.New("limit must greater than 0")
}
res, err := c.Client.TxSearch(context.Background(), strings.Join(events, " AND "), true, &page, &limit, "")
if err != nil {
return nil, err
}
return res.Txs, nil
}
/////////////////////////////////////
// STAKING -> HistoricalInfo //
/////////////////////////////////////
// QueryHistoricalInfo returns historical header data
func (c *Chain) QueryHistoricalInfo(height clienttypes.Height) (*stakingtypes.QueryHistoricalInfoResponse, error) {
//TODO: use epoch number in query once SDK gets updated
qc := stakingtypes.NewQueryClient(c.CLIContext(int64(height.GetRevisionHeight())))
return qc.HistoricalInfo(context.Background(), &stakingtypes.QueryHistoricalInfoRequest{
Height: int64(height.GetRevisionHeight()),
})
}
// QueryValsetAtHeight returns the validator set at a given height
func (c *Chain) QueryValsetAtHeight(height clienttypes.Height) (*tmproto.ValidatorSet, error) {
res, err := c.QueryHistoricalInfo(height)
if err != nil {
return nil, err
}
// create tendermint ValidatorSet from SDK Validators
tmVals, err := c.toTmValidators(res.Hist.Valset)
if err != nil {
return nil, err
}
sort.Sort(tmtypes.ValidatorsByVotingPower(tmVals))
tmValSet := &tmtypes.ValidatorSet{
Validators: tmVals,
}
tmValSet.GetProposer()
protoValSet, err := tmValSet.ToProto()
if err != nil {
return nil, err
}
protoValSet.TotalVotingPower = tmValSet.TotalVotingPower()
return protoValSet, err
}
func (c *Chain) toTmValidators(vals stakingtypes.Validators) ([]*tmtypes.Validator, error) {
validators := make([]*tmtypes.Validator, len(vals))
var err error
for i, val := range vals {
validators[i], err = c.toTmValidator(val)
if err != nil {
return nil, err
}
}
return validators, nil
}
func (c *Chain) toTmValidator(val stakingtypes.Validator) (*tmtypes.Validator, error) {
var pk cryptotypes.PubKey
if err := c.codec.UnpackAny(val.ConsensusPubkey, &pk); err != nil {
return nil, err
}
tmkey, err := cryptocodec.ToTmPubKeyInterface(pk)
if err != nil {
return nil, fmt.Errorf("pubkey not a tendermint pub key %s", err)
}
return tmtypes.NewValidator(tmkey, val.ConsensusPower(sdk.DefaultPowerReduction)), nil
}
// QueryUnbondingPeriod returns the unbonding period of the chain
func (c *Chain) QueryUnbondingPeriod() (time.Duration, error) {
req := stakingtypes.QueryParamsRequest{}
queryClient := stakingtypes.NewQueryClient(c.CLIContext(0))
res, err := queryClient.Params(context.Background(), &req)
if err != nil {
return 0, err
}
return res.Params.UnbondingTime, nil
}
const (
spTag = "send_packet"
rpTag = "recv_packet"
waTag = "write_acknowledgement"
)
func sendPacketQuery(channelID string, seq int) []string {
return []string{fmt.Sprintf("%s.packet_src_channel='%s'", spTag, channelID), fmt.Sprintf("%s.packet_sequence='%d'", spTag, seq)}
}
func recvPacketQuery(channelID string, seq int) []string {
return []string{fmt.Sprintf("%s.packet_src_channel='%s'", rpTag, channelID), fmt.Sprintf("%s.packet_sequence='%d'", rpTag, seq)}
}
func writeAckQuery(channelID string, seq int) []string {
return []string{fmt.Sprintf("%s.packet_dst_channel='%s'", waTag, channelID), fmt.Sprintf("%s.packet_sequence='%d'", waTag, seq)}
}