Skip to content

feat(payments): chunk voucher redemption into gas-bounded redeemMany transactions - #1704

Merged
alpergundogdu merged 11 commits into
mainfrom
claude/voucher-redemption-chunking-689531
Aug 17, 2026
Merged

alpergundogdu merged 11 commits into
mainfrom
claude/voucher-redemption-chunking-689531

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

What & why

A node's redeemer submitted one redeemMany for an entire sweep, which does not scale: a 100 Gbps node serving thousands-to-millions of distinct payers accumulates one lane per (pool, signer), and a single transaction carrying that many vouchers cannot fit in a block. Economics were never the blocker (~34.5k gas/voucher ≈ fractions of a cent on Arbitrum) — per-transaction size is.

This splits redemption into gas-bounded chunks (many cheap redeemMany transactions) and replaces the per-lane dust skip with a per-chunk aggregate value floor, so dust settles by riding alongside real value instead of being skipped or attacked lane-by-lane. Node-side only — no contract, wire, ABI, or storage change (redeemMany already takes an array).

How

  • Per-chunk floor (F), replacing the per-lane skip. plan_redeem no longer skips below-threshold lanes; a chunk (one transaction) is submitted only when its lanes' aggregate unredeemed value clears F, and every lane in a submitted chunk settles. High-value lanes are spread across chunks (descending sort + round-robin deal) so dust rides along rather than segregating into a below-floor chunk.
  • Chunking. chunk_redemptions partitions planned lanes into chunks of at most redeem_max_vouchers_per_tx (new config, default 300), splitting a pool across chunks when needed; group_by_pool rebuilds per-pool PoolBatches per chunk (each (pool, signer) lane is distinct, so no duplicate capability).
  • Reactive halve-retry. An oversized chunk that fails to send (is_oversize_send_err) is halved and retried, bounded by depth (MAX_SPLIT_DEPTH) and shrinking length — loss-free split_off, no swallowed errors on other arms.
  • Force path. Owner pool-close redeems every lane (floor = 0), now chunked, so a high-lane pool settles before its dispute deadline. Graceful shutdown keeps the normal floor (sub-floor dust defers to next boot / pool-close — no funds lost).
  • Partial-progress safety preserved. The paid side is still seeded only by PoolRedeemed events, so a deferred/failed/halved chunk simply leaves its lanes for the next sweep — cumulative, monotone, retry-safe, per chunk.

Design decisions

  • Dust needs no oracle. The floor is a node-local µUSDC value (reuses redeem_threshold_micro_usdc, default 1 USDC); against ~$0.0001 of gas per chunk it is ~100× margin. No gas→USDC conversion.
  • Serve-time solvency M is explicitly out of scope. The over-subscription / double-spend surface (a tiny pool with many self-capped capabilities across distinct lanes) is a separate serve-time concern; ADR 003's M ≥ N·t sizing is left untouched, with a one-line forward note.
  • Default 300 relies on the halve-retry backstop. Marginal gas is pinned at 34,521/voucher (assertApproxEqAbs, single-pool measurement); the high-fan-out (one-pool-per-client) case adds per-pool overhead, and the halve-retry mechanic is the designed answer to any chunk that still exceeds the limit.

Testing

  • Pure logic carries unit coverage (the async redemption path has no contract mock): chunk_redemptions (floor / count-cap / value-spread / force / empty), group_by_pool, is_oversize_send_err, config resolution (default + reject-zero).
  • New Foundry N-vs-(N+1) redeemMany gas micro-benchmark pins the marginal in tracked assertions (isolated in independent EVM state to avoid a warm-access-list artifact).
  • Contracts forge test: 816 passed / 0 failed; CI build --deny warnings + fmt clean.
  • Workspace build + clippy --all-targets clean; node+common suite 1288 passed; anvil-e2e compile gate green.

ADR 003 reframes the redemption threshold as the per-chunk floor (present-tense canon, M ≥ N·t untouched).

🤖 Generated with Claude Code

alpergundogdu and others added 9 commits August 17, 2026 13:11
… per-chunk floor

Rework plan_redeem to return Result<Option<PlannedLane>> (dropping the
threshold param and RedeemPlan enum), and wire the chunked submit path:
redeem_one/redeem_sweep/redeem_pool_on_close now collect PlannedLanes,
chunk_redemptions under the per-chunk floor + voucher-count cap, and
submit each chunk via submit_chunk with an oversize halve-and-retry.
Thread redeem_max_vouchers_per_tx through the redeemer loop, the
service struct, bootstrap, and final_redeem_sweep; the close path forces
floor 0 so a high-lane closing pool settles in chunks before its dispute
deadline.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ize default

Adds a two-test N/N+1 gas benchmark (each in its own EVM state, avoiding a
warm-access-list artifact that showed up when both calls shared one test
function) that measures redeemMany's marginal per-voucher cost at ~34.5k
gas. Against Arbitrum One's ~32M block gas limit (external reference), that
puts a safe chunk size around 458 vouchers, so the existing default of 300
holds with ~1.5x headroom — left unchanged, doc comment updated to cite the
measurement instead of an unverified assumption.
Capture the measured redeemMany gas from _redeemFreshLanesAndSnapshotGas
and assert it against pinned values (N: 507,093, N+1: 541,614; ±5,000
tolerance) so the figures cited by DEFAULT_REDEEM_MAX_VOUCHERS_PER_TX's
doc comment are checkable from tracked code, not only from the
gitignored snapshots/PaymentPool.json. Also logs the measured gas under
-vv and reconciles the doc comment's safe_max to 458 (was inconsistently
"roughly 460" against the worked 458 figure).
Reframe redeem_interval_secs docstrings from per-lane 'above-threshold claim'
to the per-chunk floor model, and import std::cmp::Reverse rather than fully
qualifying it in chunk_redemptions.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI lite review requested due to automatic review settings August 17, 2026 14:44

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Note

This error may be related to your runner configuration. You can now configure runners for Copilot code review separately from Copilot cloud agent by creating a copilot-code-review.yml file with your setup steps. Read the docs for details.

alpergundogdu and others added 2 commits August 17, 2026 15:59
Add a first-time-signer variant of the redeemMany N-vs-(N+1) gas
benchmark: each lane's CapabilityReg rides in the same measured call
as its voucher, instead of being pre-registered in a separate
unmeasured call. This isolates the realistic per-lane cost for the
target workload of one-time payers whose capability is registered on
their single redemption.

Measured: first-time marginal 63,404 gas/lane (vs 34,521 for an
already-registered signer), so registration itself costs ~28,883 gas.
A 300-lane all-first-time chunk runs ~19.0M gas — over a 16M
half-block budget but comfortably under a 32M full block. Measurement
only; DEFAULT_REDEEM_MAX_VOUCHERS_PER_TX is unchanged.
…fault

The pinned redeemMany benchmark measures both an already-registered-signer
marginal (~34.5k) and a first-time-signer marginal (~63.4k, redeem + one-time
capability registration). The one-time-payer workload hits the first-time case
per lane, so 300 lanes cost ~19M gas — under a 32M block, over a half-block
budget — with submit_chunk's halve-retry as the backstop. Keep the default 300.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu merged commit b57d55e into main Aug 17, 2026
23 of 25 checks passed
@alpergundogdu
alpergundogdu deleted the claude/voucher-redemption-chunking-689531 branch August 17, 2026 17:47
alpergundogdu added a commit that referenced this pull request Aug 17, 2026
Re-applies the per-pool floor-credit accounting onto main's reworked voucher
path (#1672 in-memory vouchers + periodic flush, #1699 self-describing vouchers,
#1704 redeemMany chunking):

- Serve loops (delivery.rs, serve_leg.rs): keep the FloorReservation note_unpaid/
  release_live_repaid/mid-stream-PoolExhausted hooks, now attached to main's
  per-voucher commit_one_voucher/VoucherStop recoup (dropping the removed
  batch-recoup + commit_interval).
- VoucherRejectReason: apply the three-way split (SpendingCapExhausted /
  CapabilityExpired / PoolExhausted) on top of main's removal of the RetryLater
  wire variant; a PoolError::Store fault now aborts the stream with no wire reason.
- node_origin voucher_verdict, ADR 003/005, public-api snapshot updated to match.

Verified: workspace build + clippy --all-targets + fmt clean; decdn-node 886/886,
protocol/incentive merge-sensitive tests, and public-api snapshot all pass.

Co-Authored-By: Claude Opus 4.8 <[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.

2 participants