@@ -11,14 +11,17 @@ use crate::chain::ChainSource;
1111use crate :: connection:: ConnectionManager ;
1212use crate :: logger:: { log_debug, log_error, log_info, LdkLogger , Logger } ;
1313use crate :: runtime:: Runtime ;
14- use crate :: types:: { ChannelManager , KeysManager , LiquidityManager , PeerManager , Wallet } ;
14+ use crate :: types:: {
15+ Broadcaster , ChannelManager , KeysManager , LiquidityManager , PeerManager , Wallet ,
16+ } ;
1517use crate :: { total_anchor_channels_reserve_sats, Config , Error } ;
1618
1719use lightning:: events:: HTLCHandlingFailureType ;
1820use lightning:: ln:: channelmanager:: { InterceptId , MIN_FINAL_CLTV_EXPIRY_DELTA } ;
1921use lightning:: ln:: msgs:: SocketAddress ;
2022use lightning:: ln:: types:: ChannelId ;
2123use lightning:: routing:: router:: { RouteHint , RouteHintHop } ;
24+ use lightning:: util:: errors:: APIError ;
2225
2326use lightning_invoice:: { Bolt11Invoice , Bolt11InvoiceDescription , InvoiceBuilder , RoutingFees } ;
2427
@@ -40,6 +43,7 @@ use lightning_types::payment::PaymentHash;
4043
4144use bitcoin:: hashes:: { sha256, Hash } ;
4245use bitcoin:: secp256k1:: { PublicKey , Secp256k1 } ;
46+ use bitcoin:: Transaction ;
4347
4448use tokio:: sync:: oneshot;
4549
@@ -55,7 +59,6 @@ use std::time::Duration;
5559const LIQUIDITY_REQUEST_TIMEOUT_SECS : u64 = 5 ;
5660
5761const LSPS2_GETINFO_REQUEST_EXPIRY : Duration = Duration :: from_secs ( 60 * 60 * 24 ) ;
58- const LSPS2_CLIENT_TRUSTS_LSP_MODE : bool = true ;
5962const LSPS2_CHANNEL_CLTV_EXPIRY_DELTA : u32 = 72 ;
6063
6164struct LSPS1Client {
@@ -134,6 +137,8 @@ pub struct LSPS2ServiceConfig {
134137 pub min_payment_size_msat : u64 ,
135138 /// The maximum payment size that we will accept when opening a channel.
136139 pub max_payment_size_msat : u64 ,
140+ /// Use the client trusts lsp model
141+ pub client_trusts_lsp : bool ,
137142}
138143
139144pub ( crate ) struct LiquiditySourceBuilder < L : Deref >
@@ -149,6 +154,7 @@ where
149154 chain_source : Arc < ChainSource > ,
150155 config : Arc < Config > ,
151156 logger : L ,
157+ broadcaster : Arc < Broadcaster > ,
152158}
153159
154160impl < L : Deref > LiquiditySourceBuilder < L >
@@ -158,6 +164,7 @@ where
158164 pub ( crate ) fn new (
159165 wallet : Arc < Wallet > , channel_manager : Arc < ChannelManager > , keys_manager : Arc < KeysManager > ,
160166 chain_source : Arc < ChainSource > , config : Arc < Config > , logger : L ,
167+ broadcaster : Arc < Broadcaster > ,
161168 ) -> Self {
162169 let lsps1_client = None ;
163170 let lsps2_client = None ;
@@ -172,6 +179,7 @@ where
172179 chain_source,
173180 config,
174181 logger,
182+ broadcaster,
175183 }
176184 }
177185
@@ -242,6 +250,7 @@ where
242250 Arc :: clone ( & self . keys_manager ) ,
243251 Arc :: clone ( & self . channel_manager ) ,
244252 Some ( Arc :: clone ( & self . chain_source ) ) ,
253+ Arc :: clone ( & self . broadcaster ) ,
245254 None ,
246255 liquidity_service_config,
247256 liquidity_client_config,
@@ -298,6 +307,79 @@ where
298307 self . lsps2_client . as_ref ( ) . map ( |s| ( s. lsp_node_id , s. lsp_address . clone ( ) ) )
299308 }
300309
310+ pub ( crate ) fn lsps2_channel_needs_manual_broadcast (
311+ & self , counterparty_node_id : PublicKey , user_channel_id : u128 ,
312+ ) -> Result < bool , APIError > {
313+ // if we are not in a client_trusts_lsp model, we don't check and just return false
314+ if !self . is_client_trusts_lsp ( ) {
315+ log_debug ! ( self . logger, "Skipping funding transaction broadcast as client trusts LSP." ) ;
316+ return Ok ( false ) ;
317+ }
318+
319+ // if we are in a client_trusts_lsp model, then we check if the LSP has an LSPS2 operation in progress
320+ self . lsps2_service . as_ref ( ) . map_or ( Ok ( false ) , |_| {
321+ let lsps2_service_handler = self . liquidity_manager . lsps2_service_handler ( ) ;
322+ if let Some ( handler) = lsps2_service_handler {
323+ handler. channel_needs_manual_broadcast ( user_channel_id, & counterparty_node_id)
324+ } else {
325+ log_error ! ( self . logger, "LSPS2 service handler is not available." ) ;
326+ Ok ( false )
327+ }
328+ } )
329+ }
330+
331+ pub ( crate ) fn lsps2_store_funding_transaction (
332+ & self , user_channel_id : u128 , counterparty_node_id : PublicKey , funding_tx : Transaction ,
333+ ) {
334+ if !self . is_client_trusts_lsp ( ) {
335+ log_debug ! ( self . logger, "Skipping funding transaction broadcast as client trusts LSP." ) ;
336+ return ;
337+ }
338+ self . lsps2_service . as_ref ( ) . map ( |_| {
339+ let lsps2_service_handler = self . liquidity_manager . lsps2_service_handler ( ) ;
340+ if let Some ( handler) = lsps2_service_handler {
341+ handler
342+ . store_funding_transaction ( user_channel_id, & counterparty_node_id, funding_tx)
343+ . unwrap_or_else ( |e| {
344+ debug_assert ! ( false , "Failed to store funding transaction: {:?}" , e) ;
345+ log_error ! ( self . logger, "Failed to store funding transaction: {:?}" , e) ;
346+ } ) ;
347+ } else {
348+ log_error ! ( self . logger, "LSPS2 service handler is not available." ) ;
349+ }
350+ } ) ;
351+ }
352+
353+ pub ( crate ) fn lsps2_funding_tx_broadcast_safe (
354+ & self , user_channel_id : u128 , counterparty_node_id : PublicKey ,
355+ ) {
356+ if !self . is_client_trusts_lsp ( ) {
357+ log_debug ! ( self . logger, "Skipping funding transaction broadcast as client trusts LSP." ) ;
358+ return ;
359+ }
360+ self . lsps2_service . as_ref ( ) . map ( |_| {
361+ let lsps2_service_handler = self . liquidity_manager . lsps2_service_handler ( ) ;
362+ if let Some ( handler) = lsps2_service_handler {
363+ handler
364+ . set_funding_tx_broadcast_safe ( user_channel_id, & counterparty_node_id)
365+ . unwrap_or_else ( |e| {
366+ debug_assert ! ( false , "Failed to store funding transaction: {:?}" , e) ;
367+ log_error ! ( self . logger, "Failed to store funding transaction: {:?}" , e) ;
368+ } ) ;
369+ } else {
370+ log_error ! ( self . logger, "LSPS2 service handler is not available." ) ;
371+ }
372+ } ) ;
373+ }
374+
375+ fn is_client_trusts_lsp ( & self ) -> bool {
376+ if let Some ( lsps2_service) = self . lsps2_service . as_ref ( ) {
377+ lsps2_service. service_config . client_trusts_lsp
378+ } else {
379+ false
380+ }
381+ }
382+
301383 pub ( crate ) async fn handle_next_event ( & self ) {
302384 match self . liquidity_manager . next_event_async ( ) . await {
303385 LiquidityEvent :: LSPS1Client ( LSPS1ClientEvent :: SupportedOptionsReady {
@@ -586,7 +668,7 @@ where
586668 request_id,
587669 intercept_scid,
588670 LSPS2_CHANNEL_CLTV_EXPIRY_DELTA ,
589- LSPS2_CLIENT_TRUSTS_LSP_MODE ,
671+ service_config . client_trusts_lsp ,
590672 user_channel_id,
591673 ) {
592674 Ok ( ( ) ) => { } ,
@@ -1296,10 +1378,14 @@ where
12961378 }
12971379 }
12981380
1299- pub ( crate ) fn handle_payment_forwarded ( & self , next_channel_id : Option < ChannelId > ) {
1381+ pub ( crate ) fn handle_payment_forwarded (
1382+ & self , next_channel_id : Option < ChannelId > , skimmed_fee_msat : Option < u64 > ,
1383+ ) {
13001384 if let Some ( next_channel_id) = next_channel_id {
13011385 if let Some ( lsps2_service_handler) = self . liquidity_manager . lsps2_service_handler ( ) {
1302- if let Err ( e) = lsps2_service_handler. payment_forwarded ( next_channel_id) {
1386+ if let Err ( e) = lsps2_service_handler
1387+ . payment_forwarded ( next_channel_id, skimmed_fee_msat. unwrap_or ( 0 ) )
1388+ {
13031389 log_error ! (
13041390 self . logger,
13051391 "LSPS2 service failed to handle PaymentForwarded: {:?}" ,
0 commit comments