-
Notifications
You must be signed in to change notification settings - Fork 9
/
packet.go
461 lines (409 loc) · 18.4 KB
/
packet.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
package keeper
import (
"bytes"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/bianjieai/tibc-go/modules/tibc/core/02-client/types"
"github.com/bianjieai/tibc-go/modules/tibc/core/04-packet/types"
"github.com/bianjieai/tibc-go/modules/tibc/core/exported"
)
// SendPacket is called by a module to send an TIBC packet on a port owned
// by the calling module to the corresponding module on the counterparty chain.
func (k Keeper) SendPacket(ctx sdk.Context, packet exported.PacketI) error {
if err := packet.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "packet failed basic validation")
}
if packet.GetSourceChain() != k.clientKeeper.GetChainName(ctx) {
return sdkerrors.Wrap(types.ErrInvalidPacket, "source chain of packet is not this chain")
}
targetChain := packet.GetDestChain()
if len(packet.GetRelayChain()) > 0 {
targetChain = packet.GetRelayChain()
}
_, found := k.clientKeeper.GetClientState(ctx, targetChain)
if !found {
return clienttypes.ErrClientNotFound
}
nextSequenceSend := k.GetNextSequenceSend(ctx, packet.GetSourceChain(), packet.GetDestChain())
if packet.GetSequence() != nextSequenceSend {
return sdkerrors.Wrapf(
types.ErrInvalidPacket,
"packet sequence ≠ next send sequence (%d ≠ %d)", packet.GetSequence(), nextSequenceSend,
)
}
commitment := types.CommitPacket(packet)
nextSequenceSend++
k.SetNextSequenceSend(ctx, packet.GetSourceChain(), packet.GetDestChain(), nextSequenceSend)
k.SetPacketCommitment(ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence(), commitment)
// Emit Event with Packet data along with other packet information for relayer to pick up
// and relay to other chain
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSendPacket,
sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData())),
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())),
sdk.NewAttribute(types.AttributeKeyPort, packet.GetPort()),
sdk.NewAttribute(types.AttributeKeySrcChain, packet.GetSourceChain()),
sdk.NewAttribute(types.AttributeKeyDstChain, packet.GetDestChain()),
sdk.NewAttribute(types.AttributeKeyRelayChain, packet.GetRelayChain()),
// we only support 1-hop packets now, and that is the most important hop for a relayer
// (is it going to a chain I am connected to)
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
k.Logger(ctx).Info("packet sent", "packet", fmt.Sprintf("%v", packet))
return nil
}
// RecvPacket is called by a module to receive & process an TIBC packet
// sent on the corresponding port on the counterparty chain.
func (k Keeper) RecvPacket(
ctx sdk.Context,
packet exported.PacketI,
proof []byte,
proofHeight exported.Height,
) error {
if err := k.ValidatePacket(ctx, packet); err != nil {
return sdkerrors.Wrap(err, "packet failed basic validation")
}
// check if the packet receipt has been received already for unordered channels
_, found := k.GetPacketReceipt(ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence())
if found {
return sdkerrors.Wrapf(
types.ErrInvalidPacket,
"packet sequence (%d) already has been received", packet.GetSequence(),
)
}
chainName := k.clientKeeper.GetChainName(ctx)
fromChain := packet.GetSourceChain()
if packet.GetDestChain() == chainName && len(packet.GetRelayChain()) > 0 {
fromChain = packet.GetRelayChain()
}
targetClient, found := k.clientKeeper.GetClientState(ctx, fromChain)
if !found {
return sdkerrors.Wrap(clienttypes.ErrClientNotFound, fromChain)
}
commitment := types.CommitPacket(packet)
// verify that the counterparty did commit to sending this packet
if err := targetClient.VerifyPacketCommitment(ctx,
k.clientKeeper.ClientStore(ctx, fromChain), k.cdc, proofHeight,
proof, packet.GetSourceChain(), packet.GetDestChain(),
packet.GetSequence(), commitment,
); err != nil {
return sdkerrors.Wrapf(err, "failed packet commitment verification for client (%s)", fromChain)
}
// All verification complete, update state
// For unordered channels we must set the receipt so it can be verified on the other side.
// This receipt does not contain any data, since the packet has not yet been processed,
// it's just a single store key set to an empty string to indicate that the packet has been received
k.SetPacketReceipt(ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence())
// log that a packet has been received & executed
k.Logger(ctx).Info("packet received", "packet", fmt.Sprintf("%v", packet))
// emit an event that the relayer can query for
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRecvPacket,
sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData())),
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())),
sdk.NewAttribute(types.AttributeKeyPort, packet.GetPort()),
sdk.NewAttribute(types.AttributeKeySrcChain, packet.GetSourceChain()),
sdk.NewAttribute(types.AttributeKeyDstChain, packet.GetDestChain()),
sdk.NewAttribute(types.AttributeKeyRelayChain, packet.GetRelayChain()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
if packet.GetRelayChain() == chainName {
if !k.routingKeeper.Authenticate(ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetPort()) {
return sdkerrors.ErrUnauthorized
}
if _, found = k.clientKeeper.GetClientState(ctx, packet.GetDestChain()); !found {
return sdkerrors.Wrap(clienttypes.ErrClientNotFound, fromChain)
}
k.SetPacketCommitment(ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence(), commitment)
// Emit Event with Packet data along with other packet information for relayer to pick up
// and relay to other chain
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSendPacket,
sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData())),
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())),
sdk.NewAttribute(types.AttributeKeyPort, packet.GetPort()),
sdk.NewAttribute(types.AttributeKeySrcChain, packet.GetSourceChain()),
sdk.NewAttribute(types.AttributeKeyDstChain, packet.GetDestChain()),
sdk.NewAttribute(types.AttributeKeyRelayChain, packet.GetRelayChain()),
// we only support 1-hop packets now, and that is the most important hop for a relayer
// (is it going to a chain I am connected to)
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
}
return nil
}
// WriteAcknowledgement writes the packet execution acknowledgement to the state,
// which will be verified by the counterparty chain using AcknowledgePacket.
//
// CONTRACT:
//
// 1) For synchronous execution, this function is be called in the TIBC handler .
// For async handling, it needs to be called directly by the module which originally
// processed the packet.
//
// 2) Assumes that packet receipt has been written.
// previously by RecvPacket.
func (k Keeper) WriteAcknowledgement(
ctx sdk.Context,
packet exported.PacketI,
acknowledgement []byte,
) error {
if len(acknowledgement) == 0 {
return sdkerrors.Wrap(types.ErrInvalidAcknowledgement, "acknowledgement cannot be empty")
}
// NOTE: TIBC app modules might have written the acknowledgement synchronously on
// the OnRecvPacket callback so we need to check if the acknowledgement is already
// set on the store and return an error if so.
if k.HasPacketAcknowledgement(ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence()) {
return types.ErrAcknowledgementExists
}
targetChain := packet.GetSourceChain()
if len(packet.GetRelayChain()) > 0 && packet.GetDestChain() == k.clientKeeper.GetChainName(ctx) {
targetChain = packet.GetRelayChain()
}
_, found := k.clientKeeper.GetClientState(ctx, targetChain)
if !found {
return clienttypes.ErrClientNotFound
}
// set the acknowledgement so that it can be verified on the other side
k.SetPacketAcknowledgement(
ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence(),
types.CommitAcknowledgement(acknowledgement),
)
k.SetMaxAckSequence(ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence())
// log that a packet acknowledgement has been written
k.Logger(ctx).Info("acknowledged written", "packet", fmt.Sprintf("%v", packet))
// emit an event that the relayer can query for
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeWriteAck,
sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData())),
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())),
sdk.NewAttribute(types.AttributeKeyPort, packet.GetPort()),
sdk.NewAttribute(types.AttributeKeySrcChain, packet.GetSourceChain()),
sdk.NewAttribute(types.AttributeKeyDstChain, packet.GetDestChain()),
sdk.NewAttribute(types.AttributeKeyRelayChain, packet.GetRelayChain()),
sdk.NewAttribute(types.AttributeKeyAck, string(acknowledgement)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
return nil
}
// AcknowledgePacket is called by a module to process the acknowledgement of a
// packet previously sent by the calling module on a port to a counterparty
// module on the counterparty chain. Its intended usage is within the ante
// handler. AcknowledgePacket will clean up the packet commitment,
// which is no longer necessary since the packet has been received and acted upon.
func (k Keeper) AcknowledgePacket(
ctx sdk.Context,
packet exported.PacketI,
acknowledgement []byte,
proof []byte,
proofHeight exported.Height,
) error {
if err := k.ValidatePacket(ctx, packet); err != nil {
return sdkerrors.Wrap(err, "AcknowledgePacket failed basic validation")
}
commitment := k.GetPacketCommitment(ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence())
packetCommitment := types.CommitPacket(packet)
// verify we sent the packet and haven't cleared it out yet
if !bytes.Equal(commitment, packetCommitment) {
return sdkerrors.Wrapf(types.ErrInvalidPacket, "commitment bytes are not equal: got (%v), expected (%v)", packetCommitment, commitment)
}
chainName := k.clientKeeper.GetChainName(ctx)
fromChain := packet.GetDestChain()
if packet.GetSourceChain() == chainName && len(packet.GetRelayChain()) > 0 {
fromChain = packet.GetRelayChain()
}
clientState, found := k.clientKeeper.GetClientState(ctx, fromChain)
if !found {
return sdkerrors.Wrap(clienttypes.ErrClientNotFound, fromChain)
}
ackCommitment := types.CommitAcknowledgement(acknowledgement)
if err := clientState.VerifyPacketAcknowledgement(ctx,
k.clientKeeper.ClientStore(ctx, fromChain), k.cdc, proofHeight,
proof, packet.GetSourceChain(), packet.GetDestChain(),
packet.GetSequence(), ackCommitment,
); err != nil {
return sdkerrors.Wrapf(err, "failed packet acknowledgement verification for client (%s)", fromChain)
}
// Delete packet commitment, since the packet has been acknowledged, the commitement is no longer necessary
k.deletePacketCommitment(ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence())
k.SetMaxAckSequence(ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence())
// log that a packet has been acknowledged
k.Logger(ctx).Info("packet acknowledged", "packet", fmt.Sprintf("%v", packet))
// emit an event marking that we have processed the acknowledgement
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAcknowledgePacket,
sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData())),
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())),
sdk.NewAttribute(types.AttributeKeyPort, packet.GetPort()),
sdk.NewAttribute(types.AttributeKeySrcChain, packet.GetSourceChain()),
sdk.NewAttribute(types.AttributeKeyDstChain, packet.GetDestChain()),
sdk.NewAttribute(types.AttributeKeyRelayChain, packet.GetRelayChain()),
sdk.NewAttribute(types.AttributeKeyAck, string(acknowledgement)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
if packet.GetRelayChain() == chainName {
if _, found = k.clientKeeper.GetClientState(ctx, packet.GetSourceChain()); !found {
return sdkerrors.Wrap(clienttypes.ErrClientNotFound, fromChain)
}
// set the acknowledgement so that it can be verified on the other side
k.SetPacketAcknowledgement(
ctx, packet.GetSourceChain(), packet.GetDestChain(), packet.GetSequence(),
ackCommitment,
)
// emit an event that the relayer can query for
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeWriteAck,
sdk.NewAttribute(types.AttributeKeyData, string(packet.GetData())),
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", packet.GetSequence())),
sdk.NewAttribute(types.AttributeKeyPort, packet.GetPort()),
sdk.NewAttribute(types.AttributeKeySrcChain, packet.GetSourceChain()),
sdk.NewAttribute(types.AttributeKeyDstChain, packet.GetDestChain()),
sdk.NewAttribute(types.AttributeKeyRelayChain, packet.GetRelayChain()),
sdk.NewAttribute(types.AttributeKeyAck, string(acknowledgement)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
}
return nil
}
// CleanPacket is called by users to send an TIBC clean packet to clean ack and receipt whose
// sequence smaller than the sequence in clean packet.
func (k Keeper) CleanPacket(ctx sdk.Context, cleanPacket exported.CleanPacketI) error {
sourceChain := k.clientKeeper.GetChainName(ctx)
if err := cleanPacket.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "packet failed basic validation")
}
if err := k.ValidateCleanPacket(ctx, types.CleanPacket{
Sequence: cleanPacket.GetSequence(),
SourceChain: sourceChain,
DestinationChain: cleanPacket.GetDestChain(),
RelayChain: cleanPacket.GetRelayChain(),
}); err != nil {
return sdkerrors.Wrap(err, "packet failed basic validation")
}
targetChain := cleanPacket.GetDestChain()
if len(cleanPacket.GetRelayChain()) > 0 {
targetChain = cleanPacket.GetRelayChain()
}
_, found := k.clientKeeper.GetClientState(ctx, targetChain)
if !found {
return clienttypes.ErrClientNotFound
}
k.SetCleanPacketCommitment(ctx, sourceChain, cleanPacket.GetDestChain(), cleanPacket.GetSequence())
k.cleanAcknowledgementBySeq(ctx, sourceChain, cleanPacket.GetDestChain(), cleanPacket.GetSequence())
k.cleanReceiptBySeq(ctx, sourceChain, cleanPacket.GetDestChain(), cleanPacket.GetSequence())
// Emit Event with Packet data along with other packet information for relayer to pick up
// and relay to other chain
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSendCleanPacket,
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", cleanPacket.GetSequence())),
sdk.NewAttribute(types.AttributeKeySrcChain, sourceChain),
sdk.NewAttribute(types.AttributeKeyDstChain, cleanPacket.GetDestChain()),
sdk.NewAttribute(types.AttributeKeyRelayChain, cleanPacket.GetRelayChain()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
k.Logger(ctx).Info("clean packet sent", "packet", fmt.Sprintf("%v", cleanPacket))
return nil
}
// RecvCleanPacket is called by relayer to receive & process an TIBC clean packet
// sent on the counterparty chain.
func (k Keeper) RecvCleanPacket(
ctx sdk.Context,
cleanPacket exported.CleanPacketI,
proof []byte,
proofHeight exported.Height,
) error {
if err := k.ValidateCleanPacket(ctx, cleanPacket); err != nil {
return sdkerrors.Wrap(err, "packet failed basic validation")
}
chainName := k.clientKeeper.GetChainName(ctx)
fromChain := cleanPacket.GetSourceChain()
if cleanPacket.GetDestChain() == chainName && len(cleanPacket.GetRelayChain()) > 0 {
fromChain = cleanPacket.GetRelayChain()
}
targetClient, found := k.clientKeeper.GetClientState(ctx, fromChain)
if !found {
return sdkerrors.Wrap(clienttypes.ErrClientNotFound, fromChain)
}
if err := targetClient.VerifyPacketCleanCommitment(ctx,
k.clientKeeper.ClientStore(ctx, fromChain), k.cdc, proofHeight,
proof, cleanPacket.GetSourceChain(), cleanPacket.GetDestChain(),
cleanPacket.GetSequence(),
); err != nil {
return sdkerrors.Wrapf(err, "failed packet commitment verification for client (%s)", fromChain)
}
k.cleanAcknowledgementBySeq(ctx, cleanPacket.GetSourceChain(), cleanPacket.GetDestChain(), cleanPacket.GetSequence())
k.cleanReceiptBySeq(ctx, cleanPacket.GetSourceChain(), cleanPacket.GetDestChain(), cleanPacket.GetSequence())
k.SetCleanPacketCommitment(ctx, cleanPacket.GetSourceChain(), cleanPacket.GetDestChain(), cleanPacket.GetSequence())
// emit an event that the relayer can query for
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRecvCleanPacket,
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", cleanPacket.GetSequence())),
sdk.NewAttribute(types.AttributeKeySrcChain, cleanPacket.GetSourceChain()),
sdk.NewAttribute(types.AttributeKeyDstChain, cleanPacket.GetDestChain()),
sdk.NewAttribute(types.AttributeKeyRelayChain, cleanPacket.GetRelayChain()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
if cleanPacket.GetRelayChain() == chainName {
if _, found = k.clientKeeper.GetClientState(ctx, cleanPacket.GetDestChain()); !found {
return sdkerrors.Wrap(clienttypes.ErrClientNotFound, fromChain)
}
// Emit Event with Packet data along with other packet information for relayer to pick up
// and relay to other chain
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSendCleanPacket,
sdk.NewAttribute(types.AttributeKeySequence, fmt.Sprintf("%d", cleanPacket.GetSequence())),
sdk.NewAttribute(types.AttributeKeySrcChain, cleanPacket.GetSourceChain()),
sdk.NewAttribute(types.AttributeKeyDstChain, cleanPacket.GetDestChain()),
sdk.NewAttribute(types.AttributeKeyRelayChain, cleanPacket.GetRelayChain()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
}
return nil
}