@@ -14,12 +14,14 @@ use std::time::Duration;
1414
1515use bitcoin:: hashes:: { sha256, Hash } ;
1616use bitcoin:: secp256k1:: { PublicKey , Secp256k1 } ;
17+ use bitcoin:: Transaction ;
1718use chrono:: Utc ;
1819use lightning:: events:: HTLCHandlingFailureType ;
1920use lightning:: ln:: channelmanager:: { InterceptId , MIN_FINAL_CLTV_EXPIRY_DELTA } ;
2021use lightning:: ln:: msgs:: SocketAddress ;
2122use lightning:: ln:: types:: ChannelId ;
2223use lightning:: routing:: router:: { RouteHint , RouteHintHop } ;
24+ use lightning:: util:: errors:: APIError ;
2325use lightning_invoice:: { Bolt11Invoice , Bolt11InvoiceDescription , InvoiceBuilder , RoutingFees } ;
2426use lightning_liquidity:: events:: LiquidityEvent ;
2527use lightning_liquidity:: lsps0:: ser:: { LSPSDateTime , LSPSRequestId } ;
@@ -51,7 +53,6 @@ use crate::{total_anchor_channels_reserve_sats, Config, Error};
5153const LIQUIDITY_REQUEST_TIMEOUT_SECS : u64 = 5 ;
5254
5355const LSPS2_GETINFO_REQUEST_EXPIRY : Duration = Duration :: from_secs ( 60 * 60 * 24 ) ;
54- const LSPS2_CLIENT_TRUSTS_LSP_MODE : bool = true ;
5556const LSPS2_CHANNEL_CLTV_EXPIRY_DELTA : u32 = 72 ;
5657
5758struct LSPS1Client {
@@ -130,6 +131,8 @@ pub struct LSPS2ServiceConfig {
130131 pub min_payment_size_msat : u64 ,
131132 /// The maximum payment size that we will accept when opening a channel.
132133 pub max_payment_size_msat : u64 ,
134+ /// Use the client trusts lsp model
135+ pub client_trusts_lsp : bool ,
133136}
134137
135138pub ( crate ) struct LiquiditySourceBuilder < L : Deref >
@@ -147,6 +150,7 @@ where
147150 kv_store : Arc < DynStore > ,
148151 config : Arc < Config > ,
149152 logger : L ,
153+ broadcaster : Arc < Broadcaster > ,
150154}
151155
152156impl < L : Deref > LiquiditySourceBuilder < L >
@@ -156,7 +160,7 @@ where
156160 pub ( crate ) fn new (
157161 wallet : Arc < Wallet > , channel_manager : Arc < ChannelManager > , keys_manager : Arc < KeysManager > ,
158162 chain_source : Arc < ChainSource > , tx_broadcaster : Arc < Broadcaster > , kv_store : Arc < DynStore > ,
159- config : Arc < Config > , logger : L ,
163+ config : Arc < Config > , logger : L , broadcaster : Arc < Broadcaster > ,
160164 ) -> Self {
161165 let lsps1_client = None ;
162166 let lsps2_client = None ;
@@ -173,6 +177,7 @@ where
173177 kv_store,
174178 config,
175179 logger,
180+ broadcaster,
176181 }
177182 }
178183
@@ -305,6 +310,79 @@ where
305310 self . lsps2_client . as_ref ( ) . map ( |s| ( s. lsp_node_id , s. lsp_address . clone ( ) ) )
306311 }
307312
313+ pub ( crate ) fn lsps2_channel_needs_manual_broadcast (
314+ & self , counterparty_node_id : PublicKey , user_channel_id : u128 ,
315+ ) -> Result < bool , APIError > {
316+ // if we are not in a client_trusts_lsp model, we don't check and just return false
317+ if !self . is_client_trusts_lsp ( ) {
318+ log_debug ! ( self . logger, "Skipping funding transaction broadcast as client trusts LSP." ) ;
319+ return Ok ( false ) ;
320+ }
321+
322+ // if we are in a client_trusts_lsp model, then we check if the LSP has an LSPS2 operation in progress
323+ self . lsps2_service . as_ref ( ) . map_or ( Ok ( false ) , |_| {
324+ let lsps2_service_handler = self . liquidity_manager . lsps2_service_handler ( ) ;
325+ if let Some ( handler) = lsps2_service_handler {
326+ handler. channel_needs_manual_broadcast ( user_channel_id, & counterparty_node_id)
327+ } else {
328+ log_error ! ( self . logger, "LSPS2 service handler is not available." ) ;
329+ Ok ( false )
330+ }
331+ } )
332+ }
333+
334+ pub ( crate ) fn lsps2_store_funding_transaction (
335+ & self , user_channel_id : u128 , counterparty_node_id : PublicKey , funding_tx : Transaction ,
336+ ) {
337+ if !self . is_client_trusts_lsp ( ) {
338+ log_debug ! ( self . logger, "Skipping funding transaction broadcast as client trusts LSP." ) ;
339+ return ;
340+ }
341+ self . lsps2_service . as_ref ( ) . map ( |_| {
342+ let lsps2_service_handler = self . liquidity_manager . lsps2_service_handler ( ) ;
343+ if let Some ( handler) = lsps2_service_handler {
344+ handler
345+ . store_funding_transaction ( user_channel_id, & counterparty_node_id, funding_tx)
346+ . unwrap_or_else ( |e| {
347+ debug_assert ! ( false , "Failed to store funding transaction: {:?}" , e) ;
348+ log_error ! ( self . logger, "Failed to store funding transaction: {:?}" , e) ;
349+ } ) ;
350+ } else {
351+ log_error ! ( self . logger, "LSPS2 service handler is not available." ) ;
352+ }
353+ } ) ;
354+ }
355+
356+ pub ( crate ) fn lsps2_funding_tx_broadcast_safe (
357+ & self , user_channel_id : u128 , counterparty_node_id : PublicKey ,
358+ ) {
359+ if !self . is_client_trusts_lsp ( ) {
360+ log_debug ! ( self . logger, "Skipping funding transaction broadcast as client trusts LSP." ) ;
361+ return ;
362+ }
363+ self . lsps2_service . as_ref ( ) . map ( |_| {
364+ let lsps2_service_handler = self . liquidity_manager . lsps2_service_handler ( ) ;
365+ if let Some ( handler) = lsps2_service_handler {
366+ handler
367+ . set_funding_tx_broadcast_safe ( user_channel_id, & counterparty_node_id)
368+ . unwrap_or_else ( |e| {
369+ debug_assert ! ( false , "Failed to store funding transaction: {:?}" , e) ;
370+ log_error ! ( self . logger, "Failed to store funding transaction: {:?}" , e) ;
371+ } ) ;
372+ } else {
373+ log_error ! ( self . logger, "LSPS2 service handler is not available." ) ;
374+ }
375+ } ) ;
376+ }
377+
378+ fn is_client_trusts_lsp ( & self ) -> bool {
379+ if let Some ( lsps2_service) = self . lsps2_service . as_ref ( ) {
380+ lsps2_service. service_config . client_trusts_lsp
381+ } else {
382+ false
383+ }
384+ }
385+
308386 pub ( crate ) async fn handle_next_event ( & self ) {
309387 match self . liquidity_manager . next_event_async ( ) . await {
310388 LiquidityEvent :: LSPS1Client ( LSPS1ClientEvent :: SupportedOptionsReady {
@@ -594,7 +672,7 @@ where
594672 request_id,
595673 intercept_scid,
596674 LSPS2_CHANNEL_CLTV_EXPIRY_DELTA ,
597- LSPS2_CLIENT_TRUSTS_LSP_MODE ,
675+ service_config . client_trusts_lsp ,
598676 user_channel_id,
599677 )
600678 . await
0 commit comments