Skip to content

perf(node): hoist voucher ecrecover out of the per-lane lock (#1735) - #1742

Merged
alpergundogdu merged 2 commits into
mainfrom
claude/cool-margulis-7233ac
Aug 18, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
claude/cool-margulis-7233ac

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Problem

commit_one_voucher held the per-lane Mutex<LaneDeliveryState> (crates/node/src/handlers/client/voucher.rs) across verify_voucher → stage_voucher → verify_signer, i.e. across the elliptic-curve ecrecover (~tens of µs) — the single most expensive operation on the serve path.

Concurrent same-lane streams, which the #1697 active_streams admission cap explicitly supports (N streams sharing one lane (pool_id, signer, provider)), therefore serialized on the crypto: each stream's voucher blocked the others across the recovery.

Fix

Signature validity depends only on the voucher bytes and the lane's immutable pinned signer/pool_id/provider — never the mutable watermark. So the ecrecover moves out of the critical section:

  1. Outside the lock: reconstruct the signed voucher from the lane identity carried by lane_key and recover + verify the signer. Reject a wrong/malformed signature here with no lock held.
  2. Inside the lock: re-run the cheap amount/bytes monotonicity check against the live watermark, then advance, credit_advance, and record.

This shrinks the critical section from ecrecover-time to a few map/compare ops. Concurrent same-lane streams now recover signatures in parallel and only briefly serialize on the advance.

Changes

  • incentive (lane.rs): split LaneState::advance_presigned out of stage_voucher — the amount/bytes monotonicity + cap + advance half, for a voucher whose signature the caller already verified. stage_voucher is now pool/provider check + verify_signer + advance_presigned, behavior byte-for-byte unchanged.
  • node (voucher.rs): commit_one_voucher recovers the signer lock-free, then verify_voucher calls advance_presigned under the lock against the live watermark.

Correctness

Tests

  • wrong_signer_is_rejected_without_the_lane_lock — the signature gate is fully outside the lock (verify_signer rejects) while advance_presigned ignores the signature and would advance, proving the accept/reject decision is hoisted out of the critical section.
  • concurrent_advances_recheck_the_live_watermark_no_lost_update — a straggler recovered against a stale snapshot is a benign AmountRegression against the live watermark, not a second advance.
  • All existing voucher.rs tests stay green.

cargo build && cargo clippy && cargo nextest run -p decdn-node -p decdn-incentive && cargo fmt -- --check all pass (1142 tests); anvil-e2e compile gate green.

Closes #1735.

🤖 Generated with Claude Code

`commit_one_voucher` held the per-lane `Mutex<LaneDeliveryState>` across
`verify_voucher` → `stage_voucher` → `verify_signer`, i.e. across the
elliptic-curve `ecrecover` — the most expensive op on the serve path.
Concurrent same-lane streams (which the #1697 `active_streams` cap
supports) therefore serialized on the crypto.

Signature validity depends only on the voucher bytes and the lane's
immutable pinned `signer`/`pool_id`/`provider`, never the mutable
watermark. So recover + verify the signature OUTSIDE the lock (identity
read from `lane_key`), and keep only the watermark-dependent monotonicity
re-check + advance inside it.

- incentive: split `LaneState::advance_presigned` out of `stage_voucher`
  (the amount/bytes monotonicity + cap + advance half, signature already
  verified). `stage_voucher` now = pool/provider check + verify_signer +
  advance_presigned, byte-for-byte identical behavior.
- node: `commit_one_voucher` recovers the signer lock-free (high-`s`
  rejection #836 preserved; malformed → BadSignature, wrong → WrongSigner,
  neither watermark-gated so no resume bundle), then re-checks
  monotonicity against the LIVE watermark under the lock and advances.
  The monotonicity-check + advance stay atomic per lane — no lost update.
- Preserves AlreadySatisfied benign-supersede and the #1699 divergent
  equal-amount rejection.

Tests: add `wrong_signer_is_rejected_without_the_lane_lock` and
`concurrent_advances_recheck_the_live_watermark_no_lost_update`. All
existing voucher tests stay green.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI lite review requested due to automatic review settings August 18, 2026 15:15

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 reduces lock contention on the node’s paid-delivery serve path by moving voucher signature recovery (ecrecover) out of the per-lane mutex critical section, while keeping watermark monotonicity checks and advances atomic under the lock. It also refactors the incentive lane state machine to expose a “signature already verified” advance path used by the node.

Changes:

  • Hoist voucher signature reconstruction + signer verification out of the per-lane lock in commit_one_voucher.
  • Split LaneState::stage_voucher so the watermark-dependent monotonicity + cap + advance logic can run as advance_presigned after signature verification.
  • Add targeted tests validating that wrong-signer rejection happens lock-free and that live-watermark rechecks prevent lost updates under concurrency.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
crates/node/src/handlers/client/voucher.rs Verify signer lock-free, then re-check monotonicity + advance under the lane mutex; adds concurrency-focused tests.
crates/incentive/src/lane.rs Introduces advance_presigned and wires stage_voucher through it to enable the node-side lock hoisting.

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

Comment thread crates/incentive/src/lane.rs Resolved
@github-actions

github-actions Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 80.70% (was 80.63%, ▲ +0.07%) — 58438/72415 lines

File Coverage Lines Δ
crates/incentive/src/lane.rs 96.23% 383/398 ▲ +0.08%
crates/node/src/handlers/client/voucher.rs 89.34% 394/441 ▲ +1.59%
History
Commit Total Δ vs base
9a0d3c8 80.70% ▲ +0.07%
25c7d36 (HEAD) 80.70% ▲ +0.07%

…riant asserts

- watermark_bundle_for_reject no longer re-runs `recover_signer` under the
  per-lane lock: `commit_one_voucher` already verifies the signature against
  the pinned `signer` OUTSIDE the lock (#1735), so a wrong-signer voucher is
  rejected as WrongSigner and never reaches this reject path. Re-recovering
  here would re-do the ecrecover in the very critical section this PR removes.
  Now an associated fn (no `self`).
- verify_voucher: debug_assert that `signed` matches `wire` (advance reads
  `signed.*`, rate/floor checks read `wire.*` — they must agree).
- LaneState::advance_presigned: debug_assert the voucher's pool_id/provider
  match the lane, since the fast path skips those identity checks.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu merged commit 02b0ec6 into main Aug 18, 2026
73 of 78 checks passed
@alpergundogdu
alpergundogdu deleted the claude/cool-margulis-7233ac branch August 18, 2026 16:48
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.

perf(node): per-lane lock is held across ecrecover, serializing concurrent same-lane vouchers

2 participants