Skip to content

perf(node,incentive,contracts): batch redeemer auth reads + drop per-top-up allowance read - #1727

Merged
alpergundogdu merged 10 commits into
mainfrom
claude/redeemer-batch-and-allowance-badpath
Aug 18, 2026
Merged

alpergundogdu merged 10 commits into
mainfrom
claude/redeemer-batch-and-allowance-badpath

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Summary

First of three PRs from an eth_call-efficiency sweep of the paid path. This one bundles two independent, additive workstreams that both cut redundant on-chain reads. The remaining two (lazy origin directory, watcher get_logs/block-number consolidation) follow as separate PRs.

1. Drop the per-top-up allowance() read (node daemon)

The node holds a standing unlimited USDC approval granted once at bootstrap, yet every fund_pool top-up still did an ERC20.allowance() read before topUp — one redundant eth_call per top-up across thousands of pools. Now fund_pool attempts topUp directly and only reads allowance + approves + retries once when topUp reverts with an ERC20InsufficientAllowance shortfall (the bad path: approval revoked/never granted). Detection is by revert selector match (is_erc20_allowance_shortfall), surfaced through the shared top_up via an AllowanceShortfall marker. The CLI open/top-up path (bounded deposit, no standing approval) keeps its pre-check and is untouched.

2. Batch the redeemer's getAuthorization reads

The settlement redeemer read PaymentPool.getAuthorization(pool, signer) once per lane every sweep, purely to learn whether a signer's capability was already registered — thousands of eth_calls per sweep at scale, and a thundering herd on every restart. This PR:

  • adds an additive PaymentPool.getAuthorizations(bytes32[], address[]) batch view (+ alloy binding, selector-pinned);
  • adds a durable per-lane registered_until: u64 watermark on LaneState/StoredLaneState (the observed capability expiry; 0 = unknown);
  • makes the hot path skip the read entirely when registered_until > now, batches the remaining unknown/expired lanes into one getAuthorizations call per sweep, and persists observed expiry so restarts re-read nothing already known.

Registration state is persisted with the lane (survives version-bump restarts), and record merges registered_until monotonically (max) so a concurrent voucher-path write carrying 0 can't clobber a known registration. The monotone merge is safe because on-chain _registerCapability is idempotent and expiry is immutable once set. The old per-lane plan_redeem is deleted.

Net: steady-state redeemer auth reads ≈ zero; a fresh restart with thousands of lanes = one batched call, not thousands.

Testing

  • New unit tests (contract batch view + length-mismatch revert; registered_until round-trip; monotone/clobber-safe store merge in both memory + persistent stores; is_registered/plan_lane register-decision cases; allowance-shortfall selector + bad-path retry control-flow with zero-read/one-read counters).
  • Anvil e2e proves a second sweep on the same lane issues no CapabilityReg (asserted via the redeemer's captured cap_count, verified to fail under a deliberately-injected regression).
  • Full gates green at the tip: cargo build && clippy, nextest (workspace), fmt, cargo deny, anvil-e2e compile + live run; contracts forge fmt/build --deny warnings/test (820/820), gas snapshot committed, aderyn/slither no new findings.

Notes

  • Additive contract change only — no storage layout change, no existing function touched.
  • Pre-launch: on-disk lane format changed in place (no migration shim), per repo convention.

🤖 Generated with Claude Code

alpergundogdu and others added 9 commits August 18, 2026 11:21
Extend the seller-settlement anvil e2e with a second sweep on the same
lane: the first redeem attaches a CapabilityReg and persists
registered_until, and the second sweep advances the paid watermark
without needing a fresh Authorization read. Asserted against the live
chain (getAuthorization) and, via the harness's PersistentPoolStateStore,
against the persisted registered_until.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The anvil e2e's second-sweep-attaches-no-CapabilityReg proof previously
compared redeemMany gas usage between the two redeems, but a resent
CapabilityReg is a near-gas-free no-op once PaymentPool._registerCapability
observes cap != 0 (it returns before its SSTORE/signature check), so the
gas-delta assertion could not actually detect a regression that re-attaches
one. Replace it with a tracing_subscriber Layer that captures the
redeemer's own cap_count field per landed redeem, keyed by tx hash, and
assert on that directly for both sweeps.

Also add a brief settle wait before the second delivery: firing it
back-to-back with the first redeem's on-chain confirmation occasionally
raced the redeemer's own bookkeeping closely enough to flake even though
the store already showed registered_until persisted at that instant.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI lite review requested due to automatic review settings August 18, 2026 12:27
@github-actions

github-actions Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

Contracts coverage

Line coverage: 89.68% (2545/2838 lines)
▲ +0.02pp vs base (89.66% → 89.68%)
Run: 32141505298

@github-actions

Copy link
Copy Markdown

Gas snapshot

No gas changes vs base.

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 is the first in an eth_call efficiency sweep of the paid path. It reduces redundant on-chain reads by (1) removing a per-top-up allowance() pre-check in the node daemon and (2) batching redeemer authorization reads while persisting a per-lane registered_until watermark to avoid future reads.

Changes:

  • Node buyer top-up now attempts topUp directly and only performs a just-in-time approve + single retry on an ERC20InsufficientAllowance-class revert.
  • Settlement redeemer batches PaymentPool.getAuthorization reads via a new getAuthorizations(poolIds, signers) view and persists registered_until so steady-state sweeps can skip reads.
  • Adds/updates unit tests, e2e assertions, and contract tests to pin the batching behavior and persistence semantics.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
crates/node/tests/channel_state_store.rs Adds persistence round-trip tests for registered_until and regression resistance.
crates/node/tests/anvil_settlement_e2e.rs Extends e2e to assert second sweep attaches no CapabilityReg, capturing redeemer decision via tracing.
crates/node/src/payment_settlement.rs Implements batched authorization reads, registered_until gating, and persistence writeback.
crates/node/src/channel_store.rs Extends persisted lane record to include registered_until and adds monotone merge/write APIs.
crates/node/src/buyer_channel.rs Adds allowance-recovery retry control-flow and switches funding to “try topUp first” posture.
crates/incentive/src/store.rs Adds set_registered_until to PoolStateStore and monotone merge semantics in memory store.
crates/incentive/src/pool_open_error.rs Adds selector matcher helper for allowance shortfall detection from revert data.
crates/incentive/src/payment_pool.rs Updates alloy bindings/docs to include getAuthorizations and pins its selector.
crates/incentive/src/lib.rs Re-exports is_erc20_allowance_shortfall.
crates/incentive/src/lane.rs Adds registered_until to LaneState and clarifies hydration semantics/tests.
crates/client-pull/src/buyer_pool.rs Tags top_up submit errors as allowance-shortfall for daemon retry logic.
contracts/test/PaymentPool.t.sol Adds tests for getAuthorizations correctness and length-mismatch revert.
contracts/src/PaymentPool.sol Adds getAuthorizations batch view and LengthMismatch error.
contracts/.gas-snapshot Updates gas snapshot to reflect the additive contract/test changes.

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

Comment thread crates/client-pull/src/buyer_pool.rs Resolved
Comment thread crates/node/src/payment_settlement.rs Resolved
Comment thread crates/node/src/channel_store.rs Resolved
@github-actions

github-actions Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 80.38% (was 80.35%, ▲ +0.03%) — 58144/72340 lines

File Coverage Lines Δ
crates/client-pull/src/buyer_pool.rs 51.96% 159/306 ▼ 1.16%
crates/incentive/src/lane.rs 96.15% 375/390 ▲ +0.10%
crates/incentive/src/payment_pool.rs 98.75% 79/80 ▲ +0.05%
crates/incentive/src/pool_open_error.rs 97.09% 100/103 ▲ +1.31%
crates/incentive/src/store.rs 91.64% 263/287 ▼ 0.48%
crates/node/src/buyer_channel.rs 36.06% 256/710 ▲ +11.27%
crates/node/src/channel_store.rs 85.75% 969/1130 ▲ +0.13%
crates/node/src/payment_settlement.rs 44.92% 504/1122 ▲ +7.85%
History
Commit Total Δ vs base
e9476d9 80.38% ▲ +0.03%
2f374e1 (HEAD) 80.38% ▲ +0.03%

…/allowance PR

- payment_settlement: make the `registered_until` writeback O(n) — walk the
  already-built `read_keys` (each a unique `(pool_id, signer)` at
  `provider == self`) and look each auth up in the map, instead of rescanning
  every lane per auth.
- channel_store: reword the `registered_until` doc. The old text implied no
  prior on-disk shape exists; state plainly that a record written before the
  field fails to decode (`take_from_bytes` -> `DeserializeUnexpectedEnd`) and
  that this break is the deliberate, unversioned pre-launch choice it is.
- buyer_pool: add a regression test proving `top_up`'s
  `.context("submit topUp").context(AllowanceShortfall)` layering is still
  recoverable via `downcast_ref::<AllowanceShortfall>()` (anyhow searches the
  context chain), so the daemon approve-and-retry path is not dead code. The
  existing tests injected the marker as a bare error and never exercised this.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu merged commit b5de871 into main Aug 18, 2026
26 checks passed
@alpergundogdu
alpergundogdu deleted the claude/redeemer-batch-and-allowance-badpath branch August 18, 2026 13:38
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