@@ -23,14 +23,6 @@ use crate::lsps0::ser::{
23
23
LSPS0_CLIENT_REJECTED_ERROR_CODE ,
24
24
} ;
25
25
use crate :: lsps2:: event:: LSPS2ServiceEvent ;
26
- use crate :: lsps2:: msgs:: {
27
- LSPS2BuyRequest , LSPS2BuyResponse , LSPS2GetInfoRequest , LSPS2GetInfoResponse , LSPS2Message ,
28
- LSPS2OpeningFeeParams , LSPS2RawOpeningFeeParams , LSPS2Request , LSPS2Response ,
29
- LSPS2_BUY_REQUEST_INVALID_OPENING_FEE_PARAMS_ERROR_CODE ,
30
- LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_LARGE_ERROR_CODE ,
31
- LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_SMALL_ERROR_CODE ,
32
- LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE ,
33
- } ;
34
26
use crate :: lsps2:: payment_queue:: { InterceptedHTLC , PaymentQueue } ;
35
27
use crate :: lsps2:: utils:: {
36
28
compute_opening_fee, is_expired_opening_fee_params, is_valid_opening_fee_params,
@@ -52,6 +44,15 @@ use lightning_types::payment::PaymentHash;
52
44
use bitcoin:: secp256k1:: PublicKey ;
53
45
use bitcoin:: Transaction ;
54
46
47
+ use crate :: lsps2:: msgs:: {
48
+ LSPS2BuyRequest , LSPS2BuyResponse , LSPS2GetInfoRequest , LSPS2GetInfoResponse , LSPS2Message ,
49
+ LSPS2OpeningFeeParams , LSPS2RawOpeningFeeParams , LSPS2Request , LSPS2Response ,
50
+ LSPS2_BUY_REQUEST_INVALID_OPENING_FEE_PARAMS_ERROR_CODE ,
51
+ LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_LARGE_ERROR_CODE ,
52
+ LSPS2_BUY_REQUEST_PAYMENT_SIZE_TOO_SMALL_ERROR_CODE ,
53
+ LSPS2_GET_INFO_REQUEST_UNRECOGNIZED_OR_STALE_TOKEN_ERROR_CODE ,
54
+ } ;
55
+
55
56
const MAX_PENDING_REQUESTS_PER_PEER : usize = 10 ;
56
57
const MAX_TOTAL_PENDING_REQUESTS : usize = 1000 ;
57
58
const MAX_TOTAL_PEERS : usize = 100000 ;
@@ -112,20 +113,21 @@ enum TrustModel {
112
113
ClientTrustsLsp {
113
114
funding_tx_broadcast_safe : bool ,
114
115
payment_claimed : bool ,
115
- funding_tx : Option < Transaction > ,
116
+ funding_tx : Option < Arc < Transaction > > ,
116
117
} ,
117
118
LspTrustsClient ,
118
119
}
119
120
120
121
impl TrustModel {
121
- fn should_broadcast ( & self ) -> bool {
122
+ fn should_manually_broadcast ( & self ) -> bool {
122
123
match self {
123
124
TrustModel :: ClientTrustsLsp {
124
125
funding_tx_broadcast_safe,
125
126
payment_claimed,
126
127
funding_tx,
127
128
} => * funding_tx_broadcast_safe && * payment_claimed && funding_tx. is_some ( ) ,
128
- TrustModel :: LspTrustsClient => true ,
129
+ // in lsp-trusts-client, the broadcast is automatic, so we never need to manually broadcast.
130
+ TrustModel :: LspTrustsClient => false ,
129
131
}
130
132
}
131
133
@@ -141,7 +143,7 @@ impl TrustModel {
141
143
}
142
144
}
143
145
144
- fn set_funding_tx ( & mut self , funding_tx : Transaction ) {
146
+ fn set_funding_tx ( & mut self , funding_tx : Arc < Transaction > ) {
145
147
match self {
146
148
TrustModel :: ClientTrustsLsp { funding_tx : tx, .. } => {
147
149
* tx = Some ( funding_tx) ;
@@ -174,10 +176,17 @@ impl TrustModel {
174
176
}
175
177
}
176
178
177
- fn get_funding_tx ( & self ) -> Option < Transaction > {
179
+ fn get_funding_tx ( & self ) -> Option < Arc < Transaction > > {
178
180
match self {
179
- TrustModel :: ClientTrustsLsp { funding_tx, .. } => funding_tx. clone ( ) ,
180
- TrustModel :: LspTrustsClient => None ,
181
+ TrustModel :: ClientTrustsLsp { funding_tx : Some ( tx) , .. } => Some ( Arc :: clone ( & tx) ) ,
182
+ _ => None ,
183
+ }
184
+ }
185
+
186
+ fn is_client_trusts_lsp ( & self ) -> bool {
187
+ match self {
188
+ TrustModel :: ClientTrustsLsp { .. } => true ,
189
+ TrustModel :: LspTrustsClient => false ,
181
190
}
182
191
}
183
192
}
@@ -355,7 +364,6 @@ impl OutboundJITChannelState {
355
364
channel_id,
356
365
FeePayment { htlcs : entry. htlcs , opening_fee_msat : * opening_fee_msat } ,
357
366
) ;
358
-
359
367
* self = OutboundJITChannelState :: PendingPaymentForward {
360
368
payment_queue : core:: mem:: take ( payment_queue) ,
361
369
opening_fee_msat : * opening_fee_msat,
@@ -453,8 +461,7 @@ struct OutboundJITChannel {
453
461
user_channel_id : u128 ,
454
462
opening_fee_params : LSPS2OpeningFeeParams ,
455
463
payment_size_msat : Option < u64 > ,
456
- client_trusts_lsp : bool ,
457
- trust_model : Option < TrustModel > ,
464
+ trust_model : TrustModel ,
458
465
}
459
466
460
467
impl OutboundJITChannel {
@@ -467,23 +474,15 @@ impl OutboundJITChannel {
467
474
state : OutboundJITChannelState :: new ( ) ,
468
475
opening_fee_params,
469
476
payment_size_msat,
470
- client_trusts_lsp,
471
- trust_model : None ,
477
+ trust_model : TrustModel :: new ( client_trusts_lsp) ,
472
478
}
473
479
}
474
480
475
481
fn htlc_intercepted (
476
482
& mut self , htlc : InterceptedHTLC ,
477
483
) -> Result < Option < HTLCInterceptedAction > , LightningError > {
478
- let was_initial =
479
- matches ! ( self . state, OutboundJITChannelState :: PendingInitialPayment { .. } ) ;
480
484
let action =
481
485
self . state . htlc_intercepted ( & self . opening_fee_params , & self . payment_size_msat , htlc) ?;
482
- if was_initial && self . trust_model . is_none ( ) {
483
- if !matches ! ( self . state, OutboundJITChannelState :: PendingInitialPayment { .. } ) {
484
- self . trust_model = Some ( TrustModel :: new ( self . client_trusts_lsp ) ) ;
485
- }
486
- }
487
486
Ok ( action)
488
487
}
489
488
@@ -502,9 +501,7 @@ impl OutboundJITChannel {
502
501
fn payment_forwarded ( & mut self ) -> Result < Option < ForwardHTLCsAction > , LightningError > {
503
502
let action = self . state . payment_forwarded ( ) ?;
504
503
if action. is_some ( ) {
505
- if let Some ( tm) = & mut self . trust_model {
506
- tm. set_payment_claimed ( true ) ;
507
- }
504
+ self . trust_model . set_payment_claimed ( true ) ;
508
505
}
509
506
Ok ( action)
510
507
}
@@ -520,43 +517,24 @@ impl OutboundJITChannel {
520
517
self . is_pending_initial_payment ( ) && is_expired
521
518
}
522
519
523
- fn set_funding_tx ( & mut self , funding_tx : Transaction ) -> Result < ( ) , LightningError > {
524
- if let Some ( tm) = & mut self . trust_model {
525
- tm. set_funding_tx ( funding_tx) ;
526
- Ok ( ( ) )
527
- } else {
528
- Err ( LightningError :: from ( ChannelStateError (
529
- "Store funding transaction when JIT Channel was in invalid state" . to_string ( ) ,
530
- ) ) )
531
- }
520
+ fn set_funding_tx ( & mut self , funding_tx : Arc < Transaction > ) {
521
+ self . trust_model . set_funding_tx ( funding_tx) ;
532
522
}
533
523
534
- fn set_funding_tx_broadcast_safe (
535
- & mut self , funding_tx_broadcast_safe : bool ,
536
- ) -> Result < ( ) , LightningError > {
537
- if let Some ( tm) = & mut self . trust_model {
538
- tm. set_funding_tx_broadcast_safe ( funding_tx_broadcast_safe) ;
539
- Ok ( ( ) )
540
- } else {
541
- Err ( LightningError :: from ( ChannelStateError (
542
- "Store funding transaction broadcast safe when JIT Channel was in invalid state"
543
- . to_string ( ) ,
544
- ) ) )
545
- }
524
+ fn set_funding_tx_broadcast_safe ( & mut self , funding_tx_broadcast_safe : bool ) {
525
+ self . trust_model . set_funding_tx_broadcast_safe ( funding_tx_broadcast_safe) ;
546
526
}
547
527
548
528
fn should_broadcast_funding_transaction ( & self ) -> bool {
549
- self . trust_model . as_ref ( ) . map_or ( false , |tm| tm . should_broadcast ( ) )
529
+ self . trust_model . should_manually_broadcast ( )
550
530
}
551
531
552
- fn get_funding_tx ( & self ) -> Option < Transaction > {
553
- self . trust_model . as_ref ( ) . and_then ( |tm| tm . get_funding_tx ( ) )
532
+ fn get_funding_tx ( & self ) -> Option < Arc < Transaction > > {
533
+ self . trust_model . get_funding_tx ( )
554
534
}
555
535
556
536
fn client_trusts_lsp ( & self ) -> bool {
557
- self . trust_model
558
- . as_ref ( )
559
- . map_or ( false , |tm| matches ! ( tm, TrustModel :: ClientTrustsLsp { .. } ) )
537
+ self . trust_model . is_client_trusts_lsp ( )
560
538
}
561
539
}
562
540
@@ -797,6 +775,12 @@ where
797
775
///
798
776
/// Should be called in response to receiving a [`LSPS2ServiceEvent::BuyRequest`] event.
799
777
///
778
+ /// `client_trusts_lsp`:
779
+ /// * false (default) => "LSP trusts client": LSP broadcasts the funding
780
+ /// transaction as soon as it is safe and forwards the payment normally.
781
+ /// * true => "Client trusts LSP": LSP may defer broadcasting the funding
782
+ /// transaction until after the client claims the forwarded HTLC(s).
783
+ ///
800
784
/// [`ChannelManager::get_intercept_scid`]: lightning::ln::channelmanager::ChannelManager::get_intercept_scid
801
785
/// [`LSPS2ServiceEvent::BuyRequest`]: crate::lsps2::event::LSPS2ServiceEvent::BuyRequest
802
786
pub fn invoice_parameters_generated (
@@ -1052,14 +1036,17 @@ where
1052
1036
Err ( e) => {
1053
1037
return Err ( APIError :: APIMisuseError {
1054
1038
err : format ! (
1055
- "Forwarded payment was not applicable for JIT channel: {}" ,
1056
- e. err
1057
- ) ,
1039
+ "Forwarded payment was not applicable for JIT channel: {}" ,
1040
+ e. err
1041
+ ) ,
1058
1042
} )
1059
1043
} ,
1060
1044
}
1061
1045
1062
- self . broadcast_transaction_if_applies ( & jit_channel) ;
1046
+ self . emit_broadcast_funding_transaction_event_if_applies (
1047
+ jit_channel,
1048
+ counterparty_node_id,
1049
+ ) ;
1063
1050
}
1064
1051
} else {
1065
1052
return Err ( APIError :: APIMisuseError {
@@ -1583,7 +1570,8 @@ where
1583
1570
/// Called to store the funding transaction for a JIT channel.
1584
1571
/// This should be called when the funding transaction is created but before it's broadcast.
1585
1572
pub fn store_funding_transaction (
1586
- & self , user_channel_id : u128 , counterparty_node_id : & PublicKey , funding_tx : Transaction ,
1573
+ & self , user_channel_id : u128 , counterparty_node_id : & PublicKey ,
1574
+ funding_tx : Arc < Transaction > ,
1587
1575
) -> Result < ( ) , APIError > {
1588
1576
let outer_state_lock = self . per_peer_state . read ( ) . unwrap ( ) ;
1589
1577
let inner_state_lock =
@@ -1610,11 +1598,9 @@ where
1610
1598
) ,
1611
1599
} ) ?;
1612
1600
1613
- jit_channel
1614
- . set_funding_tx ( funding_tx)
1615
- . map_err ( |e| APIError :: APIMisuseError { err : e. err . to_string ( ) } ) ?;
1601
+ jit_channel. set_funding_tx ( funding_tx) ;
1616
1602
1617
- self . broadcast_transaction_if_applies ( jit_channel) ;
1603
+ self . emit_broadcast_funding_transaction_event_if_applies ( jit_channel, counterparty_node_id ) ;
1618
1604
Ok ( ( ) )
1619
1605
}
1620
1606
@@ -1648,20 +1634,26 @@ where
1648
1634
) ,
1649
1635
} ) ?;
1650
1636
1651
- jit_channel
1652
- . set_funding_tx_broadcast_safe ( true )
1653
- . map_err ( |e| APIError :: APIMisuseError { err : e. err . to_string ( ) } ) ?;
1637
+ jit_channel. set_funding_tx_broadcast_safe ( true ) ;
1654
1638
1655
- self . broadcast_transaction_if_applies ( jit_channel) ;
1639
+ self . emit_broadcast_funding_transaction_event_if_applies ( jit_channel, counterparty_node_id ) ;
1656
1640
Ok ( ( ) )
1657
1641
}
1658
1642
1659
- fn broadcast_transaction_if_applies ( & self , jit_channel : & OutboundJITChannel ) {
1643
+ fn emit_broadcast_funding_transaction_event_if_applies (
1644
+ & self , jit_channel : & OutboundJITChannel , counterparty_node_id : & PublicKey ,
1645
+ ) {
1660
1646
if jit_channel. should_broadcast_funding_transaction ( ) {
1661
1647
let funding_tx = jit_channel. get_funding_tx ( ) ;
1662
1648
1663
1649
if let Some ( funding_tx) = funding_tx {
1664
- self . channel_manager . get_cm ( ) . broadcast_transaction ( & funding_tx) ;
1650
+ let event_queue_notifier = self . pending_events . notifier ( ) ;
1651
+ let event = LSPS2ServiceEvent :: BroadcastFundingTransaction {
1652
+ counterparty_node_id : * counterparty_node_id,
1653
+ user_channel_id : jit_channel. user_channel_id ,
1654
+ funding_tx : funding_tx. as_ref ( ) . clone ( ) ,
1655
+ } ;
1656
+ event_queue_notifier. enqueue ( event) ;
1665
1657
}
1666
1658
}
1667
1659
}
0 commit comments