Skip to content

refactor(node): split watcher CursorPolicy into CursorStart + owned Checkpoint (#1238) - #1280

Merged
thiras merged 2 commits into
mainfrom
refactor/1238-cursor-start-split
Jul 17, 2026
Merged

thiras merged 2 commits into
mainfrom
refactor/1238-cursor-start-split

Conversation

@thiras

@thiras thiras commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Context

Closes #1238. The chain-watcher CursorPolicy enum fused two orthogonal axes — persistence (durably recording a checkpoint) and floor derivation (where the first tick starts) — and a separate WatcherConfig::seed_cursor silently overrode floor derivation, leaving policy fields dead. That's the exact "a pin on a value nothing reads" defect #1227 corrected in prose but left structurally: chain_origin_directory declared a reorg_margin/none_fallback it never read (and so had no across-restart shallow-reorg coverage), and capacity_bond_registry declared a window_blocks/floor it never read.

This is the root of the watcher-refactor cluster — #1251 (WatcherConfig builder) and #1255 (ChainProjection wrapper) both rebase onto the new field set.

What changed

  • CursorPolicy → CursorStart (Seeded / FromCheckpoint / HeadMinusWindow / FullReplay) + an owned Checkpoint. Persistence rides inside the variants that carry a Checkpoint, so a checkpoint-resuming watcher can't be configured without one and a head-deriving watcher can't declare a margin/window it never reads. seed_cursor is gone.
  • Hoisted the per-variant floor into the single WatcherConfig::from_block (verified equal at every site), removing the two-source-for-one-fact footgun.
  • Removed the now-degenerate NoneFallback enum. After origin moved to Seeded, settlement is the sole FromCheckpoint user and always used Head, so a cold FromCheckpoint boot anchors at head; the dead FromBlock degrade branch is gone.
  • Behavior change: chain_origin_directory now rewinds its bootstrap replay_from by REORG_MARGIN_BLOCKS (floored at the deploy block) via replay_floor / resume_replay_floor, closing its previous lack of across-restart shallow-reorg coverage. The rewind lives in origin's bootstrap (not the generic watcher) because origin replays from an enumeration and seeds its live tail.

All six watcher sites converted; all types pub(crate) (no public-API change).

Testing

  • cargo build --all-targets, cargo clippy --all-targets, cargo fmt --check, cargo doc -D warnings — clean.
  • cargo nextest run -p decdn-node — 848 pass (was 842).
  • New: a guardrail test pinning that origin's durable read feeds the rewind (resume_replay_floor_rewinds_the_persisted_origin_checkpoint) — proves the rewind engages, not just that a value is carried; plus variant→resolver wiring tests for Seeded/FullReplay/HeadMinusWindow. Policy-pin and loop tests updated.

This branch was multi-agent-reviewed (correctness / tests / comments / silent-failures / type-design); all surfaced findings are addressed.

Follow-ups (out of scope)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Improvements
    • Updated blockchain event watchers to provide more consistent replay, resume, and checkpoint behavior.
    • Improved recovery after restarts while preserving reorganization safeguards.
    • Standardized watcher startup across capacity, origin, payment, reputation, and slash monitoring.
    • Ensured full-history replay is used where required and bounded lookbacks are applied for live monitoring.
  • Tests
    • Expanded coverage for replay, checkpoint recovery, startup positions, and reorganization handling.

…heckpoint (#1238)

Fuse-free chain-watcher config: the old `CursorPolicy` enum conflated two
orthogonal axes — persistence and floor derivation — and `WatcherConfig::
seed_cursor` silently overrode the latter, leaving policy fields dead (the
#1227 defect). Replace it with a `CursorStart` enum
(Seeded/FromCheckpoint/HeadMinusWindow/FullReplay) plus an owned `Checkpoint`,
so persistence rides inside the variants that carry a checkpoint and both
dead-field shapes become unrepresentable.

- Remove `seed_cursor`; each site now expresses one clear start.
- Hoist the per-variant `floor` into the single `WatcherConfig::from_block`
  (verified equal at every site) to kill the two-source-for-one-fact footgun.
- Drop the now-degenerate `NoneFallback` enum: after origin moved off the
  persisted-cursor floor path, settlement is the sole `FromCheckpoint` user and
  is always Head-fallback, so a cold `FromCheckpoint` boot anchors at head.
- Behavior change: `chain_origin_directory` now rewinds its bootstrap
  `replay_from` by `REORG_MARGIN_BLOCKS` (new `replay_floor`/`resume_replay_floor`),
  closing its previous lack of across-restart shallow-reorg coverage. Pinned by
  a wiring test proving the durable read feeds the rewind.

Convert all six watcher sites, update policy-pin/loop tests, and add variant→
resolver wiring tests for Seeded/FullReplay/HeadMinusWindow. All pub(crate), no
public-API change. 848 tests pass; clippy/fmt/doc clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings July 17, 2026 23:24
@coderabbitai

coderabbitai Bot commented Jul 17, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@thiras, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 53 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: b9f88ce0-9638-4a33-b8fd-9ff00a5d614b

📥 Commits

Reviewing files that changed from the base of the PR and between 0e65835 and 5bc2947.

📒 Files selected for processing (1)
  • crates/node/src/chain_events/resumable_watcher.rs

Walkthrough

The resumable watcher now uses CursorStart to unify initial scan-floor derivation, checkpoint persistence, seeded starts, bounded windows, and full replays. Durable settlement and origin watchers, along with other watcher integrations and tests, are migrated to the new configuration model.

Changes

CursorStart watcher migration

Layer / File(s) Summary
CursorStart core and runtime wiring
crates/node/src/chain_events/resumable_watcher.rs, crates/node/src/chain_events/backfill.rs
Checkpoint and CursorStart replace CursorPolicy and NoneFallback; runtime initialization, persistence, flushing, retry handling, and tests use the new model.
Durable watcher resume wiring
crates/node/src/payment_settlement.rs, crates/node/src/dht/chain_origin_directory.rs
Settlement resumes use ChannelOpened checkpoints with reorg margins, while origin bootstrap computes rewound replay floors and seeds the live watcher from the snapshot block.
Non-durable watcher start migrations
crates/node/src/blacklist_watcher.rs, crates/node/src/dht/capacity_bond_registry.rs, crates/node/src/reputation_indexer.rs, crates/node/src/slash_watcher.rs, crates/node/src/chain_events/shared_head.rs
Watcher configurations move to FullReplay, Seeded, or HeadMinusWindow CursorStart variants.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant WatcherBootstrap
  participant CursorStart
  participant CheckpointStore
  participant ResumableWatcher
  WatcherBootstrap->>CursorStart: configure start mode
  ResumableWatcher->>CursorStart: derive initial scan floor
  CursorStart->>CheckpointStore: load durable checkpoint
  CheckpointStore-->>CursorStart: return persisted cursor
  CursorStart-->>ResumableWatcher: return replay or window start
  ResumableWatcher->>CursorStart: persist completed window
  CursorStart->>CheckpointStore: save checkpoint
Loading

Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands.

@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 refactors the resumable watcher cursor logic by splitting the CursorPolicy enum into CursorStart and Checkpoint to separate floor-derivation and persistence concerns, thereby removing dead fields and preventing invalid configurations. Feedback was provided regarding a behavioral change mixed into this structural refactoring: adding reorg-margin coverage to the origin directory bootstrap should ideally be separated from the pure refactoring to ensure behavior preservation and ease of bisecting future regressions.

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

Refactors the node’s chain-watcher start/persistence configuration to eliminate “configured but never read” cursor-policy fields by splitting the old fused CursorPolicy into a CursorStart (how the first tick chooses its floor) plus an owned Checkpoint (durable persistence), and hoisting the deploy-floor into WatcherConfig::from_block for all start modes.

Changes:

  • Replaced CursorPolicy/NoneFallback/WatcherConfig::seed_cursor with CursorStart + Checkpoint, making dead-field configurations unrepresentable.
  • Updated all watcher construction sites to the new config shape, including seeded start sites (origin directory, capacity-bond registry) and persisted resume (settlement).
  • Added/updated guardrail tests and introduced a behavior change for chain_origin_directory: bootstrap replay now rewinds the persisted origin checkpoint by REORG_MARGIN_BLOCKS (floored at deploy block) to provide across-restart shallow-reorg coverage.

Reviewed changes

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

Show a summary per file
File Description
crates/node/src/chain_events/resumable_watcher.rs Introduces CursorStart + Checkpoint, removes seed_cursor and NoneFallback, updates floor resolution/persistence behavior and tests.
crates/node/src/chain_events/backfill.rs Updates REORG_MARGIN_BLOCKS documentation to match the new cursor-start/persistence split and origin bootstrap rewind.
crates/node/src/chain_events/shared_head.rs Adjusts shared-head test wiring to the new WatcherConfig { start: CursorStart, … } shape.
crates/node/src/payment_settlement.rs Migrates settlement watcher to CursorStart::FromCheckpoint with an owned Checkpoint and updates policy-pin tests/docs.
crates/node/src/dht/chain_origin_directory.rs Seeds live tail via CursorStart::Seeded { persist: Some(Checkpoint) } and adds bootstrap replay-floor rewind logic + tests.
crates/node/src/dht/capacity_bond_registry.rs Switches enumeration-seeded watcher start to CursorStart::Seeded { persist: None } (no durable resume).
crates/node/src/slash_watcher.rs Updates watcher config to CursorStart::HeadMinusWindow, relying on WatcherConfig::from_block as the clamp floor.
crates/node/src/reputation_indexer.rs Updates watcher config to CursorStart::HeadMinusWindow, relying on WatcherConfig::from_block as the clamp floor.
crates/node/src/blacklist_watcher.rs Moves blacklist watcher to CursorStart::FullReplay (deploy-floor via from_block) and updates pin test accordingly.

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

The defensive `warn!` in `CursorStart::Seeded`'s `initial_from` arm reused the
`label` field key, which everywhere else carries the watcher label — polluting
log aggregation and making the line read as a watcher named after the message.
Use an `invariant` key and include the seeded `at` value (per PR review).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Jul 17, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 81.54% (was 81.52%, ▲ +0.03%) — 48730/59759 lines

File Coverage Lines Δ
crates/node/src/blacklist_watcher.rs 23.60% 63/267 ▲ +0.18%
crates/node/src/chain_events/backfill.rs 98.11% 52/53 ● 0.00%
crates/node/src/chain_events/resumable_watcher.rs 92.83% 466/502 ▲ +0.93%
crates/node/src/chain_events/shared_head.rs 94.18% 178/189 ▼ 0.12%
crates/node/src/dht/capacity_bond_registry.rs 65.14% 256/393 ▼ 0.33%
crates/node/src/dht/chain_origin_directory.rs 71.38% 813/1139 ▲ +0.80%
crates/node/src/payment_settlement.rs 56.33% 943/1674 ▲ +0.06%
crates/node/src/reputation_indexer.rs 59.15% 307/519 ▲ +0.11%
crates/node/src/slash_watcher.rs 53.45% 124/232 ▲ +0.46%
History
Commit Total Δ vs base
0e65835 81.54% ▲ +0.02%
5bc2947 (HEAD) 81.54% ▲ +0.03%

@thiras
thiras merged commit 81295b3 into main Jul 17, 2026
23 checks passed
@thiras
thiras deleted the refactor/1238-cursor-start-split branch July 17, 2026 23:39
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.

refactor(node): split CursorPolicy's persistence and floor-derivation axes

2 participants