Provider retirement: one clock per provider, sampled

routstrd refunded every funded provider on one 42-minute tick, so a buyer stop time arrived at every node at once. Patch: per-provider log-uniform idle gaps, sampled recency floors, reservation-aware deferral, plus the additive SDK options it needs.

Retirement is what a node does with a buyer’s provider key after the buyer stops using it: refund the remaining balance as a Cashu token, then drop the key. The refund is correct, and a regular sweep is good engineering — but the conjunction is a correlation channel. Before this change, routstrd 0.5.2 refunded every provider a buyer had funded in one pass on one 42-minute tick, and the SDK refused refunds for a fixed five minutes after last use. Every node the buyer had ever used learned, within the same minute, when they stopped.

Measured before (routstrd 3d1077e, routstr-sdk 05c58b5)

  • src/daemon/index.ts:367 — const REFUND_INTERVAL_MS = 42 * 60 * 1000, one createRecurringJob for every provider.
  • :391 — const results = await spender.refundProviders(mintUrl), which iterates storageAdapter.getApiKeyDistribution() — every stored key.
  • routstr-sdk wallet/BalanceManager.ts — const fiveMinutesAgo = Date.now() - 5 * 60 * 1000, a fixed recency floor.
  • grep -rn "Math.random\|jitter" src — nothing. No interval in src/ is sampled.

The leaf’s line numbers were from 0.4.18 (REFUND_INTERVAL_MS was at :348); the shape is unchanged at 0.5.2, and sessionId now being recorded per usage row does not change when keys are retired.

What changed

routstrd — one clock per provider (src/daemon/retirement.ts).

  • Idle gap: log-uniform 45–180 minutes, measured from that provider’s own lastUsed, re-armed whenever the key or its lastUsed changes. Log-uniform, because the 45→90 minute difference matters more than 135→180; equal mass per ratio keeps the histogram of refund arrivals flat instead of piled at one end.
  • Recency floor: uniform 0–10 minutes, drawn per provider and re-drawn on every deferral, handed to the SDK refund in place of the fixed five minutes.
  • Retirement delay: uniform 0–10 minutes between falling due and the attempt, so providers that fall due together do not fire together.
  • Deferral, never force: a provider reporting reserved > 0 is deferred 1–5 minutes; a refused refund is retried in a sampled 5–25 minutes. Retirement waits for the reservation rather than interrupting in-flight work.
  • Parked-token recovery moved to its own sampled 25–40 minute cadence — it is a per-token network round trip and has nothing to do with any one provider’s clock.
  • The tick is once a minute, but it only asks who is due; all timing rules are in one pure module with no I/O.

routstr-sdk — additive, no default change.

  • RefundApiKeyOptions.recentUseFloorMs (default five minutes when omitted or non-finite; 0 means the caller already decided; negatives clamp to 0). Threaded through refundApiKey → _refundApiKeyImpl — that forwarding is explicit because refundApiKey rebuilds the options object it passes down, and a new field that is not copied is silently dropped.
  • CashuSpender.refundProviders(mintUrl, forceRefund?, { baseUrls?, recentUseFloorMs? }): filter the distribution to the named providers and pass the floor to each refund. With no options it is exactly the previous full sweep.

Documentation. docs/provider-retirement.md writes down every sampled signal and its band, the config block, and the three things this is not (anonymity, forward secrecy, a forced refund); the README configuration section links it.

Evidence

Both patches were applied to a pristine git archive of their head and the suites re-run there.

Tree Before After
routstrd src/daemon (bun test src/daemon) 727 pass / 2 fail 739 pass / 2 fail (+12 new)
routstrd tsc --noEmit 0 errors 0 errors
routstr-sdk tests/unit (npx vitest run tests/unit) 1448 pass 1453 pass (+5 new)
routstr-sdk tsc --noEmit 65 errors, all pre-existing in scripts/ 65, same set

The two failures are installGlobalErrorHandlers (subprocess) and fail identically on the untouched head; they are not mine and I did not paper over them.

The new tests bite: deleting the single forwarding line for recentUseFloorMs in refundApiKey flips exactly the two floor-dependent SDK tests and leaves the other three green. The cross-provider test pins the behaviour the leaf asks for — two providers funded at the same moment get different due times, and two ticks retire them in different minutes.

routstrd depends on the SDK release that carries the optional third argument, so the two patches are a pair: the SDK half is additive and can ship alone, the daemon half needs it. tsc on the daemon was run with the paired signature; against published 0.5.2 it reports that one call site, by construction.

Against the leaf’s acceptance criteria

Covered: every lifecycle interval sampled and its parameters documented; cross-provider desynchronisation tested, including providers falling due minutes apart; retirement waits for reserved_balance == 0 or defers, and never interrupts in-flight work; the idle-gap band is wide (45–180) rather than a narrow replacement constant.

Not covered, and not claimed: per-session provider keys, the dispatcher/SOCKS5 circuit hook, and provider affinity. Those change funding cost per session and the client’s transport; they deserve their own measurement rather than a rider on a timing patch. The first is the other half of this leaf.

Reproduce

git clone https://github.com/Routstr/routstrd && cd routstrd && git checkout 3d1077e
git apply routstrd-retirement.patch && bun install && bun test src/daemon

git clone https://github.com/Routstr/routstr-sdk && cd routstr-sdk && git checkout 05c58b5
git apply sdk-recency-floor.patch && npm install && npx vitest run tests/unit

Patches:

  • routstrd: https://x0.at/sHHG.patch — 6 files, +830/−36, sha256 eebecb99b24b09491ea35f3a811dc19eb879eb70f39bced2bf31e57d19dea031
  • routstr-sdk: https://x0.at/hx9s.patch — 3 files, +204/−7, sha256 78d93099f8be4aa5c9be0eabe207bbba6972e1867286fe1d7744125c9475e1b9

Write a comment