Skip to content

feat(node): cooperative-close provider side — sign waiver + stop serving - #952

Closed
alpergundogdu wants to merge 2 commits into
claude/coop-close-protocolfrom
claude/coop-close-node
Closed

alpergundogdu wants to merge 2 commits into
claude/coop-close-protocolfrom
claude/coop-close-node

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Part of #947. PR 2 of ~3 — the node (provider) half of cooperative close. Stacked on #949 (base = claude/coop-close-protocol); review/merge that first. ADR 003 §Cooperative close.

What the node does

  • Answers a waiver request. On cdn/client/v1, a CooperativeCloseRequest { channel_id } is dispatched (alongside the existing StreamRequest path) to handle_cooperative_close: it signs a CooperativeClose waiver over the channel's current voucher watermark (last_amount/nonce/bytes) with the node's eth key and replies CooperativeCloseAuth. Best-effort — unknown channel or no voucher yet (nonce == 0) finishes with no auth and the client falls back to closeChannel.

  • Stops serving after signing (your "refuse payment after signing" guard). A persisted cooperative_close_signed flag on ChannelState. Once set, the node:

    • refuses new StreamRequests (ServeRejectReason::CooperativeCloseSigned → NotFound, wire-indistinguishable from an unknown channel), and
    • rejects in-flight vouchers (VoucherRejectReason::CooperativeCloseSigned).

    Gating at both points means signing mid-delivery just stops the in-flight stream at its next voucher (bounded by one interval) — so no separate "don't sign while delivering" interlock is needed, which is why I collapsed your two guards into one persisted flag.

Persistence

The flag rides as an additive trailing postcard segment after the v2 trailer in channels.redb — no schema bump, defaults false for records written before it existed (old binaries ignore the extra byte; new binaries default-false a missing one). mark_cooperative_close_signed uses the same clone-record-swap durability discipline as apply_voucher (#527): persist before the waiver is returned, so a store failure leaves the channel serveable.

Tests

  • redb: flag round-trips across reopen; a record with no coop segment decodes false.
  • end-to-end loopback: the waiver recovers to the node's eth key over the on-chain CooperativeClose typed data, the flag persists, and a subsequent delivery request is refused (blob present, so it would otherwise serve).

cargo nextest run -p decdn-incentive -p decdn-protocol -p decdn-node → 951 passing; cargo clippy --workspace --all-targets + cargo fmt --check clean.

Note

Adds VoucherRejectReason::CooperativeCloseSigned to the protocol enum (handler-emitted-only, like Expired/RetryLater) — a small protocol-crate addition that logically extends #949.

🤖 Generated with Claude Code

…ing (#947)

Implements the node (provider) half of cooperative close (ADR 003 §Cooperative
close), on top of the wire + EIP-712 foundation (#949).

- Handler answers a CooperativeCloseRequest on cdn/client/v1 by signing a
  CooperativeClose waiver over the channel's current voucher watermark
  (last_amount/nonce/bytes) with the node's eth key and replying with a
  CooperativeCloseAuth. Best-effort: unknown channel or no-voucher-yet
  (nonce 0) finishes with no auth and the client falls back to closeChannel.

- Self-protection (the "refuse payment after signing" guard): a persisted
  cooperative_close_signed flag on ChannelState. Once set, the node refuses
  new StreamRequests (ServeRejectReason::CooperativeCloseSigned -> NotFound,
  wire-indistinguishable) and rejects in-flight vouchers
  (VoucherRejectReason::CooperativeCloseSigned). Checking the flag at both the
  request gate and the voucher gate bounds any signed-mid-delivery loss to one
  voucher interval, so no separate "don't sign while delivering" interlock is
  needed.

- Persistence: the flag rides as an additive trailing postcard segment after
  the v2 trailer in channels.redb — no schema bump, defaults false for records
  written before it existed (forward/backward compatible).

- mark_cooperative_close_signed uses the same clone-record-swap durability
  discipline as apply_voucher (#527): persist before the waiver is returned.

Tests: redb round-trip across reopen, no-segment-decodes-false, and an
end-to-end loopback (waiver recovers to the node key over the on-chain typed
data, flag persists, subsequent delivery refused). 951 crate tests pass;
workspace clippy + fmt clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings June 18, 2026 18:19

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements the cooperative-close flow (ADR 003 §Cooperative close) by adding a cooperative_close_signed flag to ChannelState, persisting it across restarts, and refusing subsequent delivery requests once signed. The feedback highlights three important issues: first, decoding the cooperative-close flag should only occur when the schema version is 2 or greater to prevent corruption of older records; second, the synchronous disk write in mark_cooperative_close_signed should be offloaded to tokio::task::spawn_blocking to avoid blocking the async executor; and third, the method should return early if the flag is already set to prevent redundant and expensive disk writes.

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.

Comment thread crates/node/src/channel_store.rs Resolved
Comment thread crates/node/src/handlers/client.rs Resolved
Comment thread crates/incentive/src/channel.rs Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements the provider/node half of cooperative close (ADR 003): the node can sign and return a cooperative-close waiver at the channel’s current voucher watermark, persist a “signed” flag, and stop serving/accepting vouchers on that channel thereafter.

Changes:

  • Add CooperativeCloseSigned as a new VoucherRejectReason and emit it from the node when a cooperative-close waiver has been signed.
  • Add a cdn/client/v1 handler path for CooperativeCloseRequest → CooperativeCloseAuth, including signing + persisting a cooperative-close flag and gating both new streams and in-flight voucher collection.
  • Persist the cooperative-close flag in channels.redb as an additive trailing postcard segment, plus metrics + tests (redb round-trip + loopback end-to-end).

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
crates/protocol/src/client.rs Extends VoucherRejectReason with CooperativeCloseSigned and updates discriminant-order tests.
crates/node/src/handlers/client.rs Dispatches CooperativeCloseRequest, signs waiver, persists flag, and gates serving/voucher collection when flagged.
crates/node/src/channel_store.rs Persists the cooperative-close flag as an optional trailing postcard bool segment; adds reopen/back-compat tests.
crates/node/src/metrics.rs Adds a per-reason counter for stream refusal due to cooperative-close flag.
crates/node/tests/client_loopback.rs Adds end-to-end loopback test verifying waiver signature, persistence, and post-sign serving refusal.
crates/incentive/src/channel.rs Adds cooperative_close_signed state + persisted setter/getter on ChannelState.
crates/incentive/src/store.rs Updates in-memory store tests for the extended ChannelState::hydrate signature/round-trip.
crates/node/src/admin.rs Updates admin-channel test fixtures to include the new hydrate parameter.
crates/cli/tests/admin_peers.rs Updates CLI admin tests’ channel fixtures for the new hydrate parameter.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/node/src/handlers/client.rs Resolved
…read fsync, idempotent retry

- channel_store: gate the coop-close bool decode on schema_version >= 2 so
  v1 records' trailing bytes are never mis-decoded as the flag
- client handler: run mark_cooperative_close_signed on spawn_blocking, matching
  apply_voucher / update_channel_deposit (no fsync on a runtime worker)
- incentive: early-return Ok(()) when already flagged, avoiding a redundant
  fsync on retried CooperativeCloseRequest

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu deleted the branch claude/coop-close-protocol June 19, 2026 16:02
alpergundogdu added a commit that referenced this pull request Jun 19, 2026
…gned

#952 adds VoucherRejectReason::CooperativeCloseSigned to the public surface;
the snapshot wasn't refreshed against main (it passed only vs the old stacked
base). Additive-only regeneration.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu

Copy link
Copy Markdown
Contributor Author

Superseded by #957. GitHub auto-closed this PR (and refused to reopen it) when its stacked base branch claude/coop-close-protocol was deleted on the merge of #949. #957 is the same claude/coop-close-node branch rebased onto main with the public-API snapshot regenerated; the review fixes here landed in commit f625e51.

alpergundogdu added a commit that referenced this pull request Jun 19, 2026
…gned

#952 adds VoucherRejectReason::CooperativeCloseSigned to the public surface;
the snapshot wasn't refreshed against main (it passed only vs the old stacked
base). Additive-only regeneration.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
alpergundogdu added a commit that referenced this pull request Jun 19, 2026
…ing (#957)

* feat(node): cooperative-close provider side — sign waiver + stop serving (#947)

Implements the node (provider) half of cooperative close (ADR 003 §Cooperative
close), on top of the wire + EIP-712 foundation (#949).

- Handler answers a CooperativeCloseRequest on cdn/client/v1 by signing a
  CooperativeClose waiver over the channel's current voucher watermark
  (last_amount/nonce/bytes) with the node's eth key and replying with a
  CooperativeCloseAuth. Best-effort: unknown channel or no-voucher-yet
  (nonce 0) finishes with no auth and the client falls back to closeChannel.

- Self-protection (the "refuse payment after signing" guard): a persisted
  cooperative_close_signed flag on ChannelState. Once set, the node refuses
  new StreamRequests (ServeRejectReason::CooperativeCloseSigned -> NotFound,
  wire-indistinguishable) and rejects in-flight vouchers
  (VoucherRejectReason::CooperativeCloseSigned). Checking the flag at both the
  request gate and the voucher gate bounds any signed-mid-delivery loss to one
  voucher interval, so no separate "don't sign while delivering" interlock is
  needed.

- Persistence: the flag rides as an additive trailing postcard segment after
  the v2 trailer in channels.redb — no schema bump, defaults false for records
  written before it existed (forward/backward compatible).

- mark_cooperative_close_signed uses the same clone-record-swap durability
  discipline as apply_voucher (#527): persist before the waiver is returned.

Tests: redb round-trip across reopen, no-segment-decodes-false, and an
end-to-end loopback (waiver recovers to the node key over the on-chain typed
data, flag persists, subsequent delivery refused). 951 crate tests pass;
workspace clippy + fmt clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* fix(node,incentive): coop-close review fixes — v1 flag decode, off-thread fsync, idempotent retry

- channel_store: gate the coop-close bool decode on schema_version >= 2 so
  v1 records' trailing bytes are never mis-decoded as the flag
- client handler: run mark_cooperative_close_signed on spawn_blocking, matching
  apply_voucher / update_channel_deposit (no fsync on a runtime worker)
- incentive: early-return Ok(()) when already flagged, avoiding a redundant
  fsync on retried CooperativeCloseRequest

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* test(protocol): regenerate public-API snapshot for CooperativeCloseSigned

#952 adds VoucherRejectReason::CooperativeCloseSigned to the public surface;
the snapshot wasn't refreshed against main (it passed only vs the old stacked
base). Additive-only regeneration.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants