Skip to content

Commit 5e0c5f0

Browse files
committed
Rename Event to LiquidityEvent
In order to avoid naming collisions with LDK's `Event` type, we rename `lightning-liquidity`'s `Event` to `LiquidityEvent`. To minimize furhter churn on the upcoming renaming changes, we also `impl From X for LiquidityEvent` for the protocol-specific event variants, which also allows us to reduce some boilerplate while enqueuing.
1 parent 962da67 commit 5e0c5f0

File tree

8 files changed

+119
-104
lines changed

8 files changed

+119
-104
lines changed

lightning-liquidity/src/events.rs

+45-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use core::task::{Poll, Waker};
2828
pub const MAX_EVENT_QUEUE_SIZE: usize = 1000;
2929

3030
pub(crate) struct EventQueue {
31-
queue: Arc<Mutex<VecDeque<Event>>>,
31+
queue: Arc<Mutex<VecDeque<LiquidityEvent>>>,
3232
waker: Arc<Mutex<Option<Waker>>>,
3333
#[cfg(feature = "std")]
3434
condvar: crate::sync::Condvar,
@@ -47,11 +47,11 @@ impl EventQueue {
4747
Self { queue, waker }
4848
}
4949

50-
pub fn enqueue(&self, event: Event) {
50+
pub fn enqueue<E: Into<LiquidityEvent>>(&self, event: E) {
5151
{
5252
let mut queue = self.queue.lock().unwrap();
5353
if queue.len() < MAX_EVENT_QUEUE_SIZE {
54-
queue.push_back(event);
54+
queue.push_back(event.into());
5555
} else {
5656
return;
5757
}
@@ -64,19 +64,21 @@ impl EventQueue {
6464
self.condvar.notify_one();
6565
}
6666

67-
pub fn next_event(&self) -> Option<Event> {
67+
pub fn next_event(&self) -> Option<LiquidityEvent> {
6868
self.queue.lock().unwrap().pop_front()
6969
}
7070

71-
pub async fn next_event_async(&self) -> Event {
71+
pub async fn next_event_async(&self) -> LiquidityEvent {
7272
EventFuture { event_queue: Arc::clone(&self.queue), waker: Arc::clone(&self.waker) }.await
7373
}
7474

7575
#[cfg(feature = "std")]
76-
pub fn wait_next_event(&self) -> Event {
76+
pub fn wait_next_event(&self) -> LiquidityEvent {
7777
let mut queue = self
7878
.condvar
79-
.wait_while(self.queue.lock().unwrap(), |queue: &mut VecDeque<Event>| queue.is_empty())
79+
.wait_while(self.queue.lock().unwrap(), |queue: &mut VecDeque<LiquidityEvent>| {
80+
queue.is_empty()
81+
})
8082
.unwrap();
8183

8284
let event = queue.pop_front().expect("non-empty queue");
@@ -95,14 +97,14 @@ impl EventQueue {
9597
event
9698
}
9799

98-
pub fn get_and_clear_pending_events(&self) -> Vec<Event> {
100+
pub fn get_and_clear_pending_events(&self) -> Vec<LiquidityEvent> {
99101
self.queue.lock().unwrap().split_off(0).into()
100102
}
101103
}
102104

103105
/// An event which you should probably take some action in response to.
104106
#[derive(Debug, Clone, PartialEq, Eq)]
105-
pub enum Event {
107+
pub enum LiquidityEvent {
106108
/// An LSPS0 client event.
107109
LSPS0Client(lsps0::event::LSPS0ClientEvent),
108110
/// An LSPS1 (Channel Request) client event.
@@ -116,13 +118,44 @@ pub enum Event {
116118
LSPS2Service(lsps2::event::LSPS2ServiceEvent),
117119
}
118120

121+
impl From<lsps0::event::LSPS0ClientEvent> for LiquidityEvent {
122+
fn from(event: lsps0::event::LSPS0ClientEvent) -> Self {
123+
Self::LSPS0Client(event)
124+
}
125+
}
126+
127+
impl From<lsps1::event::LSPS1ClientEvent> for LiquidityEvent {
128+
fn from(event: lsps1::event::LSPS1ClientEvent) -> Self {
129+
Self::LSPS1Client(event)
130+
}
131+
}
132+
133+
#[cfg(lsps1_service)]
134+
impl From<lsps1::event::LSPS1ServiceEvent> for LiquidityEvent {
135+
fn from(event: lsps1::event::LSPS1ServiceEvent) -> Self {
136+
Self::LSPS1Service(event)
137+
}
138+
}
139+
140+
impl From<lsps2::event::LSPS2ClientEvent> for LiquidityEvent {
141+
fn from(event: lsps2::event::LSPS2ClientEvent) -> Self {
142+
Self::LSPS2Client(event)
143+
}
144+
}
145+
146+
impl From<lsps2::event::LSPS2ServiceEvent> for LiquidityEvent {
147+
fn from(event: lsps2::event::LSPS2ServiceEvent) -> Self {
148+
Self::LSPS2Service(event)
149+
}
150+
}
151+
119152
struct EventFuture {
120-
event_queue: Arc<Mutex<VecDeque<Event>>>,
153+
event_queue: Arc<Mutex<VecDeque<LiquidityEvent>>>,
121154
waker: Arc<Mutex<Option<Waker>>>,
122155
}
123156

124157
impl Future for EventFuture {
125-
type Output = Event;
158+
type Output = LiquidityEvent;
126159

127160
fn poll(
128161
self: core::pin::Pin<&mut Self>, cx: &mut core::task::Context<'_>,
@@ -154,7 +187,7 @@ mod tests {
154187
let secp_ctx = Secp256k1::new();
155188
let counterparty_node_id =
156189
PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
157-
let expected_event = Event::LSPS0Client(LSPS0ClientEvent::ListProtocolsResponse {
190+
let expected_event = LiquidityEvent::LSPS0Client(LSPS0ClientEvent::ListProtocolsResponse {
158191
counterparty_node_id,
159192
protocols: Vec::new(),
160193
});

lightning-liquidity/src/lsps0/client.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! specifcation](https://github.com/lightning/blips/blob/master/blip-0050.md) for more
55
//! information.
66
7-
use crate::events::{Event, EventQueue};
7+
use crate::events::EventQueue;
88
use crate::lsps0::event::LSPS0ClientEvent;
99
use crate::lsps0::msgs::{
1010
LSPS0Message, LSPS0Request, LSPS0Response, ListProtocolsRequest, ListProtocolsResponse,
@@ -62,12 +62,10 @@ where
6262
) -> Result<(), LightningError> {
6363
match response {
6464
LSPS0Response::ListProtocols(ListProtocolsResponse { protocols }) => {
65-
self.pending_events.enqueue(Event::LSPS0Client(
66-
LSPS0ClientEvent::ListProtocolsResponse {
67-
counterparty_node_id: *counterparty_node_id,
68-
protocols,
69-
},
70-
));
65+
self.pending_events.enqueue(LSPS0ClientEvent::ListProtocolsResponse {
66+
counterparty_node_id: *counterparty_node_id,
67+
protocols,
68+
});
7169
Ok(())
7270
},
7371
LSPS0Response::ListProtocolsError(ResponseError { code, message, data, .. }) => {

lightning-liquidity/src/lsps1/client.rs

+25-33
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::msgs::{
1616
};
1717
use crate::message_queue::MessageQueue;
1818

19-
use crate::events::{Event, EventQueue};
19+
use crate::events::EventQueue;
2020
use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError};
2121
use crate::prelude::{new_hash_map, HashMap, HashSet};
2222
use crate::sync::{Arc, Mutex, RwLock};
@@ -119,13 +119,11 @@ where
119119
});
120120
}
121121

122-
self.pending_events.enqueue(Event::LSPS1Client(
123-
LSPS1ClientEvent::SupportedOptionsReady {
124-
counterparty_node_id: *counterparty_node_id,
125-
supported_options: result.options,
126-
request_id,
127-
},
128-
));
122+
self.pending_events.enqueue(LSPS1ClientEvent::SupportedOptionsReady {
123+
counterparty_node_id: *counterparty_node_id,
124+
supported_options: result.options,
125+
request_id,
126+
});
129127
Ok(())
130128
},
131129
None => Err(LightningError {
@@ -156,13 +154,11 @@ where
156154
});
157155
}
158156

159-
self.pending_events.enqueue(Event::LSPS1Client(
160-
LSPS1ClientEvent::SupportedOptionsRequestFailed {
161-
request_id: request_id.clone(),
162-
counterparty_node_id: *counterparty_node_id,
163-
error: error.clone(),
164-
},
165-
));
157+
self.pending_events.enqueue(LSPS1ClientEvent::SupportedOptionsRequestFailed {
158+
request_id: request_id.clone(),
159+
counterparty_node_id: *counterparty_node_id,
160+
error: error.clone(),
161+
});
166162

167163
Err(LightningError {
168164
err: format!(
@@ -233,14 +229,14 @@ where
233229
});
234230
}
235231

236-
self.pending_events.enqueue(Event::LSPS1Client(LSPS1ClientEvent::OrderCreated {
232+
self.pending_events.enqueue(LSPS1ClientEvent::OrderCreated {
237233
request_id,
238234
counterparty_node_id: *counterparty_node_id,
239235
order_id: response.order_id,
240236
order: response.order,
241237
payment: response.payment,
242238
channel: response.channel,
243-
}));
239+
});
244240
},
245241
None => {
246242
return Err(LightningError {
@@ -274,13 +270,11 @@ where
274270
});
275271
}
276272

277-
self.pending_events.enqueue(Event::LSPS1Client(
278-
LSPS1ClientEvent::OrderRequestFailed {
279-
request_id: request_id.clone(),
280-
counterparty_node_id: *counterparty_node_id,
281-
error: error.clone(),
282-
},
283-
));
273+
self.pending_events.enqueue(LSPS1ClientEvent::OrderRequestFailed {
274+
request_id: request_id.clone(),
275+
counterparty_node_id: *counterparty_node_id,
276+
error: error.clone(),
277+
});
284278

285279
Err(LightningError {
286280
err: format!(
@@ -352,14 +346,14 @@ where
352346
});
353347
}
354348

355-
self.pending_events.enqueue(Event::LSPS1Client(LSPS1ClientEvent::OrderStatus {
349+
self.pending_events.enqueue(LSPS1ClientEvent::OrderStatus {
356350
request_id,
357351
counterparty_node_id: *counterparty_node_id,
358352
order_id: response.order_id,
359353
order: response.order,
360354
payment: response.payment,
361355
channel: response.channel,
362-
}));
356+
});
363357
},
364358
None => {
365359
return Err(LightningError {
@@ -393,13 +387,11 @@ where
393387
});
394388
}
395389

396-
self.pending_events.enqueue(Event::LSPS1Client(
397-
LSPS1ClientEvent::OrderRequestFailed {
398-
request_id: request_id.clone(),
399-
counterparty_node_id: *counterparty_node_id,
400-
error: error.clone(),
401-
},
402-
));
390+
self.pending_events.enqueue(LSPS1ClientEvent::OrderRequestFailed {
391+
request_id: request_id.clone(),
392+
counterparty_node_id: *counterparty_node_id,
393+
error: error.clone(),
394+
});
403395

404396
Err(LightningError {
405397
err: format!(

lightning-liquidity/src/lsps1/service.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::msgs::{
1717
};
1818
use crate::message_queue::MessageQueue;
1919

20-
use crate::events::{Event, EventQueue};
20+
use crate::events::EventQueue;
2121
use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError};
2222
use crate::prelude::{new_hash_map, HashMap, String};
2323
use crate::sync::{Arc, Mutex, RwLock};
@@ -219,13 +219,11 @@ where
219219
.insert(request_id.clone(), LSPS1Request::CreateOrder(params.clone()));
220220
}
221221

222-
self.pending_events.enqueue(Event::LSPS1Service(
223-
LSPS1ServiceEvent::RequestForPaymentDetails {
224-
request_id,
225-
counterparty_node_id: *counterparty_node_id,
226-
order: params.order,
227-
},
228-
));
222+
self.pending_events.enqueue(LSPS1ServiceEvent::RequestForPaymentDetails {
223+
request_id,
224+
counterparty_node_id: *counterparty_node_id,
225+
order: params.order,
226+
});
229227

230228
Ok(())
231229
}
@@ -322,25 +320,23 @@ where
322320

323321
if let Err(e) = outbound_channel.awaiting_payment() {
324322
peer_state_lock.outbound_channels_by_order_id.remove(&params.order_id);
325-
self.pending_events.enqueue(Event::LSPS1Service(LSPS1ServiceEvent::Refund {
323+
self.pending_events.enqueue(LSPS1ServiceEvent::Refund {
326324
request_id,
327325
counterparty_node_id: *counterparty_node_id,
328326
order_id: params.order_id,
329-
}));
327+
});
330328
return Err(e);
331329
}
332330

333331
peer_state_lock
334332
.pending_requests
335333
.insert(request_id.clone(), LSPS1Request::GetOrder(params.clone()));
336334

337-
self.pending_events.enqueue(Event::LSPS1Service(
338-
LSPS1ServiceEvent::CheckPaymentConfirmation {
339-
request_id,
340-
counterparty_node_id: *counterparty_node_id,
341-
order_id: params.order_id,
342-
},
343-
));
335+
self.pending_events.enqueue(LSPS1ServiceEvent::CheckPaymentConfirmation {
336+
request_id,
337+
counterparty_node_id: *counterparty_node_id,
338+
order_id: params.order_id,
339+
});
344340
},
345341
None => {
346342
return Err(LightningError {

lightning-liquidity/src/lsps2/client.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
//! Contains the main bLIP-52 / LSPS2 client object, [`LSPS2ClientHandler`].
1010
11-
use crate::events::{Event, EventQueue};
11+
use crate::events::EventQueue;
1212
use crate::lsps0::ser::{ProtocolMessageHandler, RequestId, ResponseError};
1313
use crate::lsps2::event::LSPS2ClientEvent;
1414
use crate::message_queue::MessageQueue;
@@ -198,13 +198,11 @@ where
198198
});
199199
}
200200

201-
self.pending_events.enqueue(Event::LSPS2Client(
202-
LSPS2ClientEvent::OpeningParametersReady {
203-
request_id,
204-
counterparty_node_id: *counterparty_node_id,
205-
opening_fee_params_menu: result.opening_fee_params_menu,
206-
},
207-
));
201+
self.pending_events.enqueue(LSPS2ClientEvent::OpeningParametersReady {
202+
request_id,
203+
counterparty_node_id: *counterparty_node_id,
204+
opening_fee_params_menu: result.opening_fee_params_menu,
205+
});
208206
},
209207
None => {
210208
return Err(LightningError {
@@ -264,15 +262,13 @@ where
264262
})?;
265263

266264
if let Ok(intercept_scid) = result.jit_channel_scid.to_scid() {
267-
self.pending_events.enqueue(Event::LSPS2Client(
268-
LSPS2ClientEvent::InvoiceParametersReady {
269-
request_id,
270-
counterparty_node_id: *counterparty_node_id,
271-
intercept_scid,
272-
cltv_expiry_delta: result.lsp_cltv_expiry_delta,
273-
payment_size_msat: jit_channel.payment_size_msat,
274-
},
275-
));
265+
self.pending_events.enqueue(LSPS2ClientEvent::InvoiceParametersReady {
266+
request_id,
267+
counterparty_node_id: *counterparty_node_id,
268+
intercept_scid,
269+
cltv_expiry_delta: result.lsp_cltv_expiry_delta,
270+
payment_size_msat: jit_channel.payment_size_msat,
271+
});
276272
} else {
277273
return Err(LightningError {
278274
err: format!(

0 commit comments

Comments
 (0)