Repository navigation
fix(contracts): snapshot Governor vote weight to fully-elapsed epochs (#847) - #888
Conversation
…#847) DecdnGovernor read operator served-bytes and totals live from FeeRouter's per-epoch mappings, breaking the OpenZeppelin Governor snapshot invariant: the in-progress epoch's bucket keeps accumulating after a proposal snapshot, so an operator could settle bytes mid-vote and inflate its own already-counted weight, and two voters casting at different times saw different totals/quorum. FeeRouter.routeSettlement only ever writes the current epoch's bucket, so elapsed-epoch buckets are immutable forever. Restrict the vote window to the last fully-elapsed epoch (endEpoch = epoch(t) - 1) via a new underflow-guarded _endEpoch helper applied in _cappedServed, _slashedInWindow, quorum, and proposalThreshold. The slash window tracks the byte window (both end at endEpoch). This restores snapshot determinism with zero FeeRouter/interface/ gas changes. Accepted trade-offs (documented in the ADR 036 amendment), both bounded by one epoch (<=1 week): the in-progress epoch's bytes are excluded from weight and the quorum denominator until it elapses, and a same-epoch slash zeroes a proposal snapshotted earlier in that epoch only once the epoch elapses. Tests: adds the immutability regression (fails on pre-fix source), epoch-0 underflow, in-progress-only-bytes, slash-window lower-boundary, and epoch-0 slashed-operator coverage. Amends ADR 036 (formula, slash-zero-out bound, pseudocode) and the ADR 026 formula mirror. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Gas snapshotGas changes vs baseRun: 27451712145 |
Contracts coverageLine coverage: 89.61% (1837/2050 lines) |
There was a problem hiding this comment.
Code Review
This pull request shifts the voting weight window to end at the last fully-elapsed epoch instead of the in-progress epoch, ensuring that voting weights remain immutable after a proposal snapshot. This change resolves a violation of the OpenZeppelin Governor snapshot invariant. The implementation updates the governance contract, architecture decision records, and includes comprehensive test coverage. Feedback points out a potential underflow issue in proposalThreshold() when clock() is 0, which should be guarded against to prevent non-deterministic behavior.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
Pull request overview
This PR fixes a Governor snapshot-invariant violation in DecdnGovernor: vote weight, quorum, and proposal threshold are made deterministic at a proposal’s snapshot by counting only the last fully-elapsed epoch (excluding the in-progress epoch whose byte bucket can still change).
Changes:
- Add an underflow-guarded
_endEpoch(timepoint)helper and apply it to_cappedServed,_slashedInWindow,quorum, andproposalThresholdto exclude in-progress epoch tallies. - Update and expand
DecdnGovernortests to cover the regression (mid-vote settlement immutability) and epoch-0 / slashing-window edge cases. - Amend ADR 036 and mirror the formula adjustment into ADR 026 documentation.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| contracts/src/DecdnGovernor.sol | Compute window endpoint as the last fully-elapsed epoch and short-circuit epoch-0 snapshots to 0/false to preserve snapshot determinism. |
| contracts/test/DecdnGovernor.t.sol | Shift seeded epochs and add regression/edge-case tests ensuring snapshot immutability and documenting accepted trade-offs. |
| adr/036-served-bytes-voting-weight.md | Document the amendment (endEpoch semantics) and the bounded lag trade-offs for bytes/quorum and same-epoch slashes. |
| adr/026-tokenomics.md | Mirror the updated served-bytes window definition to match ADR 036’s amended formula. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…DR formulas - proposalThreshold: replace the raw `uint48(snapshot)` cast with `snapshot.toUint48()` (SafeCast), matching the `timepoint.toUint48()` reads in quorum/_cappedServed. Resolves the aderyn `Unsafe Casting` high that the helper refactor surfaced; behavior unchanged on-chain (snapshot = clock()-1 <= uint48.max - 1) and the unreachable clock()==0 case now reverts cleanly rather than silently truncating. - ADR 026 / ADR 036: carry the epoch-0 guard inline in the `endEpoch(t)` definitions so the spec matches the guarded Solidity form (review nits). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Security review (governance-weight integrity + arithmetic safety)The change surface is entirely Verdict: no critical/high/medium issues; the change is a net security improvement. Primary property — fixedThe original defect was a timing manipulation: an operator could settle bytes into the in-progress epoch during an active vote to inflate its own already-counted weight. All four read paths now anchor to Quorum ↔ vote-weight consistency — verified
Arithmetic safety
Epoch-0 bootstrap window — verified non-exploitableIn the first epoch ( Accepted residual (LOW, documented)The slash window tracks the byte window, so a slash stamped in the in-progress epoch zeroes a same-epoch-earlier snapshot only once the epoch elapses (≤1-week lag). Bounded by Conclusion: no code changes warranted — strengthens governance integrity, no new attack surface. 🤖 review via blockchain-web3:solidity-security |
The "never reverts" claim held only under the implicit clock() >= 1 precondition (block.timestamp is never 0 on a live chain). Restate it as "never reverts for any reachable timepoint" and note the checked cast is the deliberate backstop. Comment-only; no behavior change. (PR-review nit.) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Summary
Fixes #847.
DecdnGovernor(ADR 036 served-bytes-weighted governor) read operator served-bytes and totals live fromFeeRouter's per-epoch byte mappings, breaking the OpenZeppelinGovernorsnapshot invariant. Since the voting period (7d) and epoch (7d) overlap, the snapshot epoch's bucket keeps accumulating during the vote — so an operator could settle bytes mid-vote and inflate its own already-counted weight, and two voters casting at different times saw different totals/quorum.The governable params (
windowEpochs,voteCapBps,ageRampMonths) were already snapshot-frozen viaTrace208; the raw byte tallies were the one remaining un-snapshotted input.Fix
FeeRouter.routeSettlementonly ever writes the current epoch's bucket, so elapsed-epoch buckets are immutable forever. A new underflow-guarded_endEpoch(timepoint)helper returns the last fully-elapsed epoch (epoch(t) - 1, withhasElapsed = falsewhenepoch(t) == 0), applied consistently in_cappedServed,_slashedInWindow,quorum, andproposalThreshold. The slash window now tracks the byte window (both end atendEpoch). This restores snapshot determinism with zero FeeRouter / interface / ABI / gas changes — the whole production change is inDecdnGovernor.sol.Accepted trade-offs (documented in the ADR 036 amendment)
Both bounded by one epoch (≤ 1 week):
Tests
All Solidity tests pass (530/530). New coverage in
DecdnGovernor.t.sol:test_getVotes_immuneToCurrentEpochSettlement— the core regression; fails on pre-fix source (50050000 != 50000, the exact vote-weight inflation) and passes after.test_getVotes_zeroBeforeFirstElapsedEpoch— epoch-0 underflow guard acrossgetVotes/quorum/proposalThreshold.test_getVotes_inProgressOnlyBytesReadZero— operator with bytes only in the in-progress epoch reads 0 weight + 0 quorum.test_getVotes_slashAtWindowStartBoundary— inclusive lowerwindowStartedge (the off-by-one the shift perturbs).test_getVotes_epoch0_slashedOperatorStillZero— exercises the!hasElapsedbranch of_slashedInWindow.test_slash_inCurrentEpoch_doesNotZeroEarlierSnapshot— pins the accepted same-epoch slash-lag.Existing fixtures shifted to seed the last fully-elapsed epoch.
Docs
+1sentinel), and_getVotespseudocode.Verification
forge fmt --check✅ ·FOUNDRY_PROFILE=ci forge build --sizes --deny warnings✅ (no warnings; DecdnGovernor 18,038 B, well under EIP-170) · full suite 530/530 ✅ ·forge lintclean · slither (fail-on medium) no Governor findings · aderyn introduces no new finding class (replaced 4 identical casts with 1).🤖 Generated with Claude Code