Skip to content

feat(node): don't reactively top up a near-expiry channel mid-pull - #1605

Merged
alpergundogdu merged 2 commits into
mainfrom
claude/priceless-agnesi-326491
Aug 5, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
claude/priceless-agnesi-326491

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Problem

The reactive mid-pull top-up (#1530 / #1600) funds an exhausted node-to-node buyer channel and resumes at the paid frontier, but resume::decide only checked spendable headroom (via genuine_exhaustion) — never the channel's remaining time to expires_at.

topUp deliberately does not extend expiry (the contract forbids it). So a reactive top-up on a near-expiry channel escrowed a fresh working-deposit into a channel that could expire before the resumed leg spent it — stranding capital until reclaimExpired, and risking the pull failing anyway. The proactive low-water refill only runs behind the try_reuse_live non-expired gate; the reactive leg had no equivalent expiry posture.

What this PR does (the immediate correctness piece)

  • New config knob, in seconds: blockchain.buyer_reactive_topup_min_ttl_secs — minimum remaining time to a channel's on-chain expires_at below which a reactive top-up is refused. Default 86_400 (~1 day); 0 disables the guard (pre-guard always-top-up behaviour). Seconds so operators can tune it per chain lane. Placed alongside the other blockchain/node-origin fields and wired through ResolvedBlockchain → NodeOriginConfig.reactive_topup_min_ttl exactly like working_deposit / event_poll_interval were.
  • The guard: before a reactive top-up, if the channel has less than that margin to expires_at, decide returns a new ResumeAction::NearExpiry instead of TopUp. The pull ends cleanly on the original exhaustion error — the same error shape the existing "top-up added no headroom" path already returns, so downstream classification (retire the channel, our-side fault → no peer blame) is unchanged. The candidate loop falls through to the next provider, and the next miss opens a fresh, full-lifetime channel; the near-expiry channel's residual deposit is freed by the reclaim sweep at its imminent expiry.
  • Metered via a distinct counter decdn_node_pull_reactive_topup_near_expiry_total, kept apart from _refused (a lying peer / funding failure) and a healthy _topup.

The channel's expires_at is read once per pull from channel_expiry (topUp cannot move it) and carried in ResumeBudgets. NEVER_EXPIRES (0) and a zero margin both read as "never near expiry".

Follow-up (intentionally out of scope)

The issue floats a mid-flight "open a fresh channel and continue streaming the remainder through it" as an optional extension. That is not in this PR — it's left as a follow-up, as the issue allows. This PR implements the near-expiry guard (don't fund a doomed channel), which is the immediate correctness fix.

Tests

New unit tests on the pure decide:

  • a_near_expiry_channel_is_not_topped_up — genuine exhaustion inside the margin → NearExpiry.
  • a_healthy_channel_still_funds_even_with_the_guard_armed — same corroborated exhaustion, ample time left → TopUp (guard can't suppress healthy top-ups).
  • an_already_past_expiry_is_refused — saturating_sub keeps expires_at < now from wrapping.
  • a_zero_margin_disables_the_near_expiry_guard — operator opt-out.
  • a_never_expires_channel_is_never_near_expiry.

Verification

  • cargo build && cargo clippy --workspace --all-targets — clean.
  • cargo nextest run -p decdn-node -p decdn-common — 1386 passed.
  • cargo fmt -- --check — clean.
  • cargo test --no-run -p decdn-e2e --features anvil-e2e — compiles.
  • Config-template coverage + sample-schema tests pass (new key documented in the config init template).

Refs #1603.

🤖 Generated with Claude Code

The reactive mid-pull top-up (#1530/#1600) funds an exhausted channel and
resumes at the paid frontier, but `decide` only checked spendable headroom
via `genuine_exhaustion` — never the channel's remaining time to `expires_at`.
`topUp` deliberately does NOT extend expiry (the contract forbids it), so a
reactive top-up on a near-expiry channel escrowed a fresh working-deposit into
a channel that could expire before the resumed leg spent it — stranding capital
until `reclaimExpired` and risking the pull failing anyway. The proactive refill
runs behind the `try_reuse_live` non-expired gate; the reactive leg had no
equivalent expiry posture.

Add a governance-tunable near-expiry margin, in seconds, alongside the other
blockchain config fields:

- `blockchain.buyer_reactive_topup_min_ttl_secs` (default ~1 day, 86_400).
  Seconds so operators can tune it per chain lane; `0` disables the guard
  (pre-guard always-top-up behaviour). Wired through `ResolvedBlockchain` and
  `NodeOriginConfig.reactive_topup_min_ttl` exactly like `working_deposit` /
  `event_poll_interval`.

Before a reactive top-up, if the channel has less than that margin to its
on-chain `expires_at`, `decide` returns a new `ResumeAction::NearExpiry` instead
of `TopUp`: the pull ends cleanly on the original exhaustion (the same error
shape the existing "top-up added no headroom" path returns, so classification —
retire the channel, no peer blame — is unchanged), the candidate loop falls
through to the next provider, and the next miss opens a fresh, full-lifetime
channel. The near-expiry channel's residual deposit is freed by the reclaim
sweep at its imminent expiry. Metered via a distinct
`decdn_node_pull_reactive_topup_near_expiry_total` counter, apart from `_refused`
(a lying peer / funding failure) and a healthy `_topup`.

The channel's `expires_at` is read once per pull from `channel_expiry`
(`topUp` cannot move it) and carried in `ResumeBudgets`; `NEVER_EXPIRES` (0) and
a zero margin both read as "never near expiry".

Tests cover the near-expiry skip, the still-fund-when-healthy case with the guard
armed, an already-past expiry, the zero-margin disable, and the `NEVER_EXPIRES`
carve-out.

The mid-flight "open a fresh channel and continue streaming the remainder through
it" the issue floats as an optional extension is intentionally left as a
follow-up; this PR is the immediate correctness piece (don't fund a doomed
channel).

Refs #1603.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI lite review requested due to automatic review settings August 5, 2026 09:47
@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

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

Next review available in: 45 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: 902e181c-5657-4880-b4bd-8c000ab6aefb

📥 Commits

Reviewing files that changed from the base of the PR and between d3e13fc and 8fddfa6.

📒 Files selected for processing (13)
  • crates/cli/src/commands/config.rs
  • crates/cli/tests/config_validate.rs
  • crates/common/src/config/mod.rs
  • crates/common/src/config/resolved.rs
  • crates/common/src/config/types.rs
  • crates/node/src/metrics.rs
  • crates/node/src/node_origin/mod.rs
  • crates/node/src/node_origin/resume.rs
  • crates/node/src/runtime/mod.rs
  • crates/node/src/runtime/reload.rs
  • crates/node/tests/anvil_bringup_shutdown_e2e.rs
  • crates/node/tests/node_origin_pull.rs
  • crates/node/tests/sighup_signal.rs

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.

@github-actions

github-actions Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 80.49% (was 80.47%, ▲ +0.02%) — 58092/72172 lines

File Coverage Lines Δ
crates/cli/src/commands/config.rs 86.39% 546/632 ▲ +0.11%
crates/common/src/config/mod.rs 97.13% 7012/7219 ● 0.00%
crates/common/src/config/resolved.rs 100.00% 33/33 ● 0.00%
crates/node/src/metrics.rs 95.67% 1105/1155 ● 0.00%
crates/node/src/node_origin/mod.rs 93.04% 1030/1107 ● 0.00%
crates/node/src/node_origin/resume.rs 93.71% 626/668 ▲ +0.53%
crates/node/src/runtime/mod.rs 46.38% 1094/2359 ▼ 0.10%
crates/node/src/runtime/reload.rs 96.40% 1662/1724 ● 0.00%
History
Commit Total Δ vs base
a9aea4c 80.49% ▲ +0.02%
8fddfa6 (HEAD) 80.49% ▲ +0.02%

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 adds an expiry-safety guard to the node’s reactive mid-pull buyer-channel top-up logic, preventing additional funds from being escrowed into channels that are close to on-chain expires_at (since topUp cannot extend expiry). The change is wired end-to-end through config resolution, runtime wiring, node-origin resume logic, metrics, and tests.

Changes:

  • Introduces blockchain.buyer_reactive_topup_min_ttl_secs (default 86_400, 0 disables) and plumbs it through ResolvedBlockchain into NodeOriginConfig.
  • Adds a near-expiry check in node_origin::resume::decide that yields a distinct ResumeAction::NearExpiry instead of topping up.
  • Adds a dedicated metric counter for near-expiry reactive top-up declines and updates test fixtures/unit tests accordingly.

Reviewed changes

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

Show a summary per file
File Description
crates/node/tests/sighup_signal.rs Updates resolved-config seed to include the new TTL guard field.
crates/node/tests/node_origin_pull.rs Sets reactive_topup_min_ttl in node-origin test fixtures and documents why.
crates/node/tests/anvil_bringup_shutdown_e2e.rs Extends e2e config fixture with the new TTL guard field.
crates/node/src/runtime/reload.rs Updates runtime reload test/config scaffolding to include the new field.
crates/node/src/runtime/mod.rs Wires the resolved TTL seconds into NodeOriginConfig as a Duration.
crates/node/src/node_origin/resume.rs Implements near-expiry guard, new ResumeAction::NearExpiry, and unit tests.
crates/node/src/node_origin/mod.rs Extends NodeOriginConfig with reactive_topup_min_ttl and docs.
crates/node/src/metrics.rs Adds node_pull_reactive_topup_near_expiry counter and recorder.
crates/common/src/config/types.rs Adds buyer_reactive_topup_min_ttl_secs to config schema/types.
crates/common/src/config/resolved.rs Adds resolved buyer_reactive_topup_min_ttl_secs field.
crates/common/src/config/mod.rs Defines default TTL constant and resolves the new config key.
crates/cli/tests/config_validate.rs Updates CLI config validation fixture to include the new resolved field.
crates/cli/src/commands/config.rs Documents the new config key in the config init template and related tests.

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

Comment thread crates/node/src/node_origin/resume.rs Resolved
Copilot review (#1605): an `InsufficientDeposit` rejection classifies as
`OurDeadChannel` → `wedged_channel`, which KEEPS the channel row (the reclaim
sweep needs it to `reclaimExpired` the escrowed deposit) and suppresses the
provider until expiry — it deliberately does NOT `retire_channel`/delete the row.
Fix the three comments (the `NearExpiry` variant doc, the loop arm, and the
metric doc) that mis-described this as "retires the channel". Comment-only.

Refs #1603.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu merged commit 70f7640 into main Aug 5, 2026
25 checks passed
@alpergundogdu
alpergundogdu deleted the claude/priceless-agnesi-326491 branch August 5, 2026 10:01
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.

2 participants