Skip to content

feat(node): bootstrap reconciliation for orphaned buyer channel deposits (#763) - #903

Merged
thiras merged 3 commits into
mainfrom
feat/763-buyer-channel-reconciliation
Jun 14, 2026
Merged

thiras merged 3 commits into
mainfrom
feat/763-buyer-channel-reconciliation

Conversation

@thiras

@thiras thiras commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #763. Adds a one-shot, best-effort bootstrap reconciliation scan that recovers buyer payment-channel deposits the node escrowed on-chain but lost track of locally.

BuyerChannelService::open_and_persist escrows a deposit via openChannel, then decodes the ChannelOpened event and persists the row. If the decode or store.record fails after the escrow — or a binary downgrade leaves a row postcard-undecodable — the channel is tracked only on-chain, and the reclaim sweep (which iterates load_all) never sees it, stranding the deposit. The blocker (#763 was gated on the cache-miss hook calling open_or_reuse_channel) is now resolved (wired in #831).

What it does

  • New reconcile_orphans_once / reconcile_one_opened / pure reconcile_decision in crates/node/src/buyer_channel.rs, wired into bootstrap as a non-blocking _reconciler task (matches the non-fatal buyer-bootstrap posture). It enumerates ChannelOpened(client == self) over a bounded recent window (BUYER_RECONCILE_LOOKBACK_BLOCKS, ~1–2 days) and re-hydrates any still-Open channel that is missing or undecodable locally; the existing reclaim sweep then recovers the deposit on expiry.
  • Mirrors the seller-side backfill, reusing its window helpers (backfill_windows / check_backfill_range / MAX_BACKFILL_BLOCK_SPAN, promoted to pub(crate)).

Robustness (from multi-agent review)

  • No clobbering live opens: the reconciler claims the same per-provider in-flight-open slot the live path uses, so re-hydration can never overwrite a channel a concurrent cache-miss open just recorded.
  • Backend vs corrupt: an unreadable row (corrupt / future-schema) is repaired from chain; a backend/IO fault is skipped rather than overwriting a possibly-healthy row.
  • Partial-failure visibility: a single window's eth_getLogs failure skips just that window (failed_windows reported); a second still-open channel per provider (the documented one-row-per-provider residual) is logged, not dropped silently.

Tests

  • Unit: reconcile_decision table (orphan re-hydrate, watermark hydration from on-chain claimed totals, skip-healthy-row, skip-non-Open, skip-not-owned).
  • Anvil e2e (anvil-e2e feature): concurrent same-provider opens, expired rotation via open_or_reuse_channel, orphan re-hydration, undecodable-row repair + mixed corrupt/healthy reclaim. The buyer e2e path now runs under its own identity so the scan is deterministic. Adds a test-only insert_raw_buyer_record store seam.

Verification

cargo clippy -D warnings (with/without anvil-e2e), cargo fmt --check, and 779 node+incentive unit tests all pass locally. The full anvil-e2e could not be re-run to completion in the dev sandbox (anvil-spawn limits) after the review fixes; it is left to the CI anvil e2e job to validate scenarios B/C/D end-to-end.

Follow-up

A latent, pre-existing concern surfaced while testing: a reverting reclaimExpired leaks the alloy provider's cached nonce, which can wedge subsequent buyer transactions (the code expects reclaimExpired to "simply revert and be retried" under clock skew). Filed separately; this PR avoids triggering it.

🤖 Generated with Claude Code

…its (#763)

`BuyerChannelService::open_and_persist` escrows a deposit on-chain via
`openChannel`, then decodes the `ChannelOpened` event and persists the row. If
the decode or `store.record` fails *after* the escrow — or a binary downgrade
leaves a row postcard-undecodable — the channel is tracked only on-chain and the
reclaim sweep (which iterates `load_all`) never sees it, stranding the deposit.

Add a one-shot, best-effort bootstrap reconciliation scan that enumerates
`ChannelOpened(client == self)` over a bounded recent block window and
re-hydrates any still-`Open` channel missing or undecodable locally, so the
existing reclaim sweep recovers the deposit. Mirrors the seller-side backfill,
reusing its window helpers (promoted to `pub(crate)`).

- `reconcile_orphans_once` / `reconcile_one_opened` / pure `reconcile_decision`,
  wired into `bootstrap` as a non-blocking `_reconciler` task (matches the
  non-fatal buyer-bootstrap posture).
- Reconciler claims the same per-provider in-flight-open slot the live path uses,
  so its re-hydration can never clobber a channel a concurrent open just recorded.
- Distinguishes an unreadable row (corrupt / future-schema → repair from chain)
  from a backend/IO fault (skip, don't overwrite a possibly-healthy row).
- A single window's `eth_getLogs` failure skips just that window; a deferred
  second-orphan-per-provider is logged rather than dropped silently.

Tests: `reconcile_decision` unit table; anvil-e2e scenarios for concurrent
same-provider opens, expired rotation via `open_or_reuse_channel`, orphan
re-hydration, and undecodable-row repair + mixed corrupt/healthy reclaim. The
buyer e2e path now runs under its own identity so the scan is deterministic. Adds
a test-only `insert_raw_buyer_record` store seam.

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

@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 a one-shot bootstrap reconciliation scan for the buyer channel service to re-hydrate orphaned on-chain channels (e.g., due to post-escrow failures or database downgrades). It exposes helper functions from payment_settlement to reuse block windowing logic, adds a test-only database helper to simulate corrupt records, and introduces comprehensive unit and end-to-end integration tests to verify concurrent opens, expired-channel rotation, and orphan recovery. There are no review comments, so no additional feedback is provided.

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

Adds a buyer-side, one-shot bootstrap reconciliation scan to recover orphaned buyer payment-channel deposits that exist on-chain but are missing/undecodable in the local store, and extends e2e coverage for concurrency/corruption scenarios.

Changes:

  • Introduces buyer-channel reconciliation logic (reconcile_*) and spawns it during BuyerChannelService::bootstrap.
  • Promotes seller-side backfill window helpers (MAX_BACKFILL_BLOCK_SPAN, backfill_windows, check_backfill_range) to pub(crate) for reuse.
  • Extends Anvil e2e tests and adds a test-only store seam to inject corrupt buyer rows.

Reviewed changes

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

File Description
crates/node/src/buyer_channel.rs Adds bootstrap reconciliation task + decision logic and unit tests for orphan repair behavior.
crates/node/src/payment_settlement.rs Exposes backfill window helpers/constants for buyer reconciliation reuse.
crates/node/src/channel_store.rs Adds test/e2e-only insert_raw_buyer_record seam to simulate undecodable buyer rows.
crates/node/tests/anvil_settlement_e2e.rs Adds deterministic buyer identity + new e2e scenarios for concurrent opens, rotation, orphan re-hydration, and corrupt-row reclaim behavior.

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

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 14, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 85.74% (was 85.99%, ▼ 0.24%) — 35985/41968 lines

File Coverage Lines Δ
crates/node/src/buyer_channel.rs 26.06% 153/587 ▲ +9.78%
crates/node/src/channel_store.rs 87.60% 1392/1589 ▼ 1.63%
crates/node/src/payment_settlement.rs 50.51% 883/1748 ● 0.00%
History
Commit Total Δ vs base
7f0c396 85.75% ▼ 0.24%
2ca2c3b 85.74% ▼ 0.25%
be9f0ce (HEAD) 85.74% ▼ 0.24%

thiras and others added 2 commits June 14, 2026 17:02
- anvil-e2e scenario D: chmod the persistent-store tempdir to 0o700 before
  `PersistentChannelStateStore::open` (a CI umask of 002 leaves it at 0o755,
  which the data_dir security check rejects). Mirrors the seller-store setup.
  This is the anvil-e2e CI failure on the prior commit.
- reconcile scan: topic-filter the `ChannelOpened` query on the indexed `client`
  topic (`topic2(self_address)`) so `eth_getLogs` returns only this node's own
  opens, bounding result-count/latency on busy deployments instead of fetching
  every open in the window. The in-code `client == self` check stays as
  defense-in-depth, and the stale "filter is by signature, not topic" comment is
  corrected.

Addresses Copilot review feedback on #903.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…h + all-windows escalation

Follow-ups from a second review round on the #763 reconciliation scan (no
behavior change to the happy path):

- `reconcile_decision` now returns a `ReconcileOutcome` enum (Rehydrate /
  DeferredSecondOpen / Skip) instead of a bare `Option`, so the caller no longer
  re-derives the deferred-orphan predicate and each branch is directly
  unit-testable. Adds a unit test for the DeferredSecondOpen classification.
- `reconcile_one_opened` matches every `StoreError` variant explicitly (no
  catch-all), so a future variant is a compile error forcing a repair-vs-skip
  decision rather than silently defaulting to skip (which would strand an orphan
  whose row became unreadable in a new way).
- `reconcile_orphans_once` escalates the completion log to `error!` when every
  window's query failed (RPC outage → scan accomplished nothing), instead of
  burying it under an `info!` "complete".

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit 71b8b89 into main Jun 14, 2026
20 checks passed
@thiras
thiras deleted the feat/763-buyer-channel-reconciliation branch June 14, 2026 14:59
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.

Buyer-channel robustness follow-ups: orphan-deposit reconciliation + concurrent-open / multi-row reclaim e2e coverage

2 participants