Repository navigation
perf(node,incentive): run the PayWord keccak walk off the per-lane lock (#1792 item 5) - #1873
Merged
Merged
Conversation
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
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
There was a problem hiding this comment.
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 keepadvance_preimageas a single-lock convenience wrapper. - Update the node’s
commit_one_preimageflow 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.
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
Coverage reportTotal: 82.11% (was 82.09%, ▲ +0.02%) — 68315/83200 lines
History
|
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-lanetokio::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, orNonewhen 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_preimagenow 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:verify_forwardwith no lane contention,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_forwardresult 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_indexonly 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:advance_preimagedoes;walked_ok = trueclaimed against a stale frontier cannot smuggle in a bad preimage — it's re-hashed and rejected;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 passedcargo test -p decdn-node --test client_loopback— 111 passed (full paid-path family, incl. concurrent same-lane streams and hash-chain reveals)cargo fmt --checkandcargo doc --workspace --no-deps --document-private-items(-D warnings) — cleanCloses #1792 item 5.
🤖 Generated with Claude Code
https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
Generated by Claude Code