@@ -51,7 +51,6 @@ use crate::{total_anchor_channels_reserve_sats, Config, Error};
5151const LIQUIDITY_REQUEST_TIMEOUT_SECS : u64 = 5 ;
5252
5353const LSPS2_GETINFO_REQUEST_EXPIRY : Duration = Duration :: from_secs ( 60 * 60 * 24 ) ;
54- const LSPS2_CLIENT_TRUSTS_LSP_MODE : bool = true ;
5554const LSPS2_CHANNEL_CLTV_EXPIRY_DELTA : u32 = 72 ;
5655
5756struct LSPS1Client {
@@ -130,6 +129,8 @@ pub struct LSPS2ServiceConfig {
130129 pub min_payment_size_msat : u64 ,
131130 /// The maximum payment size that we will accept when opening a channel.
132131 pub max_payment_size_msat : u64 ,
132+ /// Use the client trusts lsp model
133+ pub client_trusts_lsp : bool ,
133134}
134135
135136pub ( crate ) struct LiquiditySourceBuilder < L : Deref >
@@ -147,6 +148,7 @@ where
147148 kv_store : Arc < DynStore > ,
148149 config : Arc < Config > ,
149150 logger : L ,
151+ broadcaster : Arc < Broadcaster > ,
150152}
151153
152154impl < L : Deref > LiquiditySourceBuilder < L >
@@ -155,8 +157,12 @@ where
155157{
156158 pub ( crate ) fn new (
157159 wallet : Arc < Wallet > , channel_manager : Arc < ChannelManager > , keys_manager : Arc < KeysManager > ,
158- chain_source : Arc < ChainSource > , tx_broadcaster : Arc < Broadcaster > , kv_store : Arc < DynStore > ,
159- config : Arc < Config > , logger : L ,
160+ chain_source : Arc < ChainSource > ,
161+ tx_broadcaster : Arc < Broadcaster > ,
162+ kv_store : Arc < DynStore > ,
163+ config : Arc < Config > ,
164+ logger : L ,
165+ broadcaster : Arc < Broadcaster > ,
160166 ) -> Self {
161167 let lsps1_client = None ;
162168 let lsps2_client = None ;
@@ -173,6 +179,7 @@ where
173179 kv_store,
174180 config,
175181 logger,
182+ broadcaster,
176183 }
177184 }
178185
@@ -305,6 +312,79 @@ where
305312 self . lsps2_client . as_ref ( ) . map ( |s| ( s. lsp_node_id , s. lsp_address . clone ( ) ) )
306313 }
307314
315+ pub ( crate ) fn lsps2_channel_needs_manual_broadcast (
316+ & self , counterparty_node_id : PublicKey , user_channel_id : u128 ,
317+ ) -> Result < bool , APIError > {
318+ // if we are not in a client_trusts_lsp model, we don't check and just return false
319+ if !self . is_client_trusts_lsp ( ) {
320+ log_debug ! ( self . logger, "Skipping funding transaction broadcast as client trusts LSP." ) ;
321+ return Ok ( false ) ;
322+ }
323+
324+ // if we are in a client_trusts_lsp model, then we check if the LSP has an LSPS2 operation in progress
325+ self . lsps2_service . as_ref ( ) . map_or ( Ok ( false ) , |_| {
326+ let lsps2_service_handler = self . liquidity_manager . lsps2_service_handler ( ) ;
327+ if let Some ( handler) = lsps2_service_handler {
328+ handler. channel_needs_manual_broadcast ( user_channel_id, & counterparty_node_id)
329+ } else {
330+ log_error ! ( self . logger, "LSPS2 service handler is not available." ) ;
331+ Ok ( false )
332+ }
333+ } )
334+ }
335+
336+ pub ( crate ) fn lsps2_store_funding_transaction (
337+ & self , user_channel_id : u128 , counterparty_node_id : PublicKey , funding_tx : Transaction ,
338+ ) {
339+ if !self . is_client_trusts_lsp ( ) {
340+ log_debug ! ( self . logger, "Skipping funding transaction broadcast as client trusts LSP." ) ;
341+ return ;
342+ }
343+ self . lsps2_service . as_ref ( ) . map ( |_| {
344+ let lsps2_service_handler = self . liquidity_manager . lsps2_service_handler ( ) ;
345+ if let Some ( handler) = lsps2_service_handler {
346+ handler
347+ . store_funding_transaction ( user_channel_id, & counterparty_node_id, funding_tx)
348+ . unwrap_or_else ( |e| {
349+ debug_assert ! ( false , "Failed to store funding transaction: {:?}" , e) ;
350+ log_error ! ( self . logger, "Failed to store funding transaction: {:?}" , e) ;
351+ } ) ;
352+ } else {
353+ log_error ! ( self . logger, "LSPS2 service handler is not available." ) ;
354+ }
355+ } ) ;
356+ }
357+
358+ pub ( crate ) fn lsps2_funding_tx_broadcast_safe (
359+ & self , user_channel_id : u128 , counterparty_node_id : PublicKey ,
360+ ) {
361+ if !self . is_client_trusts_lsp ( ) {
362+ log_debug ! ( self . logger, "Skipping funding transaction broadcast as client trusts LSP." ) ;
363+ return ;
364+ }
365+ self . lsps2_service . as_ref ( ) . map ( |_| {
366+ let lsps2_service_handler = self . liquidity_manager . lsps2_service_handler ( ) ;
367+ if let Some ( handler) = lsps2_service_handler {
368+ handler
369+ . set_funding_tx_broadcast_safe ( user_channel_id, & counterparty_node_id)
370+ . unwrap_or_else ( |e| {
371+ debug_assert ! ( false , "Failed to store funding transaction: {:?}" , e) ;
372+ log_error ! ( self . logger, "Failed to store funding transaction: {:?}" , e) ;
373+ } ) ;
374+ } else {
375+ log_error ! ( self . logger, "LSPS2 service handler is not available." ) ;
376+ }
377+ } ) ;
378+ }
379+
380+ fn is_client_trusts_lsp ( & self ) -> bool {
381+ if let Some ( lsps2_service) = self . lsps2_service . as_ref ( ) {
382+ lsps2_service. service_config . client_trusts_lsp
383+ } else {
384+ false
385+ }
386+ }
387+
308388 pub ( crate ) async fn handle_next_event ( & self ) {
309389 match self . liquidity_manager . next_event_async ( ) . await {
310390 LiquidityEvent :: LSPS1Client ( LSPS1ClientEvent :: SupportedOptionsReady {
@@ -594,7 +674,7 @@ where
594674 request_id,
595675 intercept_scid,
596676 LSPS2_CHANNEL_CLTV_EXPIRY_DELTA ,
597- LSPS2_CLIENT_TRUSTS_LSP_MODE ,
677+ service_config . client_trusts_lsp , ,
598678 user_channel_id,
599679 )
600680 . await
0 commit comments