Skip to content

fix(cli,incentive): adopt the pool the wallet already owns instead of opening a second - #2098

Merged
thiras merged 2 commits into
mainfrom
fix/client-adopts-owned-pool
Sep 21, 2026
Merged

thiras merged 2 commits into
mainfrom
fix/client-adopts-owned-pool

Conversation

@thiras

@thiras thiras commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

What

The client (decdn fetch, decdn bundle pull) now adopts the pool its wallet already owns on chain when its local store has no row, instead of opening a second one.

Why

This fixes a regression from #2087 that reached a user. Moving the buyer tables to _v4 made every existing client row read as absent. Their wallet held $0 USDC, all of it escrowed in one live, Open pool on the current contract ($10.00 deposit, $1.11 redeemed). The new binary could not see that pool, so it tried to escrow a second $10 and reverted:

Error: fetch bundle manifest blob: pool-open failure reason: insufficient_deposit: submit openPool:
... execution reverted: ERC20: transfer amount exceeds balance

The underlying gap: the node recovers its pool from chain, the client didn't. The node's bootstrap enumerates getPools(owner) and adopts its live pool. The client only looked at its store, which loses rows (reset data dir, new machine, store-format change) while the pool is still live on chain. enumerate_owned_pools already documents this case, and the getWatermark binding already describes the buyer reseeding a lost lane. The client just never used either.

How

  • newest_solvent_owned_pool (decdn-incentive, next to enumerate_owned_pools) finds the newest Open pool with deposit > totalRedeemed. A faulted read returns Err, never None.
  • pool_to_reuse / adopt_owned_pool (CLI) take a tracked row on this deployment, or adopt and record one from chain. The client opens a pool only when none qualifies.
  • It fails closed where the node fails open. A read that faults aborts the fetch instead of answering "no pool": a one-shot fetch can be rerun, and "could not tell" must not become a second deposit. The asymmetry is documented, so a later "make them consistent" edit doesn't quietly weaken the client.

Adoption also needs three things to be right:

  1. Lane resume. A lane with no local record resumes from getWatermark, not zero. At or below the watermark every voucher redeems nothing on chain, and an adopted pool has no local lanes. This also affects a tracked pool's first contact with a new provider: that's one extra read per new lane, and a fault on it now aborts.
  2. Refill counts totalRedeemed. An adopted row has no lanes to account for what the pool has already paid out. Without this, a pool that other lanes drained reads as a full deposit, no top-up fires, and the provider refuses the fetch. The refill policy's own doc already defines prior_amount as spend across its lanes.
  3. Never from a node's data dir. decdn fetch and bundle pull escrow into a node's data dir under the node's own keystore #2082 still accepts an explicit --data-dir <node dir>, and there the keystore is the operator's. The wallet's newest live pool is then the daemon's own, and adopting it would put a second voucher series on the daemon's lanes. ChainAdoption::for_data_dir reads the same daemon markers as the store classifier, and matching on (tracked, adoption) is exhaustive, so dropping the Refused arm won't compile.

Tests

adoption_tests drives open_or_reuse_pool against a mocked PaymentPool. Each test was checked against a mutation of the behaviour it names, and fails under it:

Mutation Caught by
Adoption skipped a_lost_row_adopts_the_live_pool_… (+2)
Lane seeded at zero a_lost_row_…, a_tracked_pool_resumes_a_new_lane_…
Adopted row not recorded a_lost_row_…, an_adopted_pool_drained_…
Faulted read swallowed as "no pool" a_faulted_enumeration_refuses_rather_than_opening
Node-dir verdict ignored a_node_data_dir_never_adopts_the_daemons_pool
totalRedeemed ignored an_adopted_pool_drained_by_other_lanes_is_topped_up

Also added: three newest_solvent_owned_pool tests (walk order with two solvent pools, so the positional mock can't pass it by accident; drained pools skipped; a fault is Err) and chain_adoption_is_refused_in_a_node_data_dir.

Verified: cargo nextest run --workspace 3699 passed / 0 failed; cargo clippy --workspace --all-targets exit 0, zero warnings; RUSTDOCFLAGS="-D warnings" cargo doc passes; cargo check -p decdn-e2e --features anvil-e2e --all-targets clean.

Not in this PR

🤖 Generated with Claude Code

… opening a second

The client decided whether it owned a pool from its local store alone, so a
lost row read as "no pool" and it escrowed a fresh working deposit beside the
live one. On a wallet whose USDC was already in that pool the second
`openPool` reverted (`ERC20: transfer amount exceeds balance`). #2087's move of
the buyer tables to `_v4` made this the common case — every client row written
before it reads as absent — but any store loss reached it.

The node already recovers from chain at bootstrap; the client now does the same
on demand. With no usable row it asks `getPools` for the newest `Open` pool
that still has deposit to spend (`newest_solvent_owned_pool`, shared in
decdn-incentive), records it and reuses it, and opens a pool only when none
qualifies. It fails closed where the node fails open: a read that faults aborts
the fetch rather than answering "no pool", because a one-shot fetch can be
rerun and "could not tell" must not become a second deposit.

Three things adoption has to get right alongside it:

- A lane with no local record resumes from the chain watermark, not zero. At
  or below it every voucher redeems nothing on chain, and an adopted pool has
  no local lanes at all.
- The refill decision counts the adopted pool's `totalRedeemed`. The row has no
  lanes to account for what the pool has paid out, so a pool other lanes
  drained otherwise reads as a full deposit, no top-up fires, and the provider
  refuses the fetch.
- A buy from a node's data dir never adopts. #2082 still accepts an explicit
  `--data-dir <node dir>`, whose keystore is the operator's, so the wallet's
  newest live pool is the daemon's own; adopting it would put a second voucher
  series on the daemon's lanes. `ChainAdoption::for_data_dir` reads the same
  daemon markers as the store classifier.

Tests drive `open_or_reuse_pool` against a mocked `PaymentPool`. Each was
checked against a mutation of the behaviour it names — skipping adoption, a zero
watermark seed, an unrecorded row, a swallowed read fault, an ignored node-dir
verdict, an ignored `totalRedeemed` — and fails under it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Copilot AI lite review requested due to automatic review settings September 21, 2026 19:07

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 review overview

🟡 Changes recommended

Adoption is keyed only to directory markers, so a client using a shared daemon keystore from another directory can adopt the daemon’s pool and create conflicting voucher lanes.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

Adds client-side recovery of live buyer pools from chain when local state is missing.

Changes:

  • Adds solvent-pool enumeration and adoption.
  • Resumes lanes from on-chain watermarks and accounts for redeemed funds during refill.
  • Applies adoption rules to fetch and bundle pull, with tests and changelog updates.
File Description
crates/​incentive/​src/​payment_pool.rs Adds solvent pool discovery.
crates/​cli/​src/​commands/​fetch.rs Implements adoption, watermark recovery, and refill handling.
crates/​cli/​src/​commands/​buyer_store.rs Adds data-directory adoption policy.
crates/​cli/​src/​commands/​bundle_pull.rs Passes adoption policy to pool reuse.
CHANGELOG.md Documents the fix.

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

Comment thread crates/cli/src/commands/buyer_store.rs Resolved
@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 83.60% (was 82.86%, ▲ +0.74%) — 85482/102248 lines

File Coverage Lines Δ
crates/cli/src/commands/bundle_pull.rs 71.69% 2560/3571 ▲ +19.62%
crates/cli/src/commands/buyer_store.rs 98.56% 205/208 new
crates/cli/src/commands/fetch.rs 51.69% 1403/2714 ▲ +14.71%
crates/incentive/src/payment_pool.rs 97.82% 224/229 ▼ 0.93%
History
Commit Total Δ vs base
ba76621 83.60% ▲ +0.11%
2a033f4 (HEAD) 83.60% ▲ +0.74%

`ChainAdoption` judged only the data dir, but the keystore resolves
independently: `--keystore` or `blockchain.eth_keystore` can point a client
dir at `/var/lib/decdn/keystore.json`. That buy classified as `Allowed` and,
after a store reset, would adopt the daemon's live pool — a second voucher
series on the daemon's lanes, which is the hazard the node-dir check exists to
stop. The wallet decides whose pools it owns, so its key's directory is now
checked against the same daemon markers as the store's.

A key copied out of a node's dir, or a wallet two clients share, is still not
detectable locally; the doc states the one-buyer-per-wallet assumption rather
than implying the check covers it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@thiras
thiras merged commit 1b0c4de into main Sep 21, 2026
25 checks passed
@thiras
thiras deleted the fix/client-adopts-owned-pool branch September 21, 2026 19:33
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