From fcae992e84c2573185c11ff171e4a789ef73fba9 Mon Sep 17 00:00:00 2001 From: Cesar Rodas Date: Tue, 5 Nov 2024 11:49:53 -0300 Subject: [PATCH] Addressed code review feedback * Added more logging * Added helper to broadcast proof status * Moved Into and From implementations to their own place --- crates/cdk-axum/src/ws/mod.rs | 19 ++++++++------- crates/cdk-mintd/src/main.rs | 2 +- crates/cdk/src/mint/check_spendable.rs | 9 +------ crates/cdk/src/mint/melt.rs | 20 +++------------- crates/cdk/src/mint/mint_nut04.rs | 6 ++--- crates/cdk/src/mint/swap.rs | 11 ++------- crates/cdk/src/nuts/nut05.rs | 17 +++++++++++++ crates/cdk/src/nuts/nut07.rs | 10 ++++++++ crates/cdk/src/nuts/nut17.rs | 33 ++++---------------------- 9 files changed, 51 insertions(+), 76 deletions(-) diff --git a/crates/cdk-axum/src/ws/mod.rs b/crates/cdk-axum/src/ws/mod.rs index 12acfe45..2af2156e 100644 --- a/crates/cdk-axum/src/ws/mod.rs +++ b/crates/cdk-axum/src/ws/mod.rs @@ -77,16 +77,17 @@ pub async fn main_websocket(mut socket: WebSocket, state: MintState) { continue; } let notification: WsNotification = (sub_id, payload).into(); - let message = if let Ok(message) = serde_json::to_string(¬ification) { - message - } else { - tracing::error!("Could not serialize notification"); - continue; + let message = match serde_json::to_string(¬ification) { + Ok(message) => message, + Err(err) => { + tracing::error!("Could not serialize notification: {}", err); + continue; + } }; if let Err(err)= socket.send(Message::Text(message)).await { - tracing::error!("Could not send websocket message: {}", err); - break; + tracing::error!("Could not send websocket message: {}", err); + break; } } Some(Ok(Message::Text(text))) = socket.next() => { @@ -100,11 +101,11 @@ pub async fn main_websocket(mut socket: WebSocket, state: MintState) { match request.method.process(request.id, &mut context).await { Ok(result) => { - if socket + if let Err(err) = socket .send(Message::Text(result.to_string())) .await - .is_err() { + tracing::error!("Could not send request: {}", err); break; } } diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index 194facbe..695f1d4c 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -545,7 +545,7 @@ async fn check_pending_mint_quotes( mint.localstore .update_mint_quote_state("e.id, state) .await?; - mint.pubsub_manager.mint_quote_bolt11_status("e, state); + mint.pubsub_manager.mint_quote_bolt11_status(quote, state); } } diff --git a/crates/cdk/src/mint/check_spendable.rs b/crates/cdk/src/mint/check_spendable.rs index 3ac3ad34..103bed2d 100644 --- a/crates/cdk/src/mint/check_spendable.rs +++ b/crates/cdk/src/mint/check_spendable.rs @@ -58,14 +58,7 @@ impl Mint { } for public_key in ys { - self.pubsub_manager.broadcast( - ProofState { - y: *public_key, - state: proof_state, - witness: None, - } - .into(), - ); + self.pubsub_manager.proof_state((*public_key, proof_state)); } Ok(()) diff --git a/crates/cdk/src/mint/melt.rs b/crates/cdk/src/mint/melt.rs index 6a73b179..a803868d 100644 --- a/crates/cdk/src/mint/melt.rs +++ b/crates/cdk/src/mint/melt.rs @@ -16,7 +16,6 @@ use crate::{ Amount, Error, }; -use super::ProofState; use super::{ CurrencyUnit, MeltBolt11Request, MeltQuote, MeltQuoteBolt11Request, MeltQuoteBolt11Response, Mint, PaymentMethod, PublicKey, State, @@ -365,14 +364,8 @@ impl Mint { } for public_key in input_ys { - self.pubsub_manager.broadcast( - ProofState { - y: public_key, - state: State::Unspent, - witness: None, - } - .into(), - ); + self.pubsub_manager + .proof_state((public_key, State::Unspent)); } Ok(()) @@ -620,14 +613,7 @@ impl Mint { ); for public_key in input_ys { - self.pubsub_manager.broadcast( - ProofState { - y: public_key, - state: State::Spent, - witness: None, - } - .into(), - ); + self.pubsub_manager.proof_state((public_key, State::Spent)); } let mut change = None; diff --git a/crates/cdk/src/mint/mint_nut04.rs b/crates/cdk/src/mint/mint_nut04.rs index 11ce0016..92d032e5 100644 --- a/crates/cdk/src/mint/mint_nut04.rs +++ b/crates/cdk/src/mint/mint_nut04.rs @@ -239,7 +239,7 @@ impl Mint { } self.pubsub_manager - .mint_quote_bolt11_status(&mint_quote, MintQuoteState::Paid); + .mint_quote_bolt11_status(mint_quote, MintQuoteState::Paid); } Ok(()) } @@ -302,7 +302,7 @@ impl Mint { .unwrap(); self.pubsub_manager - .mint_quote_bolt11_status(&mint_quote, MintQuoteState::Paid); + .mint_quote_bolt11_status(mint_quote, MintQuoteState::Paid); return Err(Error::BlindedMessageAlreadySigned); } @@ -331,7 +331,7 @@ impl Mint { .await?; self.pubsub_manager - .mint_quote_bolt11_status(&mint_quote, MintQuoteState::Issued); + .mint_quote_bolt11_status(mint_quote, MintQuoteState::Issued); Ok(nut04::MintBolt11Response { signatures: blind_signatures, diff --git a/crates/cdk/src/mint/swap.rs b/crates/cdk/src/mint/swap.rs index 682a052f..88ccd0d1 100644 --- a/crates/cdk/src/mint/swap.rs +++ b/crates/cdk/src/mint/swap.rs @@ -6,7 +6,7 @@ use crate::nuts::nut00::ProofsMethods; use crate::Error; use super::nut11::{enforce_sig_flag, EnforceSigFlag}; -use super::{Id, Mint, ProofState, PublicKey, SigFlag, State, SwapRequest, SwapResponse}; +use super::{Id, Mint, PublicKey, SigFlag, State, SwapRequest, SwapResponse}; impl Mint { /// Process Swap @@ -167,14 +167,7 @@ impl Mint { .await?; for pub_key in input_ys { - self.pubsub_manager.broadcast( - ProofState { - y: pub_key, - state: State::Spent, - witness: None, - } - .into(), - ); + self.pubsub_manager.proof_state((pub_key, State::Spent)); } self.localstore diff --git a/crates/cdk/src/nuts/nut05.rs b/crates/cdk/src/nuts/nut05.rs index e7ac3153..23cb4738 100644 --- a/crates/cdk/src/nuts/nut05.rs +++ b/crates/cdk/src/nuts/nut05.rs @@ -13,6 +13,7 @@ use super::nut00::{BlindSignature, BlindedMessage, CurrencyUnit, PaymentMethod, use super::nut15::Mpp; #[cfg(feature = "mint")] use crate::mint; +use crate::mint::MeltQuote; use crate::nuts::MeltQuoteState; use crate::{Amount, Bolt11Invoice}; @@ -111,6 +112,22 @@ pub struct MeltQuoteBolt11Response { pub change: Option>, } +#[cfg(feature = "mint")] +impl From<&MeltQuote> for MeltQuoteBolt11Response { + fn from(melt_quote: &MeltQuote) -> MeltQuoteBolt11Response { + MeltQuoteBolt11Response { + quote: melt_quote.id.clone(), + payment_preimage: None, + change: None, + state: melt_quote.state, + paid: Some(melt_quote.state == MeltQuoteState::Paid), + expiry: melt_quote.expiry, + amount: melt_quote.amount, + fee_reserve: melt_quote.fee_reserve, + } + } +} + // A custom deserializer is needed until all mints // update some will return without the required state. impl<'de> Deserialize<'de> for MeltQuoteBolt11Response { diff --git a/crates/cdk/src/nuts/nut07.rs b/crates/cdk/src/nuts/nut07.rs index b56830ea..11dbf6ec 100644 --- a/crates/cdk/src/nuts/nut07.rs +++ b/crates/cdk/src/nuts/nut07.rs @@ -88,6 +88,16 @@ pub struct ProofState { pub witness: Option, } +impl From<(PublicKey, State)> for ProofState { + fn from(value: (PublicKey, State)) -> Self { + Self { + y: value.0, + state: value.1, + witness: None, + } + } +} + /// Check Spendable Response [NUT-07] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))] diff --git a/crates/cdk/src/nuts/nut17.rs b/crates/cdk/src/nuts/nut17.rs index 089fae9a..3d64fad9 100644 --- a/crates/cdk/src/nuts/nut17.rs +++ b/crates/cdk/src/nuts/nut17.rs @@ -1,7 +1,5 @@ //! Specific Subscription for the cdk crate -#[cfg(feature = "mint")] -use crate::mint::{MeltQuote, MintQuote}; use crate::{ nuts::{ MeltQuoteBolt11Response, MeltQuoteState, MintQuoteBolt11Response, MintQuoteState, @@ -162,35 +160,12 @@ impl Deref for PubSubManager { } } -#[cfg(feature = "mint")] -impl From<&MintQuote> for MintQuoteBolt11Response { - fn from(mint_quote: &MintQuote) -> MintQuoteBolt11Response { - MintQuoteBolt11Response { - quote: mint_quote.id.clone(), - request: mint_quote.request.clone(), - state: mint_quote.state, - expiry: Some(mint_quote.expiry), - } - } -} - -#[cfg(feature = "mint")] -impl From<&MeltQuote> for MeltQuoteBolt11Response { - fn from(melt_quote: &MeltQuote) -> MeltQuoteBolt11Response { - MeltQuoteBolt11Response { - quote: melt_quote.id.clone(), - payment_preimage: None, - change: None, - state: melt_quote.state, - paid: Some(melt_quote.state == MeltQuoteState::Paid), - expiry: melt_quote.expiry, - amount: melt_quote.amount, - fee_reserve: melt_quote.fee_reserve, - } +impl PubSubManager { + /// Helper function to emit a ProofState status + pub fn proof_state>(&self, event: E) { + self.broadcast(event.into().into()); } -} -impl PubSubManager { /// Helper function to emit a MintQuoteBolt11Response status pub fn mint_quote_bolt11_status>( &self,