@@ -535,12 +535,24 @@ pub(crate) enum MonitorUpdateCompletionAction {
535
535
/// event can be generated.
536
536
PaymentClaimed { payment_hash : PaymentHash } ,
537
537
/// Indicates an [`events::Event`] should be surfaced to the user.
538
- EmitEvent { event : events:: Event } ,
538
+ EmitEventAndFreeOtherChannel {
539
+ event : events:: Event ,
540
+ downstream_counterparty_and_funding_outpoint : Option < ( PublicKey , OutPoint , RAAMonitorUpdateBlockingAction ) > ,
541
+ } ,
539
542
}
540
543
541
544
impl_writeable_tlv_based_enum_upgradable ! ( MonitorUpdateCompletionAction ,
542
545
( 0 , PaymentClaimed ) => { ( 0 , payment_hash, required) } ,
543
- ( 2 , EmitEvent ) => { ( 0 , event, upgradable_required) } ,
546
+ ( 2 , EmitEventAndFreeOtherChannel ) => {
547
+ ( 0 , event, upgradable_required) ,
548
+ // LDK prior to 0.0.115 did not have this field as the monitor update application order was
549
+ // required by clients. If we downgrade to something prior to 0.0.115 this may result in
550
+ // monitor updates which aren't properly blocked or resumed, however that's fine - we don't
551
+ // support async monitor updates even in LDK 0.0.115 and once we do we'll require no
552
+ // downgrades to prior versions. Thus, while this would break on downgrade, we don't
553
+ // support it even without downgrade, so if it breaks its not on us ¯\_(ツ)_/¯.
554
+ ( 1 , downstream_counterparty_and_funding_outpoint, option) ,
555
+ } ,
544
556
) ;
545
557
546
558
#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -557,6 +569,29 @@ impl_writeable_tlv_based_enum!(EventCompletionAction,
557
569
} ;
558
570
) ;
559
571
572
+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
573
+ pub ( crate ) enum RAAMonitorUpdateBlockingAction {
574
+ /// The inbound channel's channel_id
575
+ ForwardedPaymentOtherChannelClaim {
576
+ channel_id : [ u8 ; 32 ] ,
577
+ htlc_id : u64 ,
578
+ } ,
579
+ }
580
+
581
+ impl RAAMonitorUpdateBlockingAction {
582
+ fn from_prev_hop_data ( prev_hop : & HTLCPreviousHopData ) -> Self {
583
+ Self :: ForwardedPaymentOtherChannelClaim {
584
+ channel_id : prev_hop. outpoint . to_channel_id ( ) ,
585
+ htlc_id : prev_hop. htlc_id ,
586
+ }
587
+ }
588
+ }
589
+
590
+ impl_writeable_tlv_based_enum ! ( RAAMonitorUpdateBlockingAction ,
591
+ ( 0 , ForwardedPaymentOtherChannelClaim ) => { ( 0 , channel_id, required) , ( 2 , htlc_id, required) }
592
+ ; ) ;
593
+
594
+
560
595
/// State we hold per-peer.
561
596
pub ( super ) struct PeerState < Signer : ChannelSigner > {
562
597
/// `temporary_channel_id` or `channel_id` -> `channel`.
@@ -585,6 +620,11 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
585
620
/// to funding appearing on-chain), the downstream `ChannelMonitor` set is required to ensure
586
621
/// duplicates do not occur, so such channels should fail without a monitor update completing.
587
622
monitor_update_blocked_actions : BTreeMap < [ u8 ; 32 ] , Vec < MonitorUpdateCompletionAction > > ,
623
+ /// If another channel's [`ChannelMonitorUpdate`] needs to complete before a channel we have
624
+ /// with this peer can complete an RAA [`ChannelMonitorUpdate`] (e.g. because the RAA update
625
+ /// will remove a preimage that needs to be durably in an upstream channel first), we put an
626
+ /// entry here to note that the channel with the key's ID is blocked on a set of actions.
627
+ actions_blocking_raa_monitor_updates : BTreeMap < [ u8 ; 32 ] , Vec < RAAMonitorUpdateBlockingAction > > ,
588
628
/// The peer is currently connected (i.e. we've seen a
589
629
/// [`ChannelMessageHandler::peer_connected`] and no corresponding
590
630
/// [`ChannelMessageHandler::peer_disconnected`].
@@ -4484,23 +4524,24 @@ where
4484
4524
} ,
4485
4525
HTLCSource :: PreviousHopData ( hop_data) => {
4486
4526
let prev_outpoint = hop_data. outpoint ;
4527
+ let completed_blocker = RAAMonitorUpdateBlockingAction :: from_prev_hop_data ( & hop_data) ;
4487
4528
let res = self . claim_funds_from_hop ( hop_data, payment_preimage,
4488
4529
|htlc_claim_value_msat| {
4489
4530
if let Some ( forwarded_htlc_value) = forwarded_htlc_value_msat {
4490
4531
let fee_earned_msat = if let Some ( claimed_htlc_value) = htlc_claim_value_msat {
4491
4532
Some ( claimed_htlc_value - forwarded_htlc_value)
4492
4533
} else { None } ;
4493
4534
4494
- let prev_channel_id = Some ( prev_outpoint . to_channel_id ( ) ) ;
4495
- let next_channel_id = Some ( next_channel_id ) ;
4496
-
4497
- Some ( MonitorUpdateCompletionAction :: EmitEvent { event : events :: Event :: PaymentForwarded {
4498
- fee_earned_msat ,
4499
- claim_from_onchain_tx : from_onchain ,
4500
- prev_channel_id ,
4501
- next_channel_id ,
4502
- outbound_amount_forwarded_msat : forwarded_htlc_value_msat ,
4503
- } } )
4535
+ Some ( MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
4536
+ event : events :: Event :: PaymentForwarded {
4537
+ fee_earned_msat ,
4538
+ claim_from_onchain_tx : from_onchain ,
4539
+ prev_channel_id : Some ( prev_outpoint . to_channel_id ( ) ) ,
4540
+ next_channel_id : Some ( next_channel_id ) ,
4541
+ outbound_amount_forwarded_msat : forwarded_htlc_value_msat ,
4542
+ } ,
4543
+ downstream_counterparty_and_funding_outpoint : None ,
4544
+ } )
4504
4545
} else { None }
4505
4546
} ) ;
4506
4547
if let Err ( ( pk, err) ) = res {
@@ -4527,8 +4568,13 @@ where
4527
4568
} , None ) ) ;
4528
4569
}
4529
4570
} ,
4530
- MonitorUpdateCompletionAction :: EmitEvent { event } => {
4571
+ MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
4572
+ event, downstream_counterparty_and_funding_outpoint
4573
+ } => {
4531
4574
self . pending_events . lock ( ) . unwrap ( ) . push_back ( ( event, None ) ) ;
4575
+ if let Some ( ( node_id, funding_outpoint, blocker) ) = downstream_counterparty_and_funding_outpoint {
4576
+ self . handle_monitor_update_release ( node_id, funding_outpoint, Some ( blocker) ) ;
4577
+ }
4532
4578
} ,
4533
4579
}
4534
4580
}
@@ -5375,6 +5421,36 @@ where
5375
5421
}
5376
5422
}
5377
5423
5424
+ fn raa_monitor_updates_held ( & self ,
5425
+ actions_blocking_raa_monitor_updates : & BTreeMap < [ u8 ; 32 ] , Vec < RAAMonitorUpdateBlockingAction > > ,
5426
+ channel_funding_outpoint : OutPoint , counterparty_node_id : PublicKey
5427
+ ) -> bool {
5428
+ actions_blocking_raa_monitor_updates
5429
+ . get ( & channel_funding_outpoint. to_channel_id ( ) ) . map ( |v| !v. is_empty ( ) ) . unwrap_or ( false )
5430
+ || self . pending_events . lock ( ) . unwrap ( ) . iter ( ) . any ( |( _, action) | {
5431
+ action == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
5432
+ channel_funding_outpoint,
5433
+ counterparty_node_id,
5434
+ } )
5435
+ } )
5436
+ }
5437
+
5438
+ pub ( crate ) fn test_raa_monitor_updates_held ( & self , counterparty_node_id : PublicKey ,
5439
+ channel_id : [ u8 ; 32 ] )
5440
+ -> bool {
5441
+ let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5442
+ if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
5443
+ let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
5444
+ let peer_state = & mut * peer_state_lck;
5445
+
5446
+ if let Some ( chan) = peer_state. channel_by_id . get ( & channel_id) {
5447
+ return self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
5448
+ chan. get_funding_txo ( ) . unwrap ( ) , counterparty_node_id) ;
5449
+ }
5450
+ }
5451
+ false
5452
+ }
5453
+
5378
5454
fn internal_revoke_and_ack ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) -> Result < ( ) , MsgHandleErrInternal > {
5379
5455
let ( htlcs_to_fail, res) = {
5380
5456
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
@@ -6039,25 +6115,29 @@ where
6039
6115
self . pending_outbound_payments . clear_pending_payments ( )
6040
6116
}
6041
6117
6042
- fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint ) {
6118
+ fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint , completed_blocker : Option < RAAMonitorUpdateBlockingAction > ) {
6043
6119
let mut errors = Vec :: new ( ) ;
6044
6120
loop {
6045
6121
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
6046
6122
if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
6047
6123
let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
6048
6124
let peer_state = & mut * peer_state_lck;
6049
- if self . pending_events . lock ( ) . unwrap ( ) . iter ( )
6050
- . any ( |( _ev, action_opt) | action_opt == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
6051
- channel_funding_outpoint, counterparty_node_id
6052
- } ) )
6053
- {
6054
- // Check that, while holding the peer lock, we don't have another event
6055
- // blocking any monitor updates for this channel. If we do, let those
6056
- // events be the ones that ultimately release the monitor update(s).
6057
- log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another event is pending" ,
6125
+
6126
+ if let Some ( blocker) = & completed_blocker {
6127
+ if let Some ( blockers) = peer_state. actions_blocking_raa_monitor_updates
6128
+ . get_mut ( & channel_funding_outpoint. to_channel_id ( ) )
6129
+ {
6130
+ blockers. retain ( |iter| iter != blocker) ;
6131
+ }
6132
+ }
6133
+
6134
+ if self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
6135
+ channel_funding_outpoint, counterparty_node_id) {
6136
+ log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another channel's mon update needs to complete first" ,
6058
6137
log_bytes!( & channel_funding_outpoint. to_channel_id( ) [ ..] ) ) ;
6059
6138
break ;
6060
6139
}
6140
+
6061
6141
if let hash_map:: Entry :: Occupied ( mut chan) = peer_state. channel_by_id . entry ( channel_funding_outpoint. to_channel_id ( ) ) {
6062
6142
debug_assert_eq ! ( chan. get( ) . get_funding_txo( ) . unwrap( ) , channel_funding_outpoint) ;
6063
6143
if let Some ( ( monitor_update, further_update_exists) ) = chan. get_mut ( ) . unblock_next_blocked_monitor_update ( ) {
@@ -6099,7 +6179,7 @@ where
6099
6179
EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
6100
6180
channel_funding_outpoint, counterparty_node_id
6101
6181
} => {
6102
- self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint) ;
6182
+ self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint, None ) ;
6103
6183
}
6104
6184
}
6105
6185
}
@@ -6775,6 +6855,7 @@ where
6775
6855
latest_features : init_msg. features . clone ( ) ,
6776
6856
pending_msg_events : Vec :: new ( ) ,
6777
6857
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
6858
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
6778
6859
is_connected : true ,
6779
6860
} ) ) ;
6780
6861
} ,
@@ -7971,6 +8052,7 @@ where
7971
8052
latest_features : Readable :: read ( reader) ?,
7972
8053
pending_msg_events : Vec :: new ( ) ,
7973
8054
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
8055
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
7974
8056
is_connected : false ,
7975
8057
} ;
7976
8058
per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
@@ -8052,7 +8134,7 @@ where
8052
8134
let mut claimable_htlc_purposes = None ;
8053
8135
let mut claimable_htlc_onion_fields = None ;
8054
8136
let mut pending_claiming_payments = Some ( HashMap :: new ( ) ) ;
8055
- let mut monitor_update_blocked_actions_per_peer = Some ( Vec :: new ( ) ) ;
8137
+ let mut monitor_update_blocked_actions_per_peer: Option < Vec < ( _ , BTreeMap < _ , Vec < _ > > ) > > = Some ( Vec :: new ( ) ) ;
8056
8138
let mut events_override = None ;
8057
8139
read_tlv_fields ! ( reader, {
8058
8140
( 1 , pending_outbound_payments_no_retry, option) ,
@@ -8377,7 +8459,21 @@ where
8377
8459
}
8378
8460
8379
8461
for ( node_id, monitor_update_blocked_actions) in monitor_update_blocked_actions_per_peer. unwrap ( ) {
8380
- if let Some ( peer_state) = per_peer_state. get_mut ( & node_id) {
8462
+ if let Some ( peer_state) = per_peer_state. get ( & node_id) {
8463
+ for ( _, actions) in monitor_update_blocked_actions. iter ( ) {
8464
+ for action in actions. iter ( ) {
8465
+ if let MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
8466
+ downstream_counterparty_and_funding_outpoint :
8467
+ Some ( ( blocked_node_id, blocked_channel_outpoint, blocking_action) ) , ..
8468
+ } = action {
8469
+ if let Some ( blocked_peer_state) = per_peer_state. get ( & blocked_node_id) {
8470
+ blocked_peer_state. lock ( ) . unwrap ( ) . actions_blocking_raa_monitor_updates
8471
+ . entry ( blocked_channel_outpoint. to_channel_id ( ) )
8472
+ . or_insert_with ( Vec :: new) . push ( blocking_action. clone ( ) ) ;
8473
+ }
8474
+ }
8475
+ }
8476
+ }
8381
8477
peer_state. lock ( ) . unwrap ( ) . monitor_update_blocked_actions = monitor_update_blocked_actions;
8382
8478
} else {
8383
8479
log_error ! ( args. logger, "Got blocked actions without a per-peer-state for {}" , node_id) ;
0 commit comments