Skip to content

fix(client): pay only for bytes the decoder has verified - #2395

Merged
alpergundogdu merged 2 commits into
mainfrom
fix/pay-on-verified
Oct 9, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
fix/pay-on-verified

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

What

The client pays for a 1 MiB chunk only once its bao decoder has verified the chunk's last byte. Before this change it paid as bytes arrived, before verification.

Follows up on #2377: rather than report wasted paid bytes, stop paying for unverified ones.

Why

  • A bad group earns its sender nothing. Before, a peer that sent garbage was paid for up to about one interval of it before the decoder caught it.
  • Steal overrun is no longer paid twice. Frames a victim lane receives past a steal split never reach the decoder, so stop() no longer pays for them. The stealer pays for that tail once.
  • Bytes paid for but never verified, ahead of a lane fault, are gone. That removes the matching double payment when another lane re-fetches the range.

How

Client (decdn-client)

  • PullReader counts the wire bytes it hands to the decoder. The decoder checks each item before it reads the next one, so that count is the verified count, and UpstreamPull::mark_verified makes it payable.
  • next_chunk pays for verified bytes before it reads, because the node may be waiting on that payment. finish() sends the closing voucher before it drains to StreamEnd, and stop() settles only verified bytes.
  • New VERIFY_LAG_BYTES (one 16 KiB chunk group) is the most wire a payer receives past a chunk boundary before it can verify that boundary. PULL_WINDOW_FLOOR carries it.

Node (decdn-node)

  • The credit-window floor is CHUNK_BYTES + VERIFY_LAG_BYTES.
  • The recoup phase (delivery.rs, serve_leg.rs) collects the newest owed chunk only once VERIFY_LAG_BYTES have gone out after it, or once the range is fully sent (voucher::pop_payable). Without this, a node parked exactly on a chunk boundary waits for a proof the payer cannot yet make.

Specs: ADR 003 § Credit Window, ADR 037, and ADR 038 § Payment metering state the rule.

Cleanup: decdn_incentive::floor_micro priced the old one-chunk floor and had no production caller. It is removed, and its test callers price the floor directly.

Rollout order

New clients against old nodes stall. An old node parks at exactly 1 MiB, and a new client cannot verify, so cannot pay for, the leaf that straddles that boundary, until the node's 10 s proof timeout fires. Old clients against new nodes are fine. Upgrade testnet nodes before releasing this client.

Behaviour changes worth knowing

  • A short top-up can now end a pull. A funding top-up that lands below the owed debt, when every byte was verified before the step, ends the pull "funding needed" after one step. The verified bytes stay in the store, and a later run re-bills only the owed tail. The test a_landing_short_of_the_owed_debt_ends_the_fill_after_one_step pins this.
  • An aborted pull still leaves its sub-chunk residual unpaid, as before. One case pays less now: with a BlobTooLarge ceiling inside the first chunk, the upstream receives nothing for up to about 1 MiB of verified bytes.

Tests

  • cargo nextest run --workspace: 4776/4776 pass.
  • fmt, clippy, and the cargo doc deny-warnings gate all pass.
  • Tightened: a peer that corrupts from wire byte 2,500,000 must be paid no more than 2,500,000 bytes (was "plus one frame"). A corrupt single-leaf upstream is paid nothing. A pull stopped at a split pays exactly what it verified.
  • New unit tests: pop_payable, and the VERIFY_LAG_BYTES term in the pull-window liveness test.
  • Updated test harnesses: hand-rolled node/peer harnesses in client_loopback.rs and node_origin_pull.rs now model the new floor and send through VERIFY_LAG_BYTES before parking.

Follow-up (not in this PR)

When a stream's last two proofs are a reveal followed by a closing voucher and the node refuses the reveal, PoolLedger::resolve_reject rewinds the closing voucher instead. The client's books then run one interval ahead of what the node credited. This is safe (the node holds the preimage), but the persisted watermark overstates acceptance. The path predates this PR.

🤖 Generated with Claude Code

The client released a payment per 1 MiB chunk as bytes arrived, before
the bao decoder checked them. A peer that sent a bad group was paid for
up to one interval of it, and frames past a steal split were paid twice.

The client now pays per chunk only once its decoder has verified the
chunk's last byte. The node's credit window floor grows to one chunk
plus one chunk group (VERIFY_LAG_BYTES), and its recoup phase collects
the newest chunk only once the leaf that completes it is on the wire,
so a stream at the floor never waits on a proof the payer cannot send.

ADR 003, 037 and 038 state the rule. floor_micro, which priced the old
one-chunk floor and had no production caller, is removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 9, 2026 12:26

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.

🔵 Needs a closer look

It rewrites payment-metering and credit-window economics across the client, node, and three ADRs on a money-handling path with a documented new-client/old-node stall hazard, so a human should give final sign-off even though no defect was found.

0 open findings

What changed in this PR

This PR tightens the paid-pull payment model so the client pays for a wire chunk only once its bao decoder has verified that chunk, rather than paying as bytes arrive. It follows up on #2377 (which observed wasted/duplicate paid bytes) by stopping payment for unverified bytes entirely, so a bad group earns its sender nothing and steal-overrun/lane-fault double payments disappear. Because a payer can only verify a chunk's last byte once the bao leaf that holds it (up to one 16 KiB chunk group past the boundary) arrives, the credit-window floor is raised from one chunk to one chunk + one chunk group (VERIFY_LAG_BYTES), and the node's recoup phase holds back the newest owed chunk until that lag is on the wire.

Changes:

  • Client (decdn-client): UpstreamPull gains a verified counter and mark_verified; payment is driven by verified bytes through a new pay_verified, called at the top of next_chunk/finish and in stop. PullReader counts bytes handed to the decoder and reports them as verified.
  • Node (decdn-node): credit-window floor is CHUNK_BYTES + VERIFY_LAG_BYTES; the recoup loops use pop_payable to defer the newest chunk until its verify lag is sent or the range is fully delivered; ensure_unpaid_bytes_tracked accounts for held chunks.
  • Specs/cleanup: ADR 003/037/038 updated to state "pay only for verified bytes" and the new floor; dead decdn_incentive::floor_micro removed (test callers price the floor directly); extensive test-harness updates.
File Description
crates/​client/​src/​lib.rs Adds verified/mark_verified/pay_verified; next_chunk/finish/stop/arrived_rejection now meter verified bytes.
crates/​client/​src/​sink.rs PullReader tracks consumed and reports it verified in fill_to/into_inner.
crates/​client/​src/​pacer.rs (+tests) New VERIFY_LAG_BYTES; PULL_WINDOW_FLOOR carries it.
crates/​client/​src/​lib.rs exports Re-exports VERIFY_LAG_BYTES; doc updates.
crates/​node/​.../​voucher.rs (+tests) Adds pop_payable; ensure_unpaid_bytes_tracked counts held owed chunks.
crates/​node/​.../​delivery.rs, serve_leg.rs Recoup loops collect via pop_payable; collected_any recomputed.
crates/​node/​.../​mod.rs, ramp.rs Credit-window floor adds VERIFY_LAG_BYTES; doc updates.
crates/​incentive/​src/​rate.rs, lib.rs (+tests) Removes floor_micro and its re-export.
crates/​common/​src/​config/​{types,resolved}.rs, cli/​.../​config.rs Config docs reflect the new floor.
crates/​e2e/​tests/​*, crates/​node/​tests/​* Harnesses model the new floor and verify-lag; tightened payment bounds.
adr/​003, adr/​037, adr/​038 Spec text for verified-byte payment and the one-chunk-group floor.

I verified: all node floor-pricing sites route through credit_window(CHUNK_BYTES, 0) (so the new lag propagates uniformly to the deposit/signer guards), meter.pay correctly emits one reveal per whole chunk for multi-chunk unproved, pop_payable always leaves a payable chunk at the front whenever the window is shut (no deadlock, and the no-progress guard remains the safety net), the pay_verified gating is correct at every boundary, the floor_micro removal leaves no dangling references, and the ADR cross-reference #payment-metering resolves. I found no concrete defects.

🧠 Review effort: Balanced


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

The refused leg verifies every byte before its refusal, so the re-ask
after the settle step is the fill's last chance. A fixed 100 ms delay
let a slow runner re-ask first. The upstream now catches up as soon as
the refused leg records its progress, which precedes the step.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-code-quality

github-code-quality Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Code Coverage Overview

Languages: Rust, Solidity

Rust / code-coverage/cargo-llvm-cov

The overall line coverage in commit 4c2cd95 in the fix/pay-on-verified branch remains at 75%, unchanged from commit e7eee84 in the main branch.

Solidity / code-coverage/forge

The overall line coverage in commit 4c2cd95 in the fix/pay-on-verified branch remains at 92%, unchanged from commit b468cad in the main branch.


Updated October 09, 2026 13:01 UTC

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

Gas snapshot

No gas changes vs base.

@alpergundogdu
alpergundogdu merged commit b7617ca into main Oct 9, 2026
30 checks passed
@alpergundogdu
alpergundogdu deleted the fix/pay-on-verified branch October 9, 2026 13:11
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.

2 participants