Skip to content

fix(node): backfill ChannelOpened to close watcher bring-up race (#762) - #773

Merged
thiras merged 2 commits into
mainfrom
fix/762-watcher-bringup-backfill
Jun 1, 2026
Merged

thiras merged 2 commits into
mainfrom
fix/762-watcher-bringup-backfill

Conversation

@thiras

@thiras thiras commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #762. PaymentChannelService::bootstrap returns as soon as the watcher task is spawned, but that task then installs the .watch() (eth_newFilter) streams, which only deliver logs from the block the filter installs at forward. A ChannelOpened emitted in the sub-millisecond window between bootstrap returning and the filters installing was never observed — the channel was never registered and its vouchers were rejected with WrongChannel. Rare in production (real clients wait for confirmations before connecting) but a genuine bring-up race, not a guarantee.

Fix

Add a one-time startup log-backfill (the issue's proposed fix):

  1. bootstrap captures the chain head S.
  2. On the first watcher cycle, after the live .watch() filters install, read the block F they took over at.
  3. get_logs-backfill ChannelOpened over [S, F] and register the ones this node provides.

Backfill [S, F] ∪ live (F, ∞) cover everything with no gap. The overlap at F is harmless because register_open_channel is already idempotent (it short-circuits on a tracked channel and never resets the accepted-voucher watermark, so no dedup bookkeeping is needed). The backfill runs once — kept pending only until it succeeds, so a get_logs RPC error retries on the next resubscription.

The live ChannelOpened arm and the backfill share a new apply_channel_opened helper, so the ChannelState construction isn't duplicated.

Why deterministic — reasoning trace

  • A channel opened in [S, F] → caught by the backfill.
  • A channel opened after F → caught by the live filters (installed at/before F).
  • A channel opened at exactly the filter-install block → covered because F = get_block_number() read after install is ≥ the install block, so [S, F] includes it.

Test changes

Retired the flaky sleep(Duration::from_secs(2)) readiness bridge in anvil_settlement_e2e.rs — the existing poll_until on channel persistence is the deterministic wait the backfill enables. Confirmed e2e_onchain_payment_channel_settlement passes with the sleep removed.

Verification

  • cargo build && cargo clippy — clean (anti-panic lints satisfied)
  • cargo fmt -- --check — clean
  • cargo nextest run -p decdn-node — 371/371 (incl. new backfill_range_nonempty_boundary)
  • cargo nextest run -p decdn-node --features anvil-e2e — 372/372 (anvil + deployed contracts; settlement e2e green without the sleep)

Out of scope

🤖 Generated with Claude Code

`PaymentChannelService::bootstrap` returns as soon as the watcher task is
spawned, but that task then installs the `.watch()` (`eth_newFilter`) streams,
which only deliver logs from the block the filter installs at forward. A
`ChannelOpened` emitted in the window between `bootstrap` returning and the
filters installing was never observed — the channel was never registered and
its vouchers were rejected with `WrongChannel`.

Capture the head block `S` at bootstrap and, on the first watcher cycle, after
the live filters install, read the block `F` they took over at and
`get_logs`-backfill `ChannelOpened` over `[S, F]`. Backfill `[S, F]` plus the
live `(F, ∞)` stream cover everything with no gap; the overlap at `F` is
harmless because `register_open_channel` is idempotent. The backfill runs once
(retried on the next resubscription only if the `get_logs` RPC fails).

Retire the flaky `sleep(2s)` readiness bridge in the anvil settlement e2e — the
existing `poll_until` on channel persistence is now the deterministic wait the
backfill enables. Verified: `e2e_onchain_payment_channel_settlement` passes with
the sleep removed.

Across-restart downtime backfill (a channel opened while the node was fully
down, before `S`) remains out of scope — that needs the last-scanned block
persisted across restarts (#751).

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

@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 resolves a startup race condition in the payment settlement service by introducing a one-time bring-up backfill mechanism that replays ChannelOpened events between bootstrap and live filter installation. Feedback on these changes highlights a potential issue with load-balanced RPC environments where replication lag could cause the backfill range to appear invalid (from_block > to_block). Currently, this scenario silently disables the backfill permanently; the reviewer suggests returning an error instead to trigger a retry.

Comment thread crates/node/src/payment_settlement.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

This PR fixes a rare watcher bring-up race in the node’s on-chain PaymentChannel settlement service where a ChannelOpened event could be emitted after bootstrap returns but before .watch() filters are installed, causing the channel to never be registered and vouchers to be rejected (WrongChannel). It does so by adding a one-time get_logs backfill on the first watcher cycle, and removes a previously required fixed sleep in the anvil E2E test.

Changes:

  • Capture the chain head at PaymentChannelService::bootstrap, then on the first watcher cycle backfill ChannelOpened logs over [S, F] after installing live filters.
  • Refactor ChannelOpened handling into a shared apply_channel_opened helper used by both live-stream and backfill paths.
  • Remove the flaky watcher-readiness sleep(2s) from the anvil settlement E2E test, relying on deterministic persistence polling instead.

Reviewed changes

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

File Description
crates/node/src/payment_settlement.rs Adds bootstrap head capture + first-cycle ChannelOpened backfill and refactors event application into a helper.
crates/node/tests/anvil_settlement_e2e.rs Removes fixed sleep and documents the new deterministic backfill-based readiness behavior.

💡 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 1, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 86.77% (was 86.95%, ▼ 0.18%) — 25215/29059 lines

File Coverage Lines Δ
crates/node/src/payment_settlement.rs 15.42% 99/642 ▲ +0.10%
History
Commit Total Δ vs base
7f56b7c 86.79% ▼ 0.16%
f1c7530 (HEAD) 86.77% ▼ 0.18%

…ontract

Code-review follow-ups on the #762 watcher backfill:

- Silent failure (silent-failure-hunter): `apply_channel_opened` logged and
  swallowed `register_open_channel` failures. On the backfill path that is the
  sole delivery of a channel opened in `[S, F]`, and `backfill_from` was cleared
  right after — so a transient store error during bring-up permanently dropped
  the channel the backfill exists to recover. `apply_channel_opened` now returns
  a `Result`: the live arm logs-and-continues (unchanged), the backfill
  propagates so the whole scan retries with `backfill_from` still armed.

- Retry contract untested (pr-test-analyzer): extracted the range guard into a
  pure `check_backfill_range -> Result`; the unit test now asserts an inverted
  `from > to` range yields a retryable `Err` (not just the bool predicate).

- Misleading comment (gemini/copilot): `from == to` is a valid single-block
  range that must be scanned; `from > to` is the empty/anomalous range. Helper,
  doc, and test comments corrected accordingly.

- Documented the ChannelOpened-only backfill scope in the module header: an
  in-window top-up/settle is not back-filled (sub-second window; settle gated by
  the dispute window) — ordered multi-event backfill stays with #751.

Verified: cargo clippy/fmt clean, cargo nextest -p decdn-node (371/371), and
--features anvil-e2e settlement e2e green.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit 9cf3c41 into main Jun 1, 2026
19 checks passed
@thiras
thiras deleted the fix/762-watcher-bringup-backfill branch June 1, 2026 20:36
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.

incentive: watcher bring-up race — bootstrap returns before .watch() filters install, dropping events in the gap

2 participants