Skip to content

fix(node): retry a recovery topUp whose nonce the redeemer took - #2725

Merged
thiras merged 1 commit into
mainfrom
fix/2721-topup-nonce-lost-retry
Oct 28, 2026
Merged

thiras merged 1 commit into
mainfrom
fix/2721-topup-nonce-lost-retry

Conversation

@thiras

@thiras thiras commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Root cause

In node_pull_larger_than_working_deposit_tops_up_once_and_completes, the server node buys from its provider and sells to the client. Two services on it sign with one key, through stateless simple-nonce wallet providers (wallet() in crates/node/src/runtime/mod.rs):

  • its seller redeemer sends redeemMany (crates/node/src/payment_settlement.rs) and gets a hint on every accepted voucher;
  • its buyer sends topUp (send_top_up, crates/client/src/buyer_pool.rs).

The failure runs like this:

  1. send_top_up reads the pending nonce N.
  2. A redeemMany mines at N first.
  3. The topUp submit gets nonce too low and no hash, so it becomes a TopUpUnconfirmed at N.
  4. TopUpSettling::spent proves from the chain that N is spent at block B and that the pool's deposit at B did not grow, so the topUp escrowed nothing. It returned an untyped error.
  5. fund_pool reported that error as final, and top_up_at_least does not retry a spawned failure. recover_pool then failed, and node_origin turned the failed recovery step into PullMiss::LocalFault, which ended the fetch.

Reproduced locally: 1 of 15 baseline runs failed with the same log (topUp submit (nonce 11) returned no tx hash ... escrowed nothing, then funding recovery step failed). A redeemMany from the same node logged landed within 1 ms of the topUp's settle. The redeemer was sending one redeemMany per voucher every 30 to 300 ms at the time.

#2563 removed the old nonce-collision retry, because a retry at a fresh nonce could escrow a second time beside a copy that still mines. In this case the settle has already proved that no copy can mine.

Fix

  • TopUpSettling::spent now returns a typed TopUpNonceLost error from its escrowed-nothing branch. The error carries the nonce, the block, both deposits and the original error. The PoolNotOpen path in confirm_closed is unchanged.
  • fund_pool is split into two parts. top_up_and_settle sends the topUp and settles it. A loop around it sends a fresh topUp on TopUpNonceLost, at most MAX_TOPUP_NONCE_LOST_RETRIES (2) times, after a 250 ms backoff. The retry runs inside the same funding task and SlotGuard, so:
    • the next pending read sees the redemption;
    • a recovery step that joined the task gets the result of the retry;
    • decdn_node_pull_recovery_step_total still ticks once.
  • Each retry logs a warn! and ticks a new sibling counter, decdn_buyer_topup_nonce_lost_total. Its row is added to adr/appendix-observability.md. A call that runs out of retries still ticks decdn_buyer_topup_failure_total once.
  • The top_up doc in crates/client/src/buyer_pool.rs said the next low-water refill retries. It now describes the funding task's retry.

Why the retry cannot escrow twice

Every copy of the lost topUp was signed at nonce N, including any copy a gateway retried on another backend. The settle reaches spent only after it reads N as spent at block B, so no copy can mine after B. The deposit at B is not above the row's, so no copy mined before B either. A fresh topUp at a later nonce is therefore the only one that can escrow.

Tests

  • a_top_up_whose_nonce_a_redemption_took_is_sent_again (new) scripts this sequence:

    • the first submit gets nonce too low;
    • the settle reads nonce 5 as spent with an unchanged deposit;
    • the fresh topUp at nonce 6 escrows.

    It asserts that the row is credited once, ok is 1, failure is 0, nonce_lost is 1, and the slot and hold are released.

  • a_settled_top_up_that_escrowed_nothing_frees_the_slot (updated): every try loses its nonce. The call fails after the bound, with one failure metered and nonce_lost at 2.

  • a_hashless_top_up_waits_out_the_colliding_tx_then_escrowed_nothing now also asserts the typed error.

  • Mutation check: with the retry disabled, both fund_pool tests fail. The file was restored from a copy and touched afterwards.

Verification

  • node_pull_topup was looped after cargo build -p decdn-cli -p decdn-node, with --retries 0:
  • cargo nextest run -p decdn-node --no-fail-fast: 1865 of 1865 passed.
  • Payment e2e:
    • node_pull_topup, node_pull_pool_adopt, node_pull_pool_redeploy, anvil_pool_redeem and the decdn-node anvil_settlement_e2e (e2e_onchain_payment_pool_settlement) passed.
    • cli_fetch_topup fetch_of_blob_far_exceeding_deposit_settles_whole_wire_cost_once failed 4 of 4 times, with under-pay: settled 18083. It fails the same way 4 of 4 times on the unchanged baseline. This is e2e: deposit-short top-up fetch under-pays by about two intervals in CI since #2709 #2718 and is not related to this change.
  • cargo clippy --workspace --all-targets --all-features -- -D warnings, cargo fmt -- --check, CARGO_BUILD_WARNINGS=deny cargo doc --workspace --no-deps --document-private-items and make -C adr check-refs are all clean. The pre-commit hooks passed.

Optional follow-up (not in this PR)

A per-signer send mutex, shared by the redeemer and the buyer, would stop the two services from reading the same pending nonce at all. This PR does not add it.

Closes #2721

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings October 28, 2026 20:04

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.

🟢 Approval recommended

No active review issues block the nonce-loss funding recovery change.

No open issues 🎉

What changed in this PR

Fixes node-to-node funding recovery when the seller redeemer consumes a buyer top-up’s nonce. The funding task retries only after settlement proves that nothing escrowed, preserving the protection against double funding.

Changes:

  • Adds a typed nonce-loss error and up to two retries while retaining the funding slot.
  • Adds regression tests for successful retry and retry exhaustion.
  • Adds a retry counter and updates funding documentation.
File Description
crates/​node/​src/​metrics.rs Adds the top-up nonce-loss retry counter.
crates/​node/​src/​buyer_pool/​tests.rs Tests typed errors, retry success, limits, accounting, and slot release.
crates/​node/​src/​buyer_pool.rs Separates sending and settlement from bounded nonce-loss retries.
crates/​client/​src/​buyer_pool.rs Updates documentation to describe the node’s funding-task retry.
CHANGELOG.md Records the recovery fix and new counter.
adr/​appendix-observability.md Documents the retry counter and its operational meaning.

Review effort: Balanced

@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main f2bc01c fix/2721-topup-nonce... f751c5c +/-
77% 77% 0%

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

The node signs its sellers' redeemMany and its buyer topUp with one key.
A redemption that mined at the nonce the topUp had read made the topUp
fail `nonce too low`. TopUpSettling::spent proved from the chain that
the nonce was spent and the deposit did not grow, but returned an untyped
error, so fund_pool reported it as final, the funding recovery step
failed, and the node-to-node fetch died (#2721).

spent now returns a typed TopUpNonceLost. fund_pool splits into
top_up_and_settle and a loop that sends a fresh topUp on that error, at
most twice, after a 250 ms backoff, inside the same funding task and
SlotGuard. Every copy of the lost topUp was signed at the spent nonce, so
none can mine later, and the fresh one cannot escrow twice. Each retry
warns and ticks the new decdn_buyer_topup_nonce_lost_total counter.

Closes #2721

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@thiras
thiras force-pushed the fix/2721-topup-nonce-lost-retry branch from f751c5c to d378961 Compare October 28, 2026 22:06
@thiras
thiras merged commit 67737d4 into main Oct 28, 2026
9 checks passed
@thiras
thiras deleted the fix/2721-topup-nonce-lost-retry branch October 28, 2026 22:06
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.

node: a recovery topUp that loses its nonce to the redeemer fails the fetch instead of retrying

2 participants