Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit c5e3963

Browse files
committed
LSPS0: List Protocols
1 parent 044b73f commit c5e3963

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

src/events.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! Because we don't have a built-in runtime, it's up to the end-user to poll
1414
//! [`crate::LiquidityManager::get_and_clear_pending_events()`] to receive events.
1515
16+
use crate::transport::msgs::{LSPS0Response, RequestId};
1617
use std::collections::VecDeque;
1718
use std::sync::{Condvar, Mutex};
1819

@@ -55,4 +56,16 @@ impl EventQueue {
5556

5657
/// Event which you should probably take some action in response to.
5758
#[derive(Debug, Clone, PartialEq, Eq)]
58-
pub enum Event {}
59+
pub struct Event {
60+
/// The id from the request
61+
pub id: RequestId,
62+
/// The result of request
63+
pub result: EventResult,
64+
}
65+
66+
/// Content of the event
67+
#[derive(Debug, Clone, PartialEq, Eq)]
68+
pub enum EventResult {
69+
/// The LSPS0 response
70+
LSPS0(LSPS0Response),
71+
}

src/transport/message_handler.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::events::{Event, EventQueue};
2-
use crate::transport::msgs::{LSPSMessage, RawLSPSMessage, LSPS_MESSAGE_TYPE};
1+
use crate::events::{Event, EventQueue, EventResult};
2+
use crate::transport::msgs::{LSPSMessage, RawLSPSMessage, RequestId, LSPS_MESSAGE_TYPE};
33
use crate::transport::protocol::LSPS0MessageHandler;
44

55
use bitcoin::secp256k1::PublicKey;
@@ -32,7 +32,7 @@ pub(crate) trait ProtocolMessageHandler {
3232

3333
fn handle_message(
3434
&self, message: Self::ProtocolMessage, counterparty_node_id: &PublicKey,
35-
) -> Result<(), LightningError>;
35+
) -> Result<Option<Event>, LightningError>;
3636
}
3737

3838
/// A configuration for [`LiquidityManager`].
@@ -121,6 +121,25 @@ where {
121121
self.pending_events.get_and_clear_pending_events()
122122
}
123123

124+
/// Returns the waiting event result and close
125+
pub fn wait_event_result(self, request_id: RequestId) -> EventResult {
126+
loop {
127+
let events = self.pending_events.get_and_clear_pending_events();
128+
for Event { id, result } in events {
129+
if id == request_id {
130+
return result;
131+
}
132+
}
133+
}
134+
}
135+
136+
/// This allows the list the protocols of LSPS0
137+
pub fn list_protocols(
138+
&self, counterparty_node_id: PublicKey,
139+
) -> Result<RequestId, lightning::ln::msgs::LightningError> {
140+
Ok(self.lsps0_message_handler.list_protocols(counterparty_node_id))
141+
}
142+
124143
fn handle_lsps_message(
125144
&self, msg: LSPSMessage, sender_node_id: &PublicKey,
126145
) -> Result<(), lightning::ln::msgs::LightningError> {
@@ -129,7 +148,11 @@ where {
129148
return Err(LightningError { err: format!("{} did not understand a message we previously sent, maybe they don't support a protocol we are trying to use?", sender_node_id), action: ErrorAction::IgnoreAndLog(Level::Error)});
130149
}
131150
LSPSMessage::LSPS0(msg) => {
132-
self.lsps0_message_handler.handle_message(msg, sender_node_id)?;
151+
if let Some(event) =
152+
self.lsps0_message_handler.handle_message(msg, sender_node_id)?
153+
{
154+
self.pending_events.enqueue(event);
155+
}
133156
}
134157
}
135158
Ok(())

src/transport/protocol.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use lightning::util::logger::Level;
55
use std::ops::Deref;
66
use std::sync::{Arc, Mutex};
77

8+
use crate::events::{Event, EventResult};
89
use crate::transport::message_handler::ProtocolMessageHandler;
910
use crate::transport::msgs::{
1011
LSPS0Message, LSPS0Request, LSPS0Response, LSPSMessage, ListProtocolsRequest,
@@ -32,41 +33,40 @@ where
3233
Self { entropy_source, protocols, pending_messages }
3334
}
3435

35-
pub fn list_protocols(&self, counterparty_node_id: PublicKey) {
36+
pub fn list_protocols(&self, counterparty_node_id: PublicKey) -> RequestId {
37+
let request_id = utils::generate_request_id(&self.entropy_source);
3638
let msg = LSPS0Message::Request(
37-
utils::generate_request_id(&self.entropy_source),
39+
request_id.clone(),
3840
LSPS0Request::ListProtocols(ListProtocolsRequest {}),
3941
);
40-
4142
self.enqueue_message(counterparty_node_id, msg);
43+
request_id
4244
}
4345

4446
fn enqueue_message(&self, counterparty_node_id: PublicKey, message: LSPS0Message) {
4547
self.pending_messages.lock().unwrap().push((counterparty_node_id, message.into()));
4648
}
4749

4850
fn handle_request(
49-
&self, request_id: RequestId, request: LSPS0Request, counterparty_node_id: &PublicKey,
50-
) -> Result<(), lightning::ln::msgs::LightningError> {
51+
&self, id: RequestId, request: LSPS0Request, counterparty_node_id: &PublicKey,
52+
) -> Result<Option<Event>, lightning::ln::msgs::LightningError> {
5153
match request {
5254
LSPS0Request::ListProtocols(_) => {
53-
let msg = LSPS0Message::Response(
54-
request_id,
55-
LSPS0Response::ListProtocols(ListProtocolsResponse {
56-
protocols: self.protocols.clone(),
57-
}),
58-
);
55+
let response = LSPS0Response::ListProtocols(ListProtocolsResponse {
56+
protocols: self.protocols.clone(),
57+
});
58+
let msg = LSPS0Message::Response(id.clone(), response.clone());
5959
self.enqueue_message(*counterparty_node_id, msg);
60-
Ok(())
60+
Ok(Some(Event { id, result: EventResult::LSPS0(response) }))
6161
}
6262
}
6363
}
6464

6565
fn handle_response(
6666
&self, response: LSPS0Response, counterparty_node_id: &PublicKey,
67-
) -> Result<(), LightningError> {
67+
) -> Result<Option<Event>, LightningError> {
6868
match response {
69-
LSPS0Response::ListProtocols(ListProtocolsResponse { protocols }) => Ok(()),
69+
LSPS0Response::ListProtocols(ListProtocolsResponse { protocols }) => Ok(None),
7070
LSPS0Response::ListProtocolsError(ResponseError { code, message, data, .. }) => {
7171
Err(LightningError {
7272
err: format!(
@@ -89,7 +89,7 @@ where
8989

9090
fn handle_message(
9191
&self, message: Self::ProtocolMessage, counterparty_node_id: &PublicKey,
92-
) -> Result<(), LightningError> {
92+
) -> Result<Option<Event>, LightningError> {
9393
match message {
9494
LSPS0Message::Request(request_id, request) => {
9595
self.handle_request(request_id, request, counterparty_node_id)

0 commit comments

Comments
 (0)