Repository navigation
fix(contracts): recompute slashedAtEpoch over standing slashes on granted appeal (#709) - #728
Conversation
…nted appeal (#709) `CapacityBond._slashedAtEpoch[operator]` is a single per-operator watermark read by `DecdnGovernor` (vote-weight zero-out) and `claimVestedCredit`. On a granted appeal it was cleared based only on the reversed slash, so appealing the most-recent slash wrongly cleared the watermark while an earlier slash still stood — restoring vote weight and credit-claim eligibility for the unresolved slash (governance/claim bypass, prod-blocking). Fix (issue #709 Option 1): add a per-operator `_operatorSlashIds` index and recompute the watermark on `settleAppealGranted` to the most-recent still-standing (non-`Reversed`) slash, clearing to zero only when none remain. Since slashes append in non-decreasing epoch order, the recompute tail-scans and breaks at the first standing slash. Removes the `SlashedAtEpochCleared` event (recompute emits `SlashedAtEpochStamped(op, 0)` as the clear sentinel). To fit the change under EIP-170 (CapacityBond sits near the size ceiling and the test harness had ~9 bytes headroom), extract `_reduceStakeAtTier`'s arithmetic into a new internal `StakeMath` library (behaviorally equivalent) and retire the oversized `TestableCapacityBond` harness — the clip/unbonding branches now unit-test the pure library directly. Amend ADR 036 § Slashing zero-out (multi-slash semantics), ADR 028, and the glossary. Add multi-slash recompute tests (fall-back, reverse-middle-of-3, clear sentinel via expectEmit, claimVestedCredit gate) and StakeMath branch tests; regenerate the gas snapshot. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Contracts coverageLine coverage: 87.67% (1693/1931 lines) |
Gas snapshotGas changes vs baseRun: 26689035389 |
There was a problem hiding this comment.
Code Review
This pull request addresses a vulnerability in the multi-slash watermark mechanism (issue #709) where granting a newer slash appeal would incorrectly clear the slashedAtEpoch watermark even if older slashes remained outstanding. To fix this, CapacityBond now tracks all slash IDs per operator and recomputes the watermark to the maximum epoch of any remaining still-standing slashes upon a successful appeal. Additionally, the tier-based stake reduction logic has been refactored into a new pure library, StakeMath, to facilitate direct unit testing, and comprehensive tests have been added to verify the multi-slash recomputation behavior. I have no further feedback to provide as the changes are well-implemented and thoroughly tested.
There was a problem hiding this comment.
Pull request overview
Updates the slashing “zero-out” watermark semantics in CapacityBond to correctly handle multiple outstanding slashes when an appeal is granted, preventing vote-weight / credit-claim eligibility from being restored while earlier slashes still stand (issue #709).
Changes:
- Add a per-operator slashId index and recompute
slashedAtEpochon granted appeals by scanning for the most recent non-Reversedslash (clearing to0only when none stand). - Extract tier-based stake reduction arithmetic into a new pure
StakeMathlibrary and refactor tests to exercise the library directly (removing the oversized test harness). - Update unit/E2E tests and ADR documentation to reflect the multi-slash watermark semantics and the “
SlashedAtEpochStamped(op, 0)” clear sentinel.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| contracts/src/CapacityBond.sol | Implements per-operator slash indexing and watermark recomputation on granted appeals; refactors stake-reduction math to use StakeMath. |
| contracts/src/StakeMath.sol | Introduces a pure library encapsulating tier-based stake reduction logic formerly inlined in CapacityBond. |
| contracts/test/CapacityBond.t.sol | Adds multi-slash recompute regression tests and replaces harness-based stake-reduction tests with pure-library tests. |
| contracts/test/SlashAppeal.t.sol | Adds regression test ensuring granting the newest appeal falls back to an older standing slash’s watermark. |
| contracts/test/E2EProtocol.t.sol | Updates E2E assertions to reflect recompute semantics (watermark persists while other slashes stand). |
| contracts/.gas-snapshot | Refreshes gas snapshot for new/updated tests. |
| adr/036-served-bytes-voting-weight.md | Documents recompute-based watermark semantics for multi-slash scenarios. |
| adr/028-slashing-appeals.md | Updates appeal-settlement documentation to “recompute” rather than unconditional clear. |
| adr/glossary.md | Updates glossary entries to match recompute semantics and removes stale _clearSlashedAtEpoch references. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Context
Fixes #709.
CapacityBond._slashedAtEpoch[operator]is a single per-operator watermark (epoch+1 encoded; 0 = unslashed) read byDecdnGovernor._getVotes(vote-weight zero-out) andclaimVestedCredit(credit-claim gate) per ADR 036 § Slashing zero-out.After #708,
settleAppealGrantedcleared the watermark only when the current stamp matched the reversed slash — fixing "appeal the older slash." The remaining hole: appealing the most-recent slash cleared the watermark to0even when an earlier slash still stood, wrongly restoring vote weight and credit-claim eligibility for the unresolved slash. Labelledsecurity/high/prod-blocking.Fix (issue Option 1 — exactly correct)
_operatorSlashIdsindex (pushed in_mintSlashRecord).settleAppealGrantednow calls_recomputeSlashedAtEpoch, which re-derives the watermark to the most-recent still-standing (non-Reversed) slash, clearing to0only when none remain. Only a granted appeal (Reversed) removes a slash from the standing set;Escrowed/AppealOpen/Upheldall still stand.slashedAt = block.timestamp), so the recompute tail-scans and breaks at the first standing slash — O(1) common case.SlashedAtEpochClearedevent (no ABI consumers); recompute emitsSlashedAtEpochStamped(op, 0)as the clear sentinel.DecdnGovernorandclaimVestedCreditread the now-correct watermark unchanged.Size constraint →
StakeMathextractionCapacityBondsits near the EIP-170 ceiling and the test-onlyTestableCapacityBondharness had ~9 bytes of headroom onmain, so the loop tipped it over (productionCapacityBondstill fit). Per maintainer decision, extracted_reduceStakeAtTier's arithmetic verbatim into a new internalStakeMathlibrary (behaviorally equivalent), retired the oversized harness, and re-pointed its two clip/unbonding tests at the pure library. Net:CapacityBondactually shrank (+110 B margin).Docs
Amended ADR 036 § Slashing zero-out with the multi-slash watermark semantics, plus ADR 028 and the glossary (removing stale "unconditional clear" /
_clearSlashedAtEpochreferences).Tests
test_grant_newerAppeal_fallsBackToOlderStandingSlash,test_settleAppealGranted_multiSlash_recomputesThenClears,test_settleAppealGranted_reverseMiddleSlash_keepsNewest,test_claimVestedCredit_multiSlash_gatedByOlderStandingSlash.expectEmitassertion locking theSlashedAtEpochStamped(op, 0)clear sentinel.StakeMathbranch tests: active-only, exactslashAmount == activeboundary, partial spill, zero/tier-0 no-ops.registerSettleSlashEjectAppealassertion (it had encoded the old unconditional-clear bug while two slashes still stood).Verification
forge fmt --check,FOUNDRY_PROFILE=ci forge build --sizes --deny warnings, 440forge test, aderyn (High=0), slither (exit 0) all green;.gas-snapshotregenerated.🤖 Generated with Claude Code