@@ -23,6 +23,7 @@ use cdk::wallet::{
2323} ;
2424use std:: collections:: hash_map:: DefaultHasher ;
2525use std:: hash:: { Hash , Hasher } ;
26+ use tokio:: sync:: Mutex ;
2627
2728// Constants for organizing data in the KV store
2829const CASHU_PRIMARY_KEY : & str = "cashu_wallet" ;
@@ -114,6 +115,7 @@ impl From<DatabaseError> for cdk::cdk_database::Error {
114115/// A KV store-based implementation of the Cashu WalletDatabase trait
115116pub struct CashuKvDatabase {
116117 store : Arc < dyn DynStore > ,
118+ keyset_counter_lock : Mutex < ( ) > ,
117119 // In-memory caches for frequently accessed data
118120 mints_cache : Arc < RwLock < HashMap < MintUrl , Option < MintInfo > > > > ,
119121 proofs_cache : Arc < RwLock < Vec < ProofInfo > > > ,
@@ -123,6 +125,7 @@ impl Debug for CashuKvDatabase {
123125 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
124126 f. debug_struct ( "CashuKvDatabase" )
125127 . field ( "store" , & "<KVStore>" )
128+ . field ( "keyset_counter_lock" , & self . keyset_counter_lock )
126129 . field ( "mints_cache" , & self . mints_cache )
127130 . field ( "proofs_cache" , & self . proofs_cache )
128131 . finish ( )
@@ -146,6 +149,7 @@ impl CashuKvDatabase {
146149 pub ( crate ) async fn new ( store : Arc < dyn DynStore > ) -> Result < Self , DatabaseError > {
147150 let database = Self {
148151 store,
152+ keyset_counter_lock : Mutex :: new ( ( ) ) ,
149153 mints_cache : Arc :: new ( RwLock :: new ( HashMap :: new ( ) ) ) ,
150154 proofs_cache : Arc :: new ( RwLock :: new ( Vec :: new ( ) ) ) ,
151155 } ;
@@ -833,6 +837,7 @@ impl WalletDatabase<cdk::cdk_database::Error> for CashuKvDatabase {
833837 async fn increment_keyset_counter (
834838 & self , keyset_id : & Id , count : u32 ,
835839 ) -> Result < u32 , cdk:: cdk_database:: Error > {
840+ let _lock = self . keyset_counter_lock . lock ( ) . await ;
836841 let key = keyset_id. to_string ( ) ;
837842
838843 // Read current counter
0 commit comments