-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
msgs.go
423 lines (375 loc) · 12.7 KB
/
msgs.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
package relayer
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v2/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v2/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types"
tmclient "github.com/cosmos/ibc-go/v2/modules/light-clients/07-tendermint/types"
)
// NOTE: we explicitly call 'MustGetAddress' before 'NewMsg...'
// to ensure the correct config file is being used to generate
// the account prefix. 'NewMsg...' functions take an AccAddress
// rather than a string. The 'address.String()' function uses
// the currently set config file. Querying a counterparty would
// swap the config file. 'MustGetAddress' sets the config file
// correctly. Do not change this ordering until the SDK config
// file handling has been refactored.
// https://github.com/cosmos/cosmos-sdk/issues/8332
// CreateClient creates an sdk.Msg to update the client on src with consensus state from dst
func (c *Chain) CreateClient(
//nolint:interfacer
clientState *tmclient.ClientState,
dstHeader *tmclient.Header) (sdk.Msg, error) {
if err := dstHeader.ValidateBasic(); err != nil {
return nil, err
}
msg, err := clienttypes.NewMsgCreateClient(
clientState,
dstHeader.ConsensusState(),
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
if err != nil {
return nil, err
}
if err = msg.ValidateBasic(); err != nil {
return nil, err
}
return msg, nil
}
// UpdateClient creates an sdk.Msg to update the client on src with data pulled from dst
// at the request height..
func (c *Chain) UpdateClient(dst *Chain, dsth *tmclient.Header) (sdk.Msg, error) {
if err := dsth.ValidateBasic(); err != nil {
return nil, err
}
msg, err := clienttypes.NewMsgUpdateClient(
c.PathEnd.ClientID,
dsth,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
if err != nil {
return nil, err
}
return msg, nil
}
// ConnInit creates a MsgConnectionOpenInit
func (c *Chain) ConnInit(counterparty *Chain, counterpartyHeader *tmclient.Header) ([]sdk.Msg, error) {
updateMsg, err := c.UpdateClient(counterparty, counterpartyHeader)
if err != nil {
return nil, err
}
var version *conntypes.Version
msg := conntypes.NewMsgConnectionOpenInit(
c.PathEnd.ClientID,
counterparty.PathEnd.ClientID,
defaultChainPrefix,
version,
defaultDelayPeriod,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
return []sdk.Msg{updateMsg, msg}, nil
}
// ConnTry creates a MsgConnectionOpenTry
func (c *Chain) ConnTry(counterparty *Chain, counterpartyHeader *tmclient.Header) ([]sdk.Msg, error) {
updateMsg, err := c.UpdateClient(counterparty, counterpartyHeader)
if err != nil {
return nil, err
}
cph, err := counterparty.QueryLatestHeight()
if err != nil {
return nil, err
}
clientState, clientStateProof, consensusStateProof, connStateProof,
proofHeight, err := counterparty.GenerateConnHandshakeProof(uint64(cph))
if err != nil {
return nil, err
}
// TODO: Get DelayPeriod from counterparty connection rather than using default value
msg := conntypes.NewMsgConnectionOpenTry(
c.PathEnd.ConnectionID,
c.PathEnd.ClientID,
counterparty.PathEnd.ConnectionID,
counterparty.PathEnd.ClientID,
clientState,
defaultChainPrefix,
conntypes.ExportedVersionsToProto(conntypes.GetCompatibleVersions()),
defaultDelayPeriod,
connStateProof,
clientStateProof,
consensusStateProof,
proofHeight,
clientState.GetLatestHeight().(clienttypes.Height),
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
if err := msg.ValidateBasic(); err != nil {
return nil, err
}
return []sdk.Msg{updateMsg, msg}, nil
}
// ConnAck creates a MsgConnectionOpenAck
func (c *Chain) ConnAck(counterparty *Chain, counterpartyHeader *tmclient.Header) ([]sdk.Msg, error) {
updateMsg, err := c.UpdateClient(counterparty, counterpartyHeader)
if err != nil {
return nil, err
}
cph, err := counterparty.QueryLatestHeight()
if err != nil {
return nil, err
}
clientState, clientStateProof, consensusStateProof, connStateProof,
proofHeight, err := counterparty.GenerateConnHandshakeProof(uint64(cph))
if err != nil {
return nil, err
}
msg := conntypes.NewMsgConnectionOpenAck(
c.PathEnd.ConnectionID,
counterparty.PathEnd.ConnectionID,
clientState,
connStateProof,
clientStateProof,
consensusStateProof,
proofHeight,
clientState.GetLatestHeight().(clienttypes.Height),
conntypes.DefaultIBCVersion,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
return []sdk.Msg{updateMsg, msg}, nil
}
// ConnConfirm creates a MsgConnectionOpenConfirm
func (c *Chain) ConnConfirm(counterparty *Chain, counterpartyHeader *tmclient.Header) ([]sdk.Msg, error) {
updateMsg, err := c.UpdateClient(counterparty, counterpartyHeader)
if err != nil {
return nil, err
}
cph, err := counterparty.QueryLatestHeight()
if err != nil {
return nil, err
}
counterpartyConnState, err := counterparty.QueryConnection(cph)
if err != nil {
return nil, err
}
msg := conntypes.NewMsgConnectionOpenConfirm(
c.PathEnd.ConnectionID,
counterpartyConnState.Proof,
counterpartyConnState.ProofHeight,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
return []sdk.Msg{updateMsg, msg}, nil
}
// ChanInit creates a MsgChannelOpenInit
func (c *Chain) ChanInit(counterparty *Chain, counterpartyHeader *tmclient.Header) ([]sdk.Msg, error) {
updateMsg, err := c.UpdateClient(counterparty, counterpartyHeader)
if err != nil {
return nil, err
}
msg := chantypes.NewMsgChannelOpenInit(
c.PathEnd.PortID,
c.PathEnd.Version,
c.PathEnd.GetOrder(),
[]string{c.PathEnd.ConnectionID},
counterparty.PathEnd.PortID,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
return []sdk.Msg{updateMsg, msg}, nil
}
// ChanTry creates a MsgChannelOpenTry
func (c *Chain) ChanTry(counterparty *Chain, counterpartyHeader *tmclient.Header) ([]sdk.Msg, error) {
updateMsg, err := c.UpdateClient(counterparty, counterpartyHeader)
if err != nil {
return nil, err
}
cph, err := counterparty.QueryLatestHeight()
if err != nil {
return nil, err
}
counterpartyChannelRes, err := counterparty.QueryChannel(cph)
if err != nil {
return nil, err
}
msg := chantypes.NewMsgChannelOpenTry(
c.PathEnd.PortID,
c.PathEnd.ChannelID,
c.PathEnd.Version,
counterpartyChannelRes.Channel.Ordering,
[]string{c.PathEnd.ConnectionID},
counterparty.PathEnd.PortID,
counterparty.PathEnd.ChannelID,
counterpartyChannelRes.Channel.Version,
counterpartyChannelRes.Proof,
counterpartyChannelRes.ProofHeight,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
return []sdk.Msg{updateMsg, msg}, nil
}
// ChanAck creates a MsgChannelOpenAck
func (c *Chain) ChanAck(counterparty *Chain, counterpartyHeader *tmclient.Header) ([]sdk.Msg, error) {
updateMsg, err := c.UpdateClient(counterparty, counterpartyHeader)
if err != nil {
return nil, err
}
cph, err := counterparty.QueryLatestHeight()
if err != nil {
return nil, err
}
counterpartyChannelRes, err := counterparty.QueryChannel(cph)
if err != nil {
return nil, err
}
msg := chantypes.NewMsgChannelOpenAck(
c.PathEnd.PortID,
c.PathEnd.ChannelID,
counterparty.PathEnd.ChannelID,
counterpartyChannelRes.Channel.Version,
counterpartyChannelRes.Proof,
counterpartyChannelRes.ProofHeight,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
return []sdk.Msg{updateMsg, msg}, nil
}
// ChanConfirm creates a MsgChannelOpenConfirm
func (c *Chain) ChanConfirm(counterparty *Chain, counterpartyHeader *tmclient.Header) ([]sdk.Msg, error) {
updateMsg, err := c.UpdateClient(counterparty, counterpartyHeader)
if err != nil {
return nil, err
}
cph, err := counterparty.QueryLatestHeight()
if err != nil {
return nil, err
}
counterpartyChanState, err := counterparty.QueryChannel(cph)
if err != nil {
return nil, err
}
msg := chantypes.NewMsgChannelOpenConfirm(
c.PathEnd.PortID,
c.PathEnd.ChannelID,
counterpartyChanState.Proof,
counterpartyChanState.ProofHeight,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
return []sdk.Msg{updateMsg, msg}, nil
}
// ChanCloseInit creates a MsgChannelCloseInit
func (c *Chain) ChanCloseInit() sdk.Msg {
return chantypes.NewMsgChannelCloseInit(
c.PathEnd.PortID,
c.PathEnd.ChannelID,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
}
// ChanCloseConfirm creates a MsgChannelCloseConfirm
func (c *Chain) ChanCloseConfirm(dstChanState *chantypes.QueryChannelResponse) sdk.Msg {
return chantypes.NewMsgChannelCloseConfirm(
c.PathEnd.PortID,
c.PathEnd.ChannelID,
dstChanState.Proof,
dstChanState.ProofHeight,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
)
}
// MsgTransfer creates a new transfer message
func (c *Chain) MsgTransfer(dst *PathEnd, amount sdk.Coin, dstAddr string,
timeoutHeight, timeoutTimestamp uint64) sdk.Msg {
version := clienttypes.ParseChainID(dst.ChainID)
return transfertypes.NewMsgTransfer(
c.PathEnd.PortID,
c.PathEnd.ChannelID,
amount,
c.MustGetAddress(), // 'MustGetAddress' must be called directly before calling 'NewMsg...'
dstAddr,
clienttypes.NewHeight(version, timeoutHeight),
timeoutTimestamp,
)
}
// MsgRelayRecvPacket constructs the MsgRecvPacket which is to be sent to the receiving chain.
// The counterparty represents the sending chain where the packet commitment would be stored.
func (c *Chain) MsgRelayRecvPacket(counterparty *Chain, counterpartyHeight int64, packet *relayMsgRecvPacket) (sdk.Msg, error) {
comRes, err := counterparty.QueryPacketCommitment(counterpartyHeight, packet.seq)
switch {
case err != nil:
return nil, err
case comRes.Proof == nil || comRes.Commitment == nil:
return nil, fmt.Errorf("recv packet commitment query seq(%d) is nil", packet.seq)
case comRes == nil:
return nil, fmt.Errorf("receive packet [%s]seq{%d} has no associated proofs", c.ChainID, packet.seq)
default:
return chantypes.NewMsgRecvPacket(
chantypes.NewPacket(
packet.packetData,
packet.seq,
counterparty.PathEnd.PortID,
counterparty.PathEnd.ChannelID,
c.PathEnd.PortID,
c.PathEnd.ChannelID,
packet.timeout,
packet.timeoutStamp,
),
comRes.Proof,
comRes.ProofHeight,
c.MustGetAddress(),
), nil
}
}
// MsgRelayAcknowledgement constructs the MsgAcknowledgement which is to be sent to the sending chain.
// The counterparty represents the receiving chain where the acknowledgement would be stored.
func (c *Chain) MsgRelayAcknowledgement(counterparty *Chain, counterpartyHeight int64, packet *relayMsgPacketAck) (sdk.Msg, error) {
ackRes, err := counterparty.QueryPacketAcknowledgement(counterpartyHeight, packet.seq)
switch {
case err != nil:
return nil, err
case ackRes.Proof == nil || ackRes.Acknowledgement == nil:
return nil, fmt.Errorf("ack packet acknowledgement query seq(%d) is nil", packet.seq)
case ackRes == nil:
return nil, fmt.Errorf("ack packet [%s]seq{%d} has no associated proofs", counterparty.ChainID, packet.seq)
default:
return chantypes.NewMsgAcknowledgement(
chantypes.NewPacket(
packet.packetData,
packet.seq,
c.PathEnd.PortID,
c.PathEnd.ChannelID,
counterparty.PathEnd.PortID,
counterparty.PathEnd.ChannelID,
packet.timeout,
packet.timeoutStamp,
),
packet.ack,
ackRes.Proof,
ackRes.ProofHeight,
c.MustGetAddress()), nil
}
}
// MsgRelayTimeout constructs the MsgTimeout which is to be sent to the sending chain.
// The counterparty represents the receiving chain where the receipts would have been
// stored.
func (c *Chain) MsgRelayTimeout(counterparty *Chain, counterpartyHeight int64, packet *relayMsgTimeout) (sdk.Msg, error) {
recvRes, err := counterparty.QueryPacketReceipt(counterpartyHeight, packet.seq)
switch {
case err != nil:
return nil, err
case recvRes.Proof == nil:
return nil, fmt.Errorf("timeout packet receipt proof seq(%d) is nil", packet.seq)
case recvRes == nil:
return nil, fmt.Errorf("timeout packet [%s]seq{%d} has no associated proofs", c.ChainID, packet.seq)
default:
return chantypes.NewMsgTimeout(
chantypes.NewPacket(
packet.packetData,
packet.seq,
c.PathEnd.PortID,
c.PathEnd.ChannelID,
counterparty.PathEnd.PortID,
counterparty.PathEnd.ChannelID,
packet.timeout,
packet.timeoutStamp,
),
packet.seq,
recvRes.Proof,
recvRes.ProofHeight,
c.MustGetAddress(),
), nil
}
}