Skip to content

feat(node): idle buyer-channel cooperative-close reconcile sweep - #987

Merged
thiras merged 2 commits into
mainfrom
claude/coop-close-reconcile
Jun 25, 2026
Merged

thiras merged 2 commits into
mainfrom
claude/coop-close-reconcile

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #972. The final leg of cooperative close: the node, as a buyer on node→node cache-miss pulls (#744), now reclaims its deposit on idle channels early via cooperative close, instead of waiting out the on-chain expiry (default 90 days). Reuses the client core from #973. Base = main.

Behavior

Folds into the existing hourly buyer sweep. Per non-expired channel:

  • Active → skip. "Idle" = no voucher-nonce progress for RECONCILE_IDLE_SWEEPS (~24h) consecutive sweeps, tracked in memory (observe_idle). Any nonce progress resets the tally, so a channel we're still pulling from is never closed out from under the workload (your first constraint). In-memory state re-seeds on restart — a safe bias (nothing closed until observed idle post-restart).
  • Idle + provider reachable → cooperative close (one tx, no dispute window). On Settled the local record is forgotten and buyer_reconcile_settled is bumped.
  • Idle + unreachable / declined / reverted → left for the existing expiry-reclaim sweep (the safety net). Never-paid (nonce 0) and already-expired channels are skipped outright.

Two new primitives

  • NodeAddressResolver::node_id_for(addr) — reverse of address_of. The buyer store keys channels by the provider's operator address, but dialing for a waiver needs its NodeId. O(n) scan (hourly, few channels). A deregistered provider isn't found → treated as unreachable.
  • BuyerReconcileConfig (endpoint + resolver), threaded through BuyerChannelService::bootstrap, gated on node→node pull-through being enabled (resolver present). Absent → reconcile disabled, the proven expiry-reclaim path runs alone, untouched.

Scope calls (per discussion)

  • Unreachable → unilateral pre-expiry closeChannel→settleChannel is deferred (follow-up filed). Buyer-side unilateral close/window-tracking doesn't exist yet and the expiry sweep already recovers those deposits, just later — not worth doubling this PR's surface.
  • Idle horizon is a const, not config — YAGNI; a config knob would mean churn across many full-literal config constructors for a value operators rarely tune. Promote later if needed.

Tests

observe_idle (first-sight not idle / idle at threshold / reset on progress) and the reverse lookup (resolve + miss). cargo nextest run -p decdn-node -p decdn-incentive → 843 passing; workspace clippy + fmt + doc clean. (The anvil e2e buyer-bootstrap calls take the new None reconcile arg; reconcile itself isn't exercised there — its decision logic is covered by the pure-function tests.)

🤖 Generated with Claude Code

The node opens a buyer-side PaymentChannel against each upstream provider it
pulls from on a cache miss (#744). Until now an idle channel's deposit stayed
locked until on-chain expiry (default 90 days) — the existing reclaim sweep only
acts after expiry. This adds an idle-reconcile sweep that cooperatively closes
idle channels early (reusing the #971 client core), reclaiming the deposit in
one tx with no dispute window.

Behavior (folds into the existing hourly buyer sweep cadence):
- ACTIVE channels are skipped — idleness is "no voucher-nonce progress for
  RECONCILE_IDLE_SWEEPS (~24h) consecutive sweeps", tracked in-memory across
  sweeps (`observe_idle`). A channel we're still pulling from is never closed
  out from under the workload; any nonce progress resets the tally.
- IDLE + provider reachable → cooperative close; on Settled the local record is
  forgotten and `buyer_reconcile_settled` is bumped.
- IDLE + provider unreachable/declined/reverted → left untouched for the
  existing expiry-reclaim sweep (the safety net). A never-paid (nonce 0) or
  already-expired channel is skipped outright.

Two new primitives:
- `NodeAddressResolver::node_id_for(addr)` — reverse of `address_of`, since the
  buyer store keys channels by the provider's operator address but dialing needs
  its NodeId. O(n) scan (hourly, few channels). A deregistered provider isn't
  found → treated as unreachable.
- The reconcile task wiring (`BuyerReconcileConfig`: endpoint + resolver),
  threaded through `BuyerChannelService::bootstrap` and gated on node→node
  pull-through being enabled (resolver present). Absent → reconcile disabled,
  expiry reclaim runs alone exactly as before.

The unreachable-provider unilateral close→settle path is deferred to a
follow-up; the expiry reclaim already recovers those deposits (just later).

Idle horizon is a const (not config) — YAGNI; promote if an operator needs to
tune it. Tests: `observe_idle` first-sight/threshold/reset; reverse-lookup
resolve+miss. 843 node+incentive tests pass; workspace clippy + fmt + doc clean.

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

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

Adds an “idle buyer-channel reconcile” sweep to the node’s buyer-side PaymentChannel service so node→node cache-miss buyer channels can be cooperatively closed (deposit reclaimed early) after sustained voucher inactivity, rather than waiting for on-chain expiry. This integrates the cooperative-close core from the shared client-pull logic and wires it into the node runtime only when node→node pull-through provides the necessary dial/resolution plumbing.

Changes:

  • Thread optional reconcile dial config into BuyerChannelService::bootstrap and spawn an hourly idle-reconcile loop that cooperatively closes long-idle, non-expired, paid channels.
  • Extend NodeAddressResolver with node_id_for(address) reverse lookup to map provider operator addresses back to dialable NodeIds.
  • Add a new metric counter for reconcile-settled channels and update e2e wiring/tests to pass the new optional reconcile argument.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/node/src/buyer_channel.rs Adds reconcile config + idle observation policy + background reconcile loop that attempts cooperative close for idle buyer channels.
crates/node/src/dht/node_address.rs Extends resolver trait with reverse lookup (node_id_for) and implements it for static + chain-backed directories; adds unit tests.
crates/node/src/runtime/mod.rs Wires BuyerReconcileConfig into buyer-channel bootstrap only when a node-address resolver is available.
crates/node/src/metrics.rs Adds buyer_reconcile_settled counter + bump method for successful cooperative-close reconciliations.
crates/node/tests/anvil_settlement_e2e.rs Updates e2e test bootstrap calls to pass None for the new optional reconcile configuration.

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

Comment thread crates/node/src/dht/node_address.rs Resolved
Comment thread crates/node/src/buyer_channel.rs Resolved

@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 introduces an idle-reconcile sweep mechanism to cooperatively close idle buyer channels and reclaim deposits early, including the necessary reverse address lookup wiring and tracking metrics. The review feedback highlights a potential issue where persistent failures (such as invalid public keys, or declined/reverted cooperative closes) are not cleared from the idle observation map, leading to hourly retries and log spam. Implementing a backoff by resetting the idle tally for these channels is recommended.

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/buyer_channel.rs Resolved
Comment thread crates/node/src/buyer_channel.rs Resolved
@github-actions

github-actions Bot commented Jun 24, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 83.44% (was 83.67%, ▼ 0.23%) — 41450/49678 lines

File Coverage Lines Δ
crates/node/src/buyer_channel.rs 33.58% 277/825 ▼ 1.80%
crates/node/src/dht/node_address.rs 43.04% 99/230 ▲ +0.68%
crates/node/src/metrics.rs 82.80% 862/1041 ▼ 0.24%
crates/node/src/runtime/mod.rs 49.60% 927/1869 ▼ 0.16%
History
Commit Total Δ vs base
037df2c 83.38% ▼ 0.23%
5a029b5 (HEAD) 83.44% ▼ 0.23%

…fixes

Address review feedback on #987:

- Drop the idle observation on invalid-pubkey, Declined, and Reverted
  outcomes so a persistent fault backs off ~24h instead of re-dialing /
  re-submitting (and re-logging) on every hourly sweep. The Reverted path
  was burning gas hourly until a (≤90-day) expiry; the expiry-reclaim
  sweep stays the eventual safety net. The Err (unreachable) path keeps
  retrying hourly — that fault is transient. (gemini-code-assist)
- Remove a stray "ponytail:" word from node_id_for's comment. (Copilot)
- Correct two comments that referenced a "zero idle threshold" disable
  path the code never implements (RECONCILE_IDLE_SWEEPS is a const);
  reconcile is gated solely on dial wiring being present. (Copilot)

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit f927a2c into main Jun 25, 2026
20 checks passed
@thiras
thiras deleted the claude/coop-close-reconcile branch June 25, 2026 21:29
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.

Cooperative close: node-as-buyer periodic reconcile of node→node channels

3 participants