-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce subscription support in the Wallet crate.
The main goal is to add a subscription to CDK Mint updates into the wallet. This feature will be particularly useful for improving the code whenever loops hit the mint server to check status changes. The goal is to add an easy-to-use interface that will hide the fact that we're connecting to WebSocket and subscribing to events. This will also hide the fact that the CDK-mint server may not support WebSocket updates. To be fully backward compatible, the HttpClientMethods traits have a new method, `subscribe,` which will return an object that implements `ActiveSubscription.` In the primary implementation, there is a `SubscriptionClient` that will attempt to connect through WebSocket and will fall to the HTTP-status pull and sleep approach (the current approach), but upper stream code will receive updates as if they come from a stream of updates through WebSocket. This `SubscriptionClient` struct will also manage reconnections to WebSockets (with automatic resubscriptions) and all the low-level stuff, providing an easy-to-use interface and leaving the upper-level code with a nice interface that is hard to misuse. When `ActiveSubscription` is dropped, it will automatically unsubscribe.
- Loading branch information
Showing
26 changed files
with
1,320 additions
and
522 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,33 @@ | ||
use cdk::nuts::nut17::{NotificationPayload, Params}; | ||
use cdk::pub_sub::SubId; | ||
|
||
use super::handler::{WsHandle, WsNotification}; | ||
use super::{WsContext, WsError, JSON_RPC_VERSION}; | ||
|
||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] | ||
pub struct Method(Params); | ||
|
||
#[derive(Debug, Clone, serde::Serialize)] | ||
/// The response to a subscription request | ||
pub struct Response { | ||
/// Status | ||
status: String, | ||
/// Subscription ID | ||
#[serde(rename = "subId")] | ||
sub_id: SubId, | ||
} | ||
|
||
#[derive(Debug, Clone, serde::Serialize)] | ||
/// The notification | ||
/// | ||
/// This is the notification that is sent to the client when an event matches a | ||
/// subscription | ||
pub struct Notification { | ||
/// The subscription ID | ||
#[serde(rename = "subId")] | ||
pub sub_id: SubId, | ||
|
||
/// The notification payload | ||
pub payload: NotificationPayload, | ||
} | ||
|
||
impl From<(SubId, NotificationPayload)> for WsNotification<Notification> { | ||
fn from((sub_id, payload): (SubId, NotificationPayload)) -> Self { | ||
WsNotification { | ||
jsonrpc: JSON_RPC_VERSION.to_owned(), | ||
method: "subscribe".to_string(), | ||
params: Notification { sub_id, payload }, | ||
} | ||
use cdk::nuts::nut17::ws::{WsResponseResult, WsSubscribeResponse}; | ||
use cdk::nuts::nut17::Params; | ||
|
||
use super::{WsContext, WsError}; | ||
|
||
/// The `handle` method is called when a client sends a subscription request | ||
pub(crate) async fn handle( | ||
context: &mut WsContext, | ||
params: Params, | ||
) -> Result<WsResponseResult, WsError> { | ||
let sub_id = params.id.clone(); | ||
if context.subscriptions.contains_key(&sub_id) { | ||
// Subscription ID already exits. Returns an error instead of | ||
// replacing the other subscription or avoiding it. | ||
return Err(WsError::InvalidParams); | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl WsHandle for Method { | ||
type Response = Response; | ||
|
||
/// The `handle` method is called when a client sends a subscription request | ||
async fn handle(self, context: &mut WsContext) -> Result<Self::Response, WsError> { | ||
let sub_id = self.0.id.clone(); | ||
if context.subscriptions.contains_key(&sub_id) { | ||
// Subscription ID already exits. Returns an error instead of | ||
// replacing the other subscription or avoiding it. | ||
return Err(WsError::InvalidParams); | ||
} | ||
|
||
let mut subscription = context | ||
.state | ||
.mint | ||
.pubsub_manager | ||
.subscribe(self.0.clone()) | ||
.await; | ||
let publisher = context.publisher.clone(); | ||
context.subscriptions.insert( | ||
sub_id.clone(), | ||
tokio::spawn(async move { | ||
while let Some(response) = subscription.recv().await { | ||
let _ = publisher.send(response).await; | ||
} | ||
}), | ||
); | ||
Ok(Response { | ||
status: "OK".to_string(), | ||
sub_id, | ||
}) | ||
let mut subscription = context.state.mint.pubsub_manager.subscribe(params).await; | ||
let publisher = context.publisher.clone(); | ||
context.subscriptions.insert( | ||
sub_id.clone(), | ||
tokio::spawn(async move { | ||
while let Some(response) = subscription.recv().await { | ||
let _ = publisher.send(response).await; | ||
} | ||
}), | ||
); | ||
Ok(WsSubscribeResponse { | ||
status: "OK".to_string(), | ||
sub_id, | ||
} | ||
.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,18 @@ | ||
use cdk::pub_sub::SubId; | ||
use cdk::nuts::nut17::ws::{WsResponseResult, WsUnsubscribeRequest, WsUnsubscribeResponse}; | ||
|
||
use super::handler::WsHandle; | ||
use super::{WsContext, WsError}; | ||
|
||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] | ||
pub struct Method { | ||
#[serde(rename = "subId")] | ||
pub sub_id: SubId, | ||
} | ||
|
||
#[derive(Debug, Clone, serde::Serialize)] | ||
pub struct Response { | ||
status: String, | ||
sub_id: SubId, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl WsHandle for Method { | ||
type Response = Response; | ||
|
||
async fn handle(self, context: &mut WsContext) -> Result<Self::Response, WsError> { | ||
if context.subscriptions.remove(&self.sub_id).is_some() { | ||
Ok(Response { | ||
status: "OK".to_string(), | ||
sub_id: self.sub_id, | ||
}) | ||
} else { | ||
Err(WsError::InvalidParams) | ||
pub(crate) async fn handle( | ||
context: &mut WsContext, | ||
req: WsUnsubscribeRequest, | ||
) -> Result<WsResponseResult, WsError> { | ||
if context.subscriptions.remove(&req.sub_id).is_some() { | ||
Ok(WsUnsubscribeResponse { | ||
status: "OK".to_string(), | ||
sub_id: req.sub_id, | ||
} | ||
.into()) | ||
} else { | ||
Err(WsError::InvalidParams) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.