Skip to content

Commit

Permalink
Addressed code review feedback
Browse files Browse the repository at this point in the history
* Added more logging
* Added helper to broadcast proof status
* Moved Into and From implementations to their own place
  • Loading branch information
crodas committed Nov 5, 2024
1 parent 4222b16 commit fcae992
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 76 deletions.
19 changes: 10 additions & 9 deletions crates/cdk-axum/src/ws/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,17 @@ pub async fn main_websocket(mut socket: WebSocket, state: MintState) {
continue;
}
let notification: WsNotification<Notification> = (sub_id, payload).into();
let message = if let Ok(message) = serde_json::to_string(&notification) {
message
} else {
tracing::error!("Could not serialize notification");
continue;
let message = match serde_json::to_string(&notification) {
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() => {
Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ async fn check_pending_mint_quotes(
mint.localstore
.update_mint_quote_state(&quote.id, state)
.await?;
mint.pubsub_manager.mint_quote_bolt11_status(&quote, state);
mint.pubsub_manager.mint_quote_bolt11_status(quote, state);
}
}

Expand Down
9 changes: 1 addition & 8 deletions crates/cdk/src/mint/check_spendable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
20 changes: 3 additions & 17 deletions crates/cdk/src/mint/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::{
Amount, Error,
};

use super::ProofState;
use super::{
CurrencyUnit, MeltBolt11Request, MeltQuote, MeltQuoteBolt11Request, MeltQuoteBolt11Response,
Mint, PaymentMethod, PublicKey, State,
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk/src/mint/mint_nut04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 2 additions & 9 deletions crates/cdk/src/mint/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions crates/cdk/src/nuts/nut05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -111,6 +112,22 @@ pub struct MeltQuoteBolt11Response {
pub change: Option<Vec<BlindSignature>>,
}

#[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 {
Expand Down
10 changes: 10 additions & 0 deletions crates/cdk/src/nuts/nut07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ pub struct ProofState {
pub witness: Option<String>,
}

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))]
Expand Down
33 changes: 4 additions & 29 deletions crates/cdk/src/nuts/nut17.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<E: Into<ProofState>>(&self, event: E) {
self.broadcast(event.into().into());
}
}

impl PubSubManager {
/// Helper function to emit a MintQuoteBolt11Response status
pub fn mint_quote_bolt11_status<E: Into<MintQuoteBolt11Response>>(
&self,
Expand Down

0 comments on commit fcae992

Please sign in to comment.