Skip to content

Commit 2c3f11d

Browse files
authored
Merge pull request #3583 from tnull/2025-01-rename-all-liquidity-types
Rename most `lightning-liquidity` types for bindings compatibility
2 parents 79e1643 + 690fcb1 commit 2c3f11d

18 files changed

+555
-460
lines changed

ci/ci-tests.sh

+2
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,5 @@ RUSTFLAGS="--cfg=taproot" cargo test --verbose --color always -p lightning
132132
RUSTFLAGS="--cfg=splicing" cargo test --verbose --color always -p lightning
133133
[ "$CI_MINIMIZE_DISK_USAGE" != "" ] && cargo clean
134134
RUSTFLAGS="--cfg=async_payments" cargo test --verbose --color always -p lightning
135+
[ "$CI_MINIMIZE_DISK_USAGE" != "" ] && cargo clean
136+
RUSTFLAGS="--cfg=lsps1_service" cargo test --verbose --color always -p lightning-liquidity

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

+23-24
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
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::{
10-
LSPS0Message, LSPS0Request, LSPS0Response, ListProtocolsRequest, ListProtocolsResponse,
10+
LSPS0ListProtocolsRequest, LSPS0ListProtocolsResponse, LSPS0Message, LSPS0Request,
11+
LSPS0Response,
1112
};
12-
use crate::lsps0::ser::{ProtocolMessageHandler, ResponseError};
13+
use crate::lsps0::ser::{LSPSProtocolMessageHandler, LSPSResponseError};
1314
use crate::message_queue::MessageQueue;
1415
use crate::sync::Arc;
1516
use crate::utils;
@@ -51,7 +52,7 @@ where
5152
pub fn list_protocols(&self, counterparty_node_id: &PublicKey) {
5253
let msg = LSPS0Message::Request(
5354
utils::generate_request_id(&self.entropy_source),
54-
LSPS0Request::ListProtocols(ListProtocolsRequest {}),
55+
LSPS0Request::ListProtocols(LSPS0ListProtocolsRequest {}),
5556
);
5657

5758
self.pending_messages.enqueue(counterparty_node_id, msg.into());
@@ -61,29 +62,27 @@ where
6162
&self, response: LSPS0Response, counterparty_node_id: &PublicKey,
6263
) -> Result<(), LightningError> {
6364
match response {
64-
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+
LSPS0Response::ListProtocols(LSPS0ListProtocolsResponse { protocols }) => {
66+
self.pending_events.enqueue(LSPS0ClientEvent::ListProtocolsResponse {
67+
counterparty_node_id: *counterparty_node_id,
68+
protocols,
69+
});
7170
Ok(())
7271
},
73-
LSPS0Response::ListProtocolsError(ResponseError { code, message, data, .. }) => {
74-
Err(LightningError {
75-
err: format!(
76-
"ListProtocols error received. code = {}, message = {}, data = {:?}",
77-
code, message, data
78-
),
79-
action: ErrorAction::IgnoreAndLog(Level::Info),
80-
})
81-
},
72+
LSPS0Response::ListProtocolsError(LSPSResponseError {
73+
code, message, data, ..
74+
}) => Err(LightningError {
75+
err: format!(
76+
"ListProtocols error received. code = {}, message = {}, data = {:?}",
77+
code, message, data
78+
),
79+
action: ErrorAction::IgnoreAndLog(Level::Info),
80+
}),
8281
}
8382
}
8483
}
8584

86-
impl<ES: Deref> ProtocolMessageHandler for LSPS0ClientHandler<ES>
85+
impl<ES: Deref> LSPSProtocolMessageHandler for LSPS0ClientHandler<ES>
8786
where
8887
ES::Target: EntropySource,
8988
{
@@ -114,7 +113,7 @@ mod tests {
114113
use alloc::string::ToString;
115114
use alloc::sync::Arc;
116115

117-
use crate::lsps0::ser::{LSPSMessage, RequestId};
116+
use crate::lsps0::ser::{LSPSMessage, LSPSRequestId};
118117
use crate::tests::utils::{self, TestEntropy};
119118

120119
use super::*;
@@ -147,8 +146,8 @@ mod tests {
147146
assert_eq!(
148147
*message,
149148
LSPSMessage::LSPS0(LSPS0Message::Request(
150-
RequestId("00000000000000000000000000000000".to_string()),
151-
LSPS0Request::ListProtocols(ListProtocolsRequest {})
149+
LSPSRequestId("00000000000000000000000000000000".to_string()),
150+
LSPS0Request::ListProtocols(LSPS0ListProtocolsRequest {})
152151
))
153152
);
154153
}

lightning-liquidity/src/lsps0/msgs.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Message, request, and other primitive types used to implement LSPS0.
22
3-
use crate::lsps0::ser::{LSPSMessage, RequestId, ResponseError};
3+
use crate::lsps0::ser::{LSPSMessage, LSPSRequestId, LSPSResponseError};
44
use crate::prelude::Vec;
55

66
use serde::{Deserialize, Serialize};
@@ -15,15 +15,15 @@ pub(crate) const LSPS0_LISTPROTOCOLS_METHOD_NAME: &str = "lsps0.list_protocols";
1515
/// specification](https://github.com/lightning/blips/blob/master/blip-0050.md#lsps-specification-support-query)
1616
/// for more information.
1717
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
18-
pub struct ListProtocolsRequest {}
18+
pub struct LSPS0ListProtocolsRequest {}
1919

2020
/// A response to a `list_protocols` request.
2121
///
2222
/// Please refer to the [bLIP-50 / LSPS0
2323
/// specification](https://github.com/lightning/blips/blob/master/blip-0050.md#lsps-specification-support-query)
2424
/// for more information.
2525
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
26-
pub struct ListProtocolsResponse {
26+
pub struct LSPS0ListProtocolsResponse {
2727
/// A list of supported protocols.
2828
pub protocols: Vec<u16>,
2929
}
@@ -36,12 +36,12 @@ pub struct ListProtocolsResponse {
3636
#[derive(Clone, Debug, PartialEq, Eq)]
3737
pub enum LSPS0Request {
3838
/// A request calling `list_protocols`.
39-
ListProtocols(ListProtocolsRequest),
39+
ListProtocols(LSPS0ListProtocolsRequest),
4040
}
4141

4242
impl LSPS0Request {
4343
/// Returns the method name associated with the given request variant.
44-
pub fn method(&self) -> &str {
44+
pub fn method(&self) -> &'static str {
4545
match self {
4646
LSPS0Request::ListProtocols(_) => LSPS0_LISTPROTOCOLS_METHOD_NAME,
4747
}
@@ -56,9 +56,9 @@ impl LSPS0Request {
5656
#[derive(Clone, Debug, PartialEq, Eq)]
5757
pub enum LSPS0Response {
5858
/// A response to a `list_protocols` request.
59-
ListProtocols(ListProtocolsResponse),
59+
ListProtocols(LSPS0ListProtocolsResponse),
6060
/// An error response to a `list_protocols` request.
61-
ListProtocolsError(ResponseError),
61+
ListProtocolsError(LSPSResponseError),
6262
}
6363

6464
/// An bLIP-50 / LSPS0 protocol message.
@@ -69,9 +69,9 @@ pub enum LSPS0Response {
6969
#[derive(Clone, Debug, PartialEq, Eq)]
7070
pub enum LSPS0Message {
7171
/// A request variant.
72-
Request(RequestId, LSPS0Request),
72+
Request(LSPSRequestId, LSPS0Request),
7373
/// A response variant.
74-
Response(RequestId, LSPS0Response),
74+
Response(LSPSRequestId, LSPS0Response),
7575
}
7676

7777
impl TryFrom<LSPSMessage> for LSPS0Message {
@@ -117,17 +117,17 @@ mod tests {
117117
assert_eq!(
118118
msg,
119119
LSPSMessage::LSPS0(LSPS0Message::Request(
120-
RequestId("request:id:xyz123".to_string()),
121-
LSPS0Request::ListProtocols(ListProtocolsRequest {})
120+
LSPSRequestId("request:id:xyz123".to_string()),
121+
LSPS0Request::ListProtocols(LSPS0ListProtocolsRequest {})
122122
))
123123
);
124124
}
125125

126126
#[test]
127127
fn serializes_request() {
128128
let request = LSPSMessage::LSPS0(LSPS0Message::Request(
129-
RequestId("request:id:xyz123".to_string()),
130-
LSPS0Request::ListProtocols(ListProtocolsRequest {}),
129+
LSPSRequestId("request:id:xyz123".to_string()),
130+
LSPS0Request::ListProtocols(LSPS0ListProtocolsRequest {}),
131131
));
132132
let json = serde_json::to_string(&request).unwrap();
133133
assert_eq!(
@@ -147,16 +147,18 @@ mod tests {
147147
}"#;
148148
let mut request_id_to_method_map = new_hash_map();
149149
request_id_to_method_map
150-
.insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
150+
.insert(LSPSRequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
151151

152152
let response =
153153
LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map).unwrap();
154154

155155
assert_eq!(
156156
response,
157157
LSPSMessage::LSPS0(LSPS0Message::Response(
158-
RequestId("request:id:xyz123".to_string()),
159-
LSPS0Response::ListProtocols(ListProtocolsResponse { protocols: vec![1, 2, 3] })
158+
LSPSRequestId("request:id:xyz123".to_string()),
159+
LSPS0Response::ListProtocols(LSPS0ListProtocolsResponse {
160+
protocols: vec![1, 2, 3]
161+
})
160162
))
161163
);
162164
}
@@ -173,16 +175,16 @@ mod tests {
173175
}"#;
174176
let mut request_id_to_method_map = new_hash_map();
175177
request_id_to_method_map
176-
.insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
178+
.insert(LSPSRequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
177179

178180
let response =
179181
LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map).unwrap();
180182

181183
assert_eq!(
182184
response,
183185
LSPSMessage::LSPS0(LSPS0Message::Response(
184-
RequestId("request:id:xyz123".to_string()),
185-
LSPS0Response::ListProtocolsError(ResponseError {
186+
LSPSRequestId("request:id:xyz123".to_string()),
187+
LSPS0Response::ListProtocolsError(LSPSResponseError {
186188
code: -32617,
187189
message: "Unknown Error".to_string(),
188190
data: None
@@ -202,7 +204,7 @@ mod tests {
202204
}"#;
203205
let mut request_id_to_method_map = new_hash_map();
204206
request_id_to_method_map
205-
.insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
207+
.insert(LSPSRequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
206208

207209
let response = LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map);
208210
assert!(response.is_err());
@@ -211,8 +213,8 @@ mod tests {
211213
#[test]
212214
fn serializes_response() {
213215
let response = LSPSMessage::LSPS0(LSPS0Message::Response(
214-
RequestId("request:id:xyz123".to_string()),
215-
LSPS0Response::ListProtocols(ListProtocolsResponse { protocols: vec![1, 2, 3] }),
216+
LSPSRequestId("request:id:xyz123".to_string()),
217+
LSPS0Response::ListProtocols(LSPS0ListProtocolsResponse { protocols: vec![1, 2, 3] }),
216218
));
217219
let json = serde_json::to_string(&response).unwrap();
218220
assert_eq!(

0 commit comments

Comments
 (0)