-
Notifications
You must be signed in to change notification settings - Fork 586
/
msg_server_test.go
2758 lines (2347 loc) · 108 KB
/
msg_server_test.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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package keeper_test
import (
"context"
"errors"
"fmt"
upgradetypes "cosmossdk.io/x/upgrade/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/cometbft/cometbft/abci/types"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v9/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v9/modules/core/05-port/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
"github.com/cosmos/ibc-go/v9/modules/core/keeper"
ibctm "github.com/cosmos/ibc-go/v9/modules/light-clients/07-tendermint"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
ibcmock "github.com/cosmos/ibc-go/v9/testing/mock"
)
var (
timeoutHeight = clienttypes.NewHeight(1, 10000)
maxSequence = uint64(10)
)
// tests the IBC handler receiving a packet on ordered and unordered channels.
// It verifies that the storing of an acknowledgement on success occurs. It
// tests high level properties like ordering and basic sanity checks. More
// rigorous testing of 'RecvPacket' can be found in the
// 04-channel/keeper/packet_test.go.
func (suite *KeeperTestSuite) TestHandleRecvPacket() {
var (
packet channeltypes.Packet
path *ibctesting.Path
)
testCases := []struct {
name string
malleate func()
expPass bool
expRevert bool
async bool // indicate no ack written
replay bool // indicate replay (no-op)
}{
{"success: ORDERED", func() {
path.SetChannelOrdered()
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}, true, false, false, false},
{"success: UNORDERED", func() {
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}, true, false, false, false},
{"success: UNORDERED out of order packet", func() {
// setup uses an UNORDERED channel
path.Setup()
// attempts to receive packet with sequence 10 without receiving packet with sequence 1
for i := uint64(1); i < 10; i++ {
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}
}, true, false, false, false},
{"success: OnRecvPacket callback returns revert=true", func() {
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockFailPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockFailPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}, true, true, false, false},
{"success: ORDERED - async acknowledgement", func() {
path.SetChannelOrdered()
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibcmock.MockAsyncPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibcmock.MockAsyncPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}, true, false, true, false},
{"success: UNORDERED - async acknowledgement", func() {
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibcmock.MockAsyncPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibcmock.MockAsyncPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}, true, false, true, false},
{"failure: ORDERED out of order packet", func() {
path.SetChannelOrdered()
path.Setup()
// attempts to receive packet with sequence 10 without receiving packet with sequence 1
for i := uint64(1); i < 10; i++ {
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}
}, false, false, false, false},
{"channel does not exist", func() {
// any non-nil value of packet is valid
suite.Require().NotNil(packet)
}, false, false, false, false},
{"packet not sent", func() {
path.Setup()
packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}, false, false, false, false},
{"successful no-op: ORDERED - packet already received (replay)", func() {
// mock will panic if application callback is called twice on the same packet
path.SetChannelOrdered()
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
err = path.EndpointB.RecvPacket(packet)
suite.Require().NoError(err)
}, true, false, false, true},
{"successful no-op: UNORDERED - packet already received (replay)", func() {
// mock will panic if application callback is called twice on the same packet
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
err = path.EndpointB.RecvPacket(packet)
suite.Require().NoError(err)
}, true, false, false, true},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest() // reset
path = ibctesting.NewPath(suite.chainA, suite.chainB)
tc.malleate()
var (
proof []byte
proofHeight clienttypes.Height
)
// get proof of packet commitment from chainA
packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
if path.EndpointA.ChannelID != "" {
proof, proofHeight = path.EndpointA.QueryProof(packetKey)
}
msg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, suite.chainB.SenderAccount.GetAddress().String())
ctx := suite.chainB.GetContext()
_, err := suite.chainB.App.GetIBCKeeper().RecvPacket(ctx, msg)
events := ctx.EventManager().Events()
if tc.expPass {
suite.Require().NoError(err)
// replay should not fail since it will be treated as a no-op
_, err := suite.chainB.App.GetIBCKeeper().RecvPacket(suite.chainB.GetContext(), msg)
suite.Require().NoError(err)
if tc.expRevert {
// context events should contain error events
suite.Require().Contains(events, keeper.ConvertToErrorEvents(sdk.Events{ibcmock.NewMockRecvPacketEvent()})[0])
suite.Require().NotContains(events, ibcmock.NewMockRecvPacketEvent())
} else {
if tc.replay {
// context should not contain application events
suite.Require().NotContains(events, ibcmock.NewMockRecvPacketEvent())
suite.Require().NotContains(events, keeper.ConvertToErrorEvents(sdk.Events{ibcmock.NewMockRecvPacketEvent()})[0])
} else {
// context events should contain application events
suite.Require().Contains(events, ibcmock.NewMockRecvPacketEvent())
}
}
// verify if ack was written
ack, found := suite.chainB.App.GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
if tc.async {
suite.Require().Nil(ack)
suite.Require().False(found)
} else {
suite.Require().NotNil(ack)
suite.Require().True(found)
}
} else {
suite.Require().Error(err)
}
})
}
}
func (suite *KeeperTestSuite) TestRecoverClient() {
var msg *clienttypes.MsgRecoverClient
testCases := []struct {
name string
malleate func()
expErr error
}{
{
"success: recover client",
func() {},
nil,
},
{
"signer doesn't match authority",
func() {
msg.Signer = ibctesting.InvalidID
},
ibcerrors.ErrUnauthorized,
},
{
"invalid subject client",
func() {
msg.SubjectClientId = ibctesting.InvalidID
},
clienttypes.ErrRouteNotFound,
},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest()
subjectPath := ibctesting.NewPath(suite.chainA, suite.chainB)
subjectPath.SetupClients()
subject := subjectPath.EndpointA.ClientID
subjectClientState := suite.chainA.GetClientState(subject)
substitutePath := ibctesting.NewPath(suite.chainA, suite.chainB)
substitutePath.SetupClients()
substitute := substitutePath.EndpointA.ClientID
// update substitute twice
err := substitutePath.EndpointA.UpdateClient()
suite.Require().NoError(err)
err = substitutePath.EndpointA.UpdateClient()
suite.Require().NoError(err)
tmClientState, ok := subjectClientState.(*ibctm.ClientState)
suite.Require().True(ok)
tmClientState.FrozenHeight = tmClientState.LatestHeight
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), subject, tmClientState)
msg = clienttypes.NewMsgRecoverClient(suite.chainA.App.GetIBCKeeper().GetAuthority(), subject, substitute)
tc.malleate()
_, err = suite.chainA.App.GetIBCKeeper().RecoverClient(suite.chainA.GetContext(), msg)
expPass := tc.expErr == nil
if expPass {
suite.Require().NoError(err)
// Assert that client status is now Active
lightClientModule, err := suite.chainA.App.GetIBCKeeper().ClientKeeper.Route(suite.chainA.GetContext(), subjectPath.EndpointA.ClientID)
suite.Require().NoError(err)
suite.Require().Equal(lightClientModule.Status(suite.chainA.GetContext(), subjectPath.EndpointA.ClientID), exported.Active)
} else {
suite.Require().Error(err)
suite.Require().ErrorIs(err, tc.expErr)
}
})
}
}
// tests the IBC handler acknowledgement of a packet on ordered and unordered
// channels. It verifies that the deletion of packet commitments from state
// occurs. It test high level properties like ordering and basic sanity
// checks. More rigorous testing of 'AcknowledgePacket'
// can be found in the 04-channel/keeper/packet_test.go.
func (suite *KeeperTestSuite) TestHandleAcknowledgePacket() {
var (
packet channeltypes.Packet
path *ibctesting.Path
)
testCases := []struct {
name string
malleate func()
expPass bool
replay bool // indicate replay (no-op)
}{
{"success: ORDERED", func() {
path.SetChannelOrdered()
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
err = path.EndpointB.RecvPacket(packet)
suite.Require().NoError(err)
}, true, false},
{"success: UNORDERED", func() {
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
err = path.EndpointB.RecvPacket(packet)
suite.Require().NoError(err)
}, true, false},
{"success: UNORDERED acknowledge out of order packet", func() {
// setup uses an UNORDERED channel
path.Setup()
// attempts to acknowledge ack with sequence 10 without acknowledging ack with sequence 1 (removing packet commitment)
for i := uint64(1); i < 10; i++ {
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
err = path.EndpointB.RecvPacket(packet)
suite.Require().NoError(err)
}
}, true, false},
{"failure: ORDERED acknowledge out of order packet", func() {
path.SetChannelOrdered()
path.Setup()
// attempts to acknowledge ack with sequence 10 without acknowledging ack with sequence 1 (removing packet commitment
for i := uint64(1); i < 10; i++ {
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
err = path.EndpointB.RecvPacket(packet)
suite.Require().NoError(err)
}
}, false, false},
{"channel does not exist", func() {
// any non-nil value of packet is valid
suite.Require().NotNil(packet)
}, false, false},
{"packet not received", func() {
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}, false, false},
{"successful no-op: ORDERED - packet already acknowledged (replay)", func() {
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
err = path.EndpointB.RecvPacket(packet)
suite.Require().NoError(err)
err = path.EndpointA.AcknowledgePacket(packet, ibctesting.MockAcknowledgement)
suite.Require().NoError(err)
}, true, true},
{"successful no-op: UNORDERED - packet already acknowledged (replay)", func() {
path.Setup()
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
err = path.EndpointB.RecvPacket(packet)
suite.Require().NoError(err)
err = path.EndpointA.AcknowledgePacket(packet, ibctesting.MockAcknowledgement)
suite.Require().NoError(err)
}, true, true},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest() // reset
path = ibctesting.NewPath(suite.chainA, suite.chainB)
tc.malleate()
var (
proof []byte
proofHeight clienttypes.Height
)
packetKey := host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
if path.EndpointB.ChannelID != "" {
proof, proofHeight = path.EndpointB.QueryProof(packetKey)
}
msg := channeltypes.NewMsgAcknowledgement(packet, ibcmock.MockAcknowledgement.Acknowledgement(), proof, proofHeight, suite.chainA.SenderAccount.GetAddress().String())
ctx := suite.chainA.GetContext()
_, err := suite.chainA.App.GetIBCKeeper().Acknowledgement(ctx, msg)
events := ctx.EventManager().Events()
if tc.expPass {
suite.Require().NoError(err)
// verify packet commitment was deleted on source chain
has := suite.chainA.App.GetIBCKeeper().ChannelKeeper.HasPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
suite.Require().False(has)
// replay should not error as it is treated as a no-op
_, err := suite.chainA.App.GetIBCKeeper().Acknowledgement(suite.chainA.GetContext(), msg)
suite.Require().NoError(err)
if tc.replay {
// context should not contain application events
suite.Require().NotContains(events, ibcmock.NewMockAckPacketEvent())
} else {
// context events should contain application events
suite.Require().Contains(events, ibcmock.NewMockAckPacketEvent())
}
} else {
suite.Require().Error(err)
}
})
}
}
// tests the IBC handler timing out a packet on ordered and unordered channels.
// It verifies that the deletion of a packet commitment occurs. It tests
// high level properties like ordering and basic sanity checks. More
// rigorous testing of 'TimeoutPacket' and 'TimeoutExecuted' can be found in
// the 04-channel/keeper/timeout_test.go.
func (suite *KeeperTestSuite) TestHandleTimeoutPacket() {
var (
packet channeltypes.Packet
packetKey []byte
path *ibctesting.Path
)
testCases := []struct {
name string
malleate func()
expPass bool
noop bool // indicate no-op
}{
{"success: ORDERED", func() {
path.SetChannelOrdered()
path.Setup()
timeoutHeight := clienttypes.GetSelfHeight(suite.chainB.GetContext())
timeoutTimestamp := uint64(suite.chainB.GetContext().BlockTime().UnixNano())
// create packet commitment
sequence, err := path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
// need to update chainA client to prove missing ack
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, timeoutTimestamp)
packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel())
}, true, false},
{"success: UNORDERED", func() {
path.Setup()
timeoutHeight := clienttypes.GetSelfHeight(suite.chainB.GetContext())
timeoutTimestamp := uint64(suite.chainB.GetContext().BlockTime().UnixNano())
// create packet commitment
sequence, err := path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
// need to update chainA client to prove missing ack
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, timeoutTimestamp)
packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
}, true, false},
{"success: UNORDERED timeout out of order packet", func() {
// setup uses an UNORDERED channel
path.Setup()
// attempts to timeout the last packet sent without timing out the first packet
// packet sequences begin at 1
for i := uint64(1); i < maxSequence; i++ {
timeoutHeight := clienttypes.GetSelfHeight(suite.chainB.GetContext())
// create packet commitment
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
}, true, false},
{"success: ORDERED timeout out of order packet", func() {
path.SetChannelOrdered()
path.Setup()
// attempts to timeout the last packet sent without timing out the first packet
// packet sequences begin at 1
for i := uint64(1); i < maxSequence; i++ {
timeoutHeight := clienttypes.GetSelfHeight(suite.chainB.GetContext())
// create packet commitment
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel())
}, true, false},
{"channel does not exist", func() {
// any non-nil value of packet is valid
suite.Require().NotNil(packet)
packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel())
}, false, false},
{"successful no-op: UNORDERED - packet not sent", func() {
path.Setup()
packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 1), 0)
packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
}, true, true},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest() // reset
path = ibctesting.NewPath(suite.chainA, suite.chainB)
tc.malleate()
var (
proof []byte
proofHeight clienttypes.Height
)
if path.EndpointB.ChannelID != "" {
proof, proofHeight = path.EndpointB.QueryProof(packetKey)
}
msg := channeltypes.NewMsgTimeout(packet, 1, proof, proofHeight, suite.chainA.SenderAccount.GetAddress().String())
ctx := suite.chainA.GetContext()
_, err := suite.chainA.App.GetIBCKeeper().Timeout(ctx, msg)
events := ctx.EventManager().Events()
if tc.expPass {
suite.Require().NoError(err)
// replay should not return an error as it is treated as a no-op
_, err := suite.chainA.App.GetIBCKeeper().Timeout(suite.chainA.GetContext(), msg)
suite.Require().NoError(err)
// verify packet commitment was deleted on source chain
has := suite.chainA.App.GetIBCKeeper().ChannelKeeper.HasPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
suite.Require().False(has)
if tc.noop {
// context should not contain application events
suite.Require().NotContains(events, ibcmock.NewMockTimeoutPacketEvent())
} else {
// context should contain application events
suite.Require().Contains(events, ibcmock.NewMockTimeoutPacketEvent())
}
} else {
suite.Require().Error(err)
}
})
}
}
// tests the IBC handler timing out a packet via channel closure on ordered
// and unordered channels. It verifies that the deletion of a packet
// commitment occurs. It tests high level properties like ordering and basic
// sanity checks. More rigorous testing of 'TimeoutOnClose' and
// 'TimeoutExecuted' can be found in the 04-channel/keeper/timeout_test.go.
func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() {
var (
packet channeltypes.Packet
packetKey []byte
path *ibctesting.Path
counterpartyUpgradeSequence uint64
)
testCases := []struct {
name string
malleate func()
expPass bool
}{
{"success: ORDERED", func() {
path.SetChannelOrdered()
path.Setup()
// create packet commitment
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
// need to update chainA client to prove missing ack
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel())
// close counterparty channel
path.EndpointB.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })
}, true},
{"success: UNORDERED", func() {
path.Setup()
// create packet commitment
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
// need to update chainA client to prove missing ack
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
// close counterparty channel
path.EndpointB.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })
}, true},
{"success: UNORDERED timeout out of order packet", func() {
// setup uses an UNORDERED channel
path.Setup()
// attempts to timeout the last packet sent without timing out the first packet
// packet sequences begin at 1
for i := uint64(1); i < maxSequence; i++ {
// create packet commitment
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
// close counterparty channel
path.EndpointB.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })
}, true},
{"success: ORDERED timeout out of order packet", func() {
path.SetChannelOrdered()
path.Setup()
// attempts to timeout the last packet sent without timing out the first packet
// packet sequences begin at 1
for i := uint64(1); i < maxSequence; i++ {
// create packet commitment
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
}
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel())
// close counterparty channel
path.EndpointB.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })
}, true},
{"channel does not exist", func() {
// any non-nil value of packet is valid
suite.Require().NotNil(packet)
packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel())
}, false},
{"successful no-op: UNORDERED - packet not sent", func() {
path.Setup()
packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 1), 0)
packetKey = host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
// close counterparty channel
path.EndpointB.UpdateChannel(func(channel *channeltypes.Channel) { channel.State = channeltypes.CLOSED })
}, true},
{"ORDERED: channel not closed", func() {
path.SetChannelOrdered()
path.Setup()
// create packet commitment
sequence, err := path.EndpointA.SendPacket(timeoutHeight, 0, ibctesting.MockPacketData)
suite.Require().NoError(err)
// need to update chainA client to prove missing ack
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
packet = channeltypes.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel())
}, false},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest() // reset
path = ibctesting.NewPath(suite.chainA, suite.chainB)
tc.malleate()
proof, proofHeight := suite.chainB.QueryProof(packetKey)
channelKey := host.ChannelKey(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
closedProof, _ := suite.chainB.QueryProof(channelKey)
msg := channeltypes.NewMsgTimeoutOnClose(packet, 1, proof, closedProof, proofHeight, suite.chainA.SenderAccount.GetAddress().String(), counterpartyUpgradeSequence)
_, err := suite.chainA.App.GetIBCKeeper().TimeoutOnClose(suite.chainA.GetContext(), msg)
if tc.expPass {
suite.Require().NoError(err)
// replay should not return an error as it will be treated as a no-op
_, err := suite.chainA.App.GetIBCKeeper().TimeoutOnClose(suite.chainA.GetContext(), msg)
suite.Require().NoError(err)
// verify packet commitment was deleted on source chain
has := suite.chainA.App.GetIBCKeeper().ChannelKeeper.HasPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
suite.Require().False(has)
} else {
suite.Require().Error(err)
}
})
}
}
func (suite *KeeperTestSuite) TestUpgradeClient() {
var (
path *ibctesting.Path
newChainID string
newClientHeight clienttypes.Height
upgradedClient *ibctm.ClientState
upgradedConsState exported.ConsensusState
lastHeight exported.Height
msg *clienttypes.MsgUpgradeClient
)
cases := []struct {
name string
setup func()
expPass bool
}{
{
name: "successful upgrade",
setup: func() {
upgradedClient = ibctm.NewClientState(newChainID, ibctm.DefaultTrustLevel, ibctesting.TrustingPeriod, ibctesting.UnbondingPeriod+ibctesting.TrustingPeriod, ibctesting.MaxClockDrift, newClientHeight, commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath)
// Call ZeroCustomFields on upgraded clients to clear any client-chosen parameters in test-case upgradedClient
upgradedClient = upgradedClient.ZeroCustomFields()
upgradedConsState = &ibctm.ConsensusState{
NextValidatorsHash: []byte("nextValsHash"),
}
// last Height is at next block
lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1))
upgradedClientBz, err := clienttypes.MarshalClientState(suite.chainA.App.AppCodec(), upgradedClient)
suite.Require().NoError(err)
upgradedConsStateBz, err := clienttypes.MarshalConsensusState(suite.chainA.App.AppCodec(), upgradedConsState)
suite.Require().NoError(err)
// zero custom fields and store in upgrade store
suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) //nolint:errcheck // ignore error for testing
suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) //nolint:errcheck // ignore error for testing
// commit upgrade store changes and update clients
suite.coordinator.CommitBlock(suite.chainB)
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
latestHeight := path.EndpointA.GetClientLatestHeight()
upgradeClientProof, _ := suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), latestHeight.GetRevisionHeight())
upgradedConsensusStateProof, _ := suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedConsStateKey(int64(lastHeight.GetRevisionHeight())), latestHeight.GetRevisionHeight())
msg, err = clienttypes.NewMsgUpgradeClient(path.EndpointA.ClientID, upgradedClient, upgradedConsState,
upgradeClientProof, upgradedConsensusStateProof, suite.chainA.SenderAccount.GetAddress().String())
suite.Require().NoError(err)
},
expPass: true,
},
{
name: "VerifyUpgrade fails",
setup: func() {
upgradedClient = ibctm.NewClientState(newChainID, ibctm.DefaultTrustLevel, ibctesting.TrustingPeriod, ibctesting.UnbondingPeriod+ibctesting.TrustingPeriod, ibctesting.MaxClockDrift, newClientHeight, commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath)
// Call ZeroCustomFields on upgraded clients to clear any client-chosen parameters in test-case upgradedClient
upgradedClient = upgradedClient.ZeroCustomFields()
upgradedConsState = &ibctm.ConsensusState{
NextValidatorsHash: []byte("nextValsHash"),
}
// last Height is at next block
lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1))
upgradedClientBz, err := clienttypes.MarshalClientState(suite.chainA.App.AppCodec(), upgradedClient)
suite.Require().NoError(err)
upgradedConsStateBz, err := clienttypes.MarshalConsensusState(suite.chainA.App.AppCodec(), upgradedConsState)
suite.Require().NoError(err)
// zero custom fields and store in upgrade store
suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) //nolint:errcheck // ignore error for testing
suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) //nolint:errcheck // ignore error for testing
// commit upgrade store changes and update clients
suite.coordinator.CommitBlock(suite.chainB)
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
msg, err = clienttypes.NewMsgUpgradeClient(path.EndpointA.ClientID, upgradedClient, upgradedConsState, nil, nil, suite.chainA.SenderAccount.GetAddress().String())
suite.Require().NoError(err)
},
expPass: false,
},
}
for _, tc := range cases {
tc := tc
path = ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupClients()
var err error
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
revisionNumber := clienttypes.ParseChainID(clientState.ChainId)
newChainID, err = clienttypes.SetRevisionNumber(clientState.ChainId, revisionNumber+1)
suite.Require().NoError(err)
newClientHeight = clienttypes.NewHeight(revisionNumber+1, clientState.LatestHeight.GetRevisionHeight()+1)
tc.setup()
ctx := suite.chainA.GetContext()
_, err = suite.chainA.GetSimApp().GetIBCKeeper().UpgradeClient(ctx, msg)
if tc.expPass {
suite.Require().NoError(err, "upgrade handler failed on valid case: %s", tc.name)
newClient, ok := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID)
suite.Require().True(ok)
newChainSpecifiedClient := newClient.(*ibctm.ClientState).ZeroCustomFields()
suite.Require().Equal(upgradedClient, newChainSpecifiedClient)
expectedEvents := sdk.Events{
sdk.NewEvent(
clienttypes.EventTypeUpgradeClient,
sdk.NewAttribute(clienttypes.AttributeKeyClientID, ibctesting.FirstClientID),
sdk.NewAttribute(clienttypes.AttributeKeyClientType, path.EndpointA.GetClientState().ClientType()),
sdk.NewAttribute(clienttypes.AttributeKeyConsensusHeight, path.EndpointA.GetClientLatestHeight().String()),
),
}.ToABCIEvents()
expectedEvents = sdk.MarkEventsToIndex(expectedEvents, map[string]struct{}{})
ibctesting.AssertEvents(&suite.Suite, expectedEvents, ctx.EventManager().Events().ToABCIEvents())
} else {
suite.Require().Error(err, "upgrade handler passed on invalid case: %s", tc.name)
}
}
}
func (suite *KeeperTestSuite) TestChannelUpgradeInit() {
var (
path *ibctesting.Path
msg *channeltypes.MsgChannelUpgradeInit
)
cases := []struct {
name string
malleate func()
expResult func(res *channeltypes.MsgChannelUpgradeInitResponse, events []abci.Event, err error)
}{
{
"success",
func() {
msg = channeltypes.NewMsgChannelUpgradeInit(
path.EndpointA.ChannelConfig.PortID,
path.EndpointA.ChannelID,
path.EndpointA.GetProposedUpgrade().Fields,
path.EndpointA.Chain.GetSimApp().IBCKeeper.GetAuthority(),
)
},
func(res *channeltypes.MsgChannelUpgradeInitResponse, events []abci.Event, err error) {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(uint64(1), res.UpgradeSequence)
channel := path.EndpointA.GetChannel()
expEvents := sdk.Events{
sdk.NewEvent(
channeltypes.EventTypeChannelUpgradeInit,
sdk.NewAttribute(channeltypes.AttributeKeyPortID, path.EndpointA.ChannelConfig.PortID),
sdk.NewAttribute(channeltypes.AttributeKeyChannelID, path.EndpointA.ChannelID),
sdk.NewAttribute(channeltypes.AttributeCounterpartyPortID, path.EndpointB.ChannelConfig.PortID),
sdk.NewAttribute(channeltypes.AttributeCounterpartyChannelID, path.EndpointB.ChannelID),
sdk.NewAttribute(channeltypes.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", channel.UpgradeSequence)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, channeltypes.AttributeValueCategory),
),
}.ToABCIEvents()
ibctesting.AssertEvents(&suite.Suite, expEvents, events)
},
},
{
"authority is not signer of the upgrade init msg",
func() {
msg = channeltypes.NewMsgChannelUpgradeInit(
path.EndpointA.ChannelConfig.PortID,
path.EndpointA.ChannelID,
path.EndpointA.GetProposedUpgrade().Fields,
path.EndpointA.Chain.SenderAccount.String(),
)
},
func(res *channeltypes.MsgChannelUpgradeInitResponse, events []abci.Event, err error) {
suite.Require().Error(err)
suite.Require().ErrorContains(err, ibcerrors.ErrUnauthorized.Error())
suite.Require().Nil(res)
suite.Require().Empty(events)
},
},
{
"ibc application does not implement the UpgradeableModule interface",
func() {
path = ibctesting.NewPath(suite.chainA, suite.chainB)
path.EndpointA.ChannelConfig.PortID = ibcmock.MockBlockUpgrade
path.EndpointB.ChannelConfig.PortID = ibcmock.MockBlockUpgrade
path.Setup()
msg = channeltypes.NewMsgChannelUpgradeInit(
path.EndpointA.ChannelConfig.PortID,
path.EndpointA.ChannelID,
path.EndpointA.GetProposedUpgrade().Fields,
path.EndpointA.Chain.GetSimApp().IBCKeeper.GetAuthority(),
)
},
func(res *channeltypes.MsgChannelUpgradeInitResponse, events []abci.Event, err error) {
suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute)
suite.Require().Nil(res)
suite.Require().Empty(events)
},
},
{
"ibc application does not commit state changes in callback",
func() {
msg = channeltypes.NewMsgChannelUpgradeInit(
path.EndpointA.ChannelConfig.PortID,
path.EndpointA.ChannelID,
path.EndpointA.GetProposedUpgrade().Fields,
path.EndpointA.Chain.GetSimApp().IBCKeeper.GetAuthority(),
)