Skip to content

fix(mint): fix rotate_next_keyset - #1097

Closed
vicjuma wants to merge 1 commit into
cashubtc:mainfrom
vicjuma:fix/rotate-next-keyset
Closed

fix(mint): fix rotate_next_keyset#1097
vicjuma wants to merge 1 commit into
cashubtc:mainfrom
vicjuma:fix/rotate-next-keyset

Conversation

@vicjuma

@vicjuma vicjuma commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Summary

cashu/mint/keysets.py:37 (rotate_next_keyset) failed to update selected_keyset_counter inside its selection loop, so the "select the active keyset with the highest counter" comparison always ran against its initial value instead of the running maximum. With a single active keyset per unit — the only case exercised so far — this has no observable effect, since there's nothing to compare against. However, the moment more than one active keyset exists for the same unit (a state NUT-02 permits), selection becomes dependent on dict iteration order rather than actual counter value, which can silently select the wrong keyset for rotation.

Fixing this is low-risk and preserves current single-keyset behavior exactly, while making the function correct if that assumption ever doesn't hold — whether from future multi-keyset support, migrations, or manual DB state.

Also replaced a stray keyset.id reference in the final debug log with selected_keyset.id. keyset was a leftover loop variable from the selection loop above and no longer holds a meaningful value once the loop exits; using it there could log the wrong keyset ID as the one deactivated.

Changes

  • rotate_next_keyset: correctly track selected_keyset_counter on each iteration
  • rotate_next_keyset: fix debug log to reference selected_keyset.id instead of stale keyset.id

Testing

pytest tests/mint/test_mint_keysets.py::test_keyset_rotation

No behavioral change for the existing single-active-keyset code path, so the existing test suite passes unmodified.

- track selected_keyset_counter so the highest-counter keyset is
  actually selected, not just the last one iterated

- fix debug log to reference selected_keyset.id instead of the
  stale loop variable keyset.id
@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 74.60%. Comparing base (1853902) to head (bb33a82).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
cashu/mint/keysets.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1097      +/-   ##
==========================================
- Coverage   74.63%   74.60%   -0.03%     
==========================================
  Files         115      115              
  Lines       13200    13201       +1     
==========================================
- Hits         9852     9849       -3     
- Misses       3348     3352       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@a1denvalu3

Copy link
Copy Markdown
Collaborator

Already being fixed in #1058

@a1denvalu3 a1denvalu3 closed this Jul 27, 2026
@github-project-automation github-project-automation Bot moved this from Backlog to Done in nutshell Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants