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

Commit 3dfd51c

Browse files
committed
Add missing docs for lsps1/msgs.rs, make submodules pub
In parallel to LSPS2, we expose the internal `msgs`/`message_handler`/`event` modules of LSPS1. To this end, we add docs to all message types, mark docs in `event.rs` as TODO, and hide the public methods of LSPS1's message handler, as we'll want to expose-and-document them individually at a later date.
1 parent 4bbe70a commit 3dfd51c

File tree

7 files changed

+222
-69
lines changed

7 files changed

+222
-69
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ mod prelude {
4141
pub mod events;
4242
pub mod lsps0;
4343
#[cfg(lsps1)]
44-
mod lsps1;
44+
pub mod lsps1;
4545
pub mod lsps2;
4646
mod manager;
4747
mod sync;

src/lsps1/event.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Contains LSPS1 event types
22
3-
use super::msgs::{ChannelInfo, OptionsSupported, Order, OrderId, Payment};
3+
use super::msgs::{ChannelInfo, OptionsSupported, OrderId, OrderParams, OrderPayment};
44

55
use crate::lsps0::msgs::RequestId;
66
use crate::prelude::String;
@@ -10,52 +10,81 @@ use bitcoin::secp256k1::PublicKey;
1010
/// An event which you should probably take some action in response to.
1111
#[derive(Clone, Debug, PartialEq, Eq)]
1212
pub enum Event {
13+
/// TODO
1314
GetInfoResponse {
15+
/// TODO
1416
id: u128,
17+
/// TODO
1518
request_id: RequestId,
19+
/// TODO
1620
counterparty_node_id: PublicKey,
21+
/// TODO
1722
version: u16,
23+
/// TODO
1824
website: String,
25+
/// TODO
1926
options_supported: OptionsSupported,
2027
},
21-
28+
/// TODO
2229
CreateInvoice {
30+
/// TODO
2331
request_id: RequestId,
32+
/// TODO
2433
counterparty_node_id: PublicKey,
25-
order: Order,
34+
/// TODO
35+
order: OrderParams,
2636
},
27-
37+
/// TODO
2838
DisplayOrder {
39+
/// TODO
2940
id: u128,
41+
/// TODO
3042
counterparty_node_id: PublicKey,
31-
order: Order,
32-
payment: Payment,
43+
/// TODO
44+
order: OrderParams,
45+
/// TODO
46+
payment: OrderPayment,
47+
/// TODO
3348
channel: Option<ChannelInfo>,
3449
},
35-
50+
/// TODO
3651
PayforChannel {
52+
/// TODO
3753
request_id: RequestId,
54+
/// TODO
3855
counterparty_node_id: PublicKey,
39-
order: Order,
40-
payment: Payment,
56+
/// TODO
57+
order: OrderParams,
58+
/// TODO
59+
payment: OrderPayment,
60+
/// TODO
4161
channel: Option<ChannelInfo>,
4262
},
43-
63+
/// TODO
4464
CheckPaymentConfirmation {
65+
/// TODO
4566
request_id: RequestId,
67+
/// TODO
4668
counterparty_node_id: PublicKey,
69+
/// TODO
4770
order_id: OrderId,
4871
},
49-
72+
/// TODO
5073
OpenChannel {
74+
/// TODO
5175
request_id: RequestId,
76+
/// TODO
5277
counterparty_node_id: PublicKey,
78+
/// TODO
5379
order_id: OrderId,
5480
},
55-
81+
/// TODO
5682
Refund {
83+
/// TODO
5784
request_id: RequestId,
85+
/// TODO
5886
counterparty_node_id: PublicKey,
87+
/// TODO
5988
order_id: OrderId,
6089
},
6190
}

src/lsps1/message_handler.rs

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
use super::msgs::{
1313
ChannelInfo, CreateOrderRequest, CreateOrderResponse, GetInfoRequest, GetInfoResponse,
1414
GetOrderRequest, GetOrderResponse, LSPS1Message, LSPS1Request, LSPS1Response, OptionsSupported,
15-
Order, OrderId, OrderState, Payment, LSPS1_CREATE_ORDER_REQUEST_INVALID_VERSION_ERROR_CODE,
15+
OrderId, OrderParams, OrderPayment, OrderState,
16+
LSPS1_CREATE_ORDER_REQUEST_INVALID_VERSION_ERROR_CODE,
1617
LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE,
1718
};
1819
use super::utils::is_valid;
@@ -40,10 +41,15 @@ use core::ops::Deref;
4041

4142
const SUPPORTED_SPEC_VERSIONS: [u16; 1] = [1];
4243

44+
/// Configuration options for LSPS1 channel requests.
4345
pub struct LSPS1Config {
46+
/// A token to be send with each channel request.
4447
pub token: Option<String>,
45-
pub max_fees: Option<u64>,
48+
/// The maximally allowed channel fees.
49+
pub max_channel_fees_msat: Option<u64>,
50+
/// The options supported by the LSP.
4651
pub options_supported: Option<OptionsSupported>,
52+
/// The LSP's website.
4753
pub website: Option<String>,
4854
}
4955

@@ -59,7 +65,7 @@ impl From<ChannelStateError> for LightningError {
5965
enum InboundRequestState {
6066
InfoRequested,
6167
OptionsSupport { version: u16, options_supported: OptionsSupported },
62-
OrderRequested { version: u16, order: Order },
68+
OrderRequested { version: u16, order: OrderParams },
6369
PendingPayment { order_id: OrderId },
6470
AwaitingConfirmation { id: u128, order_id: OrderId },
6571
}
@@ -90,7 +96,7 @@ impl InboundRequestState {
9096
}
9197
}
9298

93-
pub fn order_requested(&self, order: Order) -> Result<Self, ChannelStateError> {
99+
fn order_requested(&self, order: OrderParams) -> Result<Self, ChannelStateError> {
94100
match self {
95101
InboundRequestState::OptionsSupport { version, options_supported } => {
96102
if is_valid(&order, options_supported) {
@@ -109,8 +115,8 @@ impl InboundRequestState {
109115
}
110116
}
111117

112-
pub fn order_received(
113-
&self, response_order: &Order, order_id: OrderId,
118+
fn order_received(
119+
&self, response_order: &OrderParams, order_id: OrderId,
114120
) -> Result<Self, ChannelStateError> {
115121
match self {
116122
InboundRequestState::OrderRequested { version, order } => {
@@ -130,7 +136,7 @@ impl InboundRequestState {
130136
}
131137
}
132138

133-
pub fn pay_for_channel(&self, channel_id: u128) -> Result<Self, ChannelStateError> {
139+
fn pay_for_channel(&self, channel_id: u128) -> Result<Self, ChannelStateError> {
134140
match self {
135141
InboundRequestState::PendingPayment { order_id } => {
136142
Ok(InboundRequestState::AwaitingConfirmation {
@@ -152,11 +158,11 @@ struct InboundCRChannel {
152158
}
153159

154160
impl InboundCRChannel {
155-
pub fn new(id: u128) -> Self {
161+
fn new(id: u128) -> Self {
156162
Self { id, state: InboundRequestState::InfoRequested }
157163
}
158164

159-
pub fn info_received(
165+
fn info_received(
160166
&mut self, versions: Vec<u16>, options: OptionsSupported,
161167
) -> Result<u16, LightningError> {
162168
self.state = self.state.info_received(versions, options)?;
@@ -170,7 +176,7 @@ impl InboundCRChannel {
170176
}
171177
}
172178

173-
pub fn order_requested(&mut self, order: Order) -> Result<u16, LightningError> {
179+
fn order_requested(&mut self, order: OrderParams) -> Result<u16, LightningError> {
174180
self.state = self.state.order_requested(order)?;
175181

176182
match self.state {
@@ -184,14 +190,14 @@ impl InboundCRChannel {
184190
}
185191
}
186192

187-
pub fn order_received(
188-
&mut self, order: &Order, order_id: OrderId,
193+
fn order_received(
194+
&mut self, order: &OrderParams, order_id: OrderId,
189195
) -> Result<(), LightningError> {
190196
self.state = self.state.order_received(order, order_id)?;
191197
Ok(())
192198
}
193199

194-
pub fn pay_for_channel(&mut self, channel_id: u128) -> Result<(), LightningError> {
200+
fn pay_for_channel(&mut self, channel_id: u128) -> Result<(), LightningError> {
195201
self.state = self.state.pay_for_channel(channel_id)?;
196202
Ok(())
197203
}
@@ -205,7 +211,7 @@ enum OutboundRequestState {
205211
}
206212

207213
impl OutboundRequestState {
208-
pub fn create_payment_invoice(&self) -> Result<Self, ChannelStateError> {
214+
fn create_payment_invoice(&self) -> Result<Self, ChannelStateError> {
209215
match self {
210216
OutboundRequestState::OrderCreated { order_id } => {
211217
Ok(OutboundRequestState::WaitingPayment { order_id: order_id.clone() })
@@ -219,10 +225,10 @@ impl OutboundRequestState {
219225
}
220226

221227
struct OutboundLSPS1Config {
222-
order: Order,
228+
order: OrderParams,
223229
created_at: chrono::DateTime<Utc>,
224230
expires_at: chrono::DateTime<Utc>,
225-
payment: Payment,
231+
payment: OrderPayment,
226232
}
227233

228234
struct OutboundCRChannel {
@@ -231,21 +237,21 @@ struct OutboundCRChannel {
231237
}
232238

233239
impl OutboundCRChannel {
234-
pub fn new(
235-
order: Order, created_at: chrono::DateTime<Utc>, expires_at: chrono::DateTime<Utc>,
236-
order_id: OrderId, payment: Payment,
240+
fn new(
241+
order: OrderParams, created_at: chrono::DateTime<Utc>, expires_at: chrono::DateTime<Utc>,
242+
order_id: OrderId, payment: OrderPayment,
237243
) -> Self {
238244
Self {
239245
state: OutboundRequestState::OrderCreated { order_id },
240246
config: OutboundLSPS1Config { order, created_at, expires_at, payment },
241247
}
242248
}
243-
pub fn create_payment_invoice(&mut self) -> Result<(), LightningError> {
249+
fn create_payment_invoice(&mut self) -> Result<(), LightningError> {
244250
self.state = self.state.create_payment_invoice()?;
245251
Ok(())
246252
}
247253

248-
pub fn check_order_validity(&self, options_supported: &OptionsSupported) -> bool {
254+
fn check_order_validity(&self, options_supported: &OptionsSupported) -> bool {
249255
let order = &self.config.order;
250256

251257
is_valid(order, options_supported)
@@ -261,27 +267,28 @@ struct PeerState {
261267
}
262268

263269
impl PeerState {
264-
pub fn insert_inbound_channel(&mut self, id: u128, channel: InboundCRChannel) {
270+
fn insert_inbound_channel(&mut self, id: u128, channel: InboundCRChannel) {
265271
self.inbound_channels_by_id.insert(id, channel);
266272
}
267273

268-
pub fn insert_outbound_channel(&mut self, order_id: OrderId, channel: OutboundCRChannel) {
274+
fn insert_outbound_channel(&mut self, order_id: OrderId, channel: OutboundCRChannel) {
269275
self.outbound_channels_by_order_id.insert(order_id, channel);
270276
}
271277

272-
pub fn insert_request(&mut self, request_id: RequestId, channel_id: u128) {
278+
fn insert_request(&mut self, request_id: RequestId, channel_id: u128) {
273279
self.request_to_cid.insert(request_id, channel_id);
274280
}
275281

276-
pub fn remove_inbound_channel(&mut self, id: u128) {
282+
fn remove_inbound_channel(&mut self, id: u128) {
277283
self.inbound_channels_by_id.remove(&id);
278284
}
279285

280-
pub fn remove_outbound_channel(&mut self, order_id: OrderId) {
286+
fn remove_outbound_channel(&mut self, order_id: OrderId) {
281287
self.outbound_channels_by_order_id.remove(&order_id);
282288
}
283289
}
284290

291+
/// The main object allowing to send and receive LSPS1 messages.
285292
pub struct LSPS1MessageHandler<ES: Deref, CM: Deref + Clone, PM: Deref + Clone, C: Deref>
286293
where
287294
ES::Target: EntropySource,
@@ -298,7 +305,7 @@ where
298305
per_peer_state: RwLock<HashMap<PublicKey, Mutex<PeerState>>>,
299306
options_config: Option<OptionsSupported>,
300307
website: Option<String>,
301-
max_fees: Option<u64>,
308+
max_channel_fees_msat: Option<u64>,
302309
}
303310

304311
impl<ES: Deref, CM: Deref + Clone, PM: Deref + Clone, C: Deref> LSPS1MessageHandler<ES, CM, PM, C>
@@ -324,15 +331,25 @@ where
324331
per_peer_state: RwLock::new(HashMap::new()),
325332
options_config: config.options_supported.clone(),
326333
website: config.website.clone(),
327-
max_fees: config.max_fees,
334+
max_channel_fees_msat: config.max_channel_fees_msat,
328335
}
329336
}
330337

338+
/// Set a [`PeerManager`] reference for the message handler.
339+
///
340+
/// This allows the message handler to wake the [`PeerManager`] by calling
341+
/// [`PeerManager::process_events`] after enqueing messages to be sent.
342+
///
343+
/// Without this the messages will be sent based on whatever polling interval
344+
/// your background processor uses.
345+
///
346+
/// [`PeerManager`]: lightning::ln::peer_handler::PeerManager
347+
/// [`PeerManager::process_events`]: lightning::ln::peer_handler::PeerManager::process_events
331348
pub fn set_peer_manager(&self, peer_manager: PM) {
332349
*self.peer_manager.lock().unwrap() = Some(peer_manager);
333350
}
334351

335-
pub fn request_for_info(&self, counterparty_node_id: PublicKey, channel_id: u128) {
352+
fn request_for_info(&self, counterparty_node_id: PublicKey, channel_id: u128) {
336353
let channel = InboundCRChannel::new(channel_id);
337354

338355
let mut outer_state_lock = self.per_peer_state.write().unwrap();
@@ -439,8 +456,8 @@ where
439456
Ok(())
440457
}
441458

442-
pub fn place_order(
443-
&self, channel_id: u128, counterparty_node_id: &PublicKey, order: Order,
459+
fn place_order(
460+
&self, channel_id: u128, counterparty_node_id: &PublicKey, order: OrderParams,
444461
) -> Result<(), APIError> {
445462
let outer_state_lock = self.per_peer_state.write().unwrap();
446463

@@ -548,8 +565,8 @@ where
548565
Ok(())
549566
}
550567

551-
pub fn send_invoice_for_order(
552-
&self, request_id: RequestId, counterparty_node_id: &PublicKey, payment: Payment,
568+
fn send_invoice_for_order(
569+
&self, request_id: RequestId, counterparty_node_id: &PublicKey, payment: OrderPayment,
553570
created_at: chrono::DateTime<Utc>, expires_at: chrono::DateTime<Utc>,
554571
) -> Result<(), APIError> {
555572
let outer_state_lock = self.per_peer_state.read().unwrap();
@@ -643,9 +660,11 @@ where
643660
}
644661

645662
let total_fees = response.payment.fee_total_sat + response.order.client_balance_sat;
646-
let max_fees = self.max_fees.unwrap_or(u64::MAX);
663+
let max_channel_fees_msat = self.max_channel_fees_msat.unwrap_or(u64::MAX);
647664

648-
if total_fees == response.payment.order_total_sat && total_fees < max_fees {
665+
if total_fees == response.payment.order_total_sat
666+
&& total_fees < max_channel_fees_msat
667+
{
649668
self.enqueue_event(Event::LSPS1(super::event::Event::DisplayOrder {
650669
id: channel_id,
651670
counterparty_node_id: *counterparty_node_id,
@@ -710,7 +729,7 @@ where
710729
}
711730
}
712731

713-
pub fn check_order_status(
732+
fn check_order_status(
714733
&self, counterparty_node_id: &PublicKey, order_id: OrderId, channel_id: u128,
715734
) -> Result<(), APIError> {
716735
let outer_state_lock = self.per_peer_state.write().unwrap();
@@ -811,7 +830,7 @@ where
811830
Ok(())
812831
}
813832

814-
pub fn update_order_status(
833+
fn update_order_status(
815834
&self, request_id: RequestId, counterparty_node_id: PublicKey, order_id: OrderId,
816835
order_state: OrderState, channel: Option<ChannelInfo>,
817836
) -> Result<(), APIError> {

0 commit comments

Comments
 (0)