Repository navigation
fix(node): backfill ChannelOpened to close watcher bring-up race (#762) - #773
Conversation
`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]>
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 backfillChannelOpenedlogs over[S, F]after installing live filters. - Refactor
ChannelOpenedhandling into a sharedapply_channel_openedhelper 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.
Coverage reportTotal: 86.77% (was 86.95%, ▼ 0.18%) — 25215/29059 lines
History
|
…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]>
Summary
Closes #762.
PaymentChannelService::bootstrapreturns 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. AChannelOpenedemitted in the sub-millisecond window betweenbootstrapreturning and the filters installing was never observed — the channel was never registered and its vouchers were rejected withWrongChannel. 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):
bootstrapcaptures the chain headS..watch()filters install, read the blockFthey took over at.get_logs-backfillChannelOpenedover[S, F]and register the ones this node provides.Backfill
[S, F]∪ live(F, ∞)cover everything with no gap. The overlap atFis harmless becauseregister_open_channelis 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 aget_logsRPC error retries on the next resubscription.The live
ChannelOpenedarm and the backfill share a newapply_channel_openedhelper, so theChannelStateconstruction isn't duplicated.Why deterministic — reasoning trace
[S, F]→ caught by the backfill.F→ caught by the live filters (installed at/beforeF).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 inanvil_settlement_e2e.rs— the existingpoll_untilon channel persistence is the deterministic wait the backfill enables. Confirmede2e_onchain_payment_channel_settlementpasses with the sleep removed.Verification
cargo build && cargo clippy— clean (anti-panic lints satisfied)cargo fmt -- --check— cleancargo nextest run -p decdn-node— 371/371 (incl. newbackfill_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
S) is not back-filled; that needs the last-scanned block persisted across restarts (Seller-settlement robustness follow-ups (metrics, redeemer tick, downtime backfill) #751, same root cause, separate item).ChannelToppedUp/ChannelSettled/ChannelCloseInitiated(an open+settle inside the same sub-second install window is implausible; ordered multi-event backfill belongs to Seller-settlement robustness follow-ups (metrics, redeemer tick, downtime backfill) #751).🤖 Generated with Claude Code