Skip to content

Commit 7bb1191

Browse files
committed
Move LSPS2 service logic into liquidity/service/lsps2.rs
1 parent 95c88e5 commit 7bb1191

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;
@@ -31,7 +27,6 @@ use lightning_liquidity::lsps2::event::{LSPS2ClientEvent, LSPS2ServiceEvent};
3127
use lightning_liquidity::lsps2::msgs::LSPS2RawOpeningFeeParams;
3228
use lightning_liquidity::lsps2::service::LSPS2ServiceConfig as LdkLSPS2ServiceConfig;
3329
use lightning_liquidity::{LiquidityClientConfig, LiquidityServiceConfig};
34-
use lightning_types::payment::PaymentHash;
3530

3631
use crate::builder::BuildError;
3732
use crate::logger::{log_error, LdkLogger};
@@ -45,77 +40,13 @@ pub use client::lsps1::{LSPS1Liquidity, LSPS1OrderStatus};
4540
pub(crate) use client::lsps2::{
4641
LSPS2BuyResponse, LSPS2Client, LSPS2ClientConfig, LSPS2FeeResponse,
4742
};
43+
pub use service::lsps2::LSPS2ServiceConfig;
44+
pub(crate) use service::lsps2::{
45+
LSPS2Service, LSPS2_CHANNEL_CLTV_EXPIRY_DELTA, LSPS2_GETINFO_REQUEST_EXPIRY,
46+
};
4847

4948
pub(crate) const LIQUIDITY_REQUEST_TIMEOUT_SECS: u64 = 5;
5049

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

284-
pub(crate) fn lsps2_channel_needs_manual_broadcast(
285-
&self, counterparty_node_id: PublicKey, user_channel_id: u128,
286-
) -> bool {
287-
self.lsps2_service.as_ref().map_or(false, |lsps2_service| {
288-
lsps2_service.service_config.client_trusts_lsp
289-
&& self
290-
.liquidity_manager()
291-
.lsps2_service_handler()
292-
.and_then(|handler| {
293-
handler
294-
.channel_needs_manual_broadcast(user_channel_id, &counterparty_node_id)
295-
.ok()
296-
})
297-
.unwrap_or(false)
298-
})
299-
}
300-
301-
pub(crate) fn lsps2_store_funding_transaction(
302-
&self, user_channel_id: u128, counterparty_node_id: PublicKey, funding_tx: Transaction,
303-
) {
304-
if self.lsps2_service.as_ref().map_or(false, |svc| !svc.service_config.client_trusts_lsp) {
305-
// Only necessary for client-trusts-LSP flow
306-
return;
307-
}
308-
309-
let lsps2_service_handler = self.liquidity_manager.lsps2_service_handler();
310-
if let Some(handler) = lsps2_service_handler {
311-
handler
312-
.store_funding_transaction(user_channel_id, &counterparty_node_id, funding_tx)
313-
.unwrap_or_else(|e| {
314-
debug_assert!(false, "Failed to store funding transaction: {:?}", e);
315-
log_error!(self.logger, "Failed to store funding transaction: {:?}", e);
316-
});
317-
} else {
318-
log_error!(self.logger, "LSPS2 service handler is not available.");
319-
}
320-
}
321-
322-
pub(crate) fn lsps2_funding_tx_broadcast_safe(
323-
&self, user_channel_id: u128, counterparty_node_id: PublicKey,
324-
) {
325-
if self.lsps2_service.as_ref().map_or(false, |svc| !svc.service_config.client_trusts_lsp) {
326-
// Only necessary for client-trusts-LSP flow
327-
return;
328-
}
329-
330-
let lsps2_service_handler = self.liquidity_manager.lsps2_service_handler();
331-
if let Some(handler) = lsps2_service_handler {
332-
handler
333-
.set_funding_tx_broadcast_safe(user_channel_id, &counterparty_node_id)
334-
.unwrap_or_else(|e| {
335-
debug_assert!(
336-
false,
337-
"Failed to mark funding transaction safe to broadcast: {:?}",
338-
e
339-
);
340-
log_error!(
341-
self.logger,
342-
"Failed to mark funding transaction safe to broadcast: {:?}",
343-
e
344-
);
345-
});
346-
} else {
347-
log_error!(self.logger, "LSPS2 service handler is not available.");
348-
}
349-
}
350-
351215
pub(crate) async fn handle_next_event(&self) {
352216
match self.liquidity_manager.next_event_async().await {
353217
LiquidityEvent::LSPS1Client(LSPS1ClientEvent::SupportedOptionsReady {
@@ -898,74 +762,4 @@ where
898762
},
899763
}
900764
}
901-
902-
pub(crate) async fn handle_channel_ready(
903-
&self, user_channel_id: u128, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
904-
) {
905-
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
906-
if let Err(e) = lsps2_service_handler
907-
.channel_ready(user_channel_id, channel_id, counterparty_node_id)
908-
.await
909-
{
910-
log_error!(
911-
self.logger,
912-
"LSPS2 service failed to handle ChannelReady event: {:?}",
913-
e
914-
);
915-
}
916-
}
917-
}
918-
919-
pub(crate) async fn handle_htlc_intercepted(
920-
&self, intercept_scid: u64, intercept_id: InterceptId, expected_outbound_amount_msat: u64,
921-
payment_hash: PaymentHash,
922-
) {
923-
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
924-
if let Err(e) = lsps2_service_handler
925-
.htlc_intercepted(
926-
intercept_scid,
927-
intercept_id,
928-
expected_outbound_amount_msat,
929-
payment_hash,
930-
)
931-
.await
932-
{
933-
log_error!(
934-
self.logger,
935-
"LSPS2 service failed to handle HTLCIntercepted event: {:?}",
936-
e
937-
);
938-
}
939-
}
940-
}
941-
942-
pub(crate) async fn handle_htlc_handling_failed(&self, failure_type: HTLCHandlingFailureType) {
943-
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
944-
if let Err(e) = lsps2_service_handler.htlc_handling_failed(failure_type).await {
945-
log_error!(
946-
self.logger,
947-
"LSPS2 service failed to handle HTLCHandlingFailed event: {:?}",
948-
e
949-
);
950-
}
951-
}
952-
}
953-
954-
pub(crate) async fn handle_payment_forwarded(
955-
&self, next_channel_id: Option<ChannelId>, skimmed_fee_msat: u64,
956-
) {
957-
if let Some(next_channel_id) = next_channel_id {
958-
if let Some(lsps2_service_handler) = self.liquidity_manager.lsps2_service_handler() {
959-
if let Err(e) =
960-
lsps2_service_handler.payment_forwarded(next_channel_id, skimmed_fee_msat).await
961-
{
962-
log_error!(
963-
self.logger,
964-
"LSPS2 service failed to handle PaymentForwarded: {:?}",
965-
e
966-
);
967-
}
968-
}
969-
}
970-
}
971765
}

0 commit comments

Comments
 (0)