From 711175739fe093bdc32bc876170bc395a6cd7829 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 19 May 2024 01:20:19 +0100 Subject: [PATCH] refactor(mint): memory replace Mutex with RwLock --- crates/cdk/src/cdk_database/mint_memory.rs | 88 +++++++++++----------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/crates/cdk/src/cdk_database/mint_memory.rs b/crates/cdk/src/cdk_database/mint_memory.rs index 628e9e4f..3de14d83 100644 --- a/crates/cdk/src/cdk_database/mint_memory.rs +++ b/crates/cdk/src/cdk_database/mint_memory.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::sync::Arc; use async_trait::async_trait; -use tokio::sync::Mutex; +use tokio::sync::RwLock; use super::{Error, MintDatabase}; use crate::dhke::hash_to_curve; @@ -13,14 +13,14 @@ use crate::types::{MeltQuote, MintQuote}; #[derive(Debug, Clone)] pub struct MintMemoryDatabase { - mint_info: Arc>, - active_keysets: Arc>>, - keysets: Arc>>, - mint_quotes: Arc>>, - melt_quotes: Arc>>, - pending_proofs: Arc>>, - spent_proofs: Arc>>, - blinded_signatures: Arc>>, + mint_info: Arc>, + active_keysets: Arc>>, + keysets: Arc>>, + mint_quotes: Arc>>, + melt_quotes: Arc>>, + pending_proofs: Arc>>, + spent_proofs: Arc>>, + blinded_signatures: Arc>>, } impl MintMemoryDatabase { @@ -36,28 +36,30 @@ impl MintMemoryDatabase { blinded_signatures: HashMap<[u8; 33], BlindSignature>, ) -> Result { Ok(Self { - mint_info: Arc::new(Mutex::new(mint_info)), - active_keysets: Arc::new(Mutex::new(active_keysets)), - keysets: Arc::new(Mutex::new(keysets.into_iter().map(|k| (k.id, k)).collect())), - mint_quotes: Arc::new(Mutex::new( + mint_info: Arc::new(RwLock::new(mint_info)), + active_keysets: Arc::new(RwLock::new(active_keysets)), + keysets: Arc::new(RwLock::new( + keysets.into_iter().map(|k| (k.id, k)).collect(), + )), + mint_quotes: Arc::new(RwLock::new( mint_quotes.into_iter().map(|q| (q.id.clone(), q)).collect(), )), - melt_quotes: Arc::new(Mutex::new( + melt_quotes: Arc::new(RwLock::new( melt_quotes.into_iter().map(|q| (q.id.clone(), q)).collect(), )), - pending_proofs: Arc::new(Mutex::new( + pending_proofs: Arc::new(RwLock::new( pending_proofs .into_iter() .map(|p| (hash_to_curve(&p.secret.to_bytes()).unwrap().to_bytes(), p)) .collect(), )), - spent_proofs: Arc::new(Mutex::new( + spent_proofs: Arc::new(RwLock::new( spent_proofs .into_iter() .map(|p| (hash_to_curve(&p.secret.to_bytes()).unwrap().to_bytes(), p)) .collect(), )), - blinded_signatures: Arc::new(Mutex::new(blinded_signatures)), + blinded_signatures: Arc::new(RwLock::new(blinded_signatures)), }) } } @@ -67,79 +69,79 @@ impl MintDatabase for MintMemoryDatabase { type Err = Error; async fn set_mint_info(&self, mint_info: &MintInfo) -> Result<(), Self::Err> { - let mut mi = self.mint_info.lock().await; + let mut mi = self.mint_info.write().await; *mi = mint_info.clone(); Ok(()) } async fn get_mint_info(&self) -> Result { - Ok(self.mint_info.lock().await.clone()) + Ok(self.mint_info.read().await.clone()) } async fn add_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Self::Err> { - self.active_keysets.lock().await.insert(unit, id); + self.active_keysets.write().await.insert(unit, id); Ok(()) } async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result, Self::Err> { - Ok(self.active_keysets.lock().await.get(unit).cloned()) + Ok(self.active_keysets.read().await.get(unit).cloned()) } async fn get_active_keysets(&self) -> Result, Self::Err> { - Ok(self.active_keysets.lock().await.clone()) + Ok(self.active_keysets.read().await.clone()) } async fn add_keyset_info(&self, keyset: MintKeySetInfo) -> Result<(), Self::Err> { - self.keysets.lock().await.insert(keyset.id, keyset); + self.keysets.write().await.insert(keyset.id, keyset); Ok(()) } async fn get_keyset_info(&self, keyset_id: &Id) -> Result, Self::Err> { - Ok(self.keysets.lock().await.get(keyset_id).cloned()) + Ok(self.keysets.read().await.get(keyset_id).cloned()) } async fn get_keyset_infos(&self) -> Result, Self::Err> { - Ok(self.keysets.lock().await.values().cloned().collect()) + Ok(self.keysets.read().await.values().cloned().collect()) } async fn add_mint_quote(&self, quote: MintQuote) -> Result<(), Self::Err> { self.mint_quotes - .lock() + .write() .await .insert(quote.id.clone(), quote); Ok(()) } async fn get_mint_quote(&self, quote_id: &str) -> Result, Self::Err> { - Ok(self.mint_quotes.lock().await.get(quote_id).cloned()) + Ok(self.mint_quotes.read().await.get(quote_id).cloned()) } async fn get_mint_quotes(&self) -> Result, Self::Err> { - Ok(self.mint_quotes.lock().await.values().cloned().collect()) + Ok(self.mint_quotes.read().await.values().cloned().collect()) } async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Self::Err> { - self.mint_quotes.lock().await.remove(quote_id); + self.mint_quotes.write().await.remove(quote_id); Ok(()) } async fn add_melt_quote(&self, quote: MeltQuote) -> Result<(), Self::Err> { self.melt_quotes - .lock() + .write() .await .insert(quote.id.clone(), quote); Ok(()) } async fn get_melt_quote(&self, quote_id: &str) -> Result, Self::Err> { - Ok(self.melt_quotes.lock().await.get(quote_id).cloned()) + Ok(self.melt_quotes.read().await.get(quote_id).cloned()) } async fn get_melt_quotes(&self) -> Result, Self::Err> { - Ok(self.melt_quotes.lock().await.values().cloned().collect()) + Ok(self.melt_quotes.read().await.values().cloned().collect()) } async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Self::Err> { - self.melt_quotes.lock().await.remove(quote_id); + self.melt_quotes.write().await.remove(quote_id); Ok(()) } @@ -147,7 +149,7 @@ impl MintDatabase for MintMemoryDatabase { async fn add_spent_proof(&self, proof: Proof) -> Result<(), Self::Err> { let secret_point = hash_to_curve(&proof.secret.to_bytes())?; self.spent_proofs - .lock() + .write() .await .insert(secret_point.to_bytes(), proof); Ok(()) @@ -156,19 +158,19 @@ impl MintDatabase for MintMemoryDatabase { async fn get_spent_proof_by_secret(&self, secret: &Secret) -> Result, Self::Err> { Ok(self .spent_proofs - .lock() + .read() .await .get(&hash_to_curve(&secret.to_bytes())?.to_bytes()) .cloned()) } async fn get_spent_proof_by_y(&self, y: &PublicKey) -> Result, Self::Err> { - Ok(self.spent_proofs.lock().await.get(&y.to_bytes()).cloned()) + Ok(self.spent_proofs.read().await.get(&y.to_bytes()).cloned()) } async fn add_pending_proof(&self, proof: Proof) -> Result<(), Self::Err> { self.pending_proofs - .lock() + .write() .await .insert(hash_to_curve(&proof.secret.to_bytes())?.to_bytes(), proof); Ok(()) @@ -181,20 +183,20 @@ impl MintDatabase for MintMemoryDatabase { let secret_point = hash_to_curve(&secret.to_bytes())?; Ok(self .pending_proofs - .lock() + .read() .await .get(&secret_point.to_bytes()) .cloned()) } async fn get_pending_proof_by_y(&self, y: &PublicKey) -> Result, Self::Err> { - Ok(self.pending_proofs.lock().await.get(&y.to_bytes()).cloned()) + Ok(self.pending_proofs.read().await.get(&y.to_bytes()).cloned()) } async fn remove_pending_proof(&self, secret: &Secret) -> Result<(), Self::Err> { let secret_point = hash_to_curve(&secret.to_bytes())?; self.pending_proofs - .lock() + .write() .await .remove(&secret_point.to_bytes()); Ok(()) @@ -206,7 +208,7 @@ impl MintDatabase for MintMemoryDatabase { blinded_signature: BlindSignature, ) -> Result<(), Self::Err> { self.blinded_signatures - .lock() + .write() .await .insert(blinded_message.to_bytes(), blinded_signature); Ok(()) @@ -218,7 +220,7 @@ impl MintDatabase for MintMemoryDatabase { ) -> Result, Self::Err> { Ok(self .blinded_signatures - .lock() + .read() .await .get(&blinded_message.to_bytes()) .cloned()) @@ -230,7 +232,7 @@ impl MintDatabase for MintMemoryDatabase { ) -> Result>, Self::Err> { let mut signatures = Vec::with_capacity(blinded_messages.len()); - let blinded_signatures = self.blinded_signatures.lock().await; + let blinded_signatures = self.blinded_signatures.read().await; for blinded_message in blinded_messages { let signature = blinded_signatures.get(&blinded_message.to_bytes()).cloned();