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