Cashu: a keyset the mint stopped listing blocks every receive — repair it at connect until cdk#2702 ships

Open
#799 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@Forte11Cuba is already working on this.

Since Oct 14, 2026.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
dart, rust, sqlite
Domain
api, backend

Research direction

Start in rust/src/cashu/wallet.rs at CashuWallet::connect, where the keyset repair would run before Wallet::new, and read the cdk#2702 trace to see how active flags are read back from the store. The done condition is the seam test in the issue: with two active sat keysets stored and the mint listing only one, active_keyset() picks the listed one. The UnknownKeySet mapping and Dart l10n are separate follow-ups, and the pin in docs/cashu/cdk-spike.md matters for the upstream dependency.

Written by the indexing model from the issue text.

Description

Summary

After testnut.cashu.space was reset, the wallet could no longer receive any token from
it, valid ones included. A 12-sat token in the mint's current sat keyset, which cashu.me
redeemed fine, failed with "That token could not be redeemed. It may be from another mint, or
already spent."
(CashuReceiveFailed). Neither cause was true.

The bug is in cdk and is reported upstream as cashubtc/cdk#2702. When a keyset disappears
from the mint's GET /v1/keysets (not marked inactive: gone), the wallet keeps it cached as
active: true. It can then be picked for the swap outputs, the mint answers 12001
(UnknownKeySet), and cdk's retry does not recover. This lasts until the local proof store is
edited by hand. A cdk release with the fix will take time, so this issue proposes a small
workaround in the app, plus the logging that was missing to diagnose it.

What happened, with evidence

  • testnut now lists one sat keyset (017bc604…) and one usd keyset, both
    input_fee_ppk: 100.
  • The proof store (cashu-<pubkey>.sqlite) still held the old sat keyset 0184237e… with
    active = 1, the same fee as the real one. The mint no longer lists it.
  • On a copy of that store, the app's own CashuWallet::connect refreshes keysets from the mint
    (KeysetLoadPolicy::Refresh). Even after that, active_keyset() returned 0184237e…
    5 times out of 5.
  • Moving the store aside (fresh store, same identity, same mint) fixed it at once: the same
    kind of token was received, less the mint's fee.
  • The 294 sat the wallet still showed were all proofs in 0184237e…, which the mint no longer
    knows. The balance was phantom.
  • Finding this took a manual read of the SQLite store, because the app does not log why a
    receive failed
    . cdk's error is reduced to a marker, Dart hides the tail (correctly), and no
    log line keeps it.

Why it happens (cdk 0.18.1)

Full trace in cashubtc/cdk#2702. In short:

  1. fetch_from_http starts from the cached metadata and only inserts what the mint returns. A
    keyset missing from the response keeps its stored active flag, which is written back to
    the store.
  2. Wallet::keysets() reads those flags, and active_keyset() takes the cheapest active one.
    On a fee tie it depends on HashMap order.
  3. retry_on_inactive_keyset matches only InactiveKeyset, so a 12001 is not retried.

Scope in production

NUT-02 has a mint keep listing rotated keysets as active: false. cdk then updates the flag
correctly, so a normal key rotation is fine. This only bites when a mint drops keysets from
its list: a test mint reset, or a mint that lost or replaced its database. That is rare, but
when it happens the wallet is stuck with a false message and a balance it cannot spend.

Proposal

All verified against the public API of cdk 0.18.1:

  • Wallet::localstore is pub;
  • cdk::wallet::HttpClient and MintConnector::get_mint_keysets are public;
  • cdk-sqlite's keyset upsert updates active (ON CONFLICT(id) DO UPDATE SET active = excluded.active);
  • Wallet::new builds a fresh metadata cache, whose first load reads the store before refreshing from the mint (mint_metadata_cache.rs, load_from_mint).

1. Repair the store at connect (rust/src/cashu/wallet.rs, CashuWallet::connect, before
Wallet::new):

  • fetch the mint's keyset list with HttpClient (one extra GET /v1/keysets; the wallet
    opens lazily, never at startup);
  • read the stored keysets of that mint (localstore.get_mint_keysets);
  • write back with active: false every stored keyset marked active that the mint no longer
    lists (add_mint_keysets), skipping auth keysets, whose list is not fetched.

Nothing is deleted. If the mint lists the keyset again, the next refresh makes it active again.
If the mint does not answer, the repair is skipped and probe fails with
CashuMintUnreachable as today. As a side effect, MintCapabilities::has_sat_keyset stops
counting a dead keyset.

2. A mint reset while the app is open. When an operation fails with
cdk::Error::UnknownKeySet:

  • drop the live wallet, so the next action reconnects and repairs;
  • return a stable marker, e.g. CashuMintKeysetsChanged, which Dart maps to "The mint
    changed its keys. Try again."
    (l10n in every locale).

No automatic retry: the mint rejects the whole swap, so a retry would be safe, but it adds
complexity for a rare case.

3. Log the real error. log::warn! the cdk error in cashu_receive_token,
cashu_create_token and cashu_sweep_spent_proofs before it becomes a marker. This stays
useful whatever happens upstream.

4. Remove the workaround when cdk ships the fix. Leave a comment pointing to
cashubtc/cdk#2702 on the repair. When the pin moves past the release that fixes it
(docs/cashu/cdk-spike.md), delete the repair and keep its test.

Tests

  • Seam test (fails without the repair): use a temporary WalletSqliteDatabase, as
    stored_balance does. Store two sat keysets marked active. Apply the repair with a mint
    list that contains only one of them. Build the Wallet and assert that active_keyset()
    picks the listed one. Without the repair, the same setup returns the dead keyset, as the
    test in cdk#2702 shows.
  • Unit tests for the selection: a missing keyset is deactivated; a listed one is
    untouched; an already inactive one is untouched; an auth keyset is left alone.
  • Mapping test: UnknownKeySet → CashuMintKeysetsChanged, and the live wallet is
    dropped.
  • Manual: the store from this report (kept aside) reproduces it. With the repair, a sat
    token from the mint's current keyset is received on that store.

Out of scope

  • The phantom balance. Proofs in a keyset the mint no longer lists are worthless, but the
    balance still counts them. Excluding them and saying so ("N sat are in keys this mint no
    longer recognises") is a separate UX decision. The proofs are not deleted, in case the mint
    comes back.
  • #391 / #798. Tokens in a unit other than sats are a different bug.

References

  • Upstream: cashubtc/cdk#2702
  • Found while testing #798 (#391)
  • cdk / cdk-sqlite pinned at =0.18.1
Dominant language
Dart
Stars
11
Forks
9
Avg merge
2d 1h
Merged PRs (30d)
16

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from MostroP2P/app

All issues in MostroP2P/app

Similar issues

More Dart issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.