Skip to content

fix(node): count a resolved pool's on-chain redemptions toward its remaining deposit - #2709

Merged
thiras merged 4 commits into
mainfrom
fix/node-resolved-pool-total-redeemed
Oct 28, 2026
Merged

thiras merged 4 commits into
mainfrom
fix/node-resolved-pool-total-redeemed

Conversation

@thiras

@thiras thiras commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

What

Fixes GHSA-9x5g-h559-vp5m (H2 in the internal audit).

After a restart the node's pool projection is empty, and the settlement watcher resumes at a rewound checkpoint. A pool opened earlier therefore reaches the serve path through ResolvingPoolView::resolve, which reads it with one getPool. record_resolved seeded that pool with total_redeemed = 0, so a pool drained before the restart reported its whole deposit as remaining for the rest of the node's uptime. The admission solvency gate and the mid-stream pool_funds_committed re-check then served it unpaid.

Seeding totalRedeemed alone is not enough. It would double-count: a long-lived lane's first PoolRedeemed after the seed has no per-lane baseline, so its whole cumulative would be counted again on top of the read. A watcher catching up from an old checkpoint also folds events the read already holds.

The fix makes the seed a snapshot and keeps it exact:

  • Seed. record_resolved stores getPool.totalRedeemed as an unanchored Snapshot. A drained pool reads remaining = 0 from its first admission.

  • Re-anchor. The settlement sink's on_tick_complete (reanchor_pools) reads every pending pool at one pinned block and anchors it there in a single publish:

    • at most 64 pools per tick, eight reads at a time;
    • the start rotates through a cursor, so pools past the cap get their turn;
    • each RPC is bounded by a 3 s REANCHOR_RPC_TIMEOUT, and the first fault stops the batch. A hung endpoint holds the shared poller for about 6 s per tick, and the watcher never goes into backoff. A faulted pool still moves the cursor, so it cannot hold back the pools behind it;
    • a tick whose block number is below the highest log block the sink has applied is skipped, because a redemption the watcher dropped while the pool was not yet seeded would be missing from that read;
    • a pool the read finds Closed is forgotten. One absent at the pinned block stays pending, as does one whose read reports a totalRedeemed or deposit below what the entry already holds. Both values only rise on chain, so such a read comes from a backend behind the admit read.
  • Fold. PoolRedeemed folds now take the log's block number:

    • an advance above the anchor, on a lane with a folded baseline, adds to the snapshot;
    • an event at or below the anchor only sets lane baselines;
    • any other advance (no baseline, or no block) marks the snapshot stale, so the pool is re-read rather than guessed. A stale snapshot keeps its anchor and keeps counting the advances it can place.
  • remaining. It subtracts the larger of the lane fold and snapshot.redeemed + since. Both are lower bounds on on-chain totalRedeemed, so no redemption counts twice and remaining errs only toward serving, until the next clean tick.

  • Deposit. An event-opened pool takes deposits as absolute values, as before, so a reorg can still lower them. A snapshot pool:

    • skips a top-up its read already holds;
    • takes a top-up above the read as absolute;
    • only raises its deposit for a top-up it cannot order.

    folded_through covers top-ups too, so a re-read from a lagging backend never lowers the deposit.

Reviewer notes

  • Read pool_view.rs first: the Snapshot doc, record_redeemed, reanchor, then PoolEntry::status. Then read reanchor_pools in payment_settlement.rs.
  • Unchanged on purpose: the admit path still makes exactly one getPool. The extra reads happen in the watcher, once per seeded pool, and again only after a redemption on a lane the projection has no baseline for. Such a redemption costs the sender a transaction.
  • Remaining error: a reorg that drops a counted redemption keeps it counted until on-chain totalRedeemed passes it again, because the projection never rewinds a fold.
  • No metrics yet: pending pools, skipped re-reads and re-read faults show up only in logs (warn! for faults, debug! for skips). Metrics would need adr/appendix-observability.md rows and devops dashboards, so they are tracked in node: meter the settlement re-anchor path (pending pools, skipped and faulted re-reads) #2710.
  • Extra RPC: the re-anchor reads the block number itself rather than reusing the poller's shared head, because LogSink::on_tick_complete takes no head. That costs one eth_blockNumber per tick, and only while pools are pending.
  • Out of scope: signer_spent has the same blind spot on resolved pools, so the mid-stream cap headroom fails open there. The admit getAuthorization read and the on-chain min(cap − spent) are the backstops. I'll file a follow-up issue.
  • record_resolved_rebuilds_remaining_without_double_count is deleted. It asserted the zero seed this PR fixes.

Verification

  • pre-commit on the changed files (hook run at commit; cargo-deny reports the unrelated RUSTSEC-2026-0387 crypto-bigint advisory as a warning)
  • cargo clippy --workspace --all-targets --all-features -- -D warnings
  • cargo nextest run -p decdn-node: 1841 passed. The full workspace run is left to CI; only decdn-node changed.
  • CARGO_BUILD_WARNINGS=deny cargo doc --workspace --no-deps --document-private-items
  • Mutation checks, one at a time:
    • removing the snapshot term from remaining fails 7 tests, including both admit-seed tests;
    • removing the above-anchor guard fails replayed_redemptions_at_or_below_the_anchor_are_not_counted_again;
    • stopping a stale snapshot's count fails a_stale_snapshot_keeps_counting_advances_it_can_place;
    • dropping the stale-read guard fails reanchor_below_the_seed_is_ignored;
    • dropping the block guard fails reanchor_below_a_folded_event_is_ignored;
    • dropping the applied_through skip fails reanchor_pools_skips_a_block_behind_the_watcher;
    • keeping since across a re-anchor fails anchored_snapshot_counts_a_known_lanes_advance_once.
  • PR title type fix, lowercase subject
  • CHANGELOG.md entry under Security

🤖 Generated with Claude Code

…maining deposit

After a restart the pool projection is empty, so the admit path seeds a pool
it has not folded from one `getPool`. The seed set `total_redeemed` to zero,
so a pool drained before the restart reported its whole deposit as
`remaining` for the rest of the node's uptime, and the admission solvency
gate and the mid-stream re-check served it unpaid (GHSA-9x5g-h559-vp5m).

The seed now carries `getPool.totalRedeemed` as an unanchored snapshot, and
the settlement sink's `on_tick_complete` re-reads each unanchored pool at one
pinned block (`reanchor_pools`, at most 64 pools per tick). `PoolRedeemed`
folds take the log's block: an advance above the anchor on a lane with a
folded baseline adds to the snapshot, an event at or below the anchor only
sets baselines, and an advance on a lane with no baseline drops the anchor
for the next re-read. `remaining` subtracts the larger of the lane fold and
the snapshot, both lower bounds on `totalRedeemed`, so it never counts a
redemption twice and errs only toward serving until the next clean tick.
`record_opened` / `record_topup` keep the highest deposit, so a replayed
older log never lowers a newer read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@thiras thiras added kind:security Security impact; report a vulnerability to [email protected], not here area:node decdn-node daemon: runtime, handlers, admin RPC, serve path area:payments Pools, vouchers, redeem, reclaim, staking (incentive crate and chain) labels Oct 28, 2026
Copilot AI balanced review requested due to automatic review settings October 28, 2026 15:35
@thiras thiras added area:node decdn-node daemon: runtime, handlers, admin RPC, serve path area:payments Pools, vouchers, redeem, reclaim, staking (incentive crate and chain) labels Oct 28, 2026
@thiras thiras self-assigned this Oct 28, 2026

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.

🟡 Changes recommended

Critical re-anchoring flaws can restore drained pools’ spendable balances, stall the shared poller for minutes, and indefinitely starve later pending pools of refreshes.

3 open issues
What changed in this PR

This PR addresses incorrect pool balances after node restarts by including on-chain redemptions in resolved pool snapshots used for admission and mid-stream solvency checks.

Changes:

  • Seed resolved pools with totalRedeemed and fold redemption events relative to snapshot anchors.
  • Re-anchor pending pools through bounded-count watcher reads and preserve the highest observed deposit.
  • Add regression tests and document the security fix.
File Description
crates/​node/​src/​pool_view/​tests.rs Tests snapshot accounting, replay handling, and deposit monotonicity.
crates/​node/​src/​pool_view.rs Adds redemption snapshots and block-aware accounting.
crates/​node/​src/​payment_settlement/​tests.rs Tests admission seeds and re-anchor outcomes.
crates/​node/​src/​payment_settlement.rs Seeds on-chain totals and adds end-of-tick re-anchoring.
crates/​node/​src/​handlers/​client/​tests.rs Updates redemption calls for the block parameter.
CHANGELOG.md Documents the restart-related solvency fix.

Review effort: Balanced

Comment thread crates/node/src/payment_settlement.rs Resolved
Comment thread crates/node/src/payment_settlement.rs Resolved
Comment thread crates/node/src/pool_view.rs Resolved
@github-code-quality

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

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main 80d49c9 fix/node-resolved-po... b1f869d +/-
77% 77% 0%
View coverage of the 4 most impacted files.
File main 80d49c9 fix/node-resolved-po... b1f869d +/-
crates/client/src/lib.rs 89% 89% 0%
crates/node/src...t_settlement.rs 76% 77% +1%
crates/node/src/pool_view.rs 94% 95% +1%
crates/protocol/src/framing.rs 92% 93% +1%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated October 28, 2026 16:25 UTC

- A redemption the snapshot cannot place marks it stale instead of clearing
  the anchor, so advances on baselined lanes above the old anchor keep
  counting until the re-read lands. A replayed `PoolRedeemed` with no block
  marks it stale only when a lane advances.
- `reanchor_pools` reads at most eight pools at once, stops at the first
  fault, and rotates its start through a cursor so pools past the 64-pool cap
  get their turn. A pool absent at the pinned block stays pending instead of
  being forgotten; only a `Closed` read forgets it.
- `PoolProjection::reanchor` applies a whole tick's reads in one publish.
- Event-opened pools take `PoolOpened` / `PoolToppedUp` deposits as absolute
  values again, so a reorg can lower them. A snapshot-seeded pool skips a
  top-up its read already holds, takes one above the read as absolute, and
  only raises its deposit for one it cannot order; `folded_through` counts
  top-ups, so a re-read from a lagging backend never lowers the deposit.
- Docs no longer call the sink the projection's only writer, and the
  CHANGELOG names the reorg over-count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@thiras

thiras commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Review pass (/code-review high). In every replay, catch-up and below-anchor case it traced, the fold logic never counted a redemption twice. 400a072 addresses these findings:

  • Serial re-anchor reads held the shared poller for up to 64 × 10 s. Reads now run eight at a time, in order, and the first fault stops the batch.
  • The 64-pool cap always took the same pools first, in HashMap order. pending_rereads() is sorted, and the batch starts after a cursor and wraps around (reanchor_batch, unit-tested).
  • Clearing the anchor dropped advances it could still place. An unplaceable advance now sets stale; the anchor stays, and baselined advances above it keep adding to since.
  • A lagging pinned read forgot a live pool. Only a Closed read forgets a pool; an absent one stays pending.
  • One rcu clone per re-anchored pool. reanchor(block, &[PoolRead]) applies the whole tick in one publish.
  • A replayed PoolRedeemed with no block forced a re-read. It marks the snapshot stale only when a lane advances.
  • max deposits stopped a reorg from lowering the deposit. Event-opened pools take absolute deposits again. Snapshot pools order top-ups against the read, and folded_through counts top-ups.
  • The docs called the sink the "only writer", and the CHANGELOG said "never counts twice". Both are corrected; the CHANGELOG now names the reorg over-count.

Left as is:

  • The re-anchor reads its own block number instead of the poller's shared head. on_tick_complete takes no head, and threading one through would change every sink's LogSink signature. The extra eth_blockNumber happens only on ticks with pending pools.
  • "Re-read on every redemption" instead of since and baselines. That costs one getPool per redemption on every resolved pool for the rest of the uptime. since keeps the steady state at zero reads.
  • The blind spot in signer_spent. It fails open, and the admit getAuthorization read and on-chain min(cap − spent) are the backstops. It gets a follow-up issue once this PR merges.

thiras and others added 2 commits October 28, 2026 18:59
- `PoolProjection::reanchor` skips a read whose `totalRedeemed` or deposit is
  below what the entry already holds, and the pool stays pending. Both only
  rise on chain, so such a read comes from a backend behind the admit read,
  whose block the `folded_through` guard cannot see. A drained seed with no
  folded lane no longer anchors at an older zero.
- A faulted read moves the re-anchor cursor too, so a pool whose read keeps
  failing cannot hold back the pools behind it.
- Re-anchor RPCs are bounded by a 3 s `REANCHOR_RPC_TIMEOUT`, so a hung
  endpoint holds the shared poller for at most two of them per tick.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- The settlement sink records the highest block of any log it applied, a
  pool it did not hold yet included, and `reanchor_pools` skips a tick whose
  block number is below it. A redemption the watcher dropped while the pool
  was absent never reaches the entry's `folded_through`, so a read pinned
  below it would anchor without it and leave `remaining` high for good.
- `record_resolved` takes a `PoolRead`, so the admit seed and the re-anchor
  share one input and `deposit` / `total_redeemed` cannot be swapped.
  `reanchor` sets every `Snapshot` field explicitly.
- Docs: `deposit` is the latest folded value, not a maximum; a reorg-dropped
  redemption stays counted until on-chain `totalRedeemed` passes it, not
  until the next re-read; the projection has more than one writer; `forget`
  and the module doc name the re-anchor's `Closed` drop; the skip and timeout
  docs say what the code does.
- Tests: the block guard, the `since` reset, an unordered top-up on a
  snapshot pool, a `Closing` read, a fault after a good read, and a block
  behind the watcher each have a test that fails without their rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:node decdn-node daemon: runtime, handlers, admin RPC, serve path area:payments Pools, vouchers, redeem, reclaim, staking (incentive crate and chain) kind:security Security impact; report a vulnerability to [email protected], not here

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants