-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathupdate_test.go
More file actions
833 lines (694 loc) · 32.5 KB
/
update_test.go
File metadata and controls
833 lines (694 loc) · 32.5 KB
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
package tendermint_test
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
tmtypes "github.com/tendermint/tendermint/types"
clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v6/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v6/modules/core/24-host"
"github.com/cosmos/ibc-go/v6/modules/core/exported"
ibctm "github.com/cosmos/ibc-go/v6/modules/light-clients/07-tendermint"
ibctesting "github.com/cosmos/ibc-go/v6/testing"
ibctestingmock "github.com/cosmos/ibc-go/v6/testing/mock"
)
func (suite *TendermintTestSuite) TestVerifyHeader() {
var (
path *ibctesting.Path
header *ibctm.Header
)
// Setup different validators and signers for testing different types of updates
altPrivVal := ibctestingmock.NewPV()
altPubKey, err := altPrivVal.GetPubKey()
suite.Require().NoError(err)
revisionHeight := int64(height.RevisionHeight)
// create modified heights to use for test-cases
altVal := tmtypes.NewValidator(altPubKey, 100)
// Create alternative validator set with only altVal, invalid update (too much change in valSet)
altValSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{altVal})
altSigners := getAltSigners(altVal, altPrivVal)
testCases := []struct {
name string
malleate func()
expPass bool
}{
{
name: "success",
malleate: func() {},
expPass: true,
},
{
name: "successful verify header for header with a previous height",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
// passing the CurrentHeader.Height as the block height as it will become a previous height once we commit N blocks
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
// commit some blocks so that the created Header now has a previous height as the BlockHeight
suite.coordinator.CommitNBlocks(suite.chainB, 5)
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
},
expPass: true,
},
{
name: "successful verify header: header with future height and different validator set",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
// Create bothValSet with both suite validator and altVal
bothValSet := tmtypes.NewValidatorSet(append(suite.chainB.Vals.Validators, altVal))
bothSigners := suite.chainB.Signers
bothSigners[altVal.Address.String()] = altPrivVal
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height+5, trustedHeight, suite.chainB.CurrentHeader.Time, bothValSet, suite.chainB.NextVals, trustedVals, bothSigners)
},
expPass: true,
},
{
name: "successful verify header: header with next height and different validator set",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
// Create bothValSet with both suite validator and altVal
bothValSet := tmtypes.NewValidatorSet(append(suite.chainB.Vals.Validators, altVal))
bothSigners := suite.chainB.Signers
bothSigners[altVal.Address.String()] = altPrivVal
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height, trustedHeight, suite.chainB.CurrentHeader.Time, bothValSet, suite.chainB.NextVals, trustedVals, bothSigners)
},
expPass: true,
},
{
name: "unsuccessful updates, passed in incorrect trusted validators for given consensus state",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
// Create bothValSet with both suite validator and altVal
bothValSet := tmtypes.NewValidatorSet(append(suite.chainB.Vals.Validators, altVal))
bothSigners := suite.chainB.Signers
bothSigners[altVal.Address.String()] = altPrivVal
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height+1, trustedHeight, suite.chainB.CurrentHeader.Time, bothValSet, bothValSet, bothValSet, bothSigners)
},
expPass: false,
},
{
name: "unsuccessful verify header with next height: update header mismatches nextValSetHash",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
// this will err as altValSet.Hash() != consState.NextValidatorsHash
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height+1, trustedHeight, suite.chainB.CurrentHeader.Time, altValSet, altValSet, trustedVals, altSigners)
},
expPass: false,
},
{
name: "unsuccessful update with future height: too much change in validator set",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height+1, trustedHeight, suite.chainB.CurrentHeader.Time, altValSet, altValSet, trustedVals, altSigners)
},
expPass: false,
},
{
name: "unsuccessful verify header: header height revision and trusted height revision mismatch",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
header = suite.chainB.CreateTMClientHeader(chainIDRevision1, 3, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
},
expPass: false,
},
{
name: "unsuccessful verify header: header height < consensus height",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
heightMinus1 := clienttypes.NewHeight(trustedHeight.RevisionNumber, trustedHeight.RevisionHeight-1)
// Make new header at height less than latest client state
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(heightMinus1.RevisionHeight), trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
},
expPass: false,
},
{
name: "unsuccessful verify header: header basic validation failed",
malleate: func() {
// cause header to fail validatebasic by changing commit height to mismatch header height
header.SignedHeader.Commit.Height = revisionHeight - 1
},
expPass: false,
},
{
name: "unsuccessful verify header: header timestamp is not past last client timestamp",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight))
suite.Require().True(found)
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height+1, trustedHeight, suite.chainB.CurrentHeader.Time.Add(-time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
},
expPass: false,
},
{
name: "unsuccessful verify header: header with incorrect header chain-id",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight))
suite.Require().True(found)
header = suite.chainB.CreateTMClientHeader(chainID, suite.chainB.CurrentHeader.Height+1, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
},
expPass: false,
},
{
name: "unsuccessful update: trusting period has passed since last client timestamp",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight))
suite.Require().True(found)
header = suite.chainA.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height+1, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
suite.chainB.ExpireClient(ibctesting.TrustingPeriod)
},
expPass: false,
},
{
name: "unsuccessful update for a previous revision",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
// passing the CurrentHeader.Height as the block height as it will become an update to previous revision once we upgrade the client
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
// increment the revision of the chain
err = path.EndpointB.UpgradeChain()
suite.Require().NoError(err)
},
expPass: false,
},
{
name: "successful update with identical header to a previous update",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
// passing the CurrentHeader.Height as the block height as it will become a previous height once we commit N blocks
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
// update client so the header constructed becomes a duplicate
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
},
expPass: true,
},
{
name: "unsuccessful update to a future revision",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID+"-1", suite.chainB.CurrentHeader.Height+5, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
},
expPass: false,
},
{
name: "unsuccessful update: header height revision and trusted height revision mismatch",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
// increment the revision of the chain
err = path.EndpointB.UpgradeChain()
suite.Require().NoError(err)
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
},
expPass: false,
},
}
for _, tc := range testCases {
tc := tc
suite.SetupTest()
path = ibctesting.NewPath(suite.chainA, suite.chainB)
err := path.EndpointA.CreateClient()
suite.Require().NoError(err)
// ensure counterparty state is committed
suite.coordinator.CommitBlock(suite.chainB)
header, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
suite.Require().NoError(err)
tc.malleate()
clientState := path.EndpointA.GetClientState()
clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID)
err = clientState.VerifyClientMessage(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore, header)
if tc.expPass {
suite.Require().NoError(err, tc.name)
} else {
suite.Require().Error(err)
}
}
}
func (suite *TendermintTestSuite) TestUpdateState() {
var (
path *ibctesting.Path
clientMessage exported.ClientMessage
clientStore sdk.KVStore
consensusHeights []exported.Height
pruneHeight clienttypes.Height
prevClientState exported.ClientState
prevConsensusState exported.ConsensusState
)
testCases := []struct {
name string
malleate func()
expResult func()
expPass bool
}{
{
"success with height later than latest height", func() {
tmHeader, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
suite.Require().True(path.EndpointA.GetClientState().GetLatestHeight().LT(tmHeader.GetHeight()))
},
func() {
tmHeader, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
clientState := path.EndpointA.GetClientState()
suite.Require().True(clientState.GetLatestHeight().EQ(tmHeader.GetHeight())) // new update, updated client state should have changed
suite.Require().True(clientState.GetLatestHeight().EQ(consensusHeights[0]))
}, true,
},
{
"success with height earlier than latest height", func() {
// commit a block so the pre-created ClientMessage
// isn't used to update the client to a newer height
suite.coordinator.CommitBlock(suite.chainB)
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
tmHeader, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
suite.Require().True(path.EndpointA.GetClientState().GetLatestHeight().GT(tmHeader.GetHeight()))
prevClientState = path.EndpointA.GetClientState()
},
func() {
clientState := path.EndpointA.GetClientState()
suite.Require().Equal(clientState, prevClientState) // fill in height, no change to client state
suite.Require().True(clientState.GetLatestHeight().GT(consensusHeights[0]))
}, true,
},
{
"success with duplicate header", func() {
// update client in advance
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
// use the same header which just updated the client
clientMessage, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
suite.Require().NoError(err)
tmHeader, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
suite.Require().Equal(path.EndpointA.GetClientState().GetLatestHeight(), tmHeader.GetHeight())
prevClientState = path.EndpointA.GetClientState()
prevConsensusState = path.EndpointA.GetConsensusState(tmHeader.GetHeight())
},
func() {
clientState := path.EndpointA.GetClientState()
suite.Require().Equal(clientState, prevClientState)
suite.Require().True(clientState.GetLatestHeight().EQ(consensusHeights[0]))
tmHeader, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
suite.Require().Equal(path.EndpointA.GetConsensusState(tmHeader.GetHeight()), prevConsensusState)
}, true,
},
{
"success with pruned consensus state", func() {
// this height will be expired and pruned
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
pruneHeight = path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
// Increment the time by a week
suite.coordinator.IncrementTimeBy(7 * 24 * time.Hour)
// create the consensus state that can be used as trusted height for next update
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
// Increment the time by another week, then update the client.
// This will cause the first two consensus states to become expired.
suite.coordinator.IncrementTimeBy(7 * 24 * time.Hour)
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
// ensure counterparty state is committed
suite.coordinator.CommitBlock(suite.chainB)
clientMessage, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
suite.Require().NoError(err)
},
func() {
tmHeader, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
clientState := path.EndpointA.GetClientState()
suite.Require().True(clientState.GetLatestHeight().EQ(tmHeader.GetHeight())) // new update, updated client state should have changed
suite.Require().True(clientState.GetLatestHeight().EQ(consensusHeights[0]))
// ensure consensus state was pruned
_, found := path.EndpointA.Chain.GetConsensusState(path.EndpointA.ClientID, pruneHeight)
suite.Require().False(found)
}, true,
},
{
"success with pruned consensus state using duplicate header", func() {
// this height will be expired and pruned
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
pruneHeight = path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
// assert that a consensus state exists at the prune height
consensusState, found := path.EndpointA.Chain.GetConsensusState(path.EndpointA.ClientID, pruneHeight)
suite.Require().True(found)
suite.Require().NotNil(consensusState)
// Increment the time by a week
suite.coordinator.IncrementTimeBy(7 * 24 * time.Hour)
// create the consensus state that can be used as trusted height for next update
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
// Increment the time by another week, then update the client.
// This will cause the first two consensus states to become expired.
suite.coordinator.IncrementTimeBy(7 * 24 * time.Hour)
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
// use the same header which just updated the client
clientMessage, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
suite.Require().NoError(err)
},
func() {
tmHeader, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
clientState := path.EndpointA.GetClientState()
suite.Require().True(clientState.GetLatestHeight().EQ(tmHeader.GetHeight())) // new update, updated client state should have changed
suite.Require().True(clientState.GetLatestHeight().EQ(consensusHeights[0]))
// ensure consensus state was pruned
_, found := path.EndpointA.Chain.GetConsensusState(path.EndpointA.ClientID, pruneHeight)
suite.Require().False(found)
}, true,
},
{
"invalid ClientMessage type", func() {
clientMessage = &ibctm.Misbehaviour{}
},
func() {},
false,
},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest() // reset
pruneHeight = clienttypes.ZeroHeight()
path = ibctesting.NewPath(suite.chainA, suite.chainB)
err := path.EndpointA.CreateClient()
suite.Require().NoError(err)
// ensure counterparty state is committed
suite.coordinator.CommitBlock(suite.chainB)
clientMessage, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
suite.Require().NoError(err)
tc.malleate()
clientState := path.EndpointA.GetClientState()
clientStore = suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID)
if tc.expPass {
consensusHeights = clientState.UpdateState(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore, clientMessage)
header := clientMessage.(*ibctm.Header)
expConsensusState := &ibctm.ConsensusState{
Timestamp: header.GetTime(),
Root: commitmenttypes.NewMerkleRoot(header.Header.GetAppHash()),
NextValidatorsHash: header.Header.NextValidatorsHash,
}
bz := clientStore.Get(host.ConsensusStateKey(header.GetHeight()))
updatedConsensusState := clienttypes.MustUnmarshalConsensusState(suite.chainA.App.AppCodec(), bz)
suite.Require().Equal(expConsensusState, updatedConsensusState)
} else {
suite.Require().Panics(func() {
clientState.UpdateState(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore, clientMessage)
})
}
// perform custom checks
tc.expResult()
})
}
}
func (suite *TendermintTestSuite) TestPruneConsensusState() {
// create path and setup clients
path := ibctesting.NewPath(suite.chainA, suite.chainB)
suite.coordinator.SetupClients(path)
// get the first height as it will be pruned first.
var pruneHeight exported.Height
getFirstHeightCb := func(height exported.Height) bool {
pruneHeight = height
return true
}
ctx := path.EndpointA.Chain.GetContext()
clientStore := path.EndpointA.Chain.App.GetIBCKeeper().ClientKeeper.ClientStore(ctx, path.EndpointA.ClientID)
ibctm.IterateConsensusStateAscending(clientStore, getFirstHeightCb)
// this height will be expired but not pruned
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
expiredHeight := path.EndpointA.GetClientState().GetLatestHeight()
// expected values that must still remain in store after pruning
expectedConsState, ok := path.EndpointA.Chain.GetConsensusState(path.EndpointA.ClientID, expiredHeight)
suite.Require().True(ok)
ctx = path.EndpointA.Chain.GetContext()
clientStore = path.EndpointA.Chain.App.GetIBCKeeper().ClientKeeper.ClientStore(ctx, path.EndpointA.ClientID)
expectedProcessTime, ok := ibctm.GetProcessedTime(clientStore, expiredHeight)
suite.Require().True(ok)
expectedProcessHeight, ok := ibctm.GetProcessedHeight(clientStore, expiredHeight)
suite.Require().True(ok)
expectedConsKey := ibctm.GetIterationKey(clientStore, expiredHeight)
suite.Require().NotNil(expectedConsKey)
// Increment the time by a week
suite.coordinator.IncrementTimeBy(7 * 24 * time.Hour)
// create the consensus state that can be used as trusted height for next update
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
// Increment the time by another week, then update the client.
// This will cause the first two consensus states to become expired.
suite.coordinator.IncrementTimeBy(7 * 24 * time.Hour)
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
ctx = path.EndpointA.Chain.GetContext()
clientStore = path.EndpointA.Chain.App.GetIBCKeeper().ClientKeeper.ClientStore(ctx, path.EndpointA.ClientID)
// check that the first expired consensus state got deleted along with all associated metadata
consState, ok := path.EndpointA.Chain.GetConsensusState(path.EndpointA.ClientID, pruneHeight)
suite.Require().Nil(consState, "expired consensus state not pruned")
suite.Require().False(ok)
// check processed time metadata is pruned
processTime, ok := ibctm.GetProcessedTime(clientStore, pruneHeight)
suite.Require().Equal(uint64(0), processTime, "processed time metadata not pruned")
suite.Require().False(ok)
processHeight, ok := ibctm.GetProcessedHeight(clientStore, pruneHeight)
suite.Require().Nil(processHeight, "processed height metadata not pruned")
suite.Require().False(ok)
// check iteration key metadata is pruned
consKey := ibctm.GetIterationKey(clientStore, pruneHeight)
suite.Require().Nil(consKey, "iteration key not pruned")
// check that second expired consensus state doesn't get deleted
// this ensures that there is a cap on gas cost of UpdateClient
consState, ok = path.EndpointA.Chain.GetConsensusState(path.EndpointA.ClientID, expiredHeight)
suite.Require().Equal(expectedConsState, consState, "consensus state incorrectly pruned")
suite.Require().True(ok)
// check processed time metadata is not pruned
processTime, ok = ibctm.GetProcessedTime(clientStore, expiredHeight)
suite.Require().Equal(expectedProcessTime, processTime, "processed time metadata incorrectly pruned")
suite.Require().True(ok)
// check processed height metadata is not pruned
processHeight, ok = ibctm.GetProcessedHeight(clientStore, expiredHeight)
suite.Require().Equal(expectedProcessHeight, processHeight, "processed height metadata incorrectly pruned")
suite.Require().True(ok)
// check iteration key metadata is not pruned
consKey = ibctm.GetIterationKey(clientStore, expiredHeight)
suite.Require().Equal(expectedConsKey, consKey, "iteration key incorrectly pruned")
}
func (suite *TendermintTestSuite) TestCheckForMisbehaviour() {
var (
path *ibctesting.Path
clientMessage exported.ClientMessage
)
testCases := []struct {
name string
malleate func()
expPass bool
}{
{
"valid update no misbehaviour",
func() {},
false,
},
{
"consensus state already exists, already updated",
func() {
header, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
consensusState := &ibctm.ConsensusState{
Timestamp: header.GetTime(),
Root: commitmenttypes.NewMerkleRoot(header.Header.GetAppHash()),
NextValidatorsHash: header.Header.NextValidatorsHash,
}
tmHeader, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(suite.chainA.GetContext(), path.EndpointA.ClientID, tmHeader.GetHeight(), consensusState)
},
false,
},
{
"invalid fork misbehaviour: identical headers", func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
height := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
misbehaviourHeader := suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.CurrentHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
clientMessage = &ibctm.Misbehaviour{
Header1: misbehaviourHeader,
Header2: misbehaviourHeader,
}
}, false,
},
{
"invalid time misbehaviour: monotonically increasing time", func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
clientMessage = &ibctm.Misbehaviour{
Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height+3, trustedHeight, suite.chainB.CurrentHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers),
Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers),
}
}, false,
},
{
"consensus state already exists, app hash mismatch",
func() {
header, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
consensusState := &ibctm.ConsensusState{
Timestamp: header.GetTime(),
Root: commitmenttypes.NewMerkleRoot([]byte{}), // empty bytes
NextValidatorsHash: header.Header.NextValidatorsHash,
}
tmHeader, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(suite.chainA.GetContext(), path.EndpointA.ClientID, tmHeader.GetHeight(), consensusState)
},
true,
},
{
"previous consensus state exists and header time is before previous consensus state time",
func() {
header, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
// offset header timestamp before previous consensus state timestamp
header.Header.Time = header.GetTime().Add(-time.Hour)
},
true,
},
{
"next consensus state exists and header time is after next consensus state time",
func() {
header, ok := clientMessage.(*ibctm.Header)
suite.Require().True(ok)
// commit block and update client, adding a new consensus state
suite.coordinator.CommitBlock(suite.chainB)
err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)
// increase timestamp of current header
header.Header.Time = header.Header.Time.Add(time.Hour)
},
true,
},
{
"valid fork misbehaviour returns true",
func() {
header1, err := path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
suite.Require().NoError(err)
// commit block and update client
suite.coordinator.CommitBlock(suite.chainB)
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
header2, err := path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
suite.Require().NoError(err)
// assign the same height, each header will have a different commit hash
header1.Header.Height = header2.Header.Height
clientMessage = &ibctm.Misbehaviour{
Header1: header1,
Header2: header2,
ClientId: path.EndpointA.ClientID,
}
},
true,
},
{
"valid time misbehaviour: not monotonically increasing time", func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)
clientMessage = &ibctm.Misbehaviour{
Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height+3, trustedHeight, suite.chainB.CurrentHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers),
Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers),
}
}, true,
},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
// reset suite to create fresh application state
suite.SetupTest()
path = ibctesting.NewPath(suite.chainA, suite.chainB)
err := path.EndpointA.CreateClient()
suite.Require().NoError(err)
// ensure counterparty state is committed
suite.coordinator.CommitBlock(suite.chainB)
clientMessage, err = path.EndpointA.Chain.ConstructUpdateTMClientHeader(path.EndpointA.Counterparty.Chain, path.EndpointA.ClientID)
suite.Require().NoError(err)
tc.malleate()
clientState := path.EndpointA.GetClientState()
clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID)
foundMisbehaviour := clientState.CheckForMisbehaviour(
suite.chainA.GetContext(),
suite.chainA.App.AppCodec(),
clientStore, // pass in clientID prefixed clientStore
clientMessage,
)
if tc.expPass {
suite.Require().True(foundMisbehaviour)
} else {
suite.Require().False(foundMisbehaviour)
}
})
}
}
func (suite *TendermintTestSuite) TestUpdateStateOnMisbehaviour() {
var path *ibctesting.Path
testCases := []struct {
name string
malleate func()
expPass bool
}{
{
"success",
func() {},
true,
},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
// reset suite to create fresh application state
suite.SetupTest()
path = ibctesting.NewPath(suite.chainA, suite.chainB)
err := path.EndpointA.CreateClient()
suite.Require().NoError(err)
tc.malleate()
clientState := path.EndpointA.GetClientState()
clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID)
clientState.UpdateStateOnMisbehaviour(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore, nil)
if tc.expPass {
clientStateBz := clientStore.Get(host.ClientStateKey())
suite.Require().NotEmpty(clientStateBz)
newClientState := clienttypes.MustUnmarshalClientState(suite.chainA.Codec, clientStateBz)
suite.Require().Equal(frozenHeight, newClientState.(*ibctm.ClientState).FrozenHeight)
}
})
}
}