@@ -534,12 +534,24 @@ pub(crate) enum MonitorUpdateCompletionAction {
534
534
/// event can be generated.
535
535
PaymentClaimed { payment_hash : PaymentHash } ,
536
536
/// Indicates an [`events::Event`] should be surfaced to the user.
537
- EmitEvent { event : events:: Event } ,
537
+ EmitEventAndFreeOtherChannel {
538
+ event : events:: Event ,
539
+ downstream_counterparty_and_funding_outpoint : Option < ( PublicKey , OutPoint , RAAMonitorUpdateBlockingAction ) > ,
540
+ } ,
538
541
}
539
542
540
543
impl_writeable_tlv_based_enum_upgradable ! ( MonitorUpdateCompletionAction ,
541
544
( 0 , PaymentClaimed ) => { ( 0 , payment_hash, required) } ,
542
- ( 2 , EmitEvent ) => { ( 0 , event, upgradable_required) } ,
545
+ ( 2 , EmitEventAndFreeOtherChannel ) => {
546
+ ( 0 , event, upgradable_required) ,
547
+ // LDK prior to 0.0.115 did not have this field as the monitor update application order was
548
+ // required by clients. If we downgrade to something prior to 0.0.115 this may result in
549
+ // monitor updates which aren't properly blocked or resumed, however that's fine - we don't
550
+ // support async monitor updates even in LDK 0.0.115 and once we do we'll require no
551
+ // downgrades to prior versions. Thus, while this would break on downgrade, we don't
552
+ // support it even without downgrade, so if it breaks its not on us ¯\_(ツ)_/¯.
553
+ ( 1 , downstream_counterparty_and_funding_outpoint, option) ,
554
+ } ,
543
555
) ;
544
556
545
557
#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -556,6 +568,29 @@ impl_writeable_tlv_based_enum!(EventCompletionAction,
556
568
} ;
557
569
) ;
558
570
571
+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
572
+ pub ( crate ) enum RAAMonitorUpdateBlockingAction {
573
+ /// The inbound channel's channel_id
574
+ ForwardedPaymentOtherChannelClaim {
575
+ channel_id : [ u8 ; 32 ] ,
576
+ htlc_id : u64 ,
577
+ } ,
578
+ }
579
+
580
+ impl RAAMonitorUpdateBlockingAction {
581
+ fn from_prev_hop_data ( prev_hop : & HTLCPreviousHopData ) -> Self {
582
+ Self :: ForwardedPaymentOtherChannelClaim {
583
+ channel_id : prev_hop. outpoint . to_channel_id ( ) ,
584
+ htlc_id : prev_hop. htlc_id ,
585
+ }
586
+ }
587
+ }
588
+
589
+ impl_writeable_tlv_based_enum ! ( RAAMonitorUpdateBlockingAction ,
590
+ ( 0 , ForwardedPaymentOtherChannelClaim ) => { ( 0 , channel_id, required) , ( 2 , htlc_id, required) }
591
+ ; ) ;
592
+
593
+
559
594
/// State we hold per-peer.
560
595
pub ( super ) struct PeerState < Signer : ChannelSigner > {
561
596
/// `temporary_channel_id` or `channel_id` -> `channel`.
@@ -584,6 +619,11 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
584
619
/// to funding appearing on-chain), the downstream `ChannelMonitor` set is required to ensure
585
620
/// duplicates do not occur, so such channels should fail without a monitor update completing.
586
621
monitor_update_blocked_actions : BTreeMap < [ u8 ; 32 ] , Vec < MonitorUpdateCompletionAction > > ,
622
+ /// If another channel's [`ChannelMonitorUpdate`] needs to complete before a channel we have
623
+ /// with this peer can complete an RAA [`ChannelMonitorUpdate`] (e.g. because the RAA update
624
+ /// will remove a preimage that needs to be durably in an upstream channel first), we put an
625
+ /// entry here to note that the channel with the key's ID is blocked on a set of actions.
626
+ actions_blocking_raa_monitor_updates : BTreeMap < [ u8 ; 32 ] , Vec < RAAMonitorUpdateBlockingAction > > ,
587
627
/// The peer is currently connected (i.e. we've seen a
588
628
/// [`ChannelMessageHandler::peer_connected`] and no corresponding
589
629
/// [`ChannelMessageHandler::peer_disconnected`].
@@ -4468,23 +4508,24 @@ where
4468
4508
} ,
4469
4509
HTLCSource :: PreviousHopData ( hop_data) => {
4470
4510
let prev_outpoint = hop_data. outpoint ;
4511
+ let completed_blocker = RAAMonitorUpdateBlockingAction :: from_prev_hop_data ( & hop_data) ;
4471
4512
let res = self . claim_funds_from_hop ( hop_data, payment_preimage,
4472
4513
|htlc_claim_value_msat| {
4473
4514
if let Some ( forwarded_htlc_value) = forwarded_htlc_value_msat {
4474
4515
let fee_earned_msat = if let Some ( claimed_htlc_value) = htlc_claim_value_msat {
4475
4516
Some ( claimed_htlc_value - forwarded_htlc_value)
4476
4517
} else { None } ;
4477
4518
4478
- let prev_channel_id = Some ( prev_outpoint . to_channel_id ( ) ) ;
4479
- let next_channel_id = Some ( next_channel_id ) ;
4480
-
4481
- Some ( MonitorUpdateCompletionAction :: EmitEvent { event : events :: Event :: PaymentForwarded {
4482
- fee_earned_msat ,
4483
- claim_from_onchain_tx : from_onchain ,
4484
- prev_channel_id ,
4485
- next_channel_id ,
4486
- outbound_amount_forwarded_msat : forwarded_htlc_value_msat ,
4487
- } } )
4519
+ Some ( MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
4520
+ event : events :: Event :: PaymentForwarded {
4521
+ fee_earned_msat ,
4522
+ claim_from_onchain_tx : from_onchain ,
4523
+ prev_channel_id : Some ( prev_outpoint . to_channel_id ( ) ) ,
4524
+ next_channel_id : Some ( next_channel_id ) ,
4525
+ outbound_amount_forwarded_msat : forwarded_htlc_value_msat ,
4526
+ } ,
4527
+ downstream_counterparty_and_funding_outpoint : None ,
4528
+ } )
4488
4529
} else { None }
4489
4530
} ) ;
4490
4531
if let Err ( ( pk, err) ) = res {
@@ -4511,8 +4552,13 @@ where
4511
4552
} , None ) ) ;
4512
4553
}
4513
4554
} ,
4514
- MonitorUpdateCompletionAction :: EmitEvent { event } => {
4555
+ MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
4556
+ event, downstream_counterparty_and_funding_outpoint
4557
+ } => {
4515
4558
self . pending_events . lock ( ) . unwrap ( ) . push_back ( ( event, None ) ) ;
4559
+ if let Some ( ( node_id, funding_outpoint, blocker) ) = downstream_counterparty_and_funding_outpoint {
4560
+ self . handle_monitor_update_release ( node_id, funding_outpoint, Some ( blocker) ) ;
4561
+ }
4516
4562
} ,
4517
4563
}
4518
4564
}
@@ -5359,6 +5405,36 @@ where
5359
5405
}
5360
5406
}
5361
5407
5408
+ fn raa_monitor_updates_held ( & self ,
5409
+ actions_blocking_raa_monitor_updates : & BTreeMap < [ u8 ; 32 ] , Vec < RAAMonitorUpdateBlockingAction > > ,
5410
+ channel_funding_outpoint : OutPoint , counterparty_node_id : PublicKey
5411
+ ) -> bool {
5412
+ actions_blocking_raa_monitor_updates
5413
+ . get ( & channel_funding_outpoint. to_channel_id ( ) ) . map ( |v| !v. is_empty ( ) ) . unwrap_or ( false )
5414
+ || self . pending_events . lock ( ) . unwrap ( ) . iter ( ) . any ( |( _, action) | {
5415
+ action == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
5416
+ channel_funding_outpoint,
5417
+ counterparty_node_id,
5418
+ } )
5419
+ } )
5420
+ }
5421
+
5422
+ pub ( crate ) fn test_raa_monitor_updates_held ( & self , counterparty_node_id : PublicKey ,
5423
+ channel_id : [ u8 ; 32 ] )
5424
+ -> bool {
5425
+ let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5426
+ if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
5427
+ let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
5428
+ let peer_state = & mut * peer_state_lck;
5429
+
5430
+ if let Some ( chan) = peer_state. channel_by_id . get ( & channel_id) {
5431
+ return self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
5432
+ chan. get_funding_txo ( ) . unwrap ( ) , counterparty_node_id) ;
5433
+ }
5434
+ }
5435
+ false
5436
+ }
5437
+
5362
5438
fn internal_revoke_and_ack ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) -> Result < ( ) , MsgHandleErrInternal > {
5363
5439
let ( htlcs_to_fail, res) = {
5364
5440
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
@@ -6023,25 +6099,29 @@ where
6023
6099
self . pending_outbound_payments . clear_pending_payments ( )
6024
6100
}
6025
6101
6026
- fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint ) {
6102
+ fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint , completed_blocker : Option < RAAMonitorUpdateBlockingAction > ) {
6027
6103
let mut errors = Vec :: new ( ) ;
6028
6104
loop {
6029
6105
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
6030
6106
if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
6031
6107
let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
6032
6108
let peer_state = & mut * peer_state_lck;
6033
- if self . pending_events . lock ( ) . unwrap ( ) . iter ( )
6034
- . any ( |( _ev, action_opt) | action_opt == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
6035
- channel_funding_outpoint, counterparty_node_id
6036
- } ) )
6037
- {
6038
- // Check that, while holding the peer lock, we don't have another event
6039
- // blocking any monitor updates for this channel. If we do, let those
6040
- // events be the ones that ultimately release the monitor update(s).
6041
- log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another event is pending" ,
6109
+
6110
+ if let Some ( blocker) = & completed_blocker {
6111
+ if let Some ( blockers) = peer_state. actions_blocking_raa_monitor_updates
6112
+ . get_mut ( & channel_funding_outpoint. to_channel_id ( ) )
6113
+ {
6114
+ blockers. retain ( |iter| iter != blocker) ;
6115
+ }
6116
+ }
6117
+
6118
+ if self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
6119
+ channel_funding_outpoint, counterparty_node_id) {
6120
+ log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another channel's mon update needs to complete first" ,
6042
6121
log_bytes!( & channel_funding_outpoint. to_channel_id( ) [ ..] ) ) ;
6043
6122
break ;
6044
6123
}
6124
+
6045
6125
if let hash_map:: Entry :: Occupied ( mut chan) = peer_state. channel_by_id . entry ( channel_funding_outpoint. to_channel_id ( ) ) {
6046
6126
debug_assert_eq ! ( chan. get( ) . get_funding_txo( ) . unwrap( ) , channel_funding_outpoint) ;
6047
6127
if let Some ( ( monitor_update, further_update_exists) ) = chan. get_mut ( ) . unblock_next_blocked_monitor_update ( ) {
@@ -6083,7 +6163,7 @@ where
6083
6163
EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
6084
6164
channel_funding_outpoint, counterparty_node_id
6085
6165
} => {
6086
- self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint) ;
6166
+ self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint, None ) ;
6087
6167
}
6088
6168
}
6089
6169
}
@@ -6759,6 +6839,7 @@ where
6759
6839
latest_features : init_msg. features . clone ( ) ,
6760
6840
pending_msg_events : Vec :: new ( ) ,
6761
6841
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
6842
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
6762
6843
is_connected : true ,
6763
6844
} ) ) ;
6764
6845
} ,
@@ -7955,6 +8036,7 @@ where
7955
8036
latest_features : Readable :: read ( reader) ?,
7956
8037
pending_msg_events : Vec :: new ( ) ,
7957
8038
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
8039
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
7958
8040
is_connected : false ,
7959
8041
} ;
7960
8042
per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
@@ -8036,7 +8118,7 @@ where
8036
8118
let mut claimable_htlc_purposes = None ;
8037
8119
let mut claimable_htlc_onion_fields = None ;
8038
8120
let mut pending_claiming_payments = Some ( HashMap :: new ( ) ) ;
8039
- let mut monitor_update_blocked_actions_per_peer = Some ( Vec :: new ( ) ) ;
8121
+ let mut monitor_update_blocked_actions_per_peer: Option < Vec < ( _ , BTreeMap < _ , Vec < _ > > ) > > = Some ( Vec :: new ( ) ) ;
8040
8122
let mut events_override = None ;
8041
8123
read_tlv_fields ! ( reader, {
8042
8124
( 1 , pending_outbound_payments_no_retry, option) ,
@@ -8361,7 +8443,21 @@ where
8361
8443
}
8362
8444
8363
8445
for ( node_id, monitor_update_blocked_actions) in monitor_update_blocked_actions_per_peer. unwrap ( ) {
8364
- if let Some ( peer_state) = per_peer_state. get_mut ( & node_id) {
8446
+ if let Some ( peer_state) = per_peer_state. get ( & node_id) {
8447
+ for ( _, actions) in monitor_update_blocked_actions. iter ( ) {
8448
+ for action in actions. iter ( ) {
8449
+ if let MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
8450
+ downstream_counterparty_and_funding_outpoint :
8451
+ Some ( ( blocked_node_id, blocked_channel_outpoint, blocking_action) ) , ..
8452
+ } = action {
8453
+ if let Some ( blocked_peer_state) = per_peer_state. get ( & blocked_node_id) {
8454
+ blocked_peer_state. lock ( ) . unwrap ( ) . actions_blocking_raa_monitor_updates
8455
+ . entry ( blocked_channel_outpoint. to_channel_id ( ) )
8456
+ . or_insert_with ( Vec :: new) . push ( blocking_action. clone ( ) ) ;
8457
+ }
8458
+ }
8459
+ }
8460
+ }
8365
8461
peer_state. lock ( ) . unwrap ( ) . monitor_update_blocked_actions = monitor_update_blocked_actions;
8366
8462
} else {
8367
8463
log_error ! ( args. logger, "Got blocked actions without a per-peer-state for {}" , node_id) ;
0 commit comments