Skip to content

Commit

Permalink
refactor: use async mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Dec 29, 2023
1 parent 1df5909 commit 0fe7ccc
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
4 changes: 2 additions & 2 deletions bindings/cashu-ffi/src/nuts/nut00/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Token {
let mint = UncheckedUrl::from_str(&mint)?;
let proofs = proofs.into_iter().map(|p| p.as_ref().into()).collect();

let unit = unit.map(|u| CurrencyUnitSdk::from_str(&u).unwrap_or_default().into());
let unit = unit.map(|u| CurrencyUnitSdk::from_str(&u).unwrap_or_default());

Ok(Self {
inner: TokenSdk::new(mint, proofs, memo, unit)?,
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Token {

impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.inner.to_string())
write!(f, "{}", self.inner)
}
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/cashu-ffi/src/nuts/nut04/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl MintQuoteBolt11Request {
pub fn new(amount: Arc<Amount>, unit: String) -> Self {
Self {
inner: MintQuoteBolt11RequestSdk {
amount: amount.as_ref().deref().clone(),
amount: *amount.as_ref().deref(),
unit: CurrencyUnit::from_str(&unit).unwrap(),
},
}
Expand All @@ -37,7 +37,7 @@ impl MintQuoteBolt11Request {
}

pub fn unit(&self) -> Arc<CurrencyUnit> {
Arc::new(self.inner.clone().unit.into())
Arc::new(self.inner.clone().unit)
}
}

Expand Down
2 changes: 1 addition & 1 deletion bindings/cashu-ffi/src/types/bolt11_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ impl Bolt11Invoice {
pub fn amount(&self) -> Option<Arc<Amount>> {
self.inner
.amount_milli_satoshis()
.map(|a| Arc::new(Amount::from(a / 1000).into()))
.map(|a| Arc::new(Amount::from(a / 1000)))
}
}
4 changes: 2 additions & 2 deletions bindings/cashu-ffi/src/types/melt_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ impl MeltQuote {
Self {
inner: MeltQuoteSdk {
id,
amount: amount.as_ref().deref().clone(),
amount: *amount.as_ref().deref(),
unit: CurrencyUnit::from_str(&unit).unwrap(),
request: request.as_ref().deref().clone(),
fee_reserve: fee_reserve.as_ref().deref().clone(),
fee_reserve: *fee_reserve.as_ref().deref(),
paid,
expiry,
},
Expand Down
2 changes: 1 addition & 1 deletion bindings/cashu-ffi/src/types/mint_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl MintQuote {
Self {
inner: MintQuoteSdk {
id,
amount: amount.as_ref().deref().clone(),
amount: *amount.as_ref().deref(),
unit: CurrencyUnit::from_str(&unit).unwrap(),
request: request.as_ref().deref().clone(),
paid,
Expand Down
61 changes: 30 additions & 31 deletions bindings/cashu-sdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::ops::Deref;
use std::sync::{Arc, RwLock};
use std::sync::Arc;

use cashu_ffi::{
BlindedSignature, Bolt11Invoice, CurrencyUnit, MeltQuote, MintQuote, PreMintSecrets, Proof,
Expand All @@ -11,6 +11,7 @@ use cashu_sdk::url::UncheckedUrl;
use cashu_sdk::wallet::Wallet as WalletSdk;
use once_cell::sync::Lazy;
use tokio::runtime::Runtime;
use tokio::sync::Mutex;

use crate::error::Result;
use crate::types::{Melted, SendProofs};
Expand All @@ -19,7 +20,7 @@ use crate::{Amount, Keys};
static RUNTIME: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("Can't start Tokio runtime"));

pub struct Wallet {
inner: RwLock<WalletSdk<HttpClient>>,
inner: Mutex<WalletSdk<HttpClient>>,
}

impl Wallet {
Expand Down Expand Up @@ -51,8 +52,8 @@ impl Wallet {
pub fn check_proofs_spent(&self, proofs: Vec<Arc<Proof>>) -> Result<Arc<ProofsStatus>> {
let proofs = RUNTIME.block_on(async {
self.inner
.read()
.unwrap()
.lock()
.await
.check_proofs_spent(proofs.iter().map(|p| p.as_ref().deref().clone()).collect())
.await
})?;
Expand All @@ -68,8 +69,8 @@ impl Wallet {
) -> Result<Arc<Token>> {
let token = RUNTIME.block_on(async {
self.inner
.write()
.unwrap()
.lock()
.await
.mint_token(*amount.as_ref().deref(), memo, unit.map(|u| u.into()))
.await
})?;
Expand All @@ -80,8 +81,8 @@ impl Wallet {
pub fn mint_quote(&self, amount: Arc<Amount>, unit: CurrencyUnit) -> Result<Arc<MintQuote>> {
let quote = RUNTIME.block_on(async {
self.inner
.write()
.unwrap()
.lock()
.await
.mint_quote(*amount.as_ref().deref(), unit.into())
.await
})?;
Expand All @@ -90,14 +91,14 @@ impl Wallet {
}

pub fn mint(&self, quote: String) -> Result<Vec<Arc<Proof>>> {
let proofs = RUNTIME.block_on(async { self.inner.write().unwrap().mint(&quote).await })?;
let proofs = RUNTIME.block_on(async { self.inner.lock().await.mint(&quote).await })?;

Ok(proofs.into_iter().map(|p| Arc::new(p.into())).collect())
}

pub fn receive(&self, encoded_token: String) -> Result<Vec<Arc<Proof>>> {
let proofs = RUNTIME
.block_on(async { self.inner.write().unwrap().receive(&encoded_token).await })?;
let proofs =
RUNTIME.block_on(async { self.inner.lock().await.receive(&encoded_token).await })?;

Ok(proofs.into_iter().map(|p| Arc::new(p.into())).collect())
}
Expand All @@ -107,24 +108,20 @@ impl Wallet {
blinded_messages: Arc<PreMintSecrets>,
promises: Vec<Arc<BlindedSignature>>,
) -> Result<Vec<Arc<Proof>>> {
Ok(self
.inner
.read()
.unwrap()
.process_split_response(
let proofs = RUNTIME.block_on(async {
self.inner.lock().await.process_split_response(
blinded_messages.as_ref().deref().clone(),
promises.iter().map(|p| p.as_ref().into()).collect(),
)?
.into_iter()
.map(|p| Arc::new(p.into()))
.collect())
)
})?;
Ok(proofs.into_iter().map(|p| Arc::new(p.into())).collect())
}

pub fn send(&self, amount: Arc<Amount>, proofs: Vec<Arc<Proof>>) -> Result<Arc<SendProofs>> {
let send_proofs = RUNTIME.block_on(async {
self.inner
.read()
.unwrap()
.lock()
.await
.send(
*amount.as_ref().deref(),
proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
Expand All @@ -142,8 +139,8 @@ impl Wallet {
) -> Result<Arc<MeltQuote>> {
let melt_quote = RUNTIME.block_on(async {
self.inner
.write()
.unwrap()
.lock()
.await
.melt_quote(unit.into(), request.as_ref().deref().clone())
.await
})?;
Expand All @@ -154,8 +151,8 @@ impl Wallet {
pub fn melt(&self, quote_id: String, proofs: Vec<Arc<Proof>>) -> Result<Arc<Melted>> {
let melted = RUNTIME.block_on(async {
self.inner
.write()
.unwrap()
.lock()
.await
.melt(
&quote_id,
proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
Expand All @@ -172,10 +169,12 @@ impl Wallet {
unit: Option<CurrencyUnit>,
memo: Option<String>,
) -> Result<String> {
Ok(self.inner.read().unwrap().proofs_to_token(
proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
memo,
unit.map(|u| u.into()),
)?)
Ok(RUNTIME.block_on(async {
self.inner.lock().await.proofs_to_token(
proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
memo,
unit.map(|u| u.into()),
)
})?)
}
}
3 changes: 1 addition & 2 deletions bindings/cashu-sdk-js/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ impl JsMint {
.inner
.keyset(&keyset_id)
.ok_or(JsError::new("Unknown Keyset"))?
.clone()
.into();
.clone();

Ok(KeysResponse {
keysets: vec![keyset],
Expand Down

0 comments on commit 0fe7ccc

Please sign in to comment.