@@ -520,12 +520,24 @@ pub(crate) enum MonitorUpdateCompletionAction {
520
520
/// event can be generated.
521
521
PaymentClaimed { payment_hash : PaymentHash } ,
522
522
/// Indicates an [`events::Event`] should be surfaced to the user.
523
- EmitEvent { event : events:: Event } ,
523
+ EmitEventAndFreeOtherChannel {
524
+ event : events:: Event ,
525
+ downstream_counterparty_and_funding_outpoint : Option < ( PublicKey , OutPoint , RAAMonitorUpdateBlockingAction ) > ,
526
+ } ,
524
527
}
525
528
526
529
impl_writeable_tlv_based_enum_upgradable ! ( MonitorUpdateCompletionAction ,
527
530
( 0 , PaymentClaimed ) => { ( 0 , payment_hash, required) } ,
528
- ( 2 , EmitEvent ) => { ( 0 , event, upgradable_required) } ,
531
+ ( 2 , EmitEventAndFreeOtherChannel ) => {
532
+ ( 0 , event, upgradable_required) ,
533
+ // LDK prior to 0.0.115 did not have this field as the monitor update application order was
534
+ // required by clients. If we downgrade to something prior to 0.0.115 this may result in
535
+ // monitor updates which aren't properly blocked or resumed, however that's fine - we don't
536
+ // support async monitor updates even in LDK 0.0.115 and once we do we'll require no
537
+ // downgrades to prior versions. Thus, while this would break on downgrade, we don't
538
+ // support it even without downgrade, so if it breaks its not on us ¯\_(ツ)_/¯.
539
+ ( 1 , downstream_counterparty_and_funding_outpoint, option) ,
540
+ } ,
529
541
) ;
530
542
531
543
#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -542,6 +554,29 @@ impl_writeable_tlv_based_enum!(EventCompletionAction,
542
554
} ;
543
555
) ;
544
556
557
+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
558
+ pub ( crate ) enum RAAMonitorUpdateBlockingAction {
559
+ /// The inbound channel's channel_id
560
+ ForwardedPaymentOtherChannelClaim {
561
+ channel_id : [ u8 ; 32 ] ,
562
+ htlc_id : u64 ,
563
+ } ,
564
+ }
565
+
566
+ impl RAAMonitorUpdateBlockingAction {
567
+ fn from_prev_hop_data ( prev_hop : & HTLCPreviousHopData ) -> Self {
568
+ Self :: ForwardedPaymentOtherChannelClaim {
569
+ channel_id : prev_hop. outpoint . to_channel_id ( ) ,
570
+ htlc_id : prev_hop. htlc_id ,
571
+ }
572
+ }
573
+ }
574
+
575
+ impl_writeable_tlv_based_enum ! ( RAAMonitorUpdateBlockingAction ,
576
+ ( 0 , ForwardedPaymentOtherChannelClaim ) => { ( 0 , channel_id, required) , ( 2 , htlc_id, required) }
577
+ ; ) ;
578
+
579
+
545
580
/// State we hold per-peer.
546
581
pub ( super ) struct PeerState < Signer : ChannelSigner > {
547
582
/// `temporary_channel_id` or `channel_id` -> `channel`.
@@ -570,6 +605,11 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
570
605
/// to funding appearing on-chain), the downstream `ChannelMonitor` set is required to ensure
571
606
/// duplicates do not occur, so such channels should fail without a monitor update completing.
572
607
monitor_update_blocked_actions : BTreeMap < [ u8 ; 32 ] , Vec < MonitorUpdateCompletionAction > > ,
608
+ /// If another channel's [`ChannelMonitorUpdate`] needs to complete before a channel we have
609
+ /// with this peer can complete an RAA [`ChannelMonitorUpdate`] (e.g. because the RAA update
610
+ /// will remove a preimage that needs to be durably in an upstream channel first), we put an
611
+ /// entry here to note that the channel with the key's ID is blocked on a set of actions.
612
+ actions_blocking_raa_monitor_updates : BTreeMap < [ u8 ; 32 ] , Vec < RAAMonitorUpdateBlockingAction > > ,
573
613
/// The peer is currently connected (i.e. we've seen a
574
614
/// [`ChannelMessageHandler::peer_connected`] and no corresponding
575
615
/// [`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 ( ) ;
@@ -6020,25 +6096,29 @@ where
6020
6096
self . pending_outbound_payments . clear_pending_payments ( )
6021
6097
}
6022
6098
6023
- fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint ) {
6099
+ fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint , completed_blocker : Option < RAAMonitorUpdateBlockingAction > ) {
6024
6100
let mut errors = Vec :: new ( ) ;
6025
6101
loop {
6026
6102
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
6027
6103
if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
6028
6104
let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
6029
6105
let peer_state = & mut * peer_state_lck;
6030
- if self . pending_events . lock ( ) . unwrap ( ) . iter ( )
6031
- . any ( |( _ev, action_opt) | action_opt == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
6032
- channel_funding_outpoint, counterparty_node_id
6033
- } ) )
6034
- {
6035
- // Check that, while holding the peer lock, we don't have another event
6036
- // blocking any monitor updates for this channel. If we do, let those
6037
- // events be the ones that ultimately release the monitor update(s).
6038
- log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another event is pending" ,
6106
+
6107
+ if let Some ( blocker) = & completed_blocker {
6108
+ if let Some ( blockers) = peer_state. actions_blocking_raa_monitor_updates
6109
+ . get_mut ( & channel_funding_outpoint. to_channel_id ( ) )
6110
+ {
6111
+ blockers. retain ( |iter| iter != blocker) ;
6112
+ }
6113
+ }
6114
+
6115
+ if self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
6116
+ channel_funding_outpoint, counterparty_node_id) {
6117
+ log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another channel's mon update needs to complete first" ,
6039
6118
log_bytes!( & channel_funding_outpoint. to_channel_id( ) [ ..] ) ) ;
6040
6119
break ;
6041
6120
}
6121
+
6042
6122
if let hash_map:: Entry :: Occupied ( mut chan) = peer_state. channel_by_id . entry ( channel_funding_outpoint. to_channel_id ( ) ) {
6043
6123
debug_assert_eq ! ( chan. get( ) . get_funding_txo( ) . unwrap( ) , channel_funding_outpoint) ;
6044
6124
if let Some ( ( monitor_update, further_update_exists) ) = chan. get_mut ( ) . unblock_next_blocked_monitor_update ( ) {
@@ -6080,7 +6160,7 @@ where
6080
6160
EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
6081
6161
channel_funding_outpoint, counterparty_node_id
6082
6162
} => {
6083
- self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint) ;
6163
+ self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint, None ) ;
6084
6164
}
6085
6165
}
6086
6166
}
@@ -6731,6 +6811,7 @@ where
6731
6811
latest_features : init_msg. features . clone ( ) ,
6732
6812
pending_msg_events : Vec :: new ( ) ,
6733
6813
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
6814
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
6734
6815
is_connected : true ,
6735
6816
} ) ) ;
6736
6817
} ,
@@ -7876,6 +7957,7 @@ where
7876
7957
latest_features : Readable :: read ( reader) ?,
7877
7958
pending_msg_events : Vec :: new ( ) ,
7878
7959
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
7960
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
7879
7961
is_connected : false ,
7880
7962
} ;
7881
7963
per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
@@ -7959,7 +8041,7 @@ where
7959
8041
let mut claimable_htlc_purposes = None ;
7960
8042
let mut claimable_htlc_onion_fields = None ;
7961
8043
let mut pending_claiming_payments = Some ( HashMap :: new ( ) ) ;
7962
- let mut monitor_update_blocked_actions_per_peer = Some ( Vec :: new ( ) ) ;
8044
+ let mut monitor_update_blocked_actions_per_peer: Option < Vec < ( _ , BTreeMap < _ , Vec < _ > > ) > > = Some ( Vec :: new ( ) ) ;
7963
8045
let mut events_override = None ;
7964
8046
read_tlv_fields ! ( reader, {
7965
8047
( 1 , pending_outbound_payments_no_retry, option) ,
@@ -8284,7 +8366,21 @@ where
8284
8366
}
8285
8367
8286
8368
for ( node_id, monitor_update_blocked_actions) in monitor_update_blocked_actions_per_peer. unwrap ( ) {
8287
- if let Some ( peer_state) = per_peer_state. get_mut ( & node_id) {
8369
+ if let Some ( peer_state) = per_peer_state. get ( & node_id) {
8370
+ for ( _, actions) in monitor_update_blocked_actions. iter ( ) {
8371
+ for action in actions. iter ( ) {
8372
+ if let MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
8373
+ downstream_counterparty_and_funding_outpoint :
8374
+ Some ( ( blocked_node_id, blocked_channel_outpoint, blocking_action) ) , ..
8375
+ } = action {
8376
+ if let Some ( blocked_peer_state) = per_peer_state. get ( & blocked_node_id) {
8377
+ blocked_peer_state. lock ( ) . unwrap ( ) . actions_blocking_raa_monitor_updates
8378
+ . entry ( blocked_channel_outpoint. to_channel_id ( ) )
8379
+ . or_insert_with ( Vec :: new) . push ( blocking_action. clone ( ) ) ;
8380
+ }
8381
+ }
8382
+ }
8383
+ }
8288
8384
peer_state. lock ( ) . unwrap ( ) . monitor_update_blocked_actions = monitor_update_blocked_actions;
8289
8385
} else {
8290
8386
log_error ! ( args. logger, "Got blocked actions without a per-peer-state for {}" , node_id) ;
0 commit comments