Skip to content

Commit 57fca0c

Browse files
authored
Merge pull request #68 from lightningdevkit/fix-cashu-keyset-counter-atomicity
Synchronize Cashu keyset counter increments
2 parents a346206 + 5a731df commit 57fca0c

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

orange-sdk/src/trusted_wallet/cashu/cashu_store.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use cdk::wallet::{
2323
};
2424
use std::collections::hash_map::DefaultHasher;
2525
use std::hash::{Hash, Hasher};
26+
use tokio::sync::Mutex;
2627

2728
// Constants for organizing data in the KV store
2829
const 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
115116
pub 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

Comments
 (0)