16
16
// limitations under the License.
17
17
18
18
use codec:: Encode ;
19
- use std:: vec;
19
+ use std:: {
20
+ ops:: { Mul , Sub } ,
21
+ vec,
22
+ } ;
20
23
21
24
use frame_support:: {
22
25
assert_err, assert_ok,
@@ -32,7 +35,7 @@ use sp_consensus_beefy::{
32
35
} ,
33
36
Payload , ValidatorSet , ValidatorSetId , KEY_TYPE as BEEFY_KEY_TYPE ,
34
37
} ;
35
- use sp_runtime:: DigestItem ;
38
+ use sp_runtime:: { DigestItem , Perbill } ;
36
39
use sp_session:: MembershipProof ;
37
40
38
41
use crate :: { self as beefy, mock:: * , Call , Config , Error , WeightInfoExt } ;
@@ -309,8 +312,13 @@ fn report_double_voting(
309
312
)
310
313
}
311
314
312
- fn report_equivocation_current_set_works ( mut f : impl ReportEquivocationFn ) {
315
+ fn report_equivocation_current_set_works (
316
+ mut f : impl ReportEquivocationFn ,
317
+ hardcoded_slash_fraction : Option < Perbill > ,
318
+ ) {
313
319
let authorities = test_authorities ( ) ;
320
+ let initial_balance = 10_000_000 ;
321
+ let initial_slashable_balance = 10_000 ;
314
322
315
323
ExtBuilder :: default ( ) . add_authorities ( authorities) . build_and_execute ( || {
316
324
assert_eq ! ( Staking :: current_era( ) , Some ( 0 ) ) ;
@@ -326,12 +334,16 @@ fn report_equivocation_current_set_works(mut f: impl ReportEquivocationFn) {
326
334
327
335
// make sure that all validators have the same balance
328
336
for validator in & validators {
329
- assert_eq ! ( Balances :: total_balance( validator) , 10_000_000 ) ;
330
- assert_eq ! ( Staking :: slashable_balance_of( validator) , 10_000 ) ;
337
+ assert_eq ! ( Balances :: total_balance( validator) , initial_balance ) ;
338
+ assert_eq ! ( Staking :: slashable_balance_of( validator) , initial_slashable_balance ) ;
331
339
332
340
assert_eq ! (
333
341
Staking :: eras_stakers( 1 , & validator) ,
334
- pallet_staking:: Exposure { total: 10_000 , own: 10_000 , others: vec![ ] } ,
342
+ pallet_staking:: Exposure {
343
+ total: initial_slashable_balance,
344
+ own: initial_slashable_balance,
345
+ others: vec![ ]
346
+ } ,
335
347
) ;
336
348
}
337
349
@@ -351,8 +363,22 @@ fn report_equivocation_current_set_works(mut f: impl ReportEquivocationFn) {
351
363
// check that the balance of 0-th validator is slashed 100%.
352
364
let equivocation_validator_id = validators[ equivocation_authority_index] ;
353
365
354
- assert_eq ! ( Balances :: total_balance( & equivocation_validator_id) , 10_000_000 - 10_000 ) ;
355
- assert_eq ! ( Staking :: slashable_balance_of( & equivocation_validator_id) , 0 ) ;
366
+ if let Some ( slash_fraction) = hardcoded_slash_fraction {
367
+ assert_eq ! (
368
+ Balances :: total_balance( & equivocation_validator_id) ,
369
+ initial_balance - slash_fraction. mul( initial_slashable_balance)
370
+ ) ;
371
+ assert_eq ! (
372
+ Staking :: slashable_balance_of( & equivocation_validator_id) ,
373
+ Perbill :: from_percent( 100 ) . sub( slash_fraction) . mul( initial_slashable_balance)
374
+ ) ;
375
+ } else {
376
+ assert_eq ! (
377
+ Balances :: total_balance( & equivocation_validator_id) ,
378
+ initial_balance - initial_slashable_balance
379
+ ) ;
380
+ assert_eq ! ( Staking :: slashable_balance_of( & equivocation_validator_id) , 0 ) ;
381
+ }
356
382
assert_eq ! (
357
383
Staking :: eras_stakers( 2 , & equivocation_validator_id) ,
358
384
pallet_staking:: Exposure { total: 0 , own: 0 , others: vec![ ] } ,
@@ -364,19 +390,28 @@ fn report_equivocation_current_set_works(mut f: impl ReportEquivocationFn) {
364
390
continue
365
391
}
366
392
367
- assert_eq ! ( Balances :: total_balance( validator) , 10_000_000 ) ;
368
- assert_eq ! ( Staking :: slashable_balance_of( validator) , 10_000 ) ;
393
+ assert_eq ! ( Balances :: total_balance( validator) , initial_balance ) ;
394
+ assert_eq ! ( Staking :: slashable_balance_of( validator) , initial_slashable_balance ) ;
369
395
370
396
assert_eq ! (
371
397
Staking :: eras_stakers( 2 , & validator) ,
372
- pallet_staking:: Exposure { total: 10_000 , own: 10_000 , others: vec![ ] } ,
398
+ pallet_staking:: Exposure {
399
+ total: initial_slashable_balance,
400
+ own: initial_slashable_balance,
401
+ others: vec![ ]
402
+ } ,
373
403
) ;
374
404
}
375
405
} ) ;
376
406
}
377
407
378
- fn report_equivocation_old_set_works ( mut f : impl ReportEquivocationFn ) {
408
+ fn report_equivocation_old_set_works (
409
+ mut f : impl ReportEquivocationFn ,
410
+ hardcoded_slash_fraction : Option < Perbill > ,
411
+ ) {
379
412
let authorities = test_authorities ( ) ;
413
+ let initial_balance = 10_000_000 ;
414
+ let initial_slashable_balance = 10_000 ;
380
415
381
416
ExtBuilder :: default ( ) . add_authorities ( authorities) . build_and_execute ( || {
382
417
start_era ( 1 ) ;
@@ -398,12 +433,16 @@ fn report_equivocation_old_set_works(mut f: impl ReportEquivocationFn) {
398
433
399
434
// make sure that all authorities have the same balance
400
435
for validator in & validators {
401
- assert_eq ! ( Balances :: total_balance( validator) , 10_000_000 ) ;
402
- assert_eq ! ( Staking :: slashable_balance_of( validator) , 10_000 ) ;
436
+ assert_eq ! ( Balances :: total_balance( validator) , initial_balance ) ;
437
+ assert_eq ! ( Staking :: slashable_balance_of( validator) , initial_slashable_balance ) ;
403
438
404
439
assert_eq ! (
405
440
Staking :: eras_stakers( 2 , & validator) ,
406
- pallet_staking:: Exposure { total: 10_000 , own: 10_000 , others: vec![ ] } ,
441
+ pallet_staking:: Exposure {
442
+ total: initial_slashable_balance,
443
+ own: initial_slashable_balance,
444
+ others: vec![ ]
445
+ } ,
407
446
) ;
408
447
}
409
448
@@ -421,8 +460,22 @@ fn report_equivocation_old_set_works(mut f: impl ReportEquivocationFn) {
421
460
// check that the balance of 0-th validator is slashed 100%.
422
461
let equivocation_validator_id = validators[ equivocation_authority_index] ;
423
462
424
- assert_eq ! ( Balances :: total_balance( & equivocation_validator_id) , 10_000_000 - 10_000 ) ;
425
- assert_eq ! ( Staking :: slashable_balance_of( & equivocation_validator_id) , 0 ) ;
463
+ if let Some ( slash_fraction) = hardcoded_slash_fraction {
464
+ assert_eq ! (
465
+ Balances :: total_balance( & equivocation_validator_id) ,
466
+ initial_balance - slash_fraction. mul( initial_slashable_balance)
467
+ ) ;
468
+ assert_eq ! (
469
+ Staking :: slashable_balance_of( & equivocation_validator_id) ,
470
+ Perbill :: from_percent( 100 ) . sub( slash_fraction) . mul( initial_slashable_balance)
471
+ ) ;
472
+ } else {
473
+ assert_eq ! (
474
+ Balances :: total_balance( & equivocation_validator_id) ,
475
+ initial_balance - initial_slashable_balance
476
+ ) ;
477
+ assert_eq ! ( Staking :: slashable_balance_of( & equivocation_validator_id) , 0 ) ;
478
+ }
426
479
assert_eq ! (
427
480
Staking :: eras_stakers( 3 , & equivocation_validator_id) ,
428
481
pallet_staking:: Exposure { total: 0 , own: 0 , others: vec![ ] } ,
@@ -434,12 +487,16 @@ fn report_equivocation_old_set_works(mut f: impl ReportEquivocationFn) {
434
487
continue
435
488
}
436
489
437
- assert_eq ! ( Balances :: total_balance( validator) , 10_000_000 ) ;
438
- assert_eq ! ( Staking :: slashable_balance_of( validator) , 10_000 ) ;
490
+ assert_eq ! ( Balances :: total_balance( validator) , initial_balance ) ;
491
+ assert_eq ! ( Staking :: slashable_balance_of( validator) , initial_slashable_balance ) ;
439
492
440
493
assert_eq ! (
441
494
Staking :: eras_stakers( 3 , & validator) ,
442
- pallet_staking:: Exposure { total: 10_000 , own: 10_000 , others: vec![ ] } ,
495
+ pallet_staking:: Exposure {
496
+ total: initial_slashable_balance,
497
+ own: initial_slashable_balance,
498
+ others: vec![ ]
499
+ } ,
443
500
) ;
444
501
}
445
502
} ) ;
@@ -579,12 +636,12 @@ fn valid_equivocation_reports_dont_pay_fees(mut f: impl ReportEquivocationFn) {
579
636
580
637
#[ test]
581
638
fn report_double_voting_current_set_works ( ) {
582
- report_equivocation_current_set_works ( report_double_voting) ;
639
+ report_equivocation_current_set_works ( report_double_voting, None ) ;
583
640
}
584
641
585
642
#[ test]
586
643
fn report_double_voting_old_set_works ( ) {
587
- report_equivocation_old_set_works ( report_double_voting) ;
644
+ report_equivocation_old_set_works ( report_double_voting, None ) ;
588
645
}
589
646
590
647
#[ test]
@@ -812,12 +869,12 @@ fn report_fork_voting(
812
869
813
870
#[ test]
814
871
fn report_fork_voting_current_set_works ( ) {
815
- report_equivocation_current_set_works ( report_fork_voting) ;
872
+ report_equivocation_current_set_works ( report_fork_voting, Some ( Perbill :: from_percent ( 50 ) ) ) ;
816
873
}
817
874
818
875
#[ test]
819
876
fn report_fork_voting_old_set_works ( ) {
820
- report_equivocation_old_set_works ( report_fork_voting) ;
877
+ report_equivocation_old_set_works ( report_fork_voting, Some ( Perbill :: from_percent ( 50 ) ) ) ;
821
878
}
822
879
823
880
#[ test]
@@ -1007,12 +1064,15 @@ fn report_future_block_voting(
1007
1064
1008
1065
#[ test]
1009
1066
fn report_future_block_voting_current_set_works ( ) {
1010
- report_equivocation_current_set_works ( report_future_block_voting) ;
1067
+ report_equivocation_current_set_works (
1068
+ report_future_block_voting,
1069
+ Some ( Perbill :: from_percent ( 50 ) ) ,
1070
+ ) ;
1011
1071
}
1012
1072
1013
1073
#[ test]
1014
1074
fn report_future_block_voting_old_set_works ( ) {
1015
- report_equivocation_old_set_works ( report_future_block_voting) ;
1075
+ report_equivocation_old_set_works ( report_future_block_voting, Some ( Perbill :: from_percent ( 50 ) ) ) ;
1016
1076
}
1017
1077
1018
1078
#[ test]
0 commit comments