Repository navigation
feat(contracts): cooperative close on PaymentChannel (fast settle, no window) - #948
Conversation
… window) (#947) Adds `cooperativeClose(channelId, amount, nonce, bytesDelivered, clientVoucherSig, providerCloseSig)`: settles a channel in one transaction with no dispute window when both parties sign the same final state. The client voucher caps the provider's claim; the provider's new EIP-712 `CooperativeClose` waiver attests it holds no higher voucher and waives the window. With both signatures over one tuple there is nothing to dispute, so the provider's `amount - withdrawnAmount` routes and the client's `deposit - amount` refunds in the same tx. Removes the asymmetry where the provider had a window-free path to its earnings (`withdraw`) but the client's unused-deposit refund waited the full 48h. Purely additive: if the node is offline or declines, the client falls back to the existing `closeChannel` path — no locked funds, no enforcement. Finality rests on the on-chain `claimed*` watermark, not the off-chain signature: a stale waiver below it reverts and can never under-settle. `Open`-only; mirrors `settleChannel`'s paused-router defer branch so a paused `FeeRouter` cannot freeze the client refund. Amends ADR 003 (§ Cooperative close, node-side discipline, off-chain waiver exchange). Rust wire variants + `crates/incentive` waiver signing + node-side guard follow per the ADR. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Contracts coverageLine coverage: 89.84% (2122/2362 lines) |
Gas snapshotGas changes vs baseRun: 27834944779 |
There was a problem hiding this comment.
Code Review
This pull request introduces a cooperative close mechanism to the payment channel, allowing immediate settlement without a dispute window when both parties agree on the final state. This is implemented via the cooperativeClose function in PaymentChannel.sol and documented in ADR 003, with accompanying unit tests. The reviewer suggested a valuable gas optimization in cooperativeClose by caching storage variables and reusing stack variables to avoid redundant storage reads.
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
Adds a new “cooperative close” settlement path to the Solidity PaymentChannel contract, allowing a channel to be closed and refunded immediately (no dispute window) when both client and provider sign the same final state. This addresses the client-refund latency asymmetry vs. the provider’s already-window-free withdraw path.
Changes:
- Added
PaymentChannel.cooperativeClose(...)plus a new EIP-712COOPERATIVE_CLOSE_TYPEHASHand provider-waiver signature verification. - Added a new
ChannelCooperativelyClosedevent and errorInvalidCooperativeCloseSignature. - Added a new ADR section documenting cooperative close, and added a comprehensive Foundry test suite for the new flow.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| contracts/src/PaymentChannel.sol | Implements cooperativeClose, provider waiver verification, and emits a new cooperative-close event. |
| contracts/test/PaymentChannel.t.sol | Adds helpers for signing the waiver + 10 new cooperative-close tests covering happy paths and failure modes. |
| adr/003-payments.md | Documents the cooperative-close protocol, safety properties, and off-chain exchange flow. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…router (#947) Address PR review: the paused-FeeRouter defer branch copied from settleChannel is unnecessary here. settleChannel must defer because it acts on a Closing channel where a revert would strand the refund (no path back to Open for reclaimExpired). cooperativeClose is atomic Open -> Closed: if _route reverts under a paused router the whole call rolls back, the channel stays Open, and nothing is stranded — the caller retries post-unpause or falls back to closeChannel. This mirrors withdraw's posture (#890). Drops the defer branch and its deferred-settlement bookkeeping from cooperativeClose; corrects the now-misleading "refund already left" comment and the @notice (both legs land in one tx only on success). Test updated to assert revert-then-succeed-after-unpause instead of defer-then-flush. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed in f30f1f6. The second comment was a good catch that went beyond docs: |
…veClose Cache `ch.client`/`ch.provider` into local stack vars and reuse the `amount`/`bytesDelivered` arguments instead of re-reading `ch.claimedAmount`/`ch.claimedBytes` after `_advanceClaimWatermark` (which sets the watermark to the agreed tuple). Avoids several SLOADs per cooperative close. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…y vector (#1009) (#1010) * test(incentive,contracts): pin cooperative-close EIP-712 waiver parity vector (#1009) The cooperative-close waiver (PRs #948/#949/#957/#973) is signed off-chain in Rust and verified on-chain by PaymentChannel, with the EIP-712 type and domain hand-duplicated on each side. Nothing pinned the full typed-data digest across both, so a drift in the domain (name/version) or struct encoding would produce unsettleable waivers with no test failure — the existing coverage only pinned the typehash string (Rust) and round-tripped sign/recover within each language. Commit one canonical parity vector and assert it from both sides: - Rust: EXPECTED_COOP_CLOSE_DIGEST + cooperative_close_digest_matches_committed_vector asserting sample_close().signing_hash(sample_domain()) equals it. - Solidity: test_cooperativeClose_digestMatchesVector recomputes the digest with the contract's public COOPERATIVE_CLOSE_TYPEHASH and the fixed domain, and test_cooperativeClose_domainMatchesVector ties the deployed contract's ERC-5267 eip712Domain() name/version to the vector. Verified the vector catches the drift class: bumping the domain version on either side fails the corresponding test. Test-only; no production code changed. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * test(contracts): refresh .gas-snapshot for cooperative-close vector tests The #1010 vector tests add two functions plus fixtures to PaymentChannel.t.sol, enlarging that suite's test-contract bytecode and shifting per-test deployment gas across PaymentChannelTest, plus the two new entries (test_cooperativeClose_digestMatchesVector, test_cooperativeClose_domainMatchesVector). Regenerated via `FOUNDRY_PROFILE=ci forge snapshot --snap .gas-snapshot`; non-reproducible fuzz `runs:` lines left at committed values (the CI gate strips them). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Closes #947.
What
Adds
cooperativeClose(channelId, amount, nonce, bytesDelivered, clientVoucherSig, providerCloseSig)toPaymentChannel— settles a channel in one transaction with no dispute window when both parties sign the same final state.CooperativeClosewaiver (new EIP-712COOPERATIVE_CLOSE_TYPEHASH) attests it holds no higher voucher and waives the window.With both signatures over one tuple there is nothing to dispute, so the provider's
amount − withdrawnAmountroutes throughFeeRouterand the client'sdeposit − amountrefunds in the same tx. The fully-paid case (provider alreadywithdraw-drained) collapses to an instant client refund with no router call.Why
The provider already had a window-free path to its earnings (
withdraw); the client's unused-deposit refund had to wait the full 48h dispute window even when both parties agreed. This closes that asymmetry. No funds are locked: cooperative close is purely additive — if the node is offline or declines, the client falls back to the existingcloseChannel→ window →settleChannelpath. No enforcement, timeout, or escrow.Safety
claimed*watermark, not the off-chain signature: a stale waiver below the watermark reverts (AmountRegression/NonMonotonicNonce) and can never under-settle. TerminalClosedset atomically, so exactly one settlement wins any race.Open-only — a channel already in the window settles viasettleChannel.settleChannel's defer branch so a pausedFeeRouternever freezes the client refund._verifyVoucher/_advanceClaimWatermark/_requireBytesTrackPayment/_routemachinery and the same slither/aderyn directives as the sibling signature-verifying functions.Tests
10 new
cooperativeClosetests (happy path, fully-withdrawn refund, either-party submit, both bad-sig paths, wrong-typehash waiver, stale-waiver-cannot-under-settle, not-open guard, paused-router defer+flush). Full suite: 96 passing.Scope / follow-up
ADR 003 (§ Cooperative close) + Solidity land together. The Rust off-chain side — two
cdn/client/v1ClientMessagevariants (CooperativeCloseRequest/CooperativeCloseAuth),crates/incentivewaiver signing, and the node-side "don't sign mid-stream / stop accepting payment after signing" guard — follows per the ADR.CI note
forge build(CI profile,--deny warnings),forge fmt --check,forge test, and aderyn are clean locally. slither is not installed in my local env, so it was not run; the new function mirrorswithdraw/closeChannel/disputeChannel(same external-call-then-state-write shape and the same suppression directives), so CI slither is expected to pass — please confirm on CI.🤖 Generated with Claude Code