Repository navigation
perf(node): move download-receipt formatting off the voucher hot path (#1792 item 2) - #1868
Conversation
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
There was a problem hiding this comment.
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
RawReceiptand updatesReceiptSinkto enqueue raw typed fields instead of rendered strings. - Renders
RawReceiptinto the on-disk-stableDownloadReceiptin the background writer (and inline for the direct test sink). - Updates the client wire handler receipt recording to construct/enqueue
RawReceiptwithout 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_onerendersRawReceiptintoDownloadReceipton the async writer task beforespawn_blocking, so the hex/decimal formatting still runs on a Tokio worker thread. Moving the render (but not the dequeue-time timestamp capture) into thespawn_blockingclosure 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
sizeand that timestamps are non-zero. Since this PR introduces theRawReceipt -> DownloadReceiptrendering step, the test should also assert thathash,client_node_id, andvoucher_amountare 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.
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
CI note: two flaky failures on
|
Coverage reportTotal: 82.07% (was 82.07%, ▲ +0.01%) — 68080/82949 lines
History
|
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
U256decimal (to_string), and aSystemTime::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
RawReceipt— the receipt as the hot path produces it: typed identifiers (Hash, node-id bytes,U256amount, size) with no rendering and no clock read. Every field isCopy, sorecord_receiptbecomes a few field moves plus the existing non-blockingtry_send.ReceiptSinkseam now carriesRawReceiptinstead of the renderedDownloadReceipt.DownloadReceipt(RawReceipt::render) as it dequeues — the two hex encodes, the decimaluint256, 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
DownloadReceiptand theserialized_line_is_wire_stabletest are untouched — the JSONL wire format is byte-for-byte identical.Timestamp semantics
timestamp_secsis 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:
channel_state_store's process-globalLaneBuffermutex 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.incentive-crate PayWord keccak walk to run outside the per-lane lock (optimistic verify + re-check).pool_floorsharding) — item 7 is explicitly "measure before sharding".Testing
cargo build -p decdn-node— cleancargo clippy -p decdn-node --all-targets— clean (no anti-panic policy violations)cargo test -p decdn-node --lib receipt_log— 17 passedcargo test -p decdn-node --test client_loopback receipt— 3 passed (incl. the realChannelReceiptSink+ background-writer path and the stalled-writer delivery test)cargo fmt --check— cleanCloses #1792 item 2.
🤖 Generated with Claude Code
https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
Generated by Claude Code