Skip to content

fix(incentive,node): scope buyer pool rows to their PaymentPool deployment - #2087

Merged
thiras merged 3 commits into
mainfrom
fix/buyer-pool-deployment-scoping
Sep 21, 2026
Merged

thiras merged 3 commits into
mainfrom
fix/buyer-pool-deployment-scoping

Conversation

@thiras

@thiras thiras commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

What

Carry the PaymentPool contract address in BuyerPoolState, make it part of the row's identity key, and drop a tracked row that belongs to a different deployment before anything can reuse it.

Why

PaymentPool.openPool derives

poolId = keccak256(abi.encodePacked(msg.sender, ownerPoolNonce[msg.sender]))

with neither the contract address nor the chain id in it, and a fresh deployment restarts ownerPoolNonce at zero. The same owner's Nth pool therefore has a byte-identical id on every deployment, so a persisted pool id alone cannot say which contract it belongs to.

Bootstrap had no way to tell them apart, and the failure was silent in both directions:

  1. getPools on the new contract does not list the stale id, tracked_status answers Unknown for an unlisted id, and reconcile_tracked keeps the row on Unknown. The node pins every pull to a pool the live contract has never heard of and never opens a replacement.
  2. Once the owner's nonce walks back over that id, the row starts matching a real and unrelated pool here — carrying lane progress that priced bytes the live contract never saw, so the node resumes from a frontier that pool has never seen.

This is not hypothetical. It wedged the testnet fleet after the 2026-09-20 redeploy: fra1 and lon1 each tracked keccak(owner, nonce=1) from the old contract, every node→node miss pull was refused with admit getPool: pool absent or closed; refusing, and the buy side stayed dead across restarts. Both stale ids were low-nonce, so case 2 was one and two pool opens away respectively.

How

  • BuyerPoolState gains payment_pool: Address plus is_on(); the field invariant now reads (payment_pool, pool_id).
  • adoption_applies gains an AdoptionCheck::Foreign arm; reconcile_owned_pool drops the foreign row through the new drop_foreign_row before the enumeration that would otherwise re-adopt it. A failed forget refuses the adoption rather than adopting beside a row that would shadow it at the next get_by_owner.
  • The on-disk record gains the address and the tables move to _v4. A row in the old shape is ignored, not migrated (pre-launch: no compat shims).

Tests

  • reconcile_drops_a_row_from_another_payment_pool_deployment — seeds a foreign row whose id is one the mocked contract also mints, and asserts the surviving row's deposit comes from this contract's pool. Equality with the stale row's deposit is exactly the bug.
  • reconcile_keeps_a_row_on_the_configured_deployment — the check keys on the contract address alone; a local row is still AlreadyTracked.
  • Golden bytes updated to schema v2. The diff is 01→02 plus the 20-byte address after pool_id and nothing else displaced.

Verified: cargo nextest run --workspace 3685 passed / 0 failed; cargo clippy --workspace --all-targets exit 0 with zero warnings; cargo fmt --all; pre-commit clean. The new test was confirmed to fail when is_on is stubbed to true.

Operational note

A node already wedged by this needs its buyer.redb moved aside once — this change prevents the state but does not rewrite a row that predates it. decdn pool close/reclaim cannot help: both revert on a pool the contract does not have, and decdn pool writes only the client store while a running daemon holds buyer.redb exclusively. That file holds pool tracking only; seller settlement lives in lanes.redb.

🤖 Generated with Claude Code

…yment

`poolId` is `keccak256(owner, ownerPoolNonce)` — the derivation carries
neither the contract address nor the chain id — and a fresh `PaymentPool`
deployment restarts `ownerPoolNonce` at zero. The same owner's Nth pool
therefore has the same id on every deployment, so a buyer row written
against one contract names a pool the next contract will also mint.

Bootstrap could not tell the two apart. `getPools` on the new contract
does not list the stale id, `tracked_status` answers `Unknown` for an
unlisted id, and `reconcile_tracked` keeps the row on `Unknown` — so the
node pinned every pull to a pool the live contract has never heard of and
never opened a replacement. Worse, once the owner's nonce walked back over
that id the row would start matching a real and unrelated pool, carrying
lane progress that priced bytes the live contract never saw.

Carry the `PaymentPool` address in `BuyerPoolState` and make it part of the
identity key. Bootstrap now drops a row whose `payment_pool` is not the
configured one, before the enumeration that would otherwise re-adopt it.

The on-disk record gains the address and the tables move to `_v4`; a row in
the old shape is ignored, not migrated.

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

drop_foreign_row currently ignores the Ok(false) outcome of forget_if_pool, so reconciliation can proceed without guaranteeing the foreign row was actually removed.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity · 1 Low severity

Open (2)
What changed in this PR

This PR hardens buyer pool persistence against PaymentPool redeploys by recording the contract address in BuyerPoolState, updating the on-disk schema, and dropping tracked rows that belong to a different deployment before they can be reused or collide.

Changes:

  • Add payment_pool: Address to BuyerPoolState, persist it in redb (schema v2, table suffix _v4), and plumb it through call sites/tests.
  • Teach bootstrap reconciliation to detect “foreign deployment” tracked rows and attempt to drop them before enumerating/adopting on-chain pools.
  • Update unit/e2e/CLI tests and golden bytes for the new record layout.
File Description
crates/​node/​src/​buyer_channel.rs Adds foreign-row detection during reconciliation and a helper to drop rows from other PaymentPool deployments.
crates/​node/​src/​admin.rs Updates admin tests to hydrate/construct BuyerPoolState with the new payment_pool field.
crates/​incentive/​src/​buyer_pool.rs Extends BuyerPoolState with payment_pool, adds is_on(), and updates docs/tests accordingly.
crates/​incentive/​src/​buyer_pool_table.rs Bumps tables to _v4, bumps schema version to v2, and adds payment_pool to postcard-encoded records + golden bytes.
crates/​incentive/​src/​buyer_pool_redb.rs Updates redb-backed store tests for the new BuyerPoolState::new signature.
crates/​e2e/​tests/​pool_floor_ceiling_aggregate.rs Updates e2e helper to construct BuyerPoolState with payment_pool.
crates/​e2e/​src/​client.rs Updates fixture state construction to pass payment_pool.
crates/​client-pull/​src/​buyer_pool.rs Ensures newly opened pools record the PaymentPool contract address in persisted state.
crates/​cli/​tests/​pool_list.rs Updates CLI tests that seed buyer pool state to include payment_pool.
crates/​cli/​tests/​admin_rpc.rs Updates RPC round-trip tests to seed buyer pool state with payment_pool.
crates/​cli/​src/​commands/​pool.rs Updates CLI command tests constructing buyer pool state to include payment_pool.
crates/​cli/​src/​commands/​fetch.rs Updates fetch path and tests to construct buyer pool state with the contract address.

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

Comment thread crates/node/src/buyer_channel.rs Resolved
Comment thread crates/incentive/src/buyer_pool.rs Resolved
@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 83.37% (was 83.35%, ▲ +0.02%) — 84357/101178 lines

File Coverage Lines Δ
crates/cli/src/commands/fetch.rs 44.85% 1098/2448 ▲ +0.15%
crates/cli/src/commands/pool.rs 66.53% 966/1452 ▲ +0.12%
crates/client-pull/src/buyer_pool.rs 57.07% 210/368 ● 0.00%
crates/common/src/admin.rs 95.10% 136/143 ▲ +0.03%
crates/e2e/src/client.rs 0.00% 0/455 ● 0.00%
crates/incentive/src/buyer_pool.rs 92.82% 401/432 ▲ +0.26%
crates/incentive/src/buyer_pool_redb.rs 91.16% 227/249 ▲ +0.04%
crates/incentive/src/buyer_pool_table.rs 90.38% 846/936 ▲ +0.19%
crates/node/src/admin.rs 92.27% 1146/1242 ▲ +0.02%
crates/node/src/buyer_channel.rs 71.03% 1545/2175 ▲ +1.90%
History
Commit Total Δ vs base
5e9ef7f 83.36% ▲ +0.01%
55caa12 83.37% ▲ +0.02%
026d5cc (HEAD) 83.37% ▲ +0.02%

thiras and others added 2 commits September 21, 2026 05:28
…ow drop actually guarantee

The store is keyed by `pool_id` alone, so calling `(payment_pool, pool_id)`
the identity key overstated it: two rows for one id cannot coexist, and the
tag buys detection at reconciliation, not addressability.

`drop_foreign_row` likewise promised more than it delivers. `forget_if_pool`
is a compare-and-delete on the owner index, and `get_by_owner` resolves only
through that index, so `Ok(false)` already means nothing maps this owner to
the foreign row — the property adoption needs. Say that, rather than "the row
is gone".

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…reuses a pool row

Writing the tag was half the job. Bootstrap was the only reader, so the
guard was temporal: one failed forget at boot and the node paid against a
foreign row for the life of the process, because `reuse_or_report` and the
CLI's `open_or_reuse_pool` / `cached_pool_token` all read rows without it.
The CLI path carried the original bug unfixed. All three now treat a row
from another `PaymentPool` as no row.

Name the hazard correctly while here. Resuming a foreign lane seeds the
first voucher at a cumulative the live pool has never redeemed against, so
the provider collects it for bytes it never delivered — an over-pay, not
the under-pay the docs claimed. `reseed_lane_from_chain` returns early when
a row already has lane progress, so the stale cumulative is used as-is.

The failed-drop path no longer meters `buyer_pool_adoption_failures`: that
counter means "about to escrow a second deposit beside one it holds", and
this node escrows nothing — it is stuck on a row it already has, which is
`drop_stale_row`'s reasoning for the identical shape. `AdoptionCheck::Foreign`
now carries the contract the row was written against, because the warning
promised an address it was not logging and the drop then destroyed the only
record of it.

`BUYER_SUPPORTED_SCHEMA_VERSION` is matched exactly rather than as a
ceiling. The fields are positional and `payment_pool` sits third, so a v1
record decoded under a ceiling check shifts every field after `pool_id`
silently; the table rename hid that rather than preventing it.

Tests: the previous foreign-row test passed with `drop_foreign_row` gutted
to a no-op — its enumeration returned the same colliding id, so `record`
rewrote that key either way. The new one queues an empty enumeration, where
the early return precedes any write and only the forget can clear the row.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@thiras
thiras merged commit 6dd2ee7 into main Sep 21, 2026
25 checks passed
@thiras
thiras deleted the fix/buyer-pool-deployment-scoping branch September 21, 2026 03:36
thiras added a commit that referenced this pull request Sep 21, 2026
… opening a second (#2098)

* fix(cli,incentive): adopt the pool the wallet already owns instead of 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]>

* fix(cli): refuse adoption when the buy signs with a node's operator key

`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]>

---------

Co-authored-by: Claude Opus 5 (1M context) <[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