Repository navigation
feat(contracts): enumerate deferred PaymentChannel settlements on-chain (#902) - #908
Conversation
…in (#902) Replace the `settlementDeferred` `mapping(bytes32 => bool)` with an OZ `EnumerableSet.Bytes32Set` so a keeper can list and drain pending provider-leg settlements without replaying the `SettlementDeferred` event log. Previously a deferral whose event no indexer captured (logs pruned or missed during the pause window) was invisible on-chain and the provider's share could sit indefinitely with no automatic sweep. - Preserve the public ABI `settlementDeferred(bytes32) -> bool` via an explicit view over `.contains()`, keeping the prior flush/defer/retry tests green as a regression anchor for the storage swap. - Add in the `settleChannel` defer branch; remove in `flushDeferredSettlement` via atomic `if (!remove(id)) revert`, preserving the checks-effects-interactions ordering (a paused router reverts the whole tx and restores the set entry for retry). - Add `deferredSettlementCount()` and a paginated `deferredSettlements(offset, limit)` view following the `CapacityBond.getActiveNodes` idiom; document the swap-and-pop ordering instability and the robust offset-0 drain. PaymentChannel is constructor-deployed (no proxy), so the mapping->struct storage-layout shift is safe. Slither `unused-return` on the guaranteed-true `.add` is suppressed with the documented invariant. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request replaces the settlementDeferred mapping with an EnumerableSet to allow on-chain enumeration of deferred settlements. It introduces paginated querying via deferredSettlements and helper functions for counting and checking deferred settlements, along with comprehensive unit tests. Feedback was provided to refactor the pagination logic in deferredSettlements to prevent potential overflow reverts when a very large limit is passed alongside a non-zero offset.
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.
Gas snapshotGas changes vs baseRun: 27515727729 |
Contracts coverageLine coverage: 89.61% (2071/2311 lines) |
There was a problem hiding this comment.
Pull request overview
This PR adds on-chain enumeration for deferred PaymentChannel settlements so keepers can list and drain pending provider settlement legs from contract state (without relying on replaying SettlementDeferred events).
Changes:
- Replace the non-enumerable
settlementDeferredmapping with anEnumerableSet.Bytes32Setas the single source of truth. - Preserve the legacy
settlementDeferred(bytes32) -> boolread ABI via an explicit view, while addingdeferredSettlementCount()and paginateddeferredSettlements(offset, limit). - Add tests covering enumeration, pagination, and lifecycle interactions (defer/flush).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| contracts/src/PaymentChannel.sol | Tracks deferred settlements in an enumerable set; adds count + pagination views; preserves legacy per-id read. |
| contracts/test/PaymentChannel.t.sol | Adds tests validating deferred-set enumeration behavior and pagination boundary cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…imit overflow (#902) Address PR #908 review (Gemini + Copilot): a max `limit` at a non-zero `offset` reverted on the checked `offset + limit` add instead of clamping. Compute `size = min(limit, len - offset)` directly — never forming `offset + limit` — so a defensive `limit == type(uint256).max` clamps to the remaining tail from any offset. `len - offset` cannot underflow given the `offset >= len` guard, and `at(offset + i)` stays in bounds. Also reword the `flushDeferredSettlement` NatSpec to state plainly that a reverted (still-paused) retry keeps the channel in the deferred set and a successful flush removes it (later calls then revert `NoDeferredSettlement`). Add a test pinning the clamp-not-revert behavior at a non-zero offset. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Property test over the full (offset, limit) domain, including type(uint256).max extremes: the returned page must equal the matching slice of the canonical full enumeration. Pins the overflow-safe length clamp, at(offset + i) indexing, and no duplicates/gaps in one invariant. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Summary
Closes #902. Adds on-chain enumeration of deferred
PaymentChannelsettlements so a keeper can list and drain pending provider-leg settlements without replaying theSettlementDeferredevent log.PR #889 (fix for #849) introduced a deferred-settlement path: when
FeeRouteris paused,settleChannelstill pays the client refund and closes the channel but parks the provider's settle leg undersettlementDeferred[channelId]for a later permissionlessflushDeferredSettlement. That mapping had no on-chain enumeration — a deferral whose event no indexer captured (logs pruned/missed during the pause window) was invisible on-chain, and the provider's share could sit indefinitely with no automatic sweep. This change makes the pending set readable and drainable directly from chain state.Changes
mapping(bytes32 => bool) settlementDeferredwithEnumerableSet.Bytes32Set _deferredSettlements(single source of truth; matches theOriginAssignment.solidiom).settlementDeferred(bytes32) -> boolvia an explicit view delegating to.contains()— keeps all prior flush/defer/retry tests green as a regression anchor.settleChanneldefer branch.add()s the id;flushDeferredSettlementuses atomicif (!_deferredSettlements.remove(id)) revert NoDeferredSettlement(), preserving the checks-effects-interactions ordering (a paused router reverts the whole tx and restores the set entry for retry).deferredSettlementCount()and a paginateddeferredSettlements(offset, limit)view following theCapacityBond.getActiveNodesidiom;@devdocuments the swap-and-pop ordering instability and the robust offset-0 drain pattern.Safety notes
PaymentChannelis constructor-deployed (new PaymentChannel(...), no proxy/UUPS), so the mapping→struct storage-layout shift is safe — no persistent proxy state to corrupt..add()return is safe: channel status is monotonic (Open→Closing→Closed) andClosedis written before the add, so achannelIdreaches that line at most once. Suppressed with// slither-disable-next-line unused-returnplus the documenting comment.Tests
7 new tests in
PaymentChannel.t.sol: enumeration on defer, cleared on flush, multiple deferrals + selective flush, page-contents partition across offsets (provesat(offset+i)indexing),(0, type(uint256).max)drain sentinel, pagination boundaries, and zero-amount no-deferral. The ~8 pre-existing deferred-settlement tests pass unchanged via the preserved view.Verification
All run from
contracts/:forge fmt --check✅FOUNDRY_PROFILE=ci forge build --sizes --deny warnings✅ (PaymentChannel runtime well under EIP-170)forge test✅ 600 passed (83 inPaymentChannel, 7 new)slither . --config-file slither.config.json✅ exit 0aderyn -o /tmp/aderyn.md --no-snippets --skip-update-check✅ High: 0pre-commit run --all-files✅ (forge-fmt + solhint)🤖 Generated with Claude Code