From 5a731dfd716d42f58a0ec538aa02f87e59101344 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Wed, 13 May 2026 16:26:20 -0500 Subject: [PATCH] Synchronize Cashu keyset counter increments --- orange-sdk/src/trusted_wallet/cashu/cashu_store.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/orange-sdk/src/trusted_wallet/cashu/cashu_store.rs b/orange-sdk/src/trusted_wallet/cashu/cashu_store.rs index e5f8f3e..0125337 100644 --- a/orange-sdk/src/trusted_wallet/cashu/cashu_store.rs +++ b/orange-sdk/src/trusted_wallet/cashu/cashu_store.rs @@ -23,6 +23,7 @@ use cdk::wallet::{ }; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; +use tokio::sync::Mutex; // Constants for organizing data in the KV store const CASHU_PRIMARY_KEY: &str = "cashu_wallet"; @@ -114,6 +115,7 @@ impl From for cdk::cdk_database::Error { /// A KV store-based implementation of the Cashu WalletDatabase trait pub struct CashuKvDatabase { store: Arc, + keyset_counter_lock: Mutex<()>, // In-memory caches for frequently accessed data mints_cache: Arc>>>, proofs_cache: Arc>>, @@ -123,6 +125,7 @@ impl Debug for CashuKvDatabase { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("CashuKvDatabase") .field("store", &"") + .field("keyset_counter_lock", &self.keyset_counter_lock) .field("mints_cache", &self.mints_cache) .field("proofs_cache", &self.proofs_cache) .finish() @@ -146,6 +149,7 @@ impl CashuKvDatabase { pub(crate) async fn new(store: Arc) -> Result { let database = Self { store, + keyset_counter_lock: Mutex::new(()), mints_cache: Arc::new(RwLock::new(HashMap::new())), proofs_cache: Arc::new(RwLock::new(Vec::new())), }; @@ -833,6 +837,7 @@ impl WalletDatabase for CashuKvDatabase { async fn increment_keyset_counter( &self, keyset_id: &Id, count: u32, ) -> Result { + let _lock = self.keyset_counter_lock.lock().await; let key = keyset_id.to_string(); // Read current counter