Skip to content

Commit 3de5d8f

Browse files
committed
Move LSPS2 service logic into liquidity/service/lsps2.rs
1 parent a458c7f commit 3de5d8f

3 files changed

Lines changed: 240 additions & 207 deletions

File tree

src/liquidity/mod.rs

Lines changed: 5 additions & 207 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,73 +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, clients that we open channels to will be allowed to spend their entire channel
110-
/// balance. This allows clients to try to steal your funds with no financial penalty, so
111-
/// this should only be set if you trust your clients.
112-
pub allow_client_0reserve: bool,
113-
}
114-
11550
pub(crate) struct LiquiditySourceBuilder<L: Deref>
11651
where
11752
L::Target: LdkLogger,
@@ -277,73 +212,6 @@ where
277212
Arc::clone(&self.liquidity_manager)
278213
}
279214

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

0 commit comments

Comments
 (0)