Skip to content

perf(node): move download-receipt formatting off the voucher hot path (#1792 item 2) - #1868

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

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

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

What

Addresses item 2 of #1792 (Group E of the serve-path hot-path audit): "Receipt formatting runs inline per credited voucher."

Per credited voucher/preimage, the paid-delivery task was rendering the audit receipt inline before enqueuing it: two 64-char hex encodings, a U256 decimal (to_string), and a SystemTime::now() syscall — wire.rs → receipt_log.rs. Only the disk append was already offloaded (#803); the formatting was pure per-proof allocator and CPU churn on the hot path.

Change

  • New RawReceipt — the receipt as the hot path produces it: typed identifiers (Hash, node-id bytes, U256 amount, size) with no rendering and no clock read. Every field is Copy, so record_receipt becomes a few field moves plus the existing non-blocking try_send.
  • The ReceiptSink seam now carries RawReceipt instead of the rendered DownloadReceipt.
  • The background receipt writer renders the on-disk DownloadReceipt (RawReceipt::render) as it dequeues — the two hex encodes, the decimal uint256, and the timestamp — in the task that already owns the disk write. DirectReceiptSink (test/loopback) renders inline as before.

The proof-commit path now sheds three allocations, the string formatting, and one syscall per credited voucher.

What is deliberately unchanged

  • On-disk format. DownloadReceipt and the serialized_line_is_wire_stable test are untouched — the JSONL wire format is byte-for-byte identical.
  • Payment correctness. The receipt log is an audit-only artifact; this touches neither the lane critical section nor the voucher state machine.

Timestamp semantics

timestamp_secs is now stamped when the background writer dequeues the receipt rather than at the acceptance point. Receipts drain FIFO in acceptance order, so it lags only by the queue-drain latency and stays monotonic — the trade the issue explicitly sanctions ("timestamp there or from a coarse clock"). Docstrings updated to present-tense canon.

Scope note

This PR intentionally lands only item 2 — the cleanest, self-contained, correctness-safe win. The remaining #1792 items are larger, riskier changes to the payment path and are better as separate PRs:

  • Item 1 (the headline) reshards channel_state_store's process-global LaneBuffer mutex into per-lane dirty cells, and moves the flush snapshot and settlement reads off the shared structure — a substantial change to the durability store's concurrency and crash semantics.
  • Item 5 restructures the incentive-crate PayWord keccak walk to run outside the per-lane lock (optimistic verify + re-check).
  • Items 3, 4, 6, 7 (clone reduction, coarse clock under the lane lock, ecrecover placement, pool_floor sharding) — item 7 is explicitly "measure before sharding".

Testing

  • cargo build -p decdn-node — clean
  • cargo clippy -p decdn-node --all-targets — clean (no anti-panic policy violations)
  • cargo test -p decdn-node --lib receipt_log — 17 passed
  • cargo test -p decdn-node --test client_loopback receipt — 3 passed (incl. the real ChannelReceiptSink + background-writer path and the stalled-writer delivery test)
  • cargo fmt --check — clean

Closes #1792 item 2.

🤖 Generated with Claude Code

https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu


Generated by Claude Code

Group E item 2 of the serve-path hot-path audit (#1792): per credited
voucher/preimage the delivery task rendered two 64-char hex strings, a
U256 decimal, and read SystemTime::now() before enqueuing the audit
receipt — pure per-proof allocator and CPU churn for an artifact whose
disk write was already offloaded (#803).

The paid-delivery path now enqueues a RawReceipt carrying the typed
identifiers (Hash, node id bytes, U256 amount, size) with no rendering
and no clock read — a handful of Copy field moves plus the existing
non-blocking try_send. The background receipt writer renders the on-disk
DownloadReceipt (both hex encodes, the decimal uint256, and the
timestamp) as it dequeues, in the task that already owns the disk write.

The on-disk JSONL format is unchanged (DownloadReceipt and its
wire-stability test are untouched); the receipt seam (ReceiptSink) now
carries RawReceipt. The timestamp is stamped at dequeue instead of at
acceptance, which lags only by the queue-drain latency and stays in FIFO
order — sanctioned by the issue ("timestamp there or from a coarse
clock"). Audit-only artifact, so no effect on payment correctness.

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 29, 2026 15:55

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 paid-delivery voucher hot-path overhead in decdn-node by switching the receipt sink seam from a fully-rendered DownloadReceipt to a lightweight RawReceipt, deferring hex/decimal formatting and timestamp stamping to the background receipt writer.

Changes:

  • Introduces RawReceipt and updates ReceiptSink to enqueue raw typed fields instead of rendered strings.
  • Renders RawReceipt into the on-disk-stable DownloadReceipt in the background writer (and inline for the direct test sink).
  • Updates the client wire handler receipt recording to construct/enqueue RawReceipt without formatting or clock reads.

Reviewed changes

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

File Description
crates/node/src/receipt_log.rs Adds RawReceipt, updates the sink seam and writer loop to render receipts off the voucher hot path.
crates/node/src/handlers/client/wire.rs Switches hot-path receipt capture from DownloadReceipt::new(...) to RawReceipt::new(...).
crates/node/src/handlers/client/mod.rs Updates imports/types to match the new ReceiptSink + RawReceipt seam.
Suppressed comments (2)

crates/node/src/receipt_log.rs:835

  • append_one renders RawReceipt into DownloadReceipt on the async writer task before spawn_blocking, so the hex/decimal formatting still runs on a Tokio worker thread. Moving the render (but not the dequeue-time timestamp capture) into the spawn_blocking closure keeps all potentially CPU/alloc-heavy work off the runtime workers.
async fn append_one(log: &Arc<dyn ReceiptLog>, raw: RawReceipt) {
    let receipt = raw.render(crate::payment_settlement::unix_now());
    let log = Arc::clone(log);
    let join = tokio::task::spawn_blocking(move || {
        let res = log.append(&receipt);
        (res, receipt)

crates/node/src/receipt_log.rs:955

  • The drain-order writer test now only asserts FIFO order via size and that timestamps are non-zero. Since this PR introduces the RawReceipt -> DownloadReceipt rendering step, the test should also assert that hash, client_node_id, and voucher_amount are preserved (ignoring the timestamp value).
        // The writer stamps the timestamp itself, so identity is checked on the
        // rendered fields the raw receipt carried; `size` is the per-tag key.
        let sizes: Vec<u64> = seen.iter().map(DownloadReceipt::size).collect();
        let want: Vec<u64> = (0..8u8).map(|t| u64::from(t) * 1024).collect();
        anyhow::ensure!(sizes == want, "receipts out of FIFO order: {sizes:?}");

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

Comment thread crates/node/src/receipt_log.rs Resolved
claude added 2 commits August 29, 2026 15:59
The `cargo doc` CI job runs with `-D warnings`, which promotes
`rustdoc::private-intra-doc-links` to an error: the public docs on
`DownloadReceipt` and `RawReceipt` linked to the private `render` method.
Render them as plain code spans instead of intra-doc links.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
Addresses review feedback on the receipt-formatting change:

- append_one now reads the dequeue timestamp on the writer task (to keep
  FIFO acceptance order) but performs RawReceipt::render inside the
  spawn_blocking closure, so the hex/decimal formatting and its
  allocations never run on a runtime worker thread.
- Fix the ReceiptSink seam doc heading that still said it carries
  DownloadReceipts — it carries RawReceipt.
- Strengthen the writer drain test to assert hash, client_node_id, and
  voucher_amount survive the render (ignoring the writer-stamped
  timestamp), not just size/FIFO order.

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

Copy link
Copy Markdown
Contributor Author

CI note: two flaky failures on 02c005d, unrelated to this diff — re-running

Two checks went red on the current head and I've re-run both (once); everything else on this head is green (clippy, cargo doc, doctest, rustfmt, cargo deny, public API snapshot, packaging, ed25519 vectors, anvil e2e (settlement)).

test — failed in tests/sighup_signal.rs::persistent_sighup_observes_both_signals. Reproduced locally under the full parallel suite, but it passes 3/3 in isolation (--test sighup_signal -- --test-threads=1). It's a process-global signal test (raise(Signal::SIGHUP) hits the whole process), so it races other concurrently-running signal tests under parallel/coverage-instrumented execution. This diff touches only the audit-receipt path (receipt_log.rs, wire.rs) — nothing in signal handling or config reload — so it can't be the cause.

anvil e2e (journeys) — the live anvil + subprocess-daemon + QUIC journey run (step 11). The daemon/CLI build steps passed, and the e2e crate references no receipt types, so this diff can't affect it. The sibling anvil e2e (settlement) job passed on this same commit.

Both are pre-existing flakes in heavy/global tests, not regressions here, so there's no fix to port into this PR. I'll keep the PR watched and confirm the re-runs go green.


Generated by Claude Code

@github-actions

Copy link
Copy Markdown

Coverage report

Total: 82.07% (was 82.07%, ▲ +0.01%) — 68080/82949 lines

File Coverage Lines Δ
crates/node/src/handlers/client/mod.rs 92.73% 1568/1691 ● 0.00%
crates/node/src/handlers/client/wire.rs 95.63% 394/412 ▼ 0.06%
crates/node/src/receipt_log.rs 91.48% 526/575 ▲ +0.62%
History
Commit Total Δ vs base
02c005d (HEAD) 82.07% ▲ +0.01%

@alpergundogdu
alpergundogdu merged commit f18af9f into main Aug 29, 2026
47 of 50 checks passed
@alpergundogdu
alpergundogdu deleted the claude/issue-1792-bnzq86 branch August 29, 2026 21:35
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