Newer
Older
others: vec![],
},
),
reporters: vec![],
}],
&[Perbill::from_percent(50)],
);
// The stash account should be slashed for 250 (50% of 500).
assert_eq!(Balances::free_balance(&11), 1000 - 250);
});
}
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
#[test]
fn slash_in_old_span_does_not_deselect() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
assert!(<Validators<Test>>::exists(11));
on_offence_now(
&[OffenceDetails {
offender: (
11,
Staking::stakers(&11),
),
reporters: vec![],
}],
&[Perbill::from_percent(0)],
);
assert_eq!(Staking::force_era(), Forcing::ForceNew);
assert!(!<Validators<Test>>::exists(11));
start_era(2);
Staking::validate(Origin::signed(10), Default::default()).unwrap();
assert_eq!(Staking::force_era(), Forcing::NotForcing);
assert!(<Validators<Test>>::exists(11));
start_era(3);
// this staker is in a new slashing span now, having re-registered after
// their prior slash.
on_offence_in_era(
&[OffenceDetails {
offender: (
11,
Staking::stakers(&11),
),
reporters: vec![],
}],
&[Perbill::from_percent(0)],
1,
);
// not for zero-slash.
assert_eq!(Staking::force_era(), Forcing::NotForcing);
assert!(<Validators<Test>>::exists(11));
on_offence_in_era(
&[OffenceDetails {
offender: (
11,
Staking::stakers(&11),
),
reporters: vec![],
}],
&[Perbill::from_percent(100)],
1,
);
// or non-zero.
assert_eq!(Staking::force_era(), Forcing::NotForcing);
assert!(<Validators<Test>>::exists(11));
assert_ledger_consistent(11);
});
}
#[test]
fn reporters_receive_their_slice() {
// This test verifies that the reporters of the offence receive their slice from the slashed
// amount.
ExtBuilder::default().build().execute_with(|| {
// The reporters' reward is calculated from the total exposure.
let initial_balance = 1125;
assert_eq!(Staking::stakers(&11).total, initial_balance);
&[OffenceDetails {
offender: (
11,
Staking::stakers(&11),
),
reporters: vec![1, 2],
}],
&[Perbill::from_percent(50)],
);
// F1 * (reward_proportion * slash - 0)
// 50% * (10% * initial_balance / 2)
let reward = (initial_balance / 20) / 2;
let reward_each = reward / 2; // split into two pieces.
assert_eq!(Balances::free_balance(&1), 10 + reward_each);
assert_eq!(Balances::free_balance(&2), 20 + reward_each);
assert_ledger_consistent(11);
fn subsequent_reports_in_same_span_pay_out_less() {
// This test verifies that the reporters of the offence receive their slice from the slashed
// amount.
ExtBuilder::default().build().execute_with(|| {
// The reporters' reward is calculated from the total exposure.
let initial_balance = 1125;
assert_eq!(Staking::stakers(&11).total, initial_balance);
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
on_offence_now(
&[OffenceDetails {
offender: (
11,
Staking::stakers(&11),
),
reporters: vec![1],
}],
&[Perbill::from_percent(20)],
);
// F1 * (reward_proportion * slash - 0)
// 50% * (10% * initial_balance * 20%)
let reward = (initial_balance / 5) / 20;
assert_eq!(Balances::free_balance(&1), 10 + reward);
on_offence_now(
&[OffenceDetails {
offender: (
11,
Staking::stakers(&11),
),
reporters: vec![1],
}],
&[Perbill::from_percent(50)],
);
let prior_payout = reward;
// F1 * (reward_proportion * slash - prior_payout)
// 50% * (10% * (initial_balance / 2) - prior_payout)
let reward = ((initial_balance / 20) - prior_payout) / 2;
assert_eq!(Balances::free_balance(&1), 10 + prior_payout + reward);
assert_ledger_consistent(11);
});
}
#[test]
fn invulnerables_are_not_slashed() {
// For invulnerable validators no slashing is performed.
ExtBuilder::default().invulnerables(vec![11]).build().execute_with(|| {
assert_eq!(Balances::free_balance(&11), 1000);
assert_eq!(Balances::free_balance(&21), 2000);
let exposure = Staking::stakers(&21);
let initial_balance = Staking::slashable_balance_of(&21);
let nominator_balances: Vec<_> = exposure.others
.iter().map(|o| Balances::free_balance(&o.who)).collect();
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::stakers(&11)),
reporters: vec![],
},
OffenceDetails {
offender: (21, Staking::stakers(&21)),
reporters: vec![],
},
],
&[Perbill::from_percent(50), Perbill::from_percent(20)],
);
// The validator 11 hasn't been slashed, but 21 has been.
assert_eq!(Balances::free_balance(&11), 1000);
// 2000 - (0.2 * initial_balance)
assert_eq!(Balances::free_balance(&21), 2000 - (2 * initial_balance / 10));
// ensure that nominators were slashed as well.
for (initial_balance, other) in nominator_balances.into_iter().zip(exposure.others) {
assert_eq!(
Balances::free_balance(&other.who),
initial_balance - (2 * other.value / 10),
);
}
assert_ledger_consistent(11);
assert_ledger_consistent(21);
}
#[test]
fn dont_slash_if_fraction_is_zero() {
// Don't slash if the fraction is zero.
ExtBuilder::default().build().execute_with(|| {
assert_eq!(Balances::free_balance(&11), 1000);
&[OffenceDetails {
offender: (
11,
Staking::stakers(&11),
),
reporters: vec![],
}],
&[Perbill::from_percent(0)],
);
// The validator hasn't been slashed. The new era is not forced.
assert_eq!(Balances::free_balance(&11), 1000);
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
assert_ledger_consistent(11);
});
}
#[test]
fn only_slash_for_max_in_era() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(Balances::free_balance(&11), 1000);
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::stakers(&11)),
reporters: vec![],
},
],
&[Perbill::from_percent(50)],
);
// The validator has been slashed and has been force-chilled.
assert_eq!(Balances::free_balance(&11), 500);
assert_eq!(Staking::force_era(), Forcing::ForceNew);
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::stakers(&11)),
reporters: vec![],
},
],
&[Perbill::from_percent(25)],
);
// The validator has not been slashed additionally.
assert_eq!(Balances::free_balance(&11), 500);
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::stakers(&11)),
reporters: vec![],
},
],
&[Perbill::from_percent(60)],
);
// The validator got slashed 10% more.
assert_eq!(Balances::free_balance(&11), 400);
assert_ledger_consistent(11);
})
}
#[test]
fn garbage_collection_after_slashing() {
ExtBuilder::default().existential_deposit(1).build().execute_with(|| {
assert_eq!(Balances::free_balance(&11), 256_000);
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::stakers(&11)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
assert_eq!(Balances::free_balance(&11), 256_000 - 25_600);
assert!(<Staking as crate::Store>::SlashingSpans::get(&11).is_some());
assert_eq!(<Staking as crate::Store>::SpanSlash::get(&(11, 0)).amount_slashed(), &25_600);
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::stakers(&11)),
reporters: vec![],
},
],
&[Perbill::from_percent(100)],
);
// validator and nominator slash in era are garbage-collected by era change,
// so we don't test those here.
assert_eq!(Balances::free_balance(&11), 0);
assert!(<Staking as crate::Store>::SlashingSpans::get(&11).is_none());
assert_eq!(<Staking as crate::Store>::SpanSlash::get(&(11, 0)).amount_slashed(), &0);
})
}
#[test]
fn garbage_collection_on_window_pruning() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(&11), 1000);
let exposure = Staking::stakers(&11);
assert_eq!(Balances::free_balance(&101), 2000);
let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value;
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::stakers(&11)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
let now = Staking::current_era();
assert_eq!(Balances::free_balance(&11), 900);
assert_eq!(Balances::free_balance(&101), 2000 - (nominated_value / 10));
assert!(<Staking as crate::Store>::ValidatorSlashInEra::get(&now, &11).is_some());
assert!(<Staking as crate::Store>::NominatorSlashInEra::get(&now, &101).is_some());
// + 1 because we have to exit the bonding window.
for era in (0..(BondingDuration::get() + 1)).map(|offset| offset + now + 1) {
assert!(<Staking as crate::Store>::ValidatorSlashInEra::get(&now, &11).is_some());
assert!(<Staking as crate::Store>::NominatorSlashInEra::get(&now, &101).is_some());
start_era(era);
}
assert!(<Staking as crate::Store>::ValidatorSlashInEra::get(&now, &11).is_none());
assert!(<Staking as crate::Store>::NominatorSlashInEra::get(&now, &101).is_none());
})
}
#[test]
fn slashing_nominators_by_span_max() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
start_era(2);
start_era(3);
assert_eq!(Balances::free_balance(&11), 1000);
assert_eq!(Balances::free_balance(&21), 2000);
assert_eq!(Balances::free_balance(&101), 2000);
assert_eq!(Staking::slashable_balance_of(&21), 1000);
let exposure_11 = Staking::stakers(&11);
let exposure_21 = Staking::stakers(&21);
assert_eq!(Balances::free_balance(&101), 2000);
let nominated_value_11 = exposure_11.others.iter().find(|o| o.who == 101).unwrap().value;
let nominated_value_21 = exposure_21.others.iter().find(|o| o.who == 101).unwrap().value;
on_offence_in_era(
&[
OffenceDetails {
offender: (11, Staking::stakers(&11)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
2,
);
assert_eq!(Balances::free_balance(&11), 900);
let slash_1_amount = Perbill::from_percent(10) * nominated_value_11;
assert_eq!(Balances::free_balance(&101), 2000 - slash_1_amount);
let expected_spans = vec![
slashing::SlashingSpan { index: 1, start: 4, length: None },
slashing::SlashingSpan { index: 0, start: 0, length: Some(4) },
];
let get_span = |account| <Staking as crate::Store>::SlashingSpans::get(&account).unwrap();
assert_eq!(
get_span(11).iter().collect::<Vec<_>>(),
expected_spans,
);
assert_eq!(
get_span(101).iter().collect::<Vec<_>>(),
expected_spans,
);
// second slash: higher era, higher value, same span.
on_offence_in_era(
&[
OffenceDetails {
offender: (21, Staking::stakers(&21)),
reporters: vec![],
},
],
&[Perbill::from_percent(30)],
3,
);
// 11 was not further slashed, but 21 and 101 were.
assert_eq!(Balances::free_balance(&11), 900);
assert_eq!(Balances::free_balance(&21), 1700);
let slash_2_amount = Perbill::from_percent(30) * nominated_value_21;
assert!(slash_2_amount > slash_1_amount);
// only the maximum slash in a single span is taken.
assert_eq!(Balances::free_balance(&101), 2000 - slash_2_amount);
// third slash: in same era and on same validator as first, higher
// in-era value, but lower slash value than slash 2.
on_offence_in_era(
&[
OffenceDetails {
offender: (11, Staking::stakers(&11)),
reporters: vec![],
},
],
&[Perbill::from_percent(20)],
2,
);
// 11 was further slashed, but 21 and 101 were not.
assert_eq!(Balances::free_balance(&11), 800);
assert_eq!(Balances::free_balance(&21), 1700);
let slash_3_amount = Perbill::from_percent(20) * nominated_value_21;
assert!(slash_3_amount < slash_2_amount);
assert!(slash_3_amount > slash_1_amount);
// only the maximum slash in a single span is taken.
assert_eq!(Balances::free_balance(&101), 2000 - slash_2_amount);
});
}
#[test]
fn slashes_are_summed_across_spans() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
start_era(2);
start_era(3);
assert_eq!(Balances::free_balance(&21), 2000);
assert_eq!(Staking::slashable_balance_of(&21), 1000);
let get_span = |account| <Staking as crate::Store>::SlashingSpans::get(&account).unwrap();
on_offence_now(
&[
OffenceDetails {
offender: (21, Staking::stakers(&21)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
let expected_spans = vec![
slashing::SlashingSpan { index: 1, start: 4, length: None },
slashing::SlashingSpan { index: 0, start: 0, length: Some(4) },
];
assert_eq!(get_span(21).iter().collect::<Vec<_>>(), expected_spans);
assert_eq!(Balances::free_balance(&21), 1900);
// 21 has been force-chilled. re-signal intent to validate.
Staking::validate(Origin::signed(20), Default::default()).unwrap();
start_era(4);
assert_eq!(Staking::slashable_balance_of(&21), 900);
on_offence_now(
&[
OffenceDetails {
offender: (21, Staking::stakers(&21)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
let expected_spans = vec![
slashing::SlashingSpan { index: 2, start: 5, length: None },
slashing::SlashingSpan { index: 1, start: 4, length: Some(1) },
slashing::SlashingSpan { index: 0, start: 0, length: Some(4) },
];
assert_eq!(get_span(21).iter().collect::<Vec<_>>(), expected_spans);
assert_eq!(Balances::free_balance(&21), 1810);
});
}
#[test]
fn deferred_slashes_are_deferred() {
ExtBuilder::default().slash_defer_duration(2).build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(&11), 1000);
let exposure = Staking::stakers(&11);
assert_eq!(Balances::free_balance(&101), 2000);
let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value;
on_offence_now(
&[
OffenceDetails {
offender: (11, Staking::stakers(&11)),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
assert_eq!(Balances::free_balance(&11), 1000);
assert_eq!(Balances::free_balance(&101), 2000);
start_era(2);
assert_eq!(Balances::free_balance(&11), 1000);
assert_eq!(Balances::free_balance(&101), 2000);
start_era(3);
assert_eq!(Balances::free_balance(&11), 1000);
assert_eq!(Balances::free_balance(&101), 2000);
// at the start of era 4, slashes from era 1 are processed,
// after being deferred for at least 2 full eras.
start_era(4);
assert_eq!(Balances::free_balance(&11), 900);
assert_eq!(Balances::free_balance(&101), 2000 - (nominated_value / 10));
})
}
#[test]
fn remove_deferred() {
ExtBuilder::default().slash_defer_duration(2).build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(&11), 1000);
let exposure = Staking::stakers(&11);
assert_eq!(Balances::free_balance(&101), 2000);
let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value;
on_offence_now(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
assert_eq!(Balances::free_balance(&11), 1000);
assert_eq!(Balances::free_balance(&101), 2000);
start_era(2);
on_offence_in_era(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(15)],
1,
);
Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![0]).unwrap();
assert_eq!(Balances::free_balance(&11), 1000);
assert_eq!(Balances::free_balance(&101), 2000);
start_era(3);
assert_eq!(Balances::free_balance(&11), 1000);
assert_eq!(Balances::free_balance(&101), 2000);
// at the start of era 4, slashes from era 1 are processed,
// after being deferred for at least 2 full eras.
start_era(4);
// the first slash for 10% was cancelled, so no effect.
assert_eq!(Balances::free_balance(&11), 1000);
assert_eq!(Balances::free_balance(&101), 2000);
start_era(5);
let slash_10 = Perbill::from_percent(10);
let slash_15 = Perbill::from_percent(15);
let initial_slash = slash_10 * nominated_value;
let total_slash = slash_15 * nominated_value;
let actual_slash = total_slash - initial_slash;
// 5% slash (15 - 10) processed now.
assert_eq!(Balances::free_balance(&11), 950);
assert_eq!(Balances::free_balance(&101), 2000 - actual_slash);
})
}
#[test]
fn remove_multi_deferred() {
ExtBuilder::default().slash_defer_duration(2).build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(&11), 1000);
let exposure = Staking::stakers(&11);
assert_eq!(Balances::free_balance(&101), 2000);
on_offence_now(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
on_offence_now(
&[
OffenceDetails {
offender: (21, Staking::stakers(&21)),
reporters: vec![],
}
],
&[Perbill::from_percent(10)],
);
on_offence_now(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(25)],
);
assert_eq!(<Staking as Store>::UnappliedSlashes::get(&1).len(), 3);
Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![0, 2]).unwrap();
let slashes = <Staking as Store>::UnappliedSlashes::get(&1);
assert_eq!(slashes.len(), 1);
assert_eq!(slashes[0].validator, 21);
})
}
#[test]
fn version_initialized() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(<Staking as Store>::StorageVersion::get(), crate::migration::CURRENT_VERSION);
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
#[test]
fn slash_kicks_validators_not_nominators() {
ExtBuilder::default().build().execute_with(|| {
start_era(1);
assert_eq!(Balances::free_balance(&11), 1000);
let exposure = Staking::stakers(&11);
assert_eq!(Balances::free_balance(&101), 2000);
let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value;
on_offence_now(
&[
OffenceDetails {
offender: (11, exposure.clone()),
reporters: vec![],
},
],
&[Perbill::from_percent(10)],
);
assert_eq!(Balances::free_balance(&11), 900);
assert_eq!(Balances::free_balance(&101), 2000 - (nominated_value / 10));
// This is the best way to check that the validator was chilled; `get` will
// return default value.
for (stash, _) in <Staking as Store>::Validators::enumerate() {
assert!(stash != 11);
}
let nominations = <Staking as Store>::Nominators::get(&101).unwrap();
// and make sure that the vote will be ignored even if the validator
// re-registers.
let last_slash = <Staking as Store>::SlashingSpans::get(&11).unwrap().last_start();
assert!(nominations.submitted_in < last_slash);
});
}