Skip to content

fix(node): re-check signer cap headroom mid-stream on the serve path - #1962

Merged
alpergundogdu merged 2 commits into
mainfrom
feat/midstream-signer-cap-recheck
Sep 9, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
feat/midstream-signer-cap-recheck

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Problem

A signer's on-chain cap is shared across every provider (ADR 003 §Deposit Economics). PR #1958 confirms cap − spent headroom only at admit (one getAuthorization, cached). If the client then drains that signer's cap at another node while streaming here, this node keeps serving and collecting vouchers that redeemMany pays min(desired, cap − spent) ≈ 0 for — the node ate the delivered bytes. The loss is unbounded in blob size for a large blob (deCDN's large-file wedge), not bounded to one interval.

The mid-stream pool-solvency re-check does not catch this: the pool's remaining can be healthy (other signers' budgets) while this one signer's cap is exhausted.

Change

Completes the "signer cap is a solvency dimension like the pool" model from #1956 / #1958 / #1961 by adding the missing mid-stream leg — cheap, no eth_call, mirroring the pool re-check which reads the event-fed projection rather than the chain:

  • PoolProjection::signer_spent(pool, signer) — sums the signer's (signer, provider) lane cumulatives across every provider, i.e. its total on-chain spent. The only new projection surface. Exposed to the serve loop through a non-blocking PoolView::signer_spent_cached trait read (default None), overridden by PoolProjection and ResolvingPoolView (projection-only, never a getAuthorization).
  • Serve loops (delivery.rs cache-hit + serve_leg.rs miss) compute held_cap − spent against the lane's admit-time cap on the same POOL_RECHECK_INTERVAL cadence as the pool re-check, and stop in-band with a new VoucherRejectReason::SignerCapExhausted when headroom can no longer cover a serve floor. New metric decdn_serve_stream_midstream_signer_cap_exhausted_total (wire-NotFound-equivalent at open time, consistent with fix(node): confirm signer cap headroom on-chain before admitting a serve #1958).
  • Node-to-node buyer leg classifies an upstream SignerCapExhausted as OurDeadLane, the same as SpendingCapExhausted.
  • ADR 003 §Pool solvency gains a present-tense paragraph documenting the signer-cap mid-stream re-check alongside the pool one.

Fails toward serving on the cold-start undercount: a pre-watcher-head spend not yet folded over-states headroom → does not stop. That is acceptable — the admit-time getAuthorization (#1958) already caught the already-exhausted case authoritatively, so this only catches drain since admit, which the projection deltas capture. redeemMany's min(desired, cap − spent) remains the backstop; this re-check only bounds the accepted cross-node fan-out residual (ADR 003 §Residuals), it is not a correctness gate. No mid-stream eth_call.

Spec discipline

The addition was run through decdn-spec-discipline: it is the trustless self-protection shape (the node bounds its own bad debt from a projection it folds locally), enforced in a reject path, needs no new chain/governance/wire-negotiation surface, is not redundant with the pool re-check or the admit gate, and prevents a concrete unbounded-loss failure. It only bounds a residual the ADR already accepts.

Scope

Only the mid-stream signer re-check + the signer_spent projection method + the reject-reason/metric + the ADR paragraph. The admit-time gate (#1958), the redeemer (#1961), and the pool re-check itself are untouched beyond adding the sibling check.

Tests

  • signer_spent sums a signer's lanes across providers; unknown pool → 0; tracks a drain since admit.
  • Mid-stream re-check trips when a signer drains its cap at other nodes.
  • Cold-start undercount (unfolded pool) and no-pool-view both fail toward serving.

Full gate suite green: cargo build; cargo clippy --all-targets --all-features -D warnings; cargo nextest run -p decdn-node (1101 passed) + -p decdn-protocol (157 passed); cargo fmt --check; RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --document-private-items.

🤖 Generated with Claude Code

A signer's on-chain `cap` is shared across every provider, and #1958 confirms
`cap - spent` headroom only at admit. If the client then drains that signer's
cap at another node while streaming here, this node keeps serving and collecting
vouchers `redeemMany` pays `min(desired, cap - spent) ≈ 0` for — the node ate the
delivered bytes, unbounded in blob size for the large-file wedge. The mid-stream
POOL-solvency re-check does not catch it: the pool's `remaining` stays healthy on
other signers' budgets while this one signer's cap is exhausted.

Add a per-signer sibling to the pool re-check, on the same POOL_RECHECK_INTERVAL
wall-clock cadence and reading the same event-fed projection (no chain call):

- `PoolProjection::signer_spent(pool, signer)` sums the signer's lane cumulatives
  across every provider — its total on-chain `spent`. Exposed for the serve loop
  via a non-blocking `PoolView::signer_spent_cached` trait read, overridden by the
  projection and `ResolvingPoolView` (projection-only, never a getAuthorization).
- The serve loops (delivery.rs cache-hit + serve_leg.rs miss) compute
  `held_cap - spent` against the lane's admit-time `cap` each interval and stop
  in-band with a new `VoucherRejectReason::SignerCapExhausted` when it can no
  longer cover a serve floor. New metric
  `decdn_serve_stream_midstream_signer_cap_exhausted_total`.

Fails toward serving on the cold-start undercount (a pre-watcher-head spend not
yet folded over-states headroom): the admit-time getAuthorization already caught
the already-exhausted case authoritatively, so this only catches drain since
admit, and `redeemMany`'s `min(desired, cap - spent)` is the backstop — this
re-check only BOUNDS over-delivery, it is not a correctness gate. ADR 003 §Pool
solvency documents the signer-cap mid-stream re-check alongside the pool one.

The node-to-node buyer leg classifies an upstream `SignerCapExhausted` as
`OurDeadLane`, the same as `SpendingCapExhausted`.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI lite review requested due to automatic review settings September 9, 2026 11:57

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.

🟡 Changes recommended

The new mid-stream check currently computes signer_spent by scanning all lanes in a pool (O(lanes)) on a frequent cadence, which can create avoidable hot-path CPU cost under load.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR extends the node’s serve-path solvency model by adding a mid-stream re-check for signer-level on-chain cap headroom (cross-provider cap − spent), so a signer that drains its shared cap at other providers cannot cause unbounded over-delivery on long streams.

Changes:

  • Adds PoolProjection::signer_spent(pool, signer) and a PoolView::signer_spent_cached trait hook to read per-signer cross-provider spent from the event-fed projection (no eth_call).
  • Introduces a new mid-stream stop condition VoucherRejectReason::SignerCapExhausted, plus a dedicated counter metric for observability.
  • Wires the mid-stream signer-cap check into both cache-hit and miss serve loops, and documents the new re-check in ADR 003.
File summaries
File Description
crates/protocol/tests/snapshots/public_api__public_api.snap Updates public API snapshot for the new reject reason.
crates/protocol/src/client.rs Adds VoucherRejectReason::SignerCapExhausted and includes it in tests.
crates/node/src/pool_view.rs Adds PoolView::signer_spent_cached + PoolProjection::signer_spent and unit tests.
crates/node/src/payment_settlement.rs Implements signer_spent_cached for ResolvingPoolView via the projection.
crates/node/src/node_origin/mod.rs Treats upstream SignerCapExhausted as OurDeadLane (buyer-leg classification).
crates/node/src/metrics.rs Adds decdn_serve_stream_midstream_signer_cap_exhausted_total counter and pins it in tests.
crates/node/src/handlers/client/serve_leg.rs Adds mid-stream signer-cap headroom re-check to the miss serve loop.
crates/node/src/handlers/client/mod.rs Adds signer_spent_cached and signer_cap_drained_midstream helper logic + tests.
crates/node/src/handlers/client/delivery.rs Adds mid-stream signer-cap headroom re-check to the cache-hit deliver loop.
adr/003-payments.md Documents the mid-stream signer cap re-check alongside the pool solvency re-check.
Review details
  • Files reviewed: 10/10 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/node/src/pool_view.rs Resolved
Address Copilot review on #1962: `PoolProjection::signer_spent` scanned the
pool's whole lane map (O(lanes)) on every mid-stream re-check — `streams × lanes`
on a busy pool at the POOL_RECHECK_INTERVAL cadence. Fold a per-signer `spent`
total into `PoolEntry` in `record_redeemed`, from the same positive lane delta
`total_redeemed` already folds, so the read is an O(1) map lookup. Idempotent and
monotone under replay for the same reason `total_redeemed` is (only positive
advances fold); `record_resolved` seeds it empty and it rebuilds off post-seed
deltas, exactly like `total_redeemed`.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

Coverage report

Total: 82.72% (was 82.69%, ▲ +0.03%) — 74658/90258 lines

File Coverage Lines Δ
crates/node/src/handlers/client/delivery.rs 92.73% 319/344 ▼ 1.15%
crates/node/src/handlers/client/mod.rs 94.94% 2066/2176 ▲ +0.46%
crates/node/src/handlers/client/serve_leg.rs 75.14% 139/185 ▼ 5.57%
crates/node/src/metrics.rs 94.65% 884/934 ▲ +0.01%
crates/node/src/node_origin/mod.rs 92.91% 1009/1086 ● 0.00%
crates/node/src/payment_settlement.rs 62.18% 873/1404 ▼ 0.09%
crates/node/src/pool_view.rs 98.02% 396/404 ▲ +0.31%
crates/protocol/src/client.rs 97.53% 1064/1091 ● 0.00%
History
Commit Total Δ vs base
c8462bc (HEAD) 82.72% ▲ +0.03%

@alpergundogdu
alpergundogdu merged commit a663994 into main Sep 9, 2026
25 checks passed
@alpergundogdu
alpergundogdu deleted the feat/midstream-signer-cap-recheck branch September 9, 2026 12:20
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