/
query.go
241 lines (218 loc) · 7.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
package relay
import (
"context"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
transfertypes "github.com/cosmos/ibc-go/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/modules/core/exported"
"google.golang.org/protobuf/types/known/emptypb"
)
// QueryLatestHeight queries the chain for the latest height and returns it
func (c *Chain) GetLatestHeight() (int64, error) {
return 0, nil
}
// QueryClientConsensusState retrevies the latest consensus state for a client in state at a given height
func (c *Chain) QueryClientConsensusState(height int64, dstClientConsHeight ibcexported.Height) (*clienttypes.QueryConsensusStateResponse, error) {
return c.client.clientQuery.ConsensusState(
context.TODO(),
&clienttypes.QueryConsensusStateRequest{
ClientId: c.pathEnd.ClientID,
RevisionNumber: dstClientConsHeight.GetRevisionNumber(),
RevisionHeight: dstClientConsHeight.GetRevisionHeight(),
LatestHeight: false,
},
)
}
// height represents the height of src chain
func (c *Chain) QueryClientState(height int64) (*clienttypes.QueryClientStateResponse, error) {
return c.client.clientQuery.ClientState(
context.TODO(),
&clienttypes.QueryClientStateRequest{
ClientId: c.pathEnd.ClientID,
},
)
}
// QueryConnection returns the remote end of a given connection
func (c *Chain) QueryConnection(height int64) (*conntypes.QueryConnectionResponse, error) {
return c.client.connQuery.Connection(
context.TODO(),
&conntypes.QueryConnectionRequest{
ConnectionId: c.pathEnd.ConnectionID,
},
)
}
// QueryChannel returns the channel associated with a channelID
func (c *Chain) QueryChannel(height int64) (chanRes *chantypes.QueryChannelResponse, err error) {
return c.client.chanQuery.Channel(
context.TODO(),
&chantypes.QueryChannelRequest{
PortId: c.pathEnd.PortID,
ChannelId: c.pathEnd.ChannelID,
},
)
}
// QueryBalance returns the amount of coins in the relayer account
func (c *Chain) QueryBalance(address sdk.AccAddress) (sdk.Coins, error) {
addr := address.String()
res, err := c.client.bank.QueryBank(
context.TODO(),
&emptypb.Empty{},
)
if err != nil {
return nil, err
}
var coins sdk.Coins
for denom, allocated := range res.Allocated.DenomToMap {
if amount, ok := allocated.PubkeyToAmount[addr]; ok {
amount, err := strconv.Atoi(amount)
if err != nil {
return nil, err
}
coins = append(coins, sdk.Coin{
Denom: denom,
Amount: sdk.NewInt(int64(amount)),
})
}
}
for denom, minted := range res.Minted.DenomToMap {
if amount, ok := minted.PubkeyToAmount[addr]; ok {
amount, err := strconv.Atoi(amount)
if err != nil {
return nil, err
}
coins = append(coins, sdk.Coin{
Denom: denom,
Amount: sdk.NewInt(int64(amount)),
})
}
}
return coins, nil
}
// QueryDenomTraces returns all the denom traces from a given chain
func (c *Chain) QueryDenomTraces(offset, limit uint64, height int64) (*transfertypes.QueryDenomTracesResponse, error) {
return c.client.transferQuery.DenomTraces(
context.TODO(),
&transfertypes.QueryDenomTracesRequest{Pagination: makePagination(offset, limit)},
)
}
// QueryPacketCommitment returns the packet commitment proof at a given height
func (c *Chain) QueryPacketCommitment(height int64, seq uint64) (comRes *chantypes.QueryPacketCommitmentResponse, err error) {
return c.client.chanQuery.PacketCommitment(
context.TODO(),
&chantypes.QueryPacketCommitmentRequest{
PortId: c.pathEnd.PortID,
ChannelId: c.pathEnd.ChannelID,
Sequence: seq,
},
)
}
// QueryPacketCommitments returns an array of packet commitments
func (c *Chain) QueryPacketCommitments(offset, limit uint64, height int64) (comRes *chantypes.QueryPacketCommitmentsResponse, err error) {
return c.client.chanQuery.PacketCommitments(
context.TODO(),
&chantypes.QueryPacketCommitmentsRequest{
PortId: c.pathEnd.PortID,
ChannelId: c.pathEnd.ChannelID,
Pagination: makePagination(offset, limit),
},
)
}
// QueryUnrecievedPackets returns a list of unrelayed packet commitments
func (c *Chain) QueryUnrecievedPackets(height int64, seqs []uint64) ([]uint64, error) {
res, err := c.client.chanQuery.UnreceivedPackets(
context.TODO(),
&chantypes.QueryUnreceivedPacketsRequest{
PortId: c.pathEnd.PortID,
ChannelId: c.pathEnd.ChannelID,
PacketCommitmentSequences: seqs,
},
)
if err != nil {
return nil, err
}
return res.Sequences, nil
}
// QueryPacketAcknowledgements returns an array of packet acks
func (c *Chain) QueryPacketAcknowledgementCommitments(offset, limit uint64, height int64) (comRes *chantypes.QueryPacketAcknowledgementsResponse, err error) {
return c.client.chanQuery.PacketAcknowledgements(
context.TODO(),
&chantypes.QueryPacketAcknowledgementsRequest{
PortId: c.pathEnd.PortID,
ChannelId: c.pathEnd.ChannelID,
Pagination: makePagination(offset, limit),
},
)
}
// QueryUnrecievedAcknowledgements returns a list of unrelayed packet acks
func (c *Chain) QueryUnrecievedAcknowledgements(height int64, seqs []uint64) ([]uint64, error) {
res, err := c.client.chanQuery.UnreceivedAcks(
context.TODO(),
&chantypes.QueryUnreceivedAcksRequest{
PortId: c.pathEnd.PortID,
ChannelId: c.pathEnd.ChannelID,
PacketAckSequences: seqs,
},
)
if err != nil {
return nil, err
}
return res.Sequences, nil
}
// QueryPacketAcknowledgementCommitment returns the packet ack proof at a given height
func (c *Chain) QueryPacketAcknowledgementCommitment(height int64, seq uint64) (ackRes *chantypes.QueryPacketAcknowledgementResponse, err error) {
return c.client.chanQuery.PacketAcknowledgement(
context.TODO(),
&chantypes.QueryPacketAcknowledgementRequest{
PortId: c.pathEnd.PortID,
ChannelId: c.pathEnd.ChannelID,
Sequence: seq,
},
)
}
// QueryPacket returns a packet corresponds to a given sequence
func (c *Chain) QueryPacket(height int64, sequence uint64) (*chantypes.Packet, error) {
res, err := c.client.chanQuery.PacketCommitment(
context.TODO(),
&chantypes.QueryPacketCommitmentRequest{
PortId: c.pathEnd.PortID,
ChannelId: c.pathEnd.ChannelID,
Sequence: sequence,
},
)
if err != nil {
return nil, err
}
// In Corda-IBC, packet commitment = marshaled packet :)
var packet chantypes.Packet
if err := packet.Unmarshal(res.Commitment); err != nil {
return nil, err
}
return &packet, nil
}
func (c *Chain) QueryPacketAcknowledgement(height int64, sequence uint64) ([]byte, error) {
res, err := c.client.chanQuery.PacketAcknowledgement(
context.TODO(),
&chantypes.QueryPacketAcknowledgementRequest{
PortId: c.pathEnd.PortID,
ChannelId: c.pathEnd.ChannelID,
Sequence: sequence,
},
)
if err != nil {
return nil, err
}
// In Corda-IBC, ack commitment = marshaled ack
return res.Acknowledgement, nil
}
func makePagination(offset, limit uint64) *query.PageRequest {
return &query.PageRequest{
Key: []byte(""),
Offset: offset,
Limit: limit,
CountTotal: true,
}
}