forked from cosmos/relayer
/
query.go
948 lines (807 loc) · 31.5 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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
package cosmos
import (
"context"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
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"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
transfertypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v4/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v4/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v4/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v4/modules/core/exported"
tmclient "github.com/cosmos/ibc-go/v4/modules/light-clients/07-tendermint/types"
interquerytypes "github.com/defund-labs/defund/x/query/types"
"github.com/defund-labs/relayer/v3/relayer/provider"
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
"golang.org/x/sync/errgroup"
)
var _ provider.QueryProvider = &CosmosProvider{}
// QueryTx takes a transaction hash and returns the transaction
func (cc *CosmosProvider) QueryTx(ctx context.Context, hashHex string) (*provider.RelayerTxResponse, error) {
hash, err := hex.DecodeString(hashHex)
if err != nil {
return nil, err
}
resp, err := cc.RPCClient.Tx(ctx, hash, true)
if err != nil {
return nil, err
}
events := parseEventsFromResponseDeliverTx(resp.TxResult)
return &provider.RelayerTxResponse{
Height: resp.Height,
TxHash: string(hash),
Code: resp.TxResult.Code,
Data: string(resp.TxResult.Data),
Events: events,
}, nil
}
// QueryTxs returns an array of transactions given a tag
func (cc *CosmosProvider) QueryTxs(ctx context.Context, page, limit int, events []string) ([]*provider.RelayerTxResponse, 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 := cc.RPCClient.TxSearch(ctx, strings.Join(events, " AND "), true, &page, &limit, "")
if err != nil {
return nil, err
}
// Currently, we only call QueryTxs() in two spots and in both of them we are expecting there to only be,
// at most, one tx in the response. Because of this we don't want to initialize the slice with an initial size.
var txResps []*provider.RelayerTxResponse
for _, tx := range res.Txs {
relayerEvents := parseEventsFromResponseDeliverTx(tx.TxResult)
txResps = append(txResps, &provider.RelayerTxResponse{
Height: tx.Height,
TxHash: string(tx.Hash),
Code: tx.TxResult.Code,
Data: string(tx.TxResult.Data),
Events: relayerEvents,
})
}
return txResps, nil
}
// QueryTendermintProof performs an ABCI query with the given key and returns
// the value of the query, the proto encoded merkle proof, and the height of
// the Tendermint block containing the state root. The desired tendermint height
// to perform the query should be set in the client context. The query will be
// performed at one below this height (at the IAVL version) in order to obtain
// the correct merkle proof. Proof queries at height less than or equal to 2 are
// not supported. Queries with a client context height of 0 will perform a query
// at the lastest state available.
// Issue: https://github.com/cosmos/cosmos-sdk/issues/6567
func (cc *CosmosProvider) QueryStateABCI(ctx context.Context, height int64, path string, key []byte) (*abci.ResponseQuery, clienttypes.Height, error) {
// ABCI queries at heights 1, 2 or less than or equal to 0 are not supported.
// Base app does not support queries for height less than or equal to 1.
// Therefore, a query at height 2 would be equivalent to a query at height 3.
// A height of 0 will query with the lastest state.
if height != 0 && height <= 2 {
return nil, clienttypes.Height{}, fmt.Errorf("proof queries at height <= 2 are not supported")
}
// Use the IAVL height if a valid tendermint height is passed in.
// A height of 0 will query with the latest state.
if height != 0 {
height--
}
req := abci.RequestQuery{
Path: path,
Height: height,
Data: key,
Prove: true,
}
res, err := cc.QueryABCI(ctx, req)
if err != nil {
return nil, clienttypes.Height{}, err
}
revision := clienttypes.ParseChainID(cc.PCfg.ChainID)
return &res, clienttypes.NewHeight(revision, uint64(res.Height)+1), nil
}
// parseEventsFromResponseDeliverTx parses the events from a ResponseDeliverTx and builds a slice
// of provider.RelayerEvent's.
func parseEventsFromResponseDeliverTx(resp abci.ResponseDeliverTx) []provider.RelayerEvent {
var events []provider.RelayerEvent
for _, event := range resp.Events {
attributes := make(map[string]string)
for _, attribute := range event.Attributes {
attributes[string(attribute.Key)] = string(attribute.Value)
}
events = append(events, provider.RelayerEvent{
EventType: event.Type,
Attributes: attributes,
})
}
return events
}
// QueryBalance returns the amount of coins in the relayer account
func (cc *CosmosProvider) QueryBalance(ctx context.Context, keyName string) (sdk.Coins, error) {
addr, err := cc.ShowAddress(keyName)
if err != nil {
return nil, err
}
return cc.QueryBalanceWithAddress(ctx, addr)
}
// QueryBalanceWithAddress returns the amount of coins in the relayer account with address as input
// TODO add pagination support
func (cc *CosmosProvider) QueryBalanceWithAddress(ctx context.Context, address string) (sdk.Coins, error) {
p := &bankTypes.QueryAllBalancesRequest{Address: address, Pagination: DefaultPageRequest()}
queryClient := bankTypes.NewQueryClient(cc)
res, err := queryClient.AllBalances(ctx, p)
if err != nil {
return nil, err
}
return res.Balances, nil
}
// QueryUnbondingPeriod returns the unbonding period of the chain
func (cc *CosmosProvider) QueryUnbondingPeriod(ctx context.Context) (time.Duration, error) {
req := stakingtypes.QueryParamsRequest{}
queryClient := stakingtypes.NewQueryClient(cc)
res, err := queryClient.Params(ctx, &req)
if err != nil {
return 0, err
}
return res.Params.UnbondingTime, nil
}
// QueryTendermintProof performs an ABCI query with the given key and returns
// the value of the query, the proto encoded merkle proof, and the height of
// the Tendermint block containing the state root. The desired tendermint height
// to perform the query should be set in the client context. The query will be
// performed at one below this height (at the IAVL version) in order to obtain
// the correct merkle proof. Proof queries at height less than or equal to 2 are
// not supported. Queries with a client context height of 0 will perform a query
// at the latest state available.
// Issue: https://github.com/cosmos/cosmos-sdk/issues/6567
func (cc *CosmosProvider) QueryTendermintProof(ctx context.Context, height int64, key []byte) ([]byte, []byte, clienttypes.Height, error) {
// ABCI queries at heights 1, 2 or less than or equal to 0 are not supported.
// Base app does not support queries for height less than or equal to 1.
// Therefore, a query at height 2 would be equivalent to a query at height 3.
// A height of 0 will query with the lastest state.
if height != 0 && height <= 2 {
return nil, nil, clienttypes.Height{}, fmt.Errorf("proof queries at height <= 2 are not supported")
}
// Use the IAVL height if a valid tendermint height is passed in.
// A height of 0 will query with the latest state.
if height != 0 {
height--
}
req := abci.RequestQuery{
Path: fmt.Sprintf("store/%s/key", host.StoreKey),
Height: height,
Data: key,
Prove: true,
}
res, err := cc.QueryABCI(ctx, req)
if err != nil {
return nil, nil, clienttypes.Height{}, err
}
merkleProof, err := commitmenttypes.ConvertProofs(res.ProofOps)
if err != nil {
return nil, nil, clienttypes.Height{}, err
}
cdc := codec.NewProtoCodec(cc.Codec.InterfaceRegistry)
proofBz, err := cdc.Marshal(&merkleProof)
if err != nil {
return nil, nil, clienttypes.Height{}, err
}
revision := clienttypes.ParseChainID(cc.PCfg.ChainID)
return res.Value, proofBz, clienttypes.NewHeight(revision, uint64(res.Height)+1), nil
}
// QueryClientStateResponse retrieves the latest consensus state for a client in state at a given height
func (cc *CosmosProvider) QueryClientStateResponse(ctx context.Context, height int64, srcClientId string) (*clienttypes.QueryClientStateResponse, error) {
key := host.FullClientStateKey(srcClientId)
value, proofBz, proofHeight, err := cc.QueryTendermintProof(ctx, height, key)
if err != nil {
return nil, err
}
// check if client exists
if len(value) == 0 {
return nil, sdkerrors.Wrap(clienttypes.ErrClientNotFound, srcClientId)
}
cdc := codec.NewProtoCodec(cc.Codec.InterfaceRegistry)
clientState, err := clienttypes.UnmarshalClientState(cdc, value)
if err != nil {
return nil, err
}
anyClientState, err := clienttypes.PackClientState(clientState)
if err != nil {
return nil, err
}
return &clienttypes.QueryClientStateResponse{
ClientState: anyClientState,
Proof: proofBz,
ProofHeight: proofHeight,
}, nil
}
// QueryClientState retrieves the latest consensus state for a client in state at a given height
// and unpacks it to exported client state interface
func (cc *CosmosProvider) QueryClientState(ctx context.Context, height int64, clientid string) (ibcexported.ClientState, error) {
clientStateRes, err := cc.QueryClientStateResponse(ctx, height, clientid)
if err != nil {
return nil, err
}
clientStateExported, err := clienttypes.UnpackClientState(clientStateRes.ClientState)
if err != nil {
return nil, err
}
return clientStateExported, nil
}
// QueryClientConsensusState retrieves the latest consensus state for a client in state at a given height
func (cc *CosmosProvider) QueryClientConsensusState(ctx context.Context, chainHeight int64, clientid string, clientHeight ibcexported.Height) (*clienttypes.QueryConsensusStateResponse, error) {
key := host.FullConsensusStateKey(clientid, clientHeight)
value, proofBz, proofHeight, err := cc.QueryTendermintProof(ctx, chainHeight, key)
if err != nil {
return nil, err
}
// check if consensus state exists
if len(value) == 0 {
return nil, sdkerrors.Wrap(clienttypes.ErrConsensusStateNotFound, clientid)
}
cdc := codec.NewProtoCodec(cc.Codec.InterfaceRegistry)
cs, err := clienttypes.UnmarshalConsensusState(cdc, value)
if err != nil {
return nil, err
}
anyConsensusState, err := clienttypes.PackConsensusState(cs)
if err != nil {
return nil, err
}
return &clienttypes.QueryConsensusStateResponse{
ConsensusState: anyConsensusState,
Proof: proofBz,
ProofHeight: proofHeight,
}, nil
}
// QueryUpgradeProof performs an abci query with the given key and returns the proto encoded merkle proof
// for the query and the height at which the proof will succeed on a tendermint verifier.
func (cc *CosmosProvider) QueryUpgradeProof(ctx context.Context, key []byte, height uint64) ([]byte, clienttypes.Height, error) {
res, err := cc.QueryABCI(ctx, abci.RequestQuery{
Path: "store/upgrade/key",
Height: int64(height - 1),
Data: key,
Prove: true,
})
if err != nil {
return nil, clienttypes.Height{}, err
}
merkleProof, err := commitmenttypes.ConvertProofs(res.ProofOps)
if err != nil {
return nil, clienttypes.Height{}, err
}
proof, err := cc.Codec.Marshaler.Marshal(&merkleProof)
if err != nil {
return nil, clienttypes.Height{}, err
}
revision := clienttypes.ParseChainID(cc.PCfg.ChainID)
// proof height + 1 is returned as the proof created corresponds to the height the proof
// was created in the IAVL tree. Tendermint and subsequently the clients that rely on it
// have heights 1 above the IAVL tree. Thus we return proof height + 1
return proof, clienttypes.Height{
RevisionNumber: revision,
RevisionHeight: uint64(res.Height + 1),
}, nil
}
// QueryUpgradedClient returns upgraded client info
func (cc *CosmosProvider) QueryUpgradedClient(ctx context.Context, height int64) (*clienttypes.QueryClientStateResponse, error) {
req := clienttypes.QueryUpgradedClientStateRequest{}
queryClient := clienttypes.NewQueryClient(cc)
res, err := queryClient.UpgradedClientState(ctx, &req)
if err != nil {
return nil, err
}
if res == nil || res.UpgradedClientState == nil {
return nil, fmt.Errorf("upgraded client state plan does not exist at height %d", height)
}
proof, proofHeight, err := cc.QueryUpgradeProof(ctx, upgradetypes.UpgradedClientKey(height), uint64(height))
if err != nil {
return nil, err
}
return &clienttypes.QueryClientStateResponse{
ClientState: res.UpgradedClientState,
Proof: proof,
ProofHeight: proofHeight,
}, nil
}
// QueryUpgradedConsState returns upgraded consensus state and height of client
func (cc *CosmosProvider) QueryUpgradedConsState(ctx context.Context, height int64) (*clienttypes.QueryConsensusStateResponse, error) {
req := clienttypes.QueryUpgradedConsensusStateRequest{}
queryClient := clienttypes.NewQueryClient(cc)
res, err := queryClient.UpgradedConsensusState(ctx, &req)
if err != nil {
return nil, err
}
if res == nil || res.UpgradedConsensusState == nil {
return nil, fmt.Errorf("upgraded consensus state plan does not exist at height %d", height)
}
proof, proofHeight, err := cc.QueryUpgradeProof(ctx, upgradetypes.UpgradedConsStateKey(height), uint64(height))
if err != nil {
return nil, err
}
return &clienttypes.QueryConsensusStateResponse{
ConsensusState: res.UpgradedConsensusState,
Proof: proof,
ProofHeight: proofHeight,
}, nil
}
// QueryConsensusState returns a consensus state for a given chain to be used as a
// client in another chain, fetches latest height when passed 0 as arg
func (cc *CosmosProvider) QueryConsensusState(ctx context.Context, height int64) (ibcexported.ConsensusState, int64, error) {
commit, err := cc.RPCClient.Commit(ctx, &height)
if err != nil {
return &tmclient.ConsensusState{}, 0, err
}
page := 1
count := 10_000
nextHeight := height + 1
nextVals, err := cc.RPCClient.Validators(ctx, &nextHeight, &page, &count)
if err != nil {
return &tmclient.ConsensusState{}, 0, err
}
state := &tmclient.ConsensusState{
Timestamp: commit.Time,
Root: commitmenttypes.NewMerkleRoot(commit.AppHash),
NextValidatorsHash: tmtypes.NewValidatorSet(nextVals.Validators).Hash(),
}
return state, height, nil
}
// QueryClients queries all the clients!
// TODO add pagination support
func (cc *CosmosProvider) QueryClients(ctx context.Context) (clienttypes.IdentifiedClientStates, error) {
qc := clienttypes.NewQueryClient(cc)
state, err := qc.ClientStates(ctx, &clienttypes.QueryClientStatesRequest{
Pagination: DefaultPageRequest(),
})
if err != nil {
return nil, err
}
return state.ClientStates, nil
}
// QueryConnection returns the remote end of a given connection
func (cc *CosmosProvider) QueryConnection(ctx context.Context, height int64, connectionid string) (*conntypes.QueryConnectionResponse, error) {
res, err := cc.queryConnectionABCI(ctx, height, connectionid)
if err != nil && strings.Contains(err.Error(), "not found") {
return &conntypes.QueryConnectionResponse{
Connection: &conntypes.ConnectionEnd{
ClientId: "client",
Versions: []*conntypes.Version{},
State: conntypes.UNINITIALIZED,
Counterparty: conntypes.Counterparty{
ClientId: "client",
ConnectionId: "connection",
Prefix: commitmenttypes.MerklePrefix{KeyPrefix: []byte{}},
},
DelayPeriod: 0,
},
Proof: []byte{},
ProofHeight: clienttypes.Height{RevisionNumber: 0, RevisionHeight: 0},
}, nil
} else if err != nil {
return nil, err
}
return res, nil
}
func (cc *CosmosProvider) queryConnectionABCI(ctx context.Context, height int64, connectionID string) (*conntypes.QueryConnectionResponse, error) {
key := host.ConnectionKey(connectionID)
value, proofBz, proofHeight, err := cc.QueryTendermintProof(ctx, height, key)
if err != nil {
return nil, err
}
// check if connection exists
if len(value) == 0 {
return nil, sdkerrors.Wrap(conntypes.ErrConnectionNotFound, connectionID)
}
cdc := codec.NewProtoCodec(cc.Codec.InterfaceRegistry)
var connection conntypes.ConnectionEnd
if err := cdc.Unmarshal(value, &connection); err != nil {
return nil, err
}
return &conntypes.QueryConnectionResponse{
Connection: &connection,
Proof: proofBz,
ProofHeight: proofHeight,
}, nil
}
// QueryConnections gets any connections on a chain
// TODO add pagination support
func (cc *CosmosProvider) QueryConnections(ctx context.Context) (conns []*conntypes.IdentifiedConnection, err error) {
qc := conntypes.NewQueryClient(cc)
res, err := qc.Connections(ctx, &conntypes.QueryConnectionsRequest{
Pagination: DefaultPageRequest(),
})
if err != nil || res == nil {
return nil, err
}
return res.Connections, err
}
// QueryConnectionsUsingClient gets any connections that exist between chain and counterparty
// TODO add pagination support
func (cc *CosmosProvider) QueryConnectionsUsingClient(ctx context.Context, height int64, clientid string) (*conntypes.QueryConnectionsResponse, error) {
qc := conntypes.NewQueryClient(cc)
res, err := qc.Connections(ctx, &conntypes.QueryConnectionsRequest{
Pagination: DefaultPageRequest(),
})
return res, err
}
// GenerateConnHandshakeProof generates all the proofs needed to prove the existence of the
// connection state on this chain. A counterparty should use these generated proofs.
func (cc *CosmosProvider) GenerateConnHandshakeProof(ctx context.Context, height int64, clientId, connId string) (clientState ibcexported.ClientState, clientStateProof []byte, consensusProof []byte, connectionProof []byte, connectionProofHeight ibcexported.Height, err error) {
var (
clientStateRes *clienttypes.QueryClientStateResponse
consensusStateRes *clienttypes.QueryConsensusStateResponse
connectionStateRes *conntypes.QueryConnectionResponse
eg = new(errgroup.Group)
)
// query for the client state for the proof and get the height to query the consensus state at.
clientStateRes, err = cc.QueryClientStateResponse(ctx, height, clientId)
if err != nil {
return nil, nil, nil, nil, clienttypes.Height{}, err
}
clientState, err = clienttypes.UnpackClientState(clientStateRes.ClientState)
if err != nil {
return nil, nil, nil, nil, clienttypes.Height{}, err
}
eg.Go(func() error {
var err error
consensusStateRes, err = cc.QueryClientConsensusState(ctx, height, clientId, clientState.GetLatestHeight())
return err
})
eg.Go(func() error {
var err error
connectionStateRes, err = cc.QueryConnection(ctx, height, connId)
return err
})
if err := eg.Wait(); err != nil {
return nil, nil, nil, nil, clienttypes.Height{}, err
}
return clientState, clientStateRes.Proof, consensusStateRes.Proof, connectionStateRes.Proof, connectionStateRes.ProofHeight, nil
}
// QueryChannel returns the channel associated with a channelID
func (cc *CosmosProvider) QueryChannel(ctx context.Context, height int64, channelid, portid string) (chanRes *chantypes.QueryChannelResponse, err error) {
res, err := cc.queryChannelABCI(ctx, height, portid, channelid)
if err != nil && strings.Contains(err.Error(), "not found") {
return &chantypes.QueryChannelResponse{
Channel: &chantypes.Channel{
State: chantypes.UNINITIALIZED,
Ordering: chantypes.UNORDERED,
Counterparty: chantypes.Counterparty{
PortId: "port",
ChannelId: "channel",
},
ConnectionHops: []string{},
Version: "version",
},
Proof: []byte{},
ProofHeight: clienttypes.Height{
RevisionNumber: 0,
RevisionHeight: 0,
},
}, nil
} else if err != nil {
return nil, err
}
return res, nil
}
func (cc *CosmosProvider) queryChannelABCI(ctx context.Context, height int64, portID, channelID string) (*chantypes.QueryChannelResponse, error) {
key := host.ChannelKey(portID, channelID)
value, proofBz, proofHeight, err := cc.QueryTendermintProof(ctx, height, key)
if err != nil {
return nil, err
}
// check if channel exists
if len(value) == 0 {
return nil, sdkerrors.Wrapf(chantypes.ErrChannelNotFound, "portID (%s), channelID (%s)", portID, channelID)
}
cdc := codec.NewProtoCodec(cc.Codec.InterfaceRegistry)
var channel chantypes.Channel
if err := cdc.Unmarshal(value, &channel); err != nil {
return nil, err
}
return &chantypes.QueryChannelResponse{
Channel: &channel,
Proof: proofBz,
ProofHeight: proofHeight,
}, nil
}
// QueryChannelClient returns the client state of the client supporting a given channel
func (cc *CosmosProvider) QueryChannelClient(ctx context.Context, height int64, channelid, portid string) (*clienttypes.IdentifiedClientState, error) {
qc := chantypes.NewQueryClient(cc)
cState, err := qc.ChannelClientState(ctx, &chantypes.QueryChannelClientStateRequest{
PortId: portid,
ChannelId: channelid,
})
if err != nil {
return nil, err
}
return cState.IdentifiedClientState, nil
}
// QueryConnectionChannels queries the channels associated with a connection
func (cc *CosmosProvider) QueryConnectionChannels(ctx context.Context, height int64, connectionid string) ([]*chantypes.IdentifiedChannel, error) {
qc := chantypes.NewQueryClient(cc)
chans, err := qc.ConnectionChannels(ctx, &chantypes.QueryConnectionChannelsRequest{
Connection: connectionid,
Pagination: DefaultPageRequest(),
})
if err != nil {
return nil, err
}
return chans.Channels, nil
}
// QueryChannels returns all the channels that are registered on a chain
// TODO add pagination support
func (cc *CosmosProvider) QueryChannels(ctx context.Context) ([]*chantypes.IdentifiedChannel, error) {
qc := chantypes.NewQueryClient(cc)
res, err := qc.Channels(ctx, &chantypes.QueryChannelsRequest{
Pagination: DefaultPageRequest(),
})
if err != nil {
return nil, err
}
return res.Channels, nil
}
// QueryPacketCommitments returns an array of packet commitments
// TODO add pagination support
func (cc *CosmosProvider) QueryPacketCommitments(ctx context.Context, height uint64, channelid, portid string) (commitments *chantypes.QueryPacketCommitmentsResponse, err error) {
qc := chantypes.NewQueryClient(cc)
c, err := qc.PacketCommitments(ctx, &chantypes.QueryPacketCommitmentsRequest{
PortId: portid,
ChannelId: channelid,
Pagination: DefaultPageRequest(),
})
if err != nil {
return nil, err
}
return c, nil
}
// QueryPacketAcknowledgements returns an array of packet acks
// TODO add pagination support
func (cc *CosmosProvider) QueryPacketAcknowledgements(ctx context.Context, height uint64, channelid, portid string) (acknowledgements []*chantypes.PacketState, err error) {
qc := chantypes.NewQueryClient(cc)
acks, err := qc.PacketAcknowledgements(ctx, &chantypes.QueryPacketAcknowledgementsRequest{
PortId: portid,
ChannelId: channelid,
Pagination: DefaultPageRequest(),
})
if err != nil {
return nil, err
}
return acks.Acknowledgements, nil
}
// QueryUnreceivedPackets returns a list of unrelayed packet commitments
func (cc *CosmosProvider) QueryUnreceivedPackets(ctx context.Context, height uint64, channelid, portid string, seqs []uint64) ([]uint64, error) {
qc := chantypes.NewQueryClient(cc)
res, err := qc.UnreceivedPackets(ctx, &chantypes.QueryUnreceivedPacketsRequest{
PortId: portid,
ChannelId: channelid,
PacketCommitmentSequences: seqs,
})
if err != nil {
return nil, err
}
return res.Sequences, nil
}
// QueryUnreceivedAcknowledgements returns a list of unrelayed packet acks
func (cc *CosmosProvider) QueryUnreceivedAcknowledgements(ctx context.Context, height uint64, channelid, portid string, seqs []uint64) ([]uint64, error) {
qc := chantypes.NewQueryClient(cc)
res, err := qc.UnreceivedAcks(ctx, &chantypes.QueryUnreceivedAcksRequest{
PortId: portid,
ChannelId: channelid,
PacketAckSequences: seqs,
})
if err != nil {
return nil, err
}
return res.Sequences, nil
}
// QueryNextSeqRecv returns the next seqRecv for a configured channel
func (cc *CosmosProvider) QueryNextSeqRecv(ctx context.Context, height int64, channelid, portid string) (recvRes *chantypes.QueryNextSequenceReceiveResponse, err error) {
key := host.NextSequenceRecvKey(portid, channelid)
value, proofBz, proofHeight, err := cc.QueryTendermintProof(ctx, height, key)
if err != nil {
return nil, err
}
// check if next sequence receive exists
if len(value) == 0 {
return nil, sdkerrors.Wrapf(chantypes.ErrChannelNotFound, "portID (%s), channelID (%s)", portid, channelid)
}
sequence := binary.BigEndian.Uint64(value)
return &chantypes.QueryNextSequenceReceiveResponse{
NextSequenceReceive: sequence,
Proof: proofBz,
ProofHeight: proofHeight,
}, nil
}
// QueryPacketCommitment returns the packet commitment proof at a given height
func (cc *CosmosProvider) QueryPacketCommitment(ctx context.Context, height int64, channelid, portid string, seq uint64) (comRes *chantypes.QueryPacketCommitmentResponse, err error) {
key := host.PacketCommitmentKey(portid, channelid, seq)
value, proofBz, proofHeight, err := cc.QueryTendermintProof(ctx, height, key)
if err != nil {
return nil, err
}
// check if packet commitment exists
if len(value) == 0 {
return nil, sdkerrors.Wrapf(chantypes.ErrPacketCommitmentNotFound, "portID (%s), channelID (%s), sequence (%d)", portid, channelid, seq)
}
return &chantypes.QueryPacketCommitmentResponse{
Commitment: value,
Proof: proofBz,
ProofHeight: proofHeight,
}, nil
}
// QueryPacketAcknowledgement returns the packet ack proof at a given height
func (cc *CosmosProvider) QueryPacketAcknowledgement(ctx context.Context, height int64, channelid, portid string, seq uint64) (ackRes *chantypes.QueryPacketAcknowledgementResponse, err error) {
key := host.PacketAcknowledgementKey(portid, channelid, seq)
value, proofBz, proofHeight, err := cc.QueryTendermintProof(ctx, height, key)
if err != nil {
return nil, err
}
if len(value) == 0 {
return nil, sdkerrors.Wrapf(chantypes.ErrInvalidAcknowledgement, "portID (%s), channelID (%s), sequence (%d)", portid, channelid, seq)
}
return &chantypes.QueryPacketAcknowledgementResponse{
Acknowledgement: value,
Proof: proofBz,
ProofHeight: proofHeight,
}, nil
}
// QueryPacketReceipt returns the packet receipt proof at a given height
func (cc *CosmosProvider) QueryPacketReceipt(ctx context.Context, height int64, channelid, portid string, seq uint64) (recRes *chantypes.QueryPacketReceiptResponse, err error) {
key := host.PacketReceiptKey(portid, channelid, seq)
value, proofBz, proofHeight, err := cc.QueryTendermintProof(ctx, height, key)
if err != nil {
return nil, err
}
return &chantypes.QueryPacketReceiptResponse{
Received: value != nil,
Proof: proofBz,
ProofHeight: proofHeight,
}, nil
}
func (cc *CosmosProvider) QueryLatestHeight(ctx context.Context) (int64, error) {
stat, err := cc.RPCClient.Status(ctx)
if err != nil {
return -1, err
} else if stat.SyncInfo.CatchingUp {
return -1, fmt.Errorf("node at %s running chain %s not caught up", cc.PCfg.RPCAddr, cc.PCfg.ChainID)
}
return stat.SyncInfo.LatestBlockHeight, nil
}
// QueryHeaderAtHeight returns the header at a given height
func (cc *CosmosProvider) QueryHeaderAtHeight(ctx context.Context, height int64) (ibcexported.Header, error) {
var (
page = 1
perPage = 100000
)
if height <= 0 {
return nil, fmt.Errorf("must pass in valid height, %d not valid", height)
}
res, err := cc.RPCClient.Commit(ctx, &height)
if err != nil {
return nil, err
}
val, err := cc.RPCClient.Validators(ctx, &height, &page, &perPage)
if err != nil {
return nil, err
}
protoVal, err := tmtypes.NewValidatorSet(val.Validators).ToProto()
if err != nil {
return nil, err
}
return &tmclient.Header{
// NOTE: This is not a SignedHeader
// We are missing a light.Commit type here
SignedHeader: res.SignedHeader.ToProto(),
ValidatorSet: protoVal,
}, nil
}
// QueryDenomTrace takes a denom from IBC and queries the information about it
func (cc *CosmosProvider) QueryDenomTrace(ctx context.Context, denom string) (*transfertypes.DenomTrace, error) {
transfers, err := transfertypes.NewQueryClient(cc).DenomTrace(ctx,
&transfertypes.QueryDenomTraceRequest{
Hash: denom,
})
if err != nil {
return nil, err
}
return transfers.DenomTrace, nil
}
// QueryDenomTraces returns all the denom traces from a given chain
// TODO add pagination support
func (cc *CosmosProvider) QueryDenomTraces(ctx context.Context, offset, limit uint64, height int64) ([]transfertypes.DenomTrace, error) {
transfers, err := transfertypes.NewQueryClient(cc).DenomTraces(ctx,
&transfertypes.QueryDenomTracesRequest{
Pagination: DefaultPageRequest(),
})
if err != nil {
return nil, err
}
return transfers.DenomTraces, nil
}
func (cc *CosmosProvider) QueryStakingParams(ctx context.Context) (*stakingtypes.Params, error) {
res, err := stakingtypes.NewQueryClient(cc).Params(ctx, &stakingtypes.QueryParamsRequest{})
if err != nil {
return nil, err
}
return &res.Params, nil
}
func DefaultPageRequest() *querytypes.PageRequest {
return &querytypes.PageRequest{
Key: []byte(""),
Offset: 0,
Limit: 1000,
CountTotal: true,
}
}
func (cc *CosmosProvider) QueryConsensusStateABCI(ctx context.Context, clientID string, height ibcexported.Height) (*clienttypes.QueryConsensusStateResponse, error) {
key := host.FullConsensusStateKey(clientID, height)
value, proofBz, proofHeight, err := cc.QueryTendermintProof(ctx, int64(height.GetRevisionHeight()), key)
if err != nil {
return nil, err
}
// check if consensus state exists
if len(value) == 0 {
return nil, sdkerrors.Wrap(clienttypes.ErrConsensusStateNotFound, clientID)
}
// TODO do we really want to create a new codec? ChainClient exposes proto.Marshaler
cdc := codec.NewProtoCodec(cc.Codec.InterfaceRegistry)
cs, err := clienttypes.UnmarshalConsensusState(cdc, value)
if err != nil {
return nil, err
}
anyConsensusState, err := clienttypes.PackConsensusState(cs)
if err != nil {
return nil, err
}
return &clienttypes.QueryConsensusStateResponse{
ConsensusState: anyConsensusState,
Proof: proofBz,
ProofHeight: proofHeight,
}, nil
}
// QueryInterqueries returns a list of all interquery requests
func (cc *CosmosProvider) QueryInterqueries(ctx context.Context, height uint64) ([]interquerytypes.Interquery, error) {
qc := interquerytypes.NewQueryClient(cc)
res, err := qc.InterqueryAll(ctx, &interquerytypes.QueryAllInterqueryRequest{
Pagination: DefaultPageRequest(),
})
if err != nil {
return nil, err
}
return res.Interquery, nil
}
// QueryInterqueryResults returns a list of all interquery results
func (cc *CosmosProvider) QueryInterqueryResults(ctx context.Context, height uint64) ([]interquerytypes.InterqueryResult, error) {
qc := interquerytypes.NewQueryClient(cc)
res, err := qc.InterqueryResultAll(ctx, &interquerytypes.QueryAllInterqueryResultRequest{
Pagination: DefaultPageRequest(),
})
if err != nil {
return nil, err
}
return res.Interqueryresult, nil
}
// QueryInterqueryTimeoutResults returns a list of all timeout interquery results
func (cc *CosmosProvider) QueryInterqueryTimeoutResults(ctx context.Context, height uint64) ([]interquerytypes.InterqueryTimeoutResult, error) {
qc := interquerytypes.NewQueryClient(cc)
res, err := qc.InterqueryTimeoutResultAll(ctx, &interquerytypes.QueryAllInterqueryTimeoutResultRequest{
Pagination: DefaultPageRequest(),
})
if err != nil {
return nil, err
}
return res.Interquerytimeoutresult, nil
}