Repository navigation
fix(node): confirm signer cap headroom on-chain before admitting a serve - #1958
Conversation
The signer-level twin of #1956. A capability's `spending_cap` is shared across every provider, so a signer that has drawn its full cap at other nodes is uncashable — yet the pool-confirm gate only checks pool-level solvency. A "spent" capability presented to a new/unaware node passed admission and got served for vouchers the node can never redeem (`redeemMany` pays `min(desired, cap − spent) = 0`), and the same exhausted capability sprayed across N fresh nodes yields N × cap free bytes. Confirm the signer at admission too. `PoolView` gains `signer_cap_headroom_micro`; the production `ResolvingPoolView` reads the signer's on-chain `{cap, spent}` with one `getAuthorization` (briefly cached, so a repeat fetch does no eth_call) and the admit gate refuses `SignerCapExhausted` when a registered signer's `cap − spent` cannot cover a serve floor. An unregistered signer has spent nothing on-chain, so it admits on its off-chain capability; a `getAuthorization` fault refuses. Wire-collapses to `NotFound` (no leak) with its own `decdn_serve_stream_rejected_signer_cap_exhausted_total` counter. No chain wired (dev/test) keeps prior behavior. Amends ADR 003 §Pool solvency with the signer cap-headroom admission precondition. Co-Authored-By: Claude Opus 4.8 <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The new signer-headroom logic has a confirmed fail-open edge for cap == 0 && expiry != 0 authorizations and introduces an unbounded (pool_id, signer) auth cache that can grow without limit under request floods.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR closes a payment-model hole by adding an admission-time on-chain check that the request’s voucher signer still has shared spending-cap headroom (cap − spent) for the pool, preventing nodes from serving against signers whose capabilities are already exhausted elsewhere (and therefore uncashable at redemption).
Changes:
- Extend
PoolViewwithsigner_cap_headroom_micro(pool_id, signer)and implement a productiongetAuthorization-backed, short-TTL cache inResolvingPoolView. - Add an admission gate in the client dispatch path to refuse when signer headroom cannot cover a serve floor (or the on-chain read faults), collapsing to
NotFoundon-wire. - Add a dedicated rejection reason + metric (
*_signer_cap_exhausted_total) and wire it into handler metrics recording; amend ADR 003 accordingly.
File summaries
| File | Description |
|---|---|
| crates/node/src/pool_view.rs | Adds the PoolView API for signer cap-headroom checks with a fail-open default for no-chain/test projections. |
| crates/node/src/payment_settlement.rs | Implements ResolvingPoolView::signer_cap_headroom_micro via getAuthorization with TTL caching and adds unit tests. |
| crates/node/src/handlers/client/dispatch.rs | Adds admission-time refusal when signer headroom cannot cover a serve floor (or read faults). |
| crates/node/src/handlers/client/mod.rs | Introduces a new ServeRejectReason::SignerCapExhausted and ensures it collapses to NotFound on the wire. |
| crates/node/src/handlers/client/wire.rs | Records the new per-reason metric for signer-cap exhaustion refusals. |
| crates/node/src/metrics.rs | Adds the new counter and updates metric export tests and recorder wiring. |
| adr/003-payments.md | Updates ADR 003 to document signer headroom confirmation as an admission precondition. |
Review details
- Files reviewed: 7/7 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Coverage reportTotal: 82.63% (was 82.62%, ▲ +0.01%) — 74486/90145 lines
History
|
…cap-at-admit # Conflicts: # crates/node/src/handlers/client/mod.rs # crates/node/src/handlers/client/wire.rs # crates/node/src/metrics.rs
- Fail-open fix: a registered signer with a zero `spendingCap` (`cap == 0` but `expiry != 0`) is NOT the all-zero unregistered authorization. Treat only `cap == 0 && expiry == 0` as unconstrained; a registered zero-cap (or fully-spent) signer now reports `0` headroom and is refused instead of admitted as uncashable. Adds a test. - Bound the signer-auth cache (`AUTH_CACHE_MAX`): at the cap, prune expired entries and skip caching if still full, so a flood of distinct `(pool, signer)` pairs cannot grow the map without bound (mirrors the negative cache). - Reword the `SIGNER_AUTH_TTL` doc: the stale-cache exposure is TTL-bounded, not "one more stream". Co-Authored-By: Claude Opus 4.8 <[email protected]>
) The hint-driven redeemer re-issued a `getAuthorizations` eth_call for a lane on every accepted voucher (~1 per MiB) whenever the signer was not yet marked registered. The read only answered "is this signer registered, so should I attach a CapabilityReg?" — a question the node can answer locally: it already holds a validated `owner_sig` (captured and verified against the pool owner at admit, #1956/#1958), stored in the same fsynced row as the frontier (#1906), so a lane with redeemable value always has the material to register its signer. And `_registerCapability` is idempotent, so re-attaching a CapabilityReg for a signer another provider already registered is a harmless no-op. The read was dead weight issued per voucher — thousands of billed eth_calls per stuck lane. Resolve registration locally instead: a lane is Registered iff this node has landed its registration (persisted `registered_until` still live), else Unregistered → attach a CapabilityReg from the held `owner_sig`. The first landed redemption persists `registered_until`, so every later pass attaches nothing. The redeemer's only on-chain-derived gate is now pool solvency (the event-fed projection), exactly like the serve path. The no-`owner_sig` case stops telling the client to "re-send its capability" — the client sent it once; a redeemable lane without `owner_sig` is a node durability fault, logged at error! and skipped. Deletes `batched_authorizations` and the `RegistrationStatus::Fetched` read path. Co-authored-by: Claude Opus 4.8 <[email protected]>
…1962) * fix(node): re-check signer cap headroom mid-stream on the serve path 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]> * perf(node): fold per-signer spent as an O(1) aggregate 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]> --------- Co-authored-by: Claude Opus 4.8 <[email protected]>
Problem
The signer-level twin of the hole #1956 closed. A capability's
spending_capis a shared budget across every provider (ADR 003 §Deposit Economics). A signer that has drawn its full cap at other nodes is uncashable —redeemManypaysmin(desired, cap − spent) = 0— yet the admission path only confirmed pool-level solvency. So a "spent" capability presented to a new/unaware node passed admission, got served, and the node collected vouchers it can never redeem. The same exhausted capability sprayed across N fresh nodes yields up to N × cap free bytes, quietly defeating the per-byte payment model.Fix
Confirm the signer at admission, same shape as the pool gate:
PoolViewgainssigner_cap_headroom_micro(pool, signer) -> Option<u64>(defaultSome(u64::MAX)— bare projection / no chain never refuses).ResolvingPoolViewoverrides it: onegetAuthorizationread of the signer's on-chain{cap, spent}, briefly cached (SIGNER_AUTH_TTL), so a repeat fetch does no eth_call. Returnsu64::MAXfor an unregistered signer (spent nothing on-chain → admits on its off-chain capability),cap − spentfor a registered one,Noneon an RPC fault.dispatch.rsrefusesSignerCapExhaustedwhen a registered signer's headroom can't cover a serve floor (or the read faulted). Wire-collapses toNotFound(no balance/existence leak) with its owndecdn_serve_stream_rejected_signer_cap_exhausted_totalcounter.ADR
Amends ADR 003 §Pool solvency with "Admission confirms the signer's cap headroom", following #1956's pool + capability-validity preconditions.
Scope
Cap-exhaustion only — no expiry check, no mid-stream signer recheck, and the fan-out reservation / per-lane cap machinery is untouched. Builds on #1956 (merged).
Testing
All gates pass locally (independently re-run): build · clippy
--all-targets --all-features -D warnings· fmt · doc — clean;cargo nextest run -p decdn-node— 1121 passed, 0 skipped. NewResolvingPoolViewcoverage: registered-with-headroom →Some(cap−spent); exhausted →Some(0); unregistered →Some(u64::MAX)+ second call served from cache (no eth_call);getAuthorizationfault →None; TTL cache hit. Reject-counter metric test updated.🤖 Generated with Claude Code