@@ -11,14 +11,17 @@ use crate::chain::ChainSource;
11
11
use crate :: connection:: ConnectionManager ;
12
12
use crate :: logger:: { log_debug, log_error, log_info, LdkLogger , Logger } ;
13
13
use crate :: runtime:: Runtime ;
14
- use crate :: types:: { ChannelManager , KeysManager , LiquidityManager , PeerManager , Wallet } ;
14
+ use crate :: types:: {
15
+ Broadcaster , ChannelManager , KeysManager , LiquidityManager , PeerManager , Wallet ,
16
+ } ;
15
17
use crate :: { total_anchor_channels_reserve_sats, Config , Error } ;
16
18
17
19
use lightning:: events:: HTLCHandlingFailureType ;
18
20
use lightning:: ln:: channelmanager:: { InterceptId , MIN_FINAL_CLTV_EXPIRY_DELTA } ;
19
21
use lightning:: ln:: msgs:: SocketAddress ;
20
22
use lightning:: ln:: types:: ChannelId ;
21
23
use lightning:: routing:: router:: { RouteHint , RouteHintHop } ;
24
+ use lightning:: util:: errors:: APIError ;
22
25
23
26
use lightning_invoice:: { Bolt11Invoice , Bolt11InvoiceDescription , InvoiceBuilder , RoutingFees } ;
24
27
@@ -40,6 +43,7 @@ use lightning_types::payment::PaymentHash;
40
43
41
44
use bitcoin:: hashes:: { sha256, Hash } ;
42
45
use bitcoin:: secp256k1:: { PublicKey , Secp256k1 } ;
46
+ use bitcoin:: Transaction ;
43
47
44
48
use tokio:: sync:: oneshot;
45
49
@@ -55,7 +59,6 @@ use std::time::Duration;
55
59
const LIQUIDITY_REQUEST_TIMEOUT_SECS : u64 = 5 ;
56
60
57
61
const LSPS2_GETINFO_REQUEST_EXPIRY : Duration = Duration :: from_secs ( 60 * 60 * 24 ) ;
58
- const LSPS2_CLIENT_TRUSTS_LSP_MODE : bool = true ;
59
62
const LSPS2_CHANNEL_CLTV_EXPIRY_DELTA : u32 = 72 ;
60
63
61
64
struct LSPS1Client {
@@ -134,6 +137,8 @@ pub struct LSPS2ServiceConfig {
134
137
pub min_payment_size_msat : u64 ,
135
138
/// The maximum payment size that we will accept when opening a channel.
136
139
pub max_payment_size_msat : u64 ,
140
+ /// Use the client trusts lsp model
141
+ pub client_trusts_lsp : bool ,
137
142
}
138
143
139
144
pub ( crate ) struct LiquiditySourceBuilder < L : Deref >
@@ -149,6 +154,7 @@ where
149
154
chain_source : Arc < ChainSource > ,
150
155
config : Arc < Config > ,
151
156
logger : L ,
157
+ broadcaster : Arc < Broadcaster > ,
152
158
}
153
159
154
160
impl < L : Deref > LiquiditySourceBuilder < L >
@@ -158,6 +164,7 @@ where
158
164
pub ( crate ) fn new (
159
165
wallet : Arc < Wallet > , channel_manager : Arc < ChannelManager > , keys_manager : Arc < KeysManager > ,
160
166
chain_source : Arc < ChainSource > , config : Arc < Config > , logger : L ,
167
+ broadcaster : Arc < Broadcaster > ,
161
168
) -> Self {
162
169
let lsps1_client = None ;
163
170
let lsps2_client = None ;
@@ -172,6 +179,7 @@ where
172
179
chain_source,
173
180
config,
174
181
logger,
182
+ broadcaster,
175
183
}
176
184
}
177
185
@@ -242,6 +250,7 @@ where
242
250
Arc :: clone ( & self . keys_manager ) ,
243
251
Arc :: clone ( & self . channel_manager ) ,
244
252
Some ( Arc :: clone ( & self . chain_source ) ) ,
253
+ Arc :: clone ( & self . broadcaster ) ,
245
254
None ,
246
255
liquidity_service_config,
247
256
liquidity_client_config,
@@ -298,6 +307,79 @@ where
298
307
self . lsps2_client . as_ref ( ) . map ( |s| ( s. lsp_node_id , s. lsp_address . clone ( ) ) )
299
308
}
300
309
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
+
301
383
pub ( crate ) async fn handle_next_event ( & self ) {
302
384
match self . liquidity_manager . next_event_async ( ) . await {
303
385
LiquidityEvent :: LSPS1Client ( LSPS1ClientEvent :: SupportedOptionsReady {
@@ -586,7 +668,7 @@ where
586
668
request_id,
587
669
intercept_scid,
588
670
LSPS2_CHANNEL_CLTV_EXPIRY_DELTA ,
589
- LSPS2_CLIENT_TRUSTS_LSP_MODE ,
671
+ service_config . client_trusts_lsp ,
590
672
user_channel_id,
591
673
) {
592
674
Ok ( ( ) ) => { } ,
@@ -1296,10 +1378,14 @@ where
1296
1378
}
1297
1379
}
1298
1380
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
+ ) {
1300
1384
if let Some ( next_channel_id) = next_channel_id {
1301
1385
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
+ {
1303
1389
log_error ! (
1304
1390
self . logger,
1305
1391
"LSPS2 service failed to handle PaymentForwarded: {:?}" ,
0 commit comments