Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lightning-liquidity/src/events.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's fine for now, but given that we eventually might want to get away from LSPS nomenclature, we could consider naming it differently (LiquidityEvent?). Then again, moving away from LSPS will be a larger refactor at some point anyways, so might as well rename it accordingly again at that point.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use core::task::{Poll, Waker};
pub const MAX_EVENT_QUEUE_SIZE: usize = 1000;

pub(crate) struct EventQueue {
queue: Arc<Mutex<VecDeque<Event>>>,
queue: Arc<Mutex<VecDeque<LSPSEvent>>>,
waker: Arc<Mutex<Option<Waker>>>,
#[cfg(feature = "std")]
condvar: crate::sync::Condvar,
Expand All @@ -47,7 +47,7 @@ impl EventQueue {
Self { queue, waker }
}

pub fn enqueue(&self, event: Event) {
pub fn enqueue(&self, event: LSPSEvent) {
{
let mut queue = self.queue.lock().unwrap();
if queue.len() < MAX_EVENT_QUEUE_SIZE {
Expand All @@ -64,19 +64,19 @@ impl EventQueue {
self.condvar.notify_one();
}

pub fn next_event(&self) -> Option<Event> {
pub fn next_event(&self) -> Option<LSPSEvent> {
self.queue.lock().unwrap().pop_front()
}

pub async fn next_event_async(&self) -> Event {
pub async fn next_event_async(&self) -> LSPSEvent {
EventFuture { event_queue: Arc::clone(&self.queue), waker: Arc::clone(&self.waker) }.await
}

#[cfg(feature = "std")]
pub fn wait_next_event(&self) -> Event {
pub fn wait_next_event(&self) -> LSPSEvent {
let mut queue = self
.condvar
.wait_while(self.queue.lock().unwrap(), |queue: &mut VecDeque<Event>| queue.is_empty())
.wait_while(self.queue.lock().unwrap(), |queue: &mut VecDeque<LSPSEvent>| queue.is_empty())
.unwrap();

let event = queue.pop_front().expect("non-empty queue");
Expand All @@ -95,14 +95,14 @@ impl EventQueue {
event
}

pub fn get_and_clear_pending_events(&self) -> Vec<Event> {
pub fn get_and_clear_pending_events(&self) -> Vec<LSPSEvent> {
self.queue.lock().unwrap().split_off(0).into()
}
}

/// An event which you should probably take some action in response to.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
pub enum LSPSEvent {
/// An LSPS0 client event.
LSPS0Client(lsps0::event::LSPS0ClientEvent),
/// An LSPS1 (Channel Request) client event.
Expand All @@ -117,12 +117,12 @@ pub enum Event {
}

struct EventFuture {
event_queue: Arc<Mutex<VecDeque<Event>>>,
event_queue: Arc<Mutex<VecDeque<LSPSEvent>>>,
waker: Arc<Mutex<Option<Waker>>>,
}

impl Future for EventFuture {
type Output = Event;
type Output = LSPSEvent;

fn poll(
self: core::pin::Pin<&mut Self>, cx: &mut core::task::Context<'_>,
Expand Down Expand Up @@ -154,7 +154,7 @@ mod tests {
let secp_ctx = Secp256k1::new();
let counterparty_node_id =
PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
let expected_event = Event::LSPS0Client(LSPS0ClientEvent::ListProtocolsResponse {
let expected_event = LSPSEvent::LSPS0Client(LSPS0ClientEvent::ListProtocolsResponse {
counterparty_node_id,
protocols: Vec::new(),
});
Expand Down
4 changes: 2 additions & 2 deletions lightning-liquidity/src/lsps0/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! specifcation](https://github.com/BitcoinAndLightningLayerSpecs/lsp/tree/main/LSPS0) for more
//! information.

use crate::events::{Event, EventQueue};
use crate::events::{LSPSEvent, EventQueue};
use crate::lsps0::event::LSPS0ClientEvent;
use crate::lsps0::msgs::{
LSPS0Message, LSPS0Request, LSPS0Response, ListProtocolsRequest, ListProtocolsResponse,
Expand Down Expand Up @@ -62,7 +62,7 @@ where
) -> Result<(), LightningError> {
match response {
LSPS0Response::ListProtocols(ListProtocolsResponse { protocols }) => {
self.pending_events.enqueue(Event::LSPS0Client(
self.pending_events.enqueue(LSPSEvent::LSPS0Client(
LSPS0ClientEvent::ListProtocolsResponse {
counterparty_node_id: *counterparty_node_id,
protocols,
Expand Down
14 changes: 7 additions & 7 deletions lightning-liquidity/src/lsps1/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::msgs::{
};
use crate::message_queue::MessageQueue;

use crate::events::{Event, EventQueue};
use crate::events::{LSPSEvent, EventQueue};
use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError};
use crate::prelude::{new_hash_map, HashMap, HashSet};
use crate::sync::{Arc, Mutex, RwLock};
Expand Down Expand Up @@ -119,7 +119,7 @@ where
});
}

self.pending_events.enqueue(Event::LSPS1Client(
self.pending_events.enqueue(LSPSEvent::LSPS1Client(
LSPS1ClientEvent::SupportedOptionsReady {
counterparty_node_id: *counterparty_node_id,
supported_options: result.options,
Expand Down Expand Up @@ -156,7 +156,7 @@ where
});
}

self.pending_events.enqueue(Event::LSPS1Client(
self.pending_events.enqueue(LSPSEvent::LSPS1Client(
LSPS1ClientEvent::SupportedOptionsRequestFailed {
request_id: request_id.clone(),
counterparty_node_id: *counterparty_node_id,
Expand Down Expand Up @@ -233,7 +233,7 @@ where
});
}

self.pending_events.enqueue(Event::LSPS1Client(LSPS1ClientEvent::OrderCreated {
self.pending_events.enqueue(LSPSEvent::LSPS1Client(LSPS1ClientEvent::OrderCreated {
request_id,
counterparty_node_id: *counterparty_node_id,
order_id: response.order_id,
Expand Down Expand Up @@ -274,7 +274,7 @@ where
});
}

self.pending_events.enqueue(Event::LSPS1Client(
self.pending_events.enqueue(LSPSEvent::LSPS1Client(
LSPS1ClientEvent::OrderRequestFailed {
request_id: request_id.clone(),
counterparty_node_id: *counterparty_node_id,
Expand Down Expand Up @@ -352,7 +352,7 @@ where
});
}

self.pending_events.enqueue(Event::LSPS1Client(LSPS1ClientEvent::OrderStatus {
self.pending_events.enqueue(LSPSEvent::LSPS1Client(LSPS1ClientEvent::OrderStatus {
request_id,
counterparty_node_id: *counterparty_node_id,
order_id: response.order_id,
Expand Down Expand Up @@ -393,7 +393,7 @@ where
});
}

self.pending_events.enqueue(Event::LSPS1Client(
self.pending_events.enqueue(LSPSEvent::LSPS1Client(
LSPS1ClientEvent::OrderRequestFailed {
request_id: request_id.clone(),
counterparty_node_id: *counterparty_node_id,
Expand Down
6 changes: 3 additions & 3 deletions lightning-liquidity/src/lsps2/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

//! Contains the main LSPS2 client object, [`LSPS2ClientHandler`].

use crate::events::{Event, EventQueue};
use crate::events::{LSPSEvent, EventQueue};
use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError};
use crate::lsps2::event::LSPS2ClientEvent;
use crate::message_queue::MessageQueue;
Expand Down Expand Up @@ -198,7 +198,7 @@ where
});
}

self.pending_events.enqueue(Event::LSPS2Client(
self.pending_events.enqueue(LSPSEvent::LSPS2Client(
LSPS2ClientEvent::OpeningParametersReady {
request_id,
counterparty_node_id: *counterparty_node_id,
Expand Down Expand Up @@ -264,7 +264,7 @@ where
})?;

if let Ok(intercept_scid) = result.jit_channel_scid.to_scid() {
self.pending_events.enqueue(Event::LSPS2Client(
self.pending_events.enqueue(LSPSEvent::LSPS2Client(
LSPS2ClientEvent::InvoiceParametersReady {
request_id,
counterparty_node_id: *counterparty_node_id,
Expand Down
8 changes: 4 additions & 4 deletions lightning-liquidity/src/lsps2/service.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, these bounds are (going) to be necessary as we're cloing the ChannelManager reference for the LSPS1ServiceHandler. It's just not surfaced as it lives behind cfg(lsps1_service) for now. Assuming we don't want to macroize the bounds, I'd propose to drop this commit for now.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

//! Contains the main LSPS2 server-side object, [`LSPS2ServiceHandler`].

use crate::events::{Event, EventQueue};
use crate::events::{LSPSEvent, EventQueue};
use crate::lsps0::ser::{
LSPSMessage, ProtocolMessageHandler, RequestId, ResponseError,
JSONRPC_INTERNAL_ERROR_ERROR_CODE, JSONRPC_INTERNAL_ERROR_ERROR_MESSAGE,
Expand Down Expand Up @@ -806,7 +806,7 @@ where
};
match jit_channel.htlc_intercepted(htlc) {
Ok(Some(HTLCInterceptedAction::OpenChannel(open_channel_params))) => {
let event = Event::LSPS2Service(LSPS2ServiceEvent::OpenChannel {
let event = LSPSEvent::LSPS2Service(LSPS2ServiceEvent::OpenChannel {
their_network_key: counterparty_node_id.clone(),
amt_to_forward_msat: open_channel_params.amt_to_forward_msat,
opening_fee_msat: open_channel_params.opening_fee_msat,
Expand Down Expand Up @@ -1091,7 +1091,7 @@ where
request,
) {
(Ok(()), msg) => {
let event = Event::LSPS2Service(LSPS2ServiceEvent::GetInfo {
let event = LSPSEvent::LSPS2Service(LSPS2ServiceEvent::GetInfo {
request_id,
counterparty_node_id: *counterparty_node_id,
token: params.token,
Expand Down Expand Up @@ -1210,7 +1210,7 @@ where
request,
) {
(Ok(()), msg) => {
let event = Event::LSPS2Service(LSPS2ServiceEvent::BuyRequest {
let event = LSPSEvent::LSPS2Service(LSPS2ServiceEvent::BuyRequest {
request_id,
counterparty_node_id: *counterparty_node_id,
opening_fee_params: params.opening_fee_params,
Expand Down
10 changes: 5 additions & 5 deletions lightning-liquidity/src/manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::events::{Event, EventQueue};
use crate::events::{LSPSEvent, EventQueue};
use crate::lsps0::client::LSPS0ClientHandler;
use crate::lsps0::msgs::LSPS0Message;
use crate::lsps0::ser::{
Expand Down Expand Up @@ -329,7 +329,7 @@ where {
///
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
#[cfg(feature = "std")]
pub fn wait_next_event(&self) -> Event {
pub fn wait_next_event(&self) -> LSPSEvent {
self.pending_events.wait_next_event()
}

Expand All @@ -342,7 +342,7 @@ where {
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
///
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
pub fn next_event(&self) -> Option<Event> {
pub fn next_event(&self) -> Option<LSPSEvent> {
self.pending_events.next_event()
}

Expand All @@ -355,7 +355,7 @@ where {
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
///
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
pub async fn next_event_async(&self) -> Event {
pub async fn next_event_async(&self) -> LSPSEvent {
self.pending_events.next_event_async().await
}

Expand All @@ -368,7 +368,7 @@ where {
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
///
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
pub fn get_and_clear_pending_events(&self) -> Vec<Event> {
pub fn get_and_clear_pending_events(&self) -> Vec<LSPSEvent> {
self.pending_events.get_and_clear_pending_events()
}

Expand Down
10 changes: 5 additions & 5 deletions lightning-liquidity/tests/lsps2_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod common;

use common::{create_service_and_client_nodes, get_lsps_message, Node};

use lightning_liquidity::events::Event;
use lightning_liquidity::events::LSPSEvent;
use lightning_liquidity::lsps2::client::LSPS2ClientConfig;
use lightning_liquidity::lsps2::event::{LSPS2ClientEvent, LSPS2ServiceEvent};
use lightning_liquidity::lsps2::msgs::RawOpeningFeeParams;
Expand Down Expand Up @@ -113,7 +113,7 @@ fn invoice_generation_flow() {

let get_info_event = service_node.liquidity_manager.next_event().unwrap();
match get_info_event {
Event::LSPS2Service(LSPS2ServiceEvent::GetInfo {
LSPSEvent::LSPS2Service(LSPS2ServiceEvent::GetInfo {
request_id,
counterparty_node_id,
token,
Expand Down Expand Up @@ -151,7 +151,7 @@ fn invoice_generation_flow() {

let opening_params_event = client_node.liquidity_manager.next_event().unwrap();
let opening_fee_params = match opening_params_event {
Event::LSPS2Client(LSPS2ClientEvent::OpeningParametersReady {
LSPSEvent::LSPS2Client(LSPS2ClientEvent::OpeningParametersReady {
request_id,
counterparty_node_id,
opening_fee_params_menu,
Expand All @@ -175,7 +175,7 @@ fn invoice_generation_flow() {

let buy_event = service_node.liquidity_manager.next_event().unwrap();
match buy_event {
Event::LSPS2Service(LSPS2ServiceEvent::BuyRequest {
LSPSEvent::LSPS2Service(LSPS2ServiceEvent::BuyRequest {
request_id,
counterparty_node_id,
opening_fee_params: ofp,
Expand Down Expand Up @@ -210,7 +210,7 @@ fn invoice_generation_flow() {

let invoice_params_event = client_node.liquidity_manager.next_event().unwrap();
match invoice_params_event {
Event::LSPS2Client(LSPS2ClientEvent::InvoiceParametersReady {
LSPSEvent::LSPS2Client(LSPS2ClientEvent::InvoiceParametersReady {
request_id,
counterparty_node_id,
intercept_scid: iscid,
Expand Down