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

Commit 4dad3a3

Browse files
committed
Expose ListProtocolsResponse event
1 parent 69883b6 commit 4dad3a3

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
lines changed

src/events.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//!
1616
//! [`LiquidityManager::get_and_clear_pending_events`]: crate::LiquidityManager::get_and_clear_pending_events
1717
18+
use crate::lsps0;
1819
#[cfg(lsps1)]
1920
use crate::lsps1;
2021
use crate::lsps2;
@@ -78,6 +79,8 @@ impl EventQueue {
7879
/// An event which you should probably take some action in response to.
7980
#[derive(Debug, Clone, PartialEq, Eq)]
8081
pub enum Event {
82+
/// An LSPS0 client event.
83+
LSPS0Client(lsps0::event::LSPS0ClientEvent),
8184
/// An LSPS1 (Channel Request) client event.
8285
#[cfg(lsps1)]
8386
LSPS1Client(lsps1::event::LSPS1ClientEvent),

src/lsps0/client.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! specifcation](https://github.com/BitcoinAndLightningLayerSpecs/lsp/tree/main/LSPS0) for more
55
//! information.
66
7+
use crate::events::{Event, EventQueue};
8+
use crate::lsps0::event::LSPS0ClientEvent;
79
use crate::lsps0::msgs::{
810
LSPS0Message, LSPS0Request, LSPS0Response, LSPSMessage, ListProtocolsRequest,
911
ListProtocolsResponse, ProtocolMessageHandler, ResponseError,
@@ -27,6 +29,7 @@ where
2729
{
2830
entropy_source: ES,
2931
pending_messages: Arc<Mutex<Vec<(PublicKey, LSPSMessage)>>>,
32+
pending_events: Arc<EventQueue>,
3033
}
3134

3235
impl<ES: Deref> LSPS0ClientHandler<ES>
@@ -36,8 +39,9 @@ where
3639
/// Returns a new instance of [`LSPS0ClientHandler`].
3740
pub fn new(
3841
entropy_source: ES, pending_messages: Arc<Mutex<Vec<(PublicKey, LSPSMessage)>>>,
42+
pending_events: Arc<EventQueue>,
3943
) -> Self {
40-
Self { entropy_source, pending_messages }
44+
Self { entropy_source, pending_messages, pending_events }
4145
}
4246

4347
/// Calls LSPS0's `list_protocols`.
@@ -59,10 +63,18 @@ where
5963
}
6064

6165
fn handle_response(
62-
&self, response: LSPS0Response, _counterparty_node_id: &PublicKey,
66+
&self, response: LSPS0Response, counterparty_node_id: &PublicKey,
6367
) -> Result<(), LightningError> {
6468
match response {
65-
LSPS0Response::ListProtocols(ListProtocolsResponse { protocols: _ }) => Ok(()),
69+
LSPS0Response::ListProtocols(ListProtocolsResponse { protocols }) => {
70+
self.pending_events.enqueue(Event::LSPS0Client(
71+
LSPS0ClientEvent::ListProtocolsResponse {
72+
counterparty_node_id: *counterparty_node_id,
73+
protocols,
74+
},
75+
));
76+
Ok(())
77+
}
6678
LSPS0Response::ListProtocolsError(ResponseError { code, message, data, .. }) => {
6779
Err(LightningError {
6880
err: format!(

src/lsps0/event.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Contains LSPS0 event types
11+
12+
use bitcoin::secp256k1::PublicKey;
13+
14+
/// An event which an LSPS0 client may want to take some action in response to.
15+
#[derive(Clone, Debug, PartialEq, Eq)]
16+
pub enum LSPS0ClientEvent {
17+
/// Information from the LSP about the protocols they support.
18+
ListProtocolsResponse {
19+
/// The node id of the LSP.
20+
counterparty_node_id: PublicKey,
21+
/// A list of supported protocols.
22+
protocols: Vec<u16>,
23+
},
24+
}

src/lsps0/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
//! Types and primitives that implement the LSPS0: Transport Layer specification.
1111
1212
pub mod client;
13+
pub mod event;
1314
pub mod msgs;
1415
pub mod service;

src/lsps2/client.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99

1010
//! Contains the main LSPS2 client object, [`LSPS2ClientHandler`].
1111
12-
use crate::events::EventQueue;
13-
use crate::lsps0::msgs::{LSPSMessage, ProtocolMessageHandler, RequestId};
12+
use crate::events::{Event, EventQueue};
13+
use crate::lsps0::msgs::{LSPSMessage, ProtocolMessageHandler, RequestId, ResponseError};
1414
use crate::lsps2::event::LSPS2ClientEvent;
1515
use crate::prelude::{HashMap, String, ToString, Vec};
1616
use crate::sync::{Arc, Mutex, RwLock};
17-
use crate::{events::Event, lsps0::msgs::ResponseError};
1817

1918
use lightning::ln::msgs::{ErrorAction, LightningError};
2019
use lightning::ln::peer_handler::APeerManager;

src/manager.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,11 @@ where {
126126
let pending_messages = Arc::new(Mutex::new(vec![]));
127127
let pending_events = Arc::new(EventQueue::new());
128128

129-
let lsps0_client_handler =
130-
LSPS0ClientHandler::new(entropy_source.clone(), Arc::clone(&pending_messages));
129+
let lsps0_client_handler = LSPS0ClientHandler::new(
130+
entropy_source.clone(),
131+
Arc::clone(&pending_messages),
132+
Arc::clone(&pending_events),
133+
);
131134

132135
let lsps0_service_handler = if service_config.is_some() {
133136
Some(LSPS0ServiceHandler::new(vec![], Arc::clone(&pending_messages)))

0 commit comments

Comments
 (0)