@@ -525,12 +525,24 @@ pub(crate) enum MonitorUpdateCompletionAction {
525
525
/// event can be generated.
526
526
PaymentClaimed { payment_hash : PaymentHash } ,
527
527
/// Indicates an [`events::Event`] should be surfaced to the user.
528
- EmitEvent { event : events:: Event } ,
528
+ EmitEventAndFreeOtherChannel {
529
+ event : events:: Event ,
530
+ downstream_counterparty_and_funding_outpoint : Option < ( PublicKey , OutPoint , RAAMonitorUpdateBlockingAction ) > ,
531
+ } ,
529
532
}
530
533
531
534
impl_writeable_tlv_based_enum_upgradable ! ( MonitorUpdateCompletionAction ,
532
535
( 0 , PaymentClaimed ) => { ( 0 , payment_hash, required) } ,
533
- ( 2 , EmitEvent ) => { ( 0 , event, upgradable_required) } ,
536
+ ( 2 , EmitEventAndFreeOtherChannel ) => {
537
+ ( 0 , event, upgradable_required) ,
538
+ // LDK prior to 0.0.115 did not have this field as the monitor update application order was
539
+ // required by clients. If we downgrade to something prior to 0.0.115 this may result in
540
+ // monitor updates which aren't properly blocked or resumed, however that's fine - we don't
541
+ // support async monitor updates even in LDK 0.0.115 and once we do we'll require no
542
+ // downgrades to prior versions. Thus, while this would break on downgrade, we don't
543
+ // support it even without downgrade, so if it breaks its not on us ¯\_(ツ)_/¯.
544
+ ( 1 , downstream_counterparty_and_funding_outpoint, option) ,
545
+ } ,
534
546
) ;
535
547
536
548
#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -547,6 +559,29 @@ impl_writeable_tlv_based_enum!(EventCompletionAction,
547
559
} ;
548
560
) ;
549
561
562
+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
563
+ pub ( crate ) enum RAAMonitorUpdateBlockingAction {
564
+ /// The inbound channel's channel_id
565
+ ForwardedPaymentOtherChannelClaim {
566
+ channel_id : [ u8 ; 32 ] ,
567
+ htlc_id : u64 ,
568
+ } ,
569
+ }
570
+
571
+ impl RAAMonitorUpdateBlockingAction {
572
+ fn from_prev_hop_data ( prev_hop : & HTLCPreviousHopData ) -> Self {
573
+ Self :: ForwardedPaymentOtherChannelClaim {
574
+ channel_id : prev_hop. outpoint . to_channel_id ( ) ,
575
+ htlc_id : prev_hop. htlc_id ,
576
+ }
577
+ }
578
+ }
579
+
580
+ impl_writeable_tlv_based_enum ! ( RAAMonitorUpdateBlockingAction ,
581
+ ( 0 , ForwardedPaymentOtherChannelClaim ) => { ( 0 , channel_id, required) , ( 2 , htlc_id, required) }
582
+ ; ) ;
583
+
584
+
550
585
/// State we hold per-peer.
551
586
pub ( super ) struct PeerState < Signer : ChannelSigner > {
552
587
/// `temporary_channel_id` or `channel_id` -> `channel`.
@@ -575,6 +610,11 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
575
610
/// to funding appearing on-chain), the downstream `ChannelMonitor` set is required to ensure
576
611
/// duplicates do not occur, so such channels should fail without a monitor update completing.
577
612
monitor_update_blocked_actions : BTreeMap < [ u8 ; 32 ] , Vec < MonitorUpdateCompletionAction > > ,
613
+ /// If another channel's [`ChannelMonitorUpdate`] needs to complete before a channel we have
614
+ /// with this peer can complete an RAA [`ChannelMonitorUpdate`] (e.g. because the RAA update
615
+ /// will remove a preimage that needs to be durably in an upstream channel first), we put an
616
+ /// entry here to note that the channel with the key's ID is blocked on a set of actions.
617
+ actions_blocking_raa_monitor_updates : BTreeMap < [ u8 ; 32 ] , Vec < RAAMonitorUpdateBlockingAction > > ,
578
618
/// The peer is currently connected (i.e. we've seen a
579
619
/// [`ChannelMessageHandler::peer_connected`] and no corresponding
580
620
/// [`ChannelMessageHandler::peer_disconnected`].
@@ -4459,23 +4499,24 @@ where
4459
4499
} ,
4460
4500
HTLCSource :: PreviousHopData ( hop_data) => {
4461
4501
let prev_outpoint = hop_data. outpoint ;
4502
+ let completed_blocker = RAAMonitorUpdateBlockingAction :: from_prev_hop_data ( & hop_data) ;
4462
4503
let res = self . claim_funds_from_hop ( hop_data, payment_preimage,
4463
4504
|htlc_claim_value_msat| {
4464
4505
if let Some ( forwarded_htlc_value) = forwarded_htlc_value_msat {
4465
4506
let fee_earned_msat = if let Some ( claimed_htlc_value) = htlc_claim_value_msat {
4466
4507
Some ( claimed_htlc_value - forwarded_htlc_value)
4467
4508
} else { None } ;
4468
4509
4469
- let prev_channel_id = Some ( prev_outpoint . to_channel_id ( ) ) ;
4470
- let next_channel_id = Some ( next_channel_id ) ;
4471
-
4472
- Some ( MonitorUpdateCompletionAction :: EmitEvent { event : events :: Event :: PaymentForwarded {
4473
- fee_earned_msat ,
4474
- claim_from_onchain_tx : from_onchain ,
4475
- prev_channel_id ,
4476
- next_channel_id ,
4477
- outbound_amount_forwarded_msat : forwarded_htlc_value_msat ,
4478
- } } )
4510
+ Some ( MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
4511
+ event : events :: Event :: PaymentForwarded {
4512
+ fee_earned_msat ,
4513
+ claim_from_onchain_tx : from_onchain ,
4514
+ prev_channel_id : Some ( prev_outpoint . to_channel_id ( ) ) ,
4515
+ next_channel_id : Some ( next_channel_id ) ,
4516
+ outbound_amount_forwarded_msat : forwarded_htlc_value_msat ,
4517
+ } ,
4518
+ downstream_counterparty_and_funding_outpoint : None ,
4519
+ } )
4479
4520
} else { None }
4480
4521
} ) ;
4481
4522
if let Err ( ( pk, err) ) = res {
@@ -4502,8 +4543,13 @@ where
4502
4543
} , None ) ) ;
4503
4544
}
4504
4545
} ,
4505
- MonitorUpdateCompletionAction :: EmitEvent { event } => {
4546
+ MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
4547
+ event, downstream_counterparty_and_funding_outpoint
4548
+ } => {
4506
4549
self . pending_events . lock ( ) . unwrap ( ) . push_back ( ( event, None ) ) ;
4550
+ if let Some ( ( node_id, funding_outpoint, blocker) ) = downstream_counterparty_and_funding_outpoint {
4551
+ self . handle_monitor_update_release ( node_id, funding_outpoint, Some ( blocker) ) ;
4552
+ }
4507
4553
} ,
4508
4554
}
4509
4555
}
@@ -5350,6 +5396,36 @@ where
5350
5396
}
5351
5397
}
5352
5398
5399
+ fn raa_monitor_updates_held ( & self ,
5400
+ actions_blocking_raa_monitor_updates : & BTreeMap < [ u8 ; 32 ] , Vec < RAAMonitorUpdateBlockingAction > > ,
5401
+ channel_funding_outpoint : OutPoint , counterparty_node_id : PublicKey
5402
+ ) -> bool {
5403
+ actions_blocking_raa_monitor_updates
5404
+ . get ( & channel_funding_outpoint. to_channel_id ( ) ) . map ( |v| !v. is_empty ( ) ) . unwrap_or ( false )
5405
+ || self . pending_events . lock ( ) . unwrap ( ) . iter ( ) . any ( |( _, action) | {
5406
+ action == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
5407
+ channel_funding_outpoint,
5408
+ counterparty_node_id,
5409
+ } )
5410
+ } )
5411
+ }
5412
+
5413
+ pub ( crate ) fn test_raa_monitor_updates_held ( & self , counterparty_node_id : PublicKey ,
5414
+ channel_id : [ u8 ; 32 ] )
5415
+ -> bool {
5416
+ let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5417
+ if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
5418
+ let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
5419
+ let peer_state = & mut * peer_state_lck;
5420
+
5421
+ if let Some ( chan) = peer_state. channel_by_id . get ( & channel_id) {
5422
+ return self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
5423
+ chan. get_funding_txo ( ) . unwrap ( ) , counterparty_node_id) ;
5424
+ }
5425
+ }
5426
+ false
5427
+ }
5428
+
5353
5429
fn internal_revoke_and_ack ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) -> Result < ( ) , MsgHandleErrInternal > {
5354
5430
let ( htlcs_to_fail, res) = {
5355
5431
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
@@ -6013,25 +6089,29 @@ where
6013
6089
self . pending_outbound_payments . clear_pending_payments ( )
6014
6090
}
6015
6091
6016
- fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint ) {
6092
+ fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint , completed_blocker : Option < RAAMonitorUpdateBlockingAction > ) {
6017
6093
let mut errors = Vec :: new ( ) ;
6018
6094
loop {
6019
6095
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
6020
6096
if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
6021
6097
let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
6022
6098
let peer_state = & mut * peer_state_lck;
6023
- if self . pending_events . lock ( ) . unwrap ( ) . iter ( )
6024
- . any ( |( _ev, action_opt) | action_opt == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
6025
- channel_funding_outpoint, counterparty_node_id
6026
- } ) )
6027
- {
6028
- // Check that, while holding the peer lock, we don't have another event
6029
- // blocking any monitor updates for this channel. If we do, let those
6030
- // events be the ones that ultimately release the monitor update(s).
6031
- log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another event is pending" ,
6099
+
6100
+ if let Some ( blocker) = & completed_blocker {
6101
+ if let Some ( blockers) = peer_state. actions_blocking_raa_monitor_updates
6102
+ . get_mut ( & channel_funding_outpoint. to_channel_id ( ) )
6103
+ {
6104
+ blockers. retain ( |iter| iter != blocker) ;
6105
+ }
6106
+ }
6107
+
6108
+ if self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
6109
+ channel_funding_outpoint, counterparty_node_id) {
6110
+ log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another channel's mon update needs to complete first" ,
6032
6111
log_bytes!( & channel_funding_outpoint. to_channel_id( ) [ ..] ) ) ;
6033
6112
break ;
6034
6113
}
6114
+
6035
6115
if let hash_map:: Entry :: Occupied ( mut chan) = peer_state. channel_by_id . entry ( channel_funding_outpoint. to_channel_id ( ) ) {
6036
6116
debug_assert_eq ! ( chan. get( ) . get_funding_txo( ) . unwrap( ) , channel_funding_outpoint) ;
6037
6117
if let Some ( ( monitor_update, further_update_exists) ) = chan. get_mut ( ) . unblock_next_blocked_monitor_update ( ) {
@@ -6073,7 +6153,7 @@ where
6073
6153
EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
6074
6154
channel_funding_outpoint, counterparty_node_id
6075
6155
} => {
6076
- self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint) ;
6156
+ self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint, None ) ;
6077
6157
}
6078
6158
}
6079
6159
}
@@ -6749,6 +6829,7 @@ where
6749
6829
latest_features : init_msg. features . clone ( ) ,
6750
6830
pending_msg_events : Vec :: new ( ) ,
6751
6831
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
6832
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
6752
6833
is_connected : true ,
6753
6834
} ) ) ;
6754
6835
} ,
@@ -7943,6 +8024,7 @@ where
7943
8024
latest_features : Readable :: read ( reader) ?,
7944
8025
pending_msg_events : Vec :: new ( ) ,
7945
8026
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
8027
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
7946
8028
is_connected : false ,
7947
8029
} ;
7948
8030
per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
@@ -8023,7 +8105,7 @@ where
8023
8105
let mut claimable_htlc_purposes = None ;
8024
8106
let mut claimable_htlc_onion_fields = None ;
8025
8107
let mut pending_claiming_payments = Some ( HashMap :: new ( ) ) ;
8026
- let mut monitor_update_blocked_actions_per_peer = Some ( Vec :: new ( ) ) ;
8108
+ let mut monitor_update_blocked_actions_per_peer: Option < Vec < ( _ , BTreeMap < _ , Vec < _ > > ) > > = Some ( Vec :: new ( ) ) ;
8027
8109
let mut events_override = None ;
8028
8110
read_tlv_fields ! ( reader, {
8029
8111
( 1 , pending_outbound_payments_no_retry, option) ,
@@ -8348,7 +8430,21 @@ where
8348
8430
}
8349
8431
8350
8432
for ( node_id, monitor_update_blocked_actions) in monitor_update_blocked_actions_per_peer. unwrap ( ) {
8351
- if let Some ( peer_state) = per_peer_state. get_mut ( & node_id) {
8433
+ if let Some ( peer_state) = per_peer_state. get ( & node_id) {
8434
+ for ( _, actions) in monitor_update_blocked_actions. iter ( ) {
8435
+ for action in actions. iter ( ) {
8436
+ if let MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
8437
+ downstream_counterparty_and_funding_outpoint :
8438
+ Some ( ( blocked_node_id, blocked_channel_outpoint, blocking_action) ) , ..
8439
+ } = action {
8440
+ if let Some ( blocked_peer_state) = per_peer_state. get ( & blocked_node_id) {
8441
+ blocked_peer_state. lock ( ) . unwrap ( ) . actions_blocking_raa_monitor_updates
8442
+ . entry ( blocked_channel_outpoint. to_channel_id ( ) )
8443
+ . or_insert_with ( Vec :: new) . push ( blocking_action. clone ( ) ) ;
8444
+ }
8445
+ }
8446
+ }
8447
+ }
8352
8448
peer_state. lock ( ) . unwrap ( ) . monitor_update_blocked_actions = monitor_update_blocked_actions;
8353
8449
} else {
8354
8450
log_error ! ( args. logger, "Got blocked actions without a per-peer-state for {}" , node_id) ;
0 commit comments