Skip to content

Commit

Permalink
Better naming for structs
Browse files Browse the repository at this point in the history
I think the `subscription_manager` was a bit unintuitive; I think
`pubsub_manager` is a much more descriptive name.
  • Loading branch information
crodas committed Oct 11, 2024
1 parent c25266c commit 74b9ee4
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 34 deletions.
9 changes: 2 additions & 7 deletions crates/cdk-axum/src/ws/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
};
use cdk::{
nuts::nut17::{NotificationPayload, Params},
subscription::SubId,
pub_sub::SubId,
};

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -43,12 +43,7 @@ impl WsHandle for Method {
if context.subscriptions.contains_key(&sub_id) {
return Err(WsError::InvalidParams);
}
let mut subscription = context
.state
.mint
.subscription_manager
.subscribe(self.0)
.await;
let mut subscription = context.state.mint.pubsub_manager.subscribe(self.0).await;
let publisher = context.publisher.clone();
context.subscriptions.insert(
sub_id.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk-axum/src/ws/unsubscribe.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{handler::WsHandle, WsContext, WsError};
use cdk::subscription::SubId;
use cdk::pub_sub::SubId;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Method {
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ async fn check_pending_mint_quotes(
mint.localstore
.update_mint_quote_state(&quote.id, state)
.await?;
mint.subscription_manager
mint.pubsub_manager
.mint_quote_bolt11_status(&quote, state);
}
}
Expand Down Expand Up @@ -630,7 +630,7 @@ async fn check_pending_melt_quotes(
.update_melt_quote_state(&pending_quote.id, pay_invoice_response.status)
.await?;

mint.subscription_manager.melt_quote_status(
mint.pubsub_manager.melt_quote_status(
&pending_quote,
None,
None,
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub mod util;
#[cfg(feature = "wallet")]
pub mod wallet;

pub mod subscription;
pub mod pub_sub;

pub mod fees;

Expand Down
6 changes: 3 additions & 3 deletions crates/cdk/src/mint/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Mint {

let quote: MeltQuoteBolt11Response = quote.into();

self.subscription_manager
self.pubsub_manager
.broadcast(NotificationPayload::MeltQuoteBolt11Response(quote.clone()));

Ok(quote)
Expand Down Expand Up @@ -394,7 +394,7 @@ impl Mint {
.await?;

if let Ok(Some(quote)) = self.localstore.get_melt_quote(&melt_request.quote).await {
self.subscription_manager
self.pubsub_manager
.melt_quote_status(&quote, None, None, MeltQuoteState::Unpaid);
}

Expand Down Expand Up @@ -639,7 +639,7 @@ impl Mint {
.update_melt_quote_state(&melt_request.quote, MeltQuoteState::Paid)
.await?;

self.subscription_manager.melt_quote_status(
self.pubsub_manager.melt_quote_status(
&quote,
payment_preimage.clone(),
None,
Expand Down
8 changes: 4 additions & 4 deletions crates/cdk/src/mint/mint_nut04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Mint {

let quote: MintQuoteBolt11Response = quote.into();

self.subscription_manager
self.pubsub_manager
.broadcast(NotificationPayload::MintQuoteBolt11Response(quote.clone()));

Ok(quote)
Expand Down Expand Up @@ -214,7 +214,7 @@ impl Mint {
.update_mint_quote_state(&mint_quote.id, MintQuoteState::Paid)
.await?;

self.subscription_manager
self.pubsub_manager
.mint_quote_bolt11_status(&mint_quote, MintQuoteState::Paid);
}
Ok(())
Expand Down Expand Up @@ -283,7 +283,7 @@ impl Mint {
.await
.unwrap();

self.subscription_manager
self.pubsub_manager
.mint_quote_bolt11_status(&mint_quote, MintQuoteState::Paid);

return Err(Error::BlindedMessageAlreadySigned);
Expand Down Expand Up @@ -312,7 +312,7 @@ impl Mint {
.update_mint_quote_state(&mint_request.quote, MintQuoteState::Issued)
.await?;

self.subscription_manager
self.pubsub_manager
.mint_quote_bolt11_status(&mint_quote, MintQuoteState::Issued);

Ok(nut04::MintBolt11Response {
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct Mint {
/// Ln backends for mint
pub ln: HashMap<LnKey, Arc<dyn MintLightning<Err = cdk_lightning::Error> + Send + Sync>>,
/// Subscription manager
pub subscription_manager: Arc<SubscriptionManager>,
pub pubsub_manager: Arc<PubSubManager>,
/// Active Mint Keysets
keysets: Arc<RwLock<HashMap<Id, MintKeySet>>>,
secp_ctx: Secp256k1<secp256k1::All>,
Expand Down Expand Up @@ -172,7 +172,7 @@ impl Mint {
Ok(Self {
mint_url: MintUrl::from_str(mint_url)?,
keysets: Arc::new(RwLock::new(active_keysets)),
subscription_manager: Default::default(),
pubsub_manager: Default::default(),
secp_ctx,
quote_ttl,
xpriv,
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk/src/nuts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ pub use nut12::{BlindSignatureDleq, ProofDleq};
pub use nut14::HTLCWitness;
pub use nut15::{Mpp, MppMethodSettings, Settings as NUT15Settings};

pub use nut17::{NotificationPayload, Manager as SubscriptionManager};
pub use nut17::{NotificationPayload, PubSubManager};
23 changes: 12 additions & 11 deletions crates/cdk/src/nuts/nut17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
MeltQuoteBolt11Response, MeltQuoteState, MintQuoteBolt11Response, MintQuoteState,
ProofState,
},
subscription::{self, Index, Indexable},
pub_sub::{self, Index, Indexable},
};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
Expand All @@ -22,7 +22,7 @@ pub struct Params {
pub id: SubId,
}

pub use crate::subscription::SubId;
pub use crate::pub_sub::SubId;

use super::BlindSignature;

Expand Down Expand Up @@ -110,20 +110,21 @@ impl From<Params> for Vec<Index<(String, Kind)>> {

/// Manager
#[derive(Default)]
/// Subscription Manager
/// Publish–subscribe manager
///
/// This is the Subscription Manager for the cdk crate
pub struct Manager(subscription::Manager<NotificationPayload, (String, Kind)>);
/// Nut-17 implementation is system-wide and not only through the WebSocket, so
/// it is possible for another part of the system to subscribe to events.
pub struct PubSubManager(pub_sub::Manager<NotificationPayload, (String, Kind)>);

impl Deref for Manager {
type Target = subscription::Manager<NotificationPayload, (String, Kind)>;
impl Deref for PubSubManager {
type Target = pub_sub::Manager<NotificationPayload, (String, Kind)>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Manager {
impl PubSubManager {
/// Helper function to emit a MintQuoteBolt11Response status
pub fn mint_quote_bolt11_status(&self, quote: &MintQuote, new_state: MintQuoteState) {
self.broadcast(
Expand Down Expand Up @@ -172,7 +173,7 @@ mod test {

#[tokio::test]
async fn active_and_drop() {
let manager = Manager::default();
let manager = PubSubManager::default();
let params = Params {
kind: Kind::ProofState,
filters: vec!["x".to_string()],
Expand All @@ -197,7 +198,7 @@ mod test {

#[tokio::test]
async fn broadcast() {
let manager = Manager::default();
let manager = PubSubManager::default();
let mut subscriptions = [
manager
.subscribe(Params {
Expand Down Expand Up @@ -255,7 +256,7 @@ mod test {

#[tokio::test]
async fn json_test() {
let manager = Manager::default();
let manager = PubSubManager::default();
let mut subscription = manager
.subscribe::<Params>(
serde_json::from_str(r#"{"kind":"proof_state","filters":["02194603ffa36356f4a56b7df9371fc3192472351453ec7398b8da8117e7c3e104"],"subId":"uno"}"#)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
//! Subscription manager
//! Publish–subscribe pattern.
//!
//! This is an attempt to implement [NUT-17](https://github.com/cashubtc/nuts/blob/main/17.md)
//! This is a generic implementation for
//! [NUT-17(https://github.com/cashubtc/nuts/blob/main/17.md) with a type
//! agnostic Publish-subscribe manager.
//!
//! The manager has a method for subscribers to subscribe to events with a
//! generic type that must be converted to a vector of indexes.
//!
//! Events are also generic that should implement the `Indexable` trait.
use serde::{Deserialize, Serialize};
use std::{
cmp::Ordering,
Expand Down

0 comments on commit 74b9ee4

Please sign in to comment.