Skip to content

Commit ef44501

Browse files
committed
Move LSPS2 service logic into liquidity/service/lsps2.rs
1 parent 9e6ae51 commit ef44501

3 files changed

Lines changed: 244 additions & 211 deletions

File tree

src/liquidity/mod.rs

Lines changed: 5 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@
88
//! Objects related to liquidity management.
99
1010
pub(crate) mod client;
11+
pub(crate) mod service;
1112

1213
use std::collections::HashMap;
1314
use std::ops::Deref;
1415
use std::sync::{Arc, Mutex, RwLock, Weak};
15-
use std::time::Duration;
1616

1717
use bitcoin::secp256k1::PublicKey;
18-
use bitcoin::Transaction;
1918
use chrono::Utc;
20-
use lightning::events::HTLCHandlingFailureType;
21-
use lightning::ln::channelmanager::InterceptId;
2219
use lightning::ln::msgs::SocketAddress;
23-
use lightning::ln::types::ChannelId;
2420
use lightning::sign::EntropySource;
2521
use lightning_liquidity::events::LiquidityEvent;
2622
use lightning_liquidity::lsps0::ser::LSPSDateTime;
@@ -30,7 +26,6 @@ use lightning_liquidity::lsps2::event::LSPS2ServiceEvent;
3026
use lightning_liquidity::lsps2::msgs::LSPS2RawOpeningFeeParams;
3127
use lightning_liquidity::lsps2::service::LSPS2ServiceConfig as LdkLSPS2ServiceConfig;
3228
use lightning_liquidity::{LiquidityClientConfig, LiquidityServiceConfig};
33-
use lightning_types::payment::PaymentHash;
3429

3530
use crate::builder::BuildError;
3631
use crate::logger::{log_error, LdkLogger};
@@ -42,77 +37,13 @@ use crate::{total_anchor_channels_reserve_sats, Config};
4237
pub(crate) use client::lsps1::{LSPS1Client, LSPS1ClientConfig};
4338
pub use client::lsps1::{LSPS1Liquidity, LSPS1OrderStatus};
4439
pub(crate) use client::lsps2::{LSPS2Client, LSPS2ClientConfig};
40+
pub use service::lsps2::LSPS2ServiceConfig;
41+
pub(crate) use service::lsps2::{
42+
LSPS2Service, LSPS2_CHANNEL_CLTV_EXPIRY_DELTA, LSPS2_GETINFO_REQUEST_EXPIRY,
43+
};
4544

4645
pub(crate) const LIQUIDITY_REQUEST_TIMEOUT_SECS: u64 = 5;
4746

48-
const LSPS2_GETINFO_REQUEST_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
49-
const LSPS2_CHANNEL_CLTV_EXPIRY_DELTA: u32 = 72;
50-
51-
struct LSPS2Service {
52-
service_config: LSPS2ServiceConfig,
53-
ldk_service_config: LdkLSPS2ServiceConfig,
54-
}
55-
56-
/// Represents the configuration of the LSPS2 service.
57-
///
58-
/// See [bLIP-52 / LSPS2] for more information.
59-
///
60-
/// [bLIP-52 / LSPS2]: https://github.com/lightning/blips/blob/master/blip-0052.md
61-
#[derive(Debug, Clone)]
62-
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
63-
pub struct LSPS2ServiceConfig {
64-
/// A token we may require to be sent by the clients.
65-
///
66-
/// If set, only requests matching this token will be accepted.
67-
pub require_token: Option<String>,
68-
/// Indicates whether the LSPS service will be announced via the gossip network.
69-
pub advertise_service: bool,
70-
/// The fee we withhold for the channel open from the initial payment.
71-
///
72-
/// This fee is proportional to the client-requested amount, in parts-per-million.
73-
pub channel_opening_fee_ppm: u32,
74-
/// The proportional overprovisioning for the channel.
75-
///
76-
/// This determines, in parts-per-million, how much value we'll provision on top of the amount
77-
/// we need to forward the payment to the client.
78-
///
79-
/// For example, setting this to `100_000` will result in a channel being opened that is 10%
80-
/// larger than then the to-be-forwarded amount (i.e., client-requested amount minus the
81-
/// channel opening fee fee).
82-
pub channel_over_provisioning_ppm: u32,
83-
/// The minimum fee required for opening a channel.
84-
pub min_channel_opening_fee_msat: u64,
85-
/// The minimum number of blocks after confirmation we promise to keep the channel open.
86-
pub min_channel_lifetime: u32,
87-
/// The maximum number of blocks that the client is allowed to set its `to_self_delay` parameter.
88-
pub max_client_to_self_delay: u32,
89-
/// The minimum payment size that we will accept when opening a channel.
90-
pub min_payment_size_msat: u64,
91-
/// The maximum payment size that we will accept when opening a channel.
92-
pub max_payment_size_msat: u64,
93-
/// Use the 'client-trusts-LSP' trust model.
94-
///
95-
/// When set, the service will delay *broadcasting* the JIT channel's funding transaction until
96-
/// the client claimed sufficient HTLC parts to pay for the channel open.
97-
///
98-
/// Note this will render the flow incompatible with clients utilizing the 'LSP-trust-client'
99-
/// trust model, i.e., in turn delay *claiming* any HTLCs until they see the funding
100-
/// transaction in the mempool.
101-
///
102-
/// Please refer to [`bLIP-52`] for more information.
103-
///
104-
/// [`bLIP-52`]: https://github.com/lightning/blips/blob/master/blip-0052.md#trust-models
105-
pub client_trusts_lsp: bool,
106-
/// When set, we will allow clients to spend their entire channel balance in the channels
107-
/// we open to them. This allows clients to try to steal your channel balance with
108-
/// no financial penalty, so this should only be set if you trust your clients.
109-
///
110-
/// See [`Node::open_0reserve_channel`] to manually open these channels.
111-
///
112-
/// [`Node::open_0reserve_channel`]: crate::Node::open_0reserve_channel
113-
pub disable_client_reserve: bool,
114-
}
115-
11647
pub(crate) struct LiquiditySourceBuilder<L: Deref>
11748
where
11849
L::Target: LdkLogger,
@@ -278,73 +209,6 @@ where
278209
Arc::clone(&self.liquidity_manager)
279210
}
280211

281-
pub(crate) fn lsps2_channel_needs_manual_broadcast(
282-
&self, counterparty_node_id: PublicKey, user_channel_id: u128,
283-
) -> bool {
284-
self.lsps2_service.as_ref().map_or(false, |lsps2_service| {
285-
lsps2_service.service_config.client_trusts_lsp
286-
&& self
287-
.liquidity_manager()
288-
.lsps2_service_handler()
289-
.and_then(|handler| {
290-
handler
291-
.channel_needs_manual_broadcast(user_channel_id, &counterparty_node_id)
292-
.ok()
293-
})
294-
.unwrap_or(false)
295-
})
296-
}
297-
298-
pub(crate) fn lsps2_store_funding_transaction(
299-
&self, user_channel_id: u128, counterparty_node_id: PublicKey, funding_tx: Transaction,
300-
) {
301-
if self.lsps2_service.as_ref().map_or(false, |svc| !svc.service_config.client_trusts_lsp) {
302-
// Only necessary for client-trusts-LSP flow
303-
return;
304-
}
305-
306-
let lsps2_service_handler = self.liquidity_manager.lsps2_service_handler();
307-
if let Some(handler) = lsps2_service_handler {
308-
handler
309-
.store_funding_transaction(user_channel_id, &counterparty_node_id, funding_tx)
310-
.unwrap_or_else(|e| {
311-
debug_assert!(false, "Failed to store funding transaction: {:?}", e);
312-
log_error!(self.logger, "Failed to store funding transaction: {:?}", e);
313-
});
314-
} else {
315-
log_error!(self.logger, "LSPS2 service handler is not available.");
316-
}
317-
}
318-
319-
pub(crate) fn lsps2_funding_tx_broadcast_safe(
320-
&self, user_channel_id: u128, counterparty_node_id: PublicKey,
321-
) {
322-
if self.lsps2_service.as_ref().map_or(false, |svc| !svc.service_config.client_trusts_lsp) {
323-
// Only necessary for client-trusts-LSP flow
324-
return;
325-
}
326-
327-
let lsps2_service_handler = self.liquidity_manager.lsps2_service_handler();
328-
if let Some(handler) = lsps2_service_handler {
329-
handler
330-
.set_funding_tx_broadcast_safe(user_channel_id, &counterparty_node_id)
331-
.unwrap_or_else(|e| {
332-
debug_assert!(
333-
false,
334-
"Failed to mark funding transaction safe to broadcast: {:?}",
335-
e
336-
);
337-
log_error!(
338-
self.logger,
339-
"Failed to mark funding transaction safe to broadcast: {:?}",
340-
e
341-
);
342-
});
343-
} else {
344-
log_error!(self.logger, "LSPS2 service handler is not available.");
345-
}
346-
}
347-
348212
pub(crate) async fn handle_next_event(&self) {
349213
match self.liquidity_manager.next_event_async().await {
350214
LiquidityEvent::LSPS1Client(event) => {
@@ -641,74 +505,4 @@ where
641505
},
642506
}
643507
}
644-
645-
pub(crate) async fn handle_channel_ready(
646-
&self, user_channel_id: u128, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
647-
) {
648-
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
649-
if let Err(e) = lsps2_service_handler
650-
.channel_ready(user_channel_id, channel_id, counterparty_node_id)
651-
.await
652-
{
653-
log_error!(
654-
self.logger,
655-
"LSPS2 service failed to handle ChannelReady event: {:?}",
656-
e
657-
);
658-
}
659-
}
660-
}
661-
662-
pub(crate) async fn handle_htlc_intercepted(
663-
&self, intercept_scid: u64, intercept_id: InterceptId, expected_outbound_amount_msat: u64,
664-
payment_hash: PaymentHash,
665-
) {
666-
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
667-
if let Err(e) = lsps2_service_handler
668-
.htlc_intercepted(
669-
intercept_scid,
670-
intercept_id,
671-
expected_outbound_amount_msat,
672-
payment_hash,
673-
)
674-
.await
675-
{
676-
log_error!(
677-
self.logger,
678-
"LSPS2 service failed to handle HTLCIntercepted event: {:?}",
679-
e
680-
);
681-
}
682-
}
683-
}
684-
685-
pub(crate) async fn handle_htlc_handling_failed(&self, failure_type: HTLCHandlingFailureType) {
686-
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
687-
if let Err(e) = lsps2_service_handler.htlc_handling_failed(failure_type).await {
688-
log_error!(
689-
self.logger,
690-
"LSPS2 service failed to handle HTLCHandlingFailed event: {:?}",
691-
e
692-
);
693-
}
694-
}
695-
}
696-
697-
pub(crate) async fn handle_payment_forwarded(
698-
&self, next_channel_id: Option<ChannelId>, skimmed_fee_msat: u64,
699-
) {
700-
if let Some(next_channel_id) = next_channel_id {
701-
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
702-
if let Err(e) =
703-
lsps2_service_handler.payment_forwarded(next_channel_id, skimmed_fee_msat).await
704-
{
705-
log_error!(
706-
self.logger,
707-
"LSPS2 service failed to handle PaymentForwarded: {:?}",
708-
e
709-
);
710-
}
711-
}
712-
}
713-
}
714508
}

0 commit comments

Comments
 (0)