Repository navigation
fix(node): re-check signer cap headroom mid-stream on the serve path - #1962
Conversation
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]>
There was a problem hiding this comment.
🟡 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 aPoolView::signer_spent_cachedtrait hook to read per-signer cross-providerspentfrom the event-fed projection (noeth_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.
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]>
Coverage reportTotal: 82.72% (was 82.69%, ▲ +0.03%) — 74658/90258 lines
History
|
Problem
A signer's on-chain
capis shared across every provider (ADR 003 §Deposit Economics). PR #1958 confirmscap − spentheadroom only at admit (onegetAuthorization, cached). If the client then drains that signer's cap at another node while streaming here, this node keeps serving and collecting vouchers thatredeemManypaysmin(desired, cap − spent) ≈ 0for — 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
remainingcan 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-chainspent. The only new projection surface. Exposed to the serve loop through a non-blockingPoolView::signer_spent_cachedtrait read (defaultNone), overridden byPoolProjectionandResolvingPoolView(projection-only, never agetAuthorization).delivery.rscache-hit +serve_leg.rsmiss) computeheld_cap − spentagainst the lane's admit-timecapon the samePOOL_RECHECK_INTERVALcadence as the pool re-check, and stop in-band with a newVoucherRejectReason::SignerCapExhaustedwhen headroom can no longer cover a serve floor. New metricdecdn_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).SignerCapExhaustedasOurDeadLane, the same asSpendingCapExhausted.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'smin(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-streameth_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_spentprojection 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_spentsums a signer's lanes across providers; unknown pool → 0; tracks a drain since admit.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