Repository navigation
feat(node): don't reactively top up a near-expiry channel mid-pull - #1605
Conversation
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]>
|
Warning Review limit reached
Next review available in: 45 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Free Run ID: 📒 Files selected for processing (13)
Note 🎁 Summarized by CodeRabbit FreeYour 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 |
Coverage reportTotal: 80.49% (was 80.47%, ▲ +0.02%) — 58092/72172 lines
History
|
There was a problem hiding this comment.
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(default86_400,0disables) and plumbs it throughResolvedBlockchainintoNodeOriginConfig. - Adds a near-expiry check in
node_origin::resume::decidethat yields a distinctResumeAction::NearExpiryinstead 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.
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]>
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::decideonly checked spendable headroom (viagenuine_exhaustion) — never the channel's remaining time toexpires_at.topUpdeliberately 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 untilreclaimExpired, and risking the pull failing anyway. The proactive low-water refill only runs behind thetry_reuse_livenon-expired gate; the reactive leg had no equivalent expiry posture.What this PR does (the immediate correctness piece)
blockchain.buyer_reactive_topup_min_ttl_secs— minimum remaining time to a channel's on-chainexpires_atbelow which a reactive top-up is refused. Default86_400(~1 day);0disables 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 throughResolvedBlockchain→NodeOriginConfig.reactive_topup_min_ttlexactly likeworking_deposit/event_poll_intervalwere.expires_at,decidereturns a newResumeAction::NearExpiryinstead ofTopUp. 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.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_atis read once per pull fromchannel_expiry(topUpcannot move it) and carried inResumeBudgets.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_subkeepsexpires_at < nowfrom 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 inittemplate).Refs #1603.
🤖 Generated with Claude Code