forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisbehaviour.go
595 lines (548 loc) · 19.1 KB
/
misbehaviour.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
package integration
import (
"time"
ibctmtypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
tmtypes "github.com/cometbft/cometbft/types"
testutil "github.com/cosmos/interchain-security/v6/testutil/crypto"
"github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
)
// TestHandleConsumerMisbehaviour tests the handling of consumer misbehavior.
// @Long Description@
// * Set up a CCV channel and send an empty VSC packet to ensure that the consumer client revision height is greater than 0.
// * Construct a Misbehaviour object with two conflicting headers and process the equivocation evidence.
// * Verify that the provider chain correctly processes this misbehavior.
// * Ensure that all involved validators are jailed, tombstoned, and slashed according to the expected outcomes.
// * Assert that their tokens are adjusted based on the slashing fraction.
func (s *CCVTestSuite) TestHandleConsumerMisbehaviour() {
s.SetupCCVChannel(s.path)
// required to have the consumer client revision height greater than 0
s.SendEmptyVSCPacket()
for _, v := range s.providerChain.Vals.Validators {
s.setDefaultValSigningInfo(*v)
}
altTime := s.providerCtx().BlockTime().Add(time.Minute)
clientHeight := s.consumerChain.LastHeader.TrustedHeight
clientTMValset := tmtypes.NewValidatorSet(s.consumerChain.Vals.Validators)
clientSigners := s.consumerChain.Signers
misb := &ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime,
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
),
// create a different header by changing the header timestamp only
// in order to create an equivocation, i.e. both headers have the same deterministic states
Header2: s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime.Add(10*time.Second),
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
),
}
// we assume that all validators have the same number of initial tokens
validator, _ := s.getValByIdx(0)
initialTokens := math.LegacyNewDecFromInt(validator.GetTokens())
err := s.providerApp.GetProviderKeeper().HandleConsumerMisbehaviour(s.providerCtx(), s.getFirstBundle().ConsumerId, *misb)
s.NoError(err)
// verify that validators are jailed, tombstoned, and slashed
for _, v := range clientTMValset.Validators {
consuAddr := sdk.ConsAddress(v.Address.Bytes())
provAddr := s.providerApp.GetProviderKeeper().GetProviderAddrFromConsumerAddr(s.providerCtx(), s.getFirstBundle().ConsumerId, types.NewConsumerConsAddress(consuAddr))
val, err := s.providerApp.GetTestStakingKeeper().GetValidatorByConsAddr(s.providerCtx(), provAddr.Address)
s.Require().NoError(err)
s.Require().True(val.Jailed)
s.Require().True(s.providerApp.GetTestSlashingKeeper().IsTombstoned(s.providerCtx(), provAddr.ToSdkConsAddr()))
validator, _ := s.providerApp.GetTestStakingKeeper().GetValidator(s.providerCtx(), provAddr.ToSdkConsAddr().Bytes())
slashFraction, err := s.providerApp.GetTestSlashingKeeper().SlashFractionDoubleSign(s.providerCtx())
s.Require().NoError(err)
actualTokens := math.LegacyNewDecFromInt(validator.GetTokens())
s.Require().True(initialTokens.Sub(initialTokens.Mul(slashFraction)).Equal(actualTokens))
}
}
// TestGetByzantineValidators checks the GetByzantineValidators function on various instances of misbehaviour.
// @Long Description@
// * Set up a provider and consumer chain.
// * Create a header with a subset of the validators on the consumer chain, then create a second header (in a variety of different ways),
// and check which validators are considered Byzantine by calling the GetByzantineValidators function.
// * The test scenarios are:
// - when one of the headers is empty, the function should return an error
// - when one of the headers has a corrupted validator set (e.g. by a validator having a different public key), the function should return an error
// - when the signatures in one of the headers are corrupted, the function should return an error
// - when the attack is an amnesia attack (i.e. the headers have different block IDs), no validator is considered byzantine
// - for non-amnesia misbehaviour, all validators that signed both headers are considered byzantine
func (s *CCVTestSuite) TestGetByzantineValidators() {
s.SetupCCVChannel(s.path)
// required to have the consumer client revision height greater than 0
s.SendEmptyVSCPacket()
altTime := s.providerCtx().BlockTime().Add(time.Minute)
// Get the consumer client validator set
clientHeight := s.consumerChain.LastHeader.TrustedHeight
clientTMValset := tmtypes.NewValidatorSet(s.consumerChain.Vals.Validators)
clientSigners := s.consumerChain.Signers
// Create a subset of the consumer client validator set
altValset := tmtypes.NewValidatorSet(s.consumerChain.Vals.Validators[0:3])
altSigners := make(map[string]tmtypes.PrivValidator, 3)
altSigners[clientTMValset.Validators[0].Address.String()] = clientSigners[clientTMValset.Validators[0].Address.String()]
altSigners[clientTMValset.Validators[1].Address.String()] = clientSigners[clientTMValset.Validators[1].Address.String()]
altSigners[clientTMValset.Validators[2].Address.String()] = clientSigners[clientTMValset.Validators[2].Address.String()]
// create a consumer client header
clientHeader := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime,
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
)
testCases := []struct {
name string
getMisbehaviour func() *ibctmtypes.Misbehaviour
expByzantineValidators []*tmtypes.Validator
expPass bool
}{
{
"invalid misbehaviour - Header1 is empty",
func() *ibctmtypes.Misbehaviour {
return &ibctmtypes.Misbehaviour{
Header1: &ibctmtypes.Header{},
Header2: clientHeader,
}
},
nil,
false,
},
{
"invalid headers - Header2 is empty",
func() *ibctmtypes.Misbehaviour {
return &ibctmtypes.Misbehaviour{
Header1: clientHeader,
Header2: &ibctmtypes.Header{},
}
},
nil,
false,
},
{
"incorrect valset - shouldn't pass",
func() *ibctmtypes.Misbehaviour {
clientHeader := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime.Add(time.Minute),
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
)
clientHeaderWithCorruptedValset := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime.Add(time.Hour),
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
)
// change a validator public key in one the second header
testutil.CorruptValidatorPubkeyInHeader(clientHeaderWithCorruptedValset, clientTMValset.Validators[0].Address)
return &ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: clientHeader,
Header2: clientHeaderWithCorruptedValset,
}
},
[]*tmtypes.Validator{},
false,
},
{
"incorrect valset 2 - shouldn't pass",
func() *ibctmtypes.Misbehaviour {
clientHeader := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime.Add(time.Minute),
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
)
clientHeaderWithCorruptedSigs := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime.Add(time.Hour),
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
)
// change the valset in the header
vs, _ := altValset.ToProto()
clientHeader.ValidatorSet.Validators = vs.Validators[:3]
clientHeaderWithCorruptedSigs.ValidatorSet.Validators = vs.Validators[:3]
return &ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: clientHeader,
Header2: clientHeaderWithCorruptedSigs,
}
},
[]*tmtypes.Validator{},
false,
},
{
"incorrect signatures - shouldn't pass",
func() *ibctmtypes.Misbehaviour {
clientHeader := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime.Add(time.Minute),
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
)
clientHeaderWithCorruptedSigs := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime.Add(time.Hour),
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
)
// change the signature of one of the validator in the header
testutil.CorruptCommitSigsInHeader(clientHeaderWithCorruptedSigs, clientTMValset.Validators[0].Address)
return &ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: clientHeader,
Header2: clientHeaderWithCorruptedSigs,
}
},
[]*tmtypes.Validator{},
false,
},
{
"light client attack - lunatic attack",
func() *ibctmtypes.Misbehaviour {
return &ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: clientHeader,
// the resulting header contains invalid fields
// i.e. ValidatorsHash, NextValidatorsHash.
Header2: s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime,
altValset,
altValset,
clientTMValset,
altSigners,
),
}
},
// Expect to get only the validators
// who signed both headers
altValset.Validators,
true,
},
{
"light client attack - equivocation",
func() *ibctmtypes.Misbehaviour {
return &ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: clientHeader,
// the resulting header contains a different BlockID
Header2: s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime.Add(time.Minute),
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
),
}
},
// Expect to get the entire valset since
// all validators double-signed
clientTMValset.Validators,
true,
},
{
"light client attack - amnesia",
func() *ibctmtypes.Misbehaviour {
// create a valid header with a different hash
// and commit round
amnesiaHeader := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
altTime.Add(time.Minute),
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
)
amnesiaHeader.Commit.Round = 2
return &ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: clientHeader,
Header2: amnesiaHeader,
}
},
// Expect no validators
// since amnesia attacks are dropped
[]*tmtypes.Validator{},
true,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
byzantineValidators, err := s.providerApp.GetProviderKeeper().GetByzantineValidators(
s.providerCtx(),
*tc.getMisbehaviour(),
)
if tc.expPass {
s.NoError(err)
s.Equal(len(tc.expByzantineValidators), len(byzantineValidators))
// For both lunatic and equivocation attacks, all the validators
// who signed both headers
if len(tc.expByzantineValidators) > 0 {
equivocatingVals := tc.getMisbehaviour().Header2.ValidatorSet
s.Equal(len(equivocatingVals.Validators), len(byzantineValidators))
vs, err := tmtypes.ValidatorSetFromProto(equivocatingVals)
s.NoError(err)
for _, v := range tc.expByzantineValidators {
idx, _ := vs.GetByAddress(v.Address)
s.True(idx >= 0)
}
}
} else {
s.Error(err)
}
})
}
}
// TestCheckMisbehaviour tests that the CheckMisbehaviour function correctly checks for misbehaviour.
// @Long Description@
// * Set up a provider and consumer chain.
// * Create a valid client header and then create a misbehaviour by creating a second header in a variety of different ways.
// * Check that the CheckMisbehaviour function correctly checks for misbehaviour by verifying that
// it returns an error when the misbehaviour is invalid and no error when the misbehaviour is valid.
// * The test scenarios are:
// - both headers are identical (returns an error)
// - the misbehaviour is not for the consumer chain (returns an error)
// - passing an invalid client id (returns an error)
// - passing a misbehaviour with different header height (returns an error)
// - passing a misbehaviour older than the min equivocation evidence height (returns an error)
// - one header of the misbehaviour has insufficient voting power (returns an error)
// - passing a valid misbehaviour (no error)
//
// * Test does not test actually submitting the misbehaviour to the chain or freezing the client.
func (s *CCVTestSuite) TestCheckMisbehaviour() {
s.SetupCCVChannel(s.path)
// required to have the consumer client revision height greater than 0
s.SendEmptyVSCPacket()
// create signing info for all validators
for _, v := range s.providerChain.Vals.Validators {
s.setDefaultValSigningInfo(*v)
}
// create a new header timestamp
headerTs := s.providerCtx().BlockTime().Add(time.Minute)
// get trusted validators and height
clientHeight := s.consumerChain.LastHeader.TrustedHeight
clientTMValset := tmtypes.NewValidatorSet(s.consumerChain.Vals.Validators)
clientSigners := s.consumerChain.Signers
// create a valid client header
clientHeader := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
headerTs,
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
)
// create an alternative validator set using more than 1/3 of the trusted validator set
altValset := tmtypes.NewValidatorSet(s.consumerChain.Vals.Validators[0:2])
altSigners := make(map[string]tmtypes.PrivValidator, 2)
altSigners[clientTMValset.Validators[0].Address.String()] = clientSigners[clientTMValset.Validators[0].Address.String()]
altSigners[clientTMValset.Validators[1].Address.String()] = clientSigners[clientTMValset.Validators[1].Address.String()]
// create a conflicting client with different block ID using
// to alternative validator set
clientHeaderWithDiffBlockID := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
headerTs,
altValset,
altValset,
clientTMValset, // trusted valset stays the same
altSigners,
)
// create an alternative validator set using less than 1/3 of the trusted validator set
altValset2 := tmtypes.NewValidatorSet(s.consumerChain.Vals.Validators[0:1])
altSigners2 := make(map[string]tmtypes.PrivValidator, 1)
altSigners2[clientTMValset.Validators[0].Address.String()] = clientSigners[clientTMValset.Validators[0].Address.String()]
// create a conflicting client header with insufficient voting power
clientHeaderWithInsufficientVotingPower := s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+1),
clientHeight,
// use a different block time to change the header BlockID
headerTs.Add(time.Hour),
altValset2,
altValset2,
clientTMValset,
altSigners2,
)
// Set the equivocation evidence min height to the previous block height
equivocationEvidenceMinHeight := clientHeight.RevisionHeight + 1
s.providerApp.GetProviderKeeper().SetEquivocationEvidenceMinHeight(
s.providerCtx(),
s.getFirstBundle().ConsumerId,
equivocationEvidenceMinHeight,
)
testCases := []struct {
name string
misbehaviour *ibctmtypes.Misbehaviour
expPass bool
}{
{
"identical headers - shouldn't pass",
&ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: clientHeader,
Header2: clientHeader,
},
false,
},
{
"misbehaviour isn't for a consumer chain - shouldn't pass",
&ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: s.consumerChain.CreateTMClientHeader(
"aChainID",
int64(clientHeight.RevisionHeight+1),
clientHeight,
headerTs,
altValset,
altValset,
clientTMValset,
altSigners,
),
Header2: clientHeader,
},
false,
},
{
"client ID doesn't correspond to the client ID of consumer chain - shouldn't pass",
&ibctmtypes.Misbehaviour{
ClientId: "clientID",
Header1: clientHeader,
Header2: clientHeaderWithDiffBlockID,
},
false,
},
{
"invalid misbehaviour with different header height - shouldn't pass",
&ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: clientHeader,
Header2: s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(clientHeight.RevisionHeight+2),
clientHeight,
headerTs,
altValset,
altValset,
clientTMValset,
altSigners,
),
},
false,
},
{
"invalid misbehaviour older than the min equivocation evidence height - shouldn't pass",
&ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(equivocationEvidenceMinHeight-1),
clientHeight,
headerTs,
altValset,
altValset,
clientTMValset,
altSigners,
),
Header2: s.consumerChain.CreateTMClientHeader(
s.getFirstBundle().Chain.ChainID,
int64(equivocationEvidenceMinHeight-1),
clientHeight,
headerTs,
clientTMValset,
clientTMValset,
clientTMValset,
clientSigners,
),
},
false,
},
{
"one header of the misbehaviour has insufficient voting power - shouldn't pass",
&ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: clientHeader,
Header2: clientHeaderWithInsufficientVotingPower,
},
false,
},
{
"valid misbehaviour - should pass",
&ibctmtypes.Misbehaviour{
ClientId: s.path.EndpointA.ClientID,
Header1: clientHeader,
// create header using a different validator set
Header2: clientHeaderWithDiffBlockID,
},
true,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
err := s.providerApp.GetProviderKeeper().CheckMisbehaviour(s.providerCtx(), s.getFirstBundle().ConsumerId, *tc.misbehaviour)
cs, ok := s.providerApp.GetIBCKeeper().ClientKeeper.GetClientState(s.providerCtx(), s.path.EndpointA.ClientID)
s.Require().True(ok)
// verify that the client wasn't frozen
s.Require().Zero(cs.(*ibctmtypes.ClientState).FrozenHeight)
if tc.expPass {
s.NoError(err)
} else {
s.Error(err)
}
})
}
}