Skip to content

fix(node): reconcile provider-side Closing channels into the settle set (#839) - #892

Merged
thiras merged 3 commits into
mainfrom
fix/839-closing-reconciliation
Jun 13, 2026
Merged

thiras merged 3 commits into
mainfrom
fix/839-closing-reconciliation

Conversation

@thiras

@thiras thiras commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #839. The provider node closes a PaymentChannel in two non-atomic steps — closeChannel lands on-chain (channel → Closing), then record_pending_after_close separately persists the PendingSettle obligation. A crash (or a getChannel/record_pending failure) in that gap loses the obligation. Since the settle sweep only acts on the pending-settle store, a fully-drawn channel's (clientRefund == 0) un-withdrawn remainder is stranded until a manual operator settleChannel. The ChannelOpened bring-up backfill can't recover it (the channel is Closing, not Open).

A second, related gap: closeChannel is callable by either party, so a client-initiated close on a channel we provide was never recorded at all — the live ChannelCloseInitiated arm was observe-only.

Changes

  • pending_settle_for_closing (pure decision gate) + reconcile_closing_channel (thin RPC wrapper): getChannel → verify provider == self && status == Closing → record_pending with the authoritative on-chain disputeDeadline. Idempotent overwrite. The pure/RPC split follows the existing convention (pure logic → unit tests, on-chain orchestration → anvil-e2e).
  • backfill_closing_channels: a bring-up scan mirroring backfill_opened_channels over the same [start, head] range, querying ChannelCloseInitiated logs and reconciling each. Wired into run_watcher_once after the opened backfill, before clearing backfill_from.
  • Live ChannelCloseInitiated arm now reconciles too (closing the client-initiated-close gap), and on failure re-arms the backfill and resubscribes — mirroring the opened arm — so a live failure self-heals within the process instead of relying on the next boot scan.
  • watcher_persist_failures metric doc updated: it now also covers the closing-reconciliation path (a getChannel read or a record_pending write), so it is no longer store-writes-only.

Known limitation (documented, not silently overclaimed)

The closing scan reuses the ChannelOpened checkpoint as its start. That checkpoint advances only on open persistence — a true floor for a downtime gap, but not a per-close completeness guarantee. The live-arm re-arm closes the common case; a crash in the retry window after the checkpoint advanced past the close block still requires a manual settleChannel. A separate close-scan checkpoint is a reasonable follow-up for belt-and-suspenders durability.

Testing

  • Unit: pending_settle_for_closing gate — owned-Closing records (deadline + channel_id pass-through), foreign provider skipped, Closed/Open skipped.
  • anvil-e2e (extends e2e_onchain_payment_channel_settlement): covers both gaps — a node-initiated close while the service is down, recovered by the boot scan (recovered deadline == on-chain disputeDeadline, then settled by a third party and the entry dropped); and a client-initiated close against the running service, recorded by the live arm.
  • cargo nextest run -p decdn-node → 601/601 pass; anvil-e2e passes; clippy --all-targets --features anvil-e2e and fmt --check clean. Contracts unchanged.

🤖 Generated with Claude Code

…et (#839)

`closeChannel` lands on-chain (channel → `Closing`), then `record_pending`
persists the `PendingSettle` obligation in a separate step. A crash — or a
`getChannel`/`record_pending` failure — in that gap loses the obligation: the
settle sweep only acts on the pending-settle store, so a fully-drawn channel's
(`clientRefund == 0`) un-withdrawn remainder is stranded until a manual
operator `settleChannel`. The `ChannelOpened` bring-up backfill can't recover
it — the channel is `Closing`, not `Open`. Separately, `closeChannel` is
callable by either party, so a client-initiated close was never recorded at all
(the live `ChannelCloseInitiated` arm was observe-only).

Add a boot-time reconciliation:

- `pending_settle_for_closing` (pure gate) + `reconcile_closing_channel` (thin
  RPC wrapper): getChannel → verify provider == self && status == Closing →
  record_pending with the authoritative on-chain disputeDeadline. Idempotent.
- `backfill_closing_channels`: a bring-up scan mirroring backfill_opened_channels
  over the same `[start, head]` range, querying ChannelCloseInitiated logs and
  reconciling each. Recovers the crash gap and any close during downtime.
- The live `ChannelCloseInitiated` arm now reconciles too (closing the
  client-initiated-close gap), and on failure re-arms the backfill and
  resubscribes — mirroring the `opened` arm — so a live failure self-heals
  within the process rather than relying on the next boot scan.

Floor caveat: the closing scan reuses the `ChannelOpened` checkpoint as its
start, which advances only on open persistence — a true floor for a downtime
gap, but not a per-close completeness guarantee. The live-arm re-arm closes the
common case; a crash in the retry window after the checkpoint advanced past the
close block still needs a manual settleChannel. Documented at the call site; a
separate close-scan checkpoint is a possible follow-up.

Tests: unit tests for the pure gate (owned-Closing records, foreign/non-Closing
skip); anvil-e2e covering both gaps — a node-initiated close while down recovered
by the boot scan (deadline matches on-chain, then settled and dropped), and a
client-initiated close against the running service recorded by the live arm.

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

@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 closing-reconciliation backfill and live tracking for payment channels to address issue #839. It ensures that any provider-owned channel left in a Closing state on-chain—either due to a crash gap or a client-initiated close—correctly records a pending settlement obligation. This is achieved by adding a new backfill scan (backfill_closing_channels) during startup, updating the live event watcher to reconcile ChannelCloseInitiated events, and adding comprehensive unit and end-to-end tests. There are no review comments, and I have no additional feedback to provide.

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.

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

This PR fixes a durability gap in provider-side payment-channel settlement by ensuring channels that reach on-chain Closing state are reconciled into the local pending-settlement set, even if the node crashes or the close is initiated by the client.

Changes:

  • Add closing-channel reconciliation logic (pending_settle_for_closing + reconcile_closing_channel) and a boot-time ChannelCloseInitiated backfill to restore missing PendingSettle entries.
  • Update the live ChannelCloseInitiated watcher arm to reconcile and to re-arm backfill/resubscribe on failure.
  • Extend the anvil e2e settlement test to cover both the “crash gap” and “client-initiated close” gap; update watcher persist-failure metric documentation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
crates/node/src/payment_settlement.rs Adds closing reconciliation (boot backfill + live arm) and unit tests for the pure decision gate.
crates/node/tests/anvil_settlement_e2e.rs Extends e2e coverage to validate closing reconciliation works across restart and for client-initiated closes.
crates/node/src/metrics.rs Updates watcher_persist_failures docs to include close-reconciliation read/write failures.

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

Comment thread crates/node/src/payment_settlement.rs Resolved
@github-actions

github-actions Bot commented Jun 13, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 86.02% (was 86.16%, ▼ 0.14%) — 35596/41383 lines

File Coverage Lines Δ
crates/node/src/metrics.rs 84.31% 763/905 ● 0.00%
crates/node/src/payment_settlement.rs 50.51% 883/1748 ▼ 0.69%
History
Commit Total Δ vs base
afc587b 86.01% ▼ 0.15%
a1a28f5 86.01% ▼ 0.15%
97b1198 (HEAD) 86.02% ▼ 0.14%

thiras and others added 2 commits June 13, 2026 07:49
…r both triggers

The close-reconcile arm (#839) now also returns RecoveryPending, but the enum
doc and the watcher_loop warning still described only the failed-open-persist
case ("holding checkpoint after a failed open-persist") — misleading when the
trigger was a close reconciliation, and the "holding checkpoint" wording is
open-specific (the close arm only re-arms backfill_from; the checkpoint is
open-tied). Make both generic and accurate over both triggers.

Per PR #892 review (Copilot).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…allback, test guards

Second-round PR #892 review (code-reviewer, pr-test-analyzer, comment-analyzer,
silent-failure-hunter). Three actionable fixes:

- comment-analyzer (Critical): the `record_pending` failure-leg comment in
  `record_pending_after_close` still said a lost write "is not re-derivable",
  contradicting the docstring this PR updated. #839's backfill re-reads on-chain
  status independent of the local forget, so a still-`Closing` channel IS
  re-derivable. Reworded the comment and the error log.

- silent-failure-hunter (Important): the live close arm's block fallback used
  `unwrap_or(checkpoint_hw)`. Unlike the `opened` arm (where checkpoint_hw <=
  failed by block-ordering), the close arm's checkpoint_hw is *open*-advanced and
  unordered against the close, so on a missing block_number it could re-arm
  *above* the close block and silently skip it. Fall back to `0` — an idempotent,
  backoff-paced full re-scan that cannot overshoot. Documented the divergence.

- pr-test-analyzer (Suggestion, highest-value): assert
  `decdn_watcher_persist_failures_total 0` after the live-arm scenario so the
  test pins recovery to the *direct* reconcile path, not the re-arm+resubscribe
  failure path the 60s poll could otherwise mask. Also yield_now after
  drop(service2) to let the async AbortOnDrop wind down before the no-pending
  precondition.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit 097eca1 into main Jun 13, 2026
20 checks passed
@thiras
thiras deleted the fix/839-closing-reconciliation branch June 13, 2026 05:04
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.

node: closeChannel→record_pending is non-atomic — a crash in the gap strands a fully-drawn channel's remainder with no recovery scan

2 participants