Commit dda2cb5
[Staking] Bounded Slashing: Paginated Offence Processing & Slash Application (#7424)
closes #3610.
helps #6344, but need
to migrate storage `Offences::Reports` before we can remove exposure
dependency in RC pallets.
replaces #6788.
## Context
Slashing in staking is unbounded currently, which is a major blocker
until staking can move to a parachain (AH).
### Current Slashing Process (Unbounded)
1. **Offence Reported**
- Offences include multiple validators, each with potentially large
exposure pages.
- Slashes are **computed immediately** and scheduled for application
after **28 eras**.
2. **Slash Applied**
- All unapplied slashes are executed in **one block** at the start of
the **28th era**. This is an **unbounded operation**.
### Proposed Slashing Process (Bounded)
1. **Offence Queueing**
- Offences are **queued** after basic sanity checks.
2. **Paged Offence Processing (Computing Slash)**
- Slashes are **computed one validator exposure page at a time**.
- **Unapplied slashes** are stored in a **double map**:
- **Key 1 (k1):** `EraIndex`
- **Key 2 (k2):** `(Validator, SlashFraction, PageIndex)` — a unique
identifier for each slash page
3. **Paged Slash Application**
- Slashes are **applied one page at a time** across multiple blocks.
- Slash application starts at the **27th era** (one era earlier than
before) to ensure all slashes are applied **before stakers can unbond**
(which starts from era 28 onwards).
---
## Worst-Case Block Calculation for Slash Application
### Polkadot:
- **1 era = 24 hours**, **1 block = 6s** → **14,400 blocks/era**
- On parachains (**12s blocks**) → **7,200 blocks/era**
### Kusama:
- **1 era = 6 hours**, **1 block = 6s** → **3,600 blocks/era**
- On parachains (**12s blocks**) → **1,800 blocks/era**
### Worst-Case Assumptions:
- **Total stakers:** 40,000 nominators, 1000 validators. (Polkadot
currently has ~23k nominators and 500 validators)
- **Max slashed:** 50% so 20k nominators, 250 validators.
- **Page size:** Validators with multiple page: (512 + 1)/2 = 256 ,
Validators with single page: 1
### Calculation:
There might be a more accurate way to calculate this worst-case number,
and this estimate could be significantly higher than necessary, but it
shouldn’t exceed this value.
Blocks needed: 250 + 20k/256 = ~330 blocks.
## *Potential Improvement:*
- Consider adding an **Offchain Worker (OCW)** task to further optimize
slash application in future updates.
- Dynamically batch unapplied slashes based on number of nominators in
the page, or process until reserved weight limit is exhausted.
----
## Summary of Changes
### Storage
- **New:**
- `OffenceQueue` *(StorageDoubleMap)*
- **K1:** Era
- **K2:** Offending validator account
- **V:** `OffenceRecord`
- `OffenceQueueEras` *(StorageValue)*
- **V:** `BoundedVec<EraIndex, BoundingDuration>`
- `ProcessingOffence` *(StorageValue)*
- **V:** `(Era, offending validator account, OffenceRecord)`
- **Changed:**
- `UnappliedSlashes`:
- **Old:** `StorageMap<K -> Era, V -> Vec<UnappliedSlash>>`
- **New:** `StorageDoubleMap<K1 -> Era, K2 -> (validator_acc, perbill,
page_index), V -> UnappliedSlash>`
### Events
- **New:**
- `SlashComputed { offence_era, slash_era, offender, page }`
- `SlashCancelled { slash_era, slash_key, payout }`
### Error
- **Changed:**
- `InvalidSlashIndex` → Renamed to `InvalidSlashRecord`
- **Removed:**
- `NotSortedAndUnique`
- **Added:**
- `EraNotStarted`
### Call
- **Changed:**
- `cancel_deferred_slash(era, slash_indices: Vec<u32>)`
→ Now takes `Vec<(validator_acc, slash_fraction, page_index)>`
- **New:**
- `apply_slash(slash_era, slash_key: (validator_acc, slash_fraction,
page_index))`
### Runtime Config
- `FullIdentification` is now set to a unit type (`()`) / null identity,
replacing the previous exposure type for all runtimes using
`pallet_session::historical`.
## TODO
- [x] Fixed broken `CancelDeferredSlashes`.
- [x] Ensure on_offence called only with validator account for
identification everywhere.
- [ ] Ensure we never need to read full exposure.
- [x] Tests for multi block processing and application of slash.
- [x] Migrate UnappliedSlashes
- [x] Bench (crude, needs proper bench as followup)
- [x] on_offence()
- [x] process_offence()
- [x] apply_slash()
## Followups (tracker
[link](#7596))
- [ ] OCW task to process offence + apply slashes.
- [ ] Minimum time for governance to cancel deferred slash.
- [ ] Allow root or staking admin to add a custom slash.
- [ ] Test HistoricalSession proof works fine with eras before removing
exposure as full identity.
- [ ] Properly bench offence processing and slashing.
- [ ] Handle Offences::Reports migration when removing validator
exposure as identity.
---------
Co-authored-by: Gonçalo Pestana <[email protected]>
Co-authored-by: command-bot <>
Co-authored-by: Kian Paimani <[email protected]>
Co-authored-by: Guillaume Thiolliere <[email protected]>
Co-authored-by: kianenigma <[email protected]>
Co-authored-by: Giuseppe Re <[email protected]>
Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>1 parent 6b6dae8 commit dda2cb5
File tree
24 files changed
+1605
-1061
lines changed- polkadot/runtime
- test-runtime/src
- westend/src
- weights
- prdoc
- substrate
- bin/node/runtime/src
- frame
- babe/src
- beefy/src
- election-provider-multi-phase/test-staking-e2e/src
- grandpa/src
- offences/benchmarking/src
- root-offences/src
- session/benchmarking/src
- staking/src
- pallet
24 files changed
+1605
-1061
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
322 | 322 | | |
323 | 323 | | |
324 | 324 | | |
325 | | - | |
326 | | - | |
| 325 | + | |
| 326 | + | |
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1871 | 1871 | | |
1872 | 1872 | | |
1873 | 1873 | | |
| 1874 | + | |
1874 | 1875 | | |
1875 | 1876 | | |
1876 | 1877 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
805 | 805 | | |
806 | 806 | | |
807 | 807 | | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
808 | 812 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
676 | 676 | | |
677 | 677 | | |
678 | 678 | | |
679 | | - | |
680 | | - | |
681 | 679 | | |
682 | 680 | | |
683 | 681 | | |
| |||
712 | 710 | | |
713 | 711 | | |
714 | 712 | | |
715 | | - | |
716 | | - | |
717 | | - | |
| 713 | + | |
718 | 714 | | |
719 | | - | |
720 | | - | |
721 | | - | |
722 | | - | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
723 | 718 | | |
724 | 719 | | |
725 | | - | |
726 | | - | |
727 | | - | |
728 | | - | |
729 | | - | |
730 | | - | |
731 | | - | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
732 | 724 | | |
733 | 725 | | |
734 | | - | |
735 | | - | |
736 | | - | |
737 | | - | |
738 | | - | |
739 | | - | |
740 | | - | |
741 | | - | |
| 726 | + | |
742 | 727 | | |
743 | 728 | | |
744 | 729 | | |
745 | 730 | | |
746 | 731 | | |
747 | | - | |
| 732 | + | |
748 | 733 | | |
749 | 734 | | |
750 | | - | |
| 735 | + | |
751 | 736 | | |
752 | 737 | | |
753 | 738 | | |
754 | 739 | | |
755 | 740 | | |
756 | | - | |
757 | | - | |
758 | | - | |
759 | | - | |
760 | | - | |
| 741 | + | |
761 | 742 | | |
762 | 743 | | |
763 | 744 | | |
764 | 745 | | |
765 | 746 | | |
766 | | - | |
767 | | - | |
768 | | - | |
769 | | - | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
770 | 750 | | |
771 | 751 | | |
772 | 752 | | |
| |||
790 | 770 | | |
791 | 771 | | |
792 | 772 | | |
793 | | - | |
794 | | - | |
| 773 | + | |
| 774 | + | |
795 | 775 | | |
796 | 776 | | |
797 | 777 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
108 | | - | |
| 107 | + | |
| 108 | + | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
191 | | - | |
192 | | - | |
| 191 | + | |
| 192 | + | |
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
| |||
Lines changed: 3 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
148 | | - | |
149 | | - | |
| 148 | + | |
| 149 | + | |
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| |||
908 | 908 | | |
909 | 909 | | |
910 | 910 | | |
911 | | - | |
912 | | - | |
913 | | - | |
914 | | - | |
| 911 | + | |
915 | 912 | | |
916 | 913 | | |
917 | 914 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
112 | | - | |
| 111 | + | |
| 112 | + | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
173 | 180 | | |
174 | 181 | | |
175 | 182 | | |
| |||
182 | 189 | | |
183 | 190 | | |
184 | 191 | | |
185 | | - | |
| 192 | + | |
186 | 193 | | |
187 | 194 | | |
188 | | - | |
| 195 | + | |
189 | 196 | | |
190 | 197 | | |
191 | 198 | | |
| |||
232 | 239 | | |
233 | 240 | | |
234 | 241 | | |
| 242 | + | |
| 243 | + | |
235 | 244 | | |
236 | 245 | | |
237 | 246 | | |
| |||
266 | 275 | | |
267 | 276 | | |
268 | 277 | | |
| 278 | + | |
| 279 | + | |
269 | 280 | | |
270 | 281 | | |
271 | 282 | | |
| |||
0 commit comments