Skip to content

perf(node,incentive): run the PayWord keccak walk off the per-lane lock (#1792 item 5) - #1873

Merged
alpergundogdu merged 3 commits into
mainfrom
claude/issue-1792-bnzq86
Aug 30, 2026
Merged

alpergundogdu merged 3 commits into
mainfrom
claude/issue-1792-bnzq86

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

What

Addresses item 5 of #1792 (Group E of the serve-path hot-path audit): "The PayWord keccak walk runs under the per-lane lock, with payer-influenced length."

Accepting a released hash-chain preimage hashed up to 255 keccaks (index − verified, a length the payer controls by releasing sparse indices) inside the per-lane tokio::Mutex. A payer trickling sparse indices could make every sibling stream on the lane wait out that walk — worst-case tens of µs of serialized keccak per proof, adding pause/resume latency at every credit-window boundary (ADR 005 §Payment lanes). The buyer half already keeps its equivalent walk off the issuance lock; this brings the seller side in line.

Change

Split the seller-side fold (incentive/src/lane.rs) into an optimistic verify:

  • LaneState::preimage_frontier(root, index) -> Option<(u8, B256)> — an O(1) snapshot of the (verified_index, tip) a reveal must hash to, or None when it folds nothing (root not tracked, or index at/below the frontier). Read under the lock; hashes nothing.
  • LaneState::advance_preimage_verified(root, index, preimage, walked, walked_ok) — applies a preimage whose forward walk the caller ran off the lock, trusting that result only while the lane's live frontier still equals the snapshot. A concurrent same-lane sibling that advanced the chain in the gap moved the frontier, so it re-hashes against the live tip under the lock (the rare race). The tracked-root, at-or-below-frontier, and spending-cap gates stay O(1) under the lock.
  • advance_preimage now delegates to these two, so its single-lock behavior — and every existing incentive test — is unchanged.

The node's commit_one_preimage (handlers/client/voucher.rs) now:

  1. snapshots the frontier under the lock (O(1), hashes nothing),
  2. releases the lock and runs verify_forward with no lane contention,
  3. re-locks and applies via advance_preimage_verified.

So the lock hold becomes O(1) per preimage. The advance itself stays atomic under the lock, so two streams still cannot lose a reveal.

Correctness

The safety of moving the walk off the lock rests on the trust gate: the caller's out-of-lock verify_forward result is trusted iff the live (verified_index, tip) still equals what it hashed against. Roots are random 256-bit, so a retired epoch never reappears; verified_index only advances, so a frontier match ⟺ unchanged. On any mismatch (a sibling advanced, or the snapshot said "covered" but a voucher adopted the root in the gap) the value is re-hashed under the lock — never trusted blindly. New tests pin this:

  • a walk against the live frontier yields exactly what the single-lock advance_preimage does;
  • a stale snapshot is re-hashed and the reveal still lands, crediting only the uncovered steps;
  • a walked_ok = true claimed against a stale frontier cannot smuggle in a bad preimage — it's re-hashed and rejected;
  • a covered reveal ignores the verdict entirely.

Testing

  • cargo build -p decdn-node / clippy --all-targets — clean (no anti-panic policy violations)
  • cargo test -p decdn-incentive --lib — 261 passed (incl. 5 new optimistic-walk tests)
  • cargo test -p decdn-node --lib — 771 passed
  • cargo test -p decdn-node --test client_loopback — 111 passed (full paid-path family, incl. concurrent same-lane streams and hash-chain reveals)
  • cargo fmt --check and cargo doc --workspace --no-deps --document-private-items (-D warnings) — clean

Closes #1792 item 5.

🤖 Generated with Claude Code

https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu


Generated by Claude Code

Group E item 5 of the serve-path hot-path audit (#1792): accepting a
released hash-chain preimage hashed up to 255 keccaks (index − verified,
a payer-influenced length) inside the per-lane lock. A payer releasing
sparse indices could make every sibling stream on the lane wait out that
walk, adding pause/resume latency at each credit-window boundary — the
buyer half already keeps its equivalent walk off the issuance lock.

Split the seller-side fold into an optimistic verify:

- `LaneState::preimage_frontier` — an O(1) snapshot of the
  `(verified_index, tip)` a reveal must hash to, or `None` when it folds
  nothing. Read under the lane lock; hashes nothing.
- `LaneState::advance_preimage_verified` — applies a preimage whose
  forward walk the caller ran OFF the lock, trusting that result only
  while the lane's live frontier still equals the snapshot; a concurrent
  sibling that advanced the chain moved the frontier, so it re-hashes
  against the live tip under the lock (the rare race). Tracked-root,
  at-or-below-frontier, and spending-cap gates stay O(1) under the lock.
  `advance_preimage` now delegates to these, so its single-lock behavior
  (and every incentive test) is unchanged.

The node's `commit_one_preimage` snapshots the frontier under the lock,
releases it, runs `verify_forward` with no lane contention, then re-locks
and applies — so the lock hold is O(1) per preimage. The advance itself
stays atomic under the lock, so two streams cannot lose a reveal.

New incentive tests cover the trust gate: a walk against the live
frontier matches the single-lock form; a stale snapshot is re-hashed and
still lands; a `true` verdict claimed against a stale frontier cannot
bypass the walk; a covered reveal ignores the verdict.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
Copilot AI lite review requested due to automatic review settings August 30, 2026 12:09
The incentive crate's test module does not allow `clippy::expect_used`
(the workspace-wide anti-panic deny applies there), so the `.expect()` on
the `preimage_frontier` Option failed the workspace clippy job. Use a
`let else` bail instead, matching the surrounding tests.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu

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 improves the paid-delivery hot path by moving the seller-side PayWord hash-chain “keccak walk” out from under the per-lane tokio::Mutex, reducing lock hold time from O(steps) (up to 255) to O(1) in the common case while preserving atomic lane advancement.

Changes:

  • Add an optimistic snapshot/apply API in LaneState (preimage_frontier + advance_preimage_verified) and keep advance_preimage as a single-lock convenience wrapper.
  • Update the node’s commit_one_preimage flow to snapshot under lock, verify forward off-lock, then re-lock and apply with a trust gate + under-lock fallback.
  • Add incentive-crate tests covering live-frontier, stale-frontier, and “lying walked_ok” scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
crates/node/src/handlers/client/voucher.rs Uses the new optimistic API to perform verify_forward off the per-lane lock during preimage commits.
crates/incentive/src/lane.rs Introduces preimage_frontier / advance_preimage_verified, refactors advance_preimage, and adds optimistic-walk tests.

💡 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
advance_preimage can return PoolError::CapExceeded (the spending-cap gate
lives in advance_preimage_verified, which it delegates to), but its
rustdoc # Errors listed only BadPreimage. Add CapExceeded so callers see
the full error surface.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
@github-actions

Copy link
Copy Markdown

Coverage report

Total: 82.11% (was 82.09%, ▲ +0.02%) — 68315/83200 lines

File Coverage Lines Δ
crates/incentive/src/lane.rs 94.54% 952/1007 ▼ 0.01%
crates/node/src/handlers/client/voucher.rs 93.31% 502/538 ▲ +0.10%
History
Commit Total Δ vs base
47d9fb0 (HEAD) 82.11% ▲ +0.02%

@alpergundogdu
alpergundogdu merged commit ded52e7 into main Aug 30, 2026
25 checks passed
@alpergundogdu
alpergundogdu deleted the claude/issue-1792-bnzq86 branch August 30, 2026 14:33
yigitdot added a commit that referenced this pull request Aug 30, 2026
One conflict, in `dispatch.rs`'s `use super::{…}` list. Main's per-lane
perf work (#1870/#1872/#1873) dropped `Mutex` from it when the accepted
`LaneState` moved into the lane instead of being cloned; this branch had
added `FloorRefusalSite` for the split refusal logging. Took the union:
main's list, minus `Mutex` (no remaining use in the file), plus
`FloorRefusalSite`.

Everything else auto-merged, including both serve loops where the perf
commits and this branch's floor calls are adjacent. Verified the floor
surface survived intact: `release_if_repaid` and the pool-only mid-stream
re-check in `delivery.rs` / `serve_leg.rs`, both admission gates and the
refusal logging in `dispatch.rs`, and the accumulator itself.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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,incentive): voucher path — global LaneBuffer mutex nested under the per-lane lock; per-proof clones, allocs, and syscalls

3 participants