@@ -504,12 +504,24 @@ pub(crate) enum MonitorUpdateCompletionAction {
504
504
/// event can be generated.
505
505
PaymentClaimed { payment_hash : PaymentHash } ,
506
506
/// Indicates an [`events::Event`] should be surfaced to the user.
507
- EmitEvent { event : events:: Event } ,
507
+ EmitEventAndFreeOtherChannel {
508
+ event : events:: Event ,
509
+ downstream_counterparty_and_funding_outpoint : Option < ( PublicKey , OutPoint , RAAMonitorUpdateBlockingAction ) > ,
510
+ } ,
508
511
}
509
512
510
513
impl_writeable_tlv_based_enum_upgradable ! ( MonitorUpdateCompletionAction ,
511
514
( 0 , PaymentClaimed ) => { ( 0 , payment_hash, required) } ,
512
- ( 2 , EmitEvent ) => { ( 0 , event, upgradable_required) } ,
515
+ ( 2 , EmitEventAndFreeOtherChannel ) => {
516
+ ( 0 , event, upgradable_required) ,
517
+ // LDK prior to 0.0.115 did not have this field as the monitor update application order was
518
+ // required by clients. If we downgrade to something prior to 0.0.115 this may result in
519
+ // monitor updates which aren't properly blocked or resumed, however that's fine - we don't
520
+ // support async monitor updates even in LDK 0.0.115 and once we do we'll require no
521
+ // downgrades to prior versions. Thus, while this would break on downgrade, we don't
522
+ // support it even without downgrade, so if it breaks its not on us ¯\_(ツ)_/¯.
523
+ ( 1 , downstream_counterparty_and_funding_outpoint, option) ,
524
+ } ,
513
525
) ;
514
526
515
527
#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -526,6 +538,29 @@ impl_writeable_tlv_based_enum!(EventCompletionAction,
526
538
} ;
527
539
) ;
528
540
541
+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
542
+ pub ( crate ) enum RAAMonitorUpdateBlockingAction {
543
+ /// The inbound channel's channel_id
544
+ ForwardedPaymentOtherChannelClaim {
545
+ channel_id : [ u8 ; 32 ] ,
546
+ htlc_id : u64 ,
547
+ } ,
548
+ }
549
+
550
+ impl RAAMonitorUpdateBlockingAction {
551
+ fn from_prev_hop_data ( prev_hop : & HTLCPreviousHopData ) -> Self {
552
+ Self :: ForwardedPaymentOtherChannelClaim {
553
+ channel_id : prev_hop. outpoint . to_channel_id ( ) ,
554
+ htlc_id : prev_hop. htlc_id ,
555
+ }
556
+ }
557
+ }
558
+
559
+ impl_writeable_tlv_based_enum ! ( RAAMonitorUpdateBlockingAction ,
560
+ ( 0 , ForwardedPaymentOtherChannelClaim ) => { ( 0 , channel_id, required) , ( 2 , htlc_id, required) }
561
+ ; ) ;
562
+
563
+
529
564
/// State we hold per-peer.
530
565
pub ( super ) struct PeerState < Signer : ChannelSigner > {
531
566
/// `temporary_channel_id` or `channel_id` -> `channel`.
@@ -554,6 +589,11 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
554
589
/// to funding appearing on-chain), the downstream `ChannelMonitor` set is required to ensure
555
590
/// duplicates do not occur, so such channels should fail without a monitor update completing.
556
591
monitor_update_blocked_actions : BTreeMap < [ u8 ; 32 ] , Vec < MonitorUpdateCompletionAction > > ,
592
+ /// If another channel's [`ChannelMonitorUpdate`] needs to complete before a channel we have
593
+ /// with this peer can complete an RAA [`ChannelMonitorUpdate`] (e.g. because the RAA update
594
+ /// will remove a preimage that needs to be durably in an upstream channel first), we put an
595
+ /// entry here to note that the channel with the key's ID is blocked on a set of actions.
596
+ actions_blocking_raa_monitor_updates : BTreeMap < [ u8 ; 32 ] , Vec < RAAMonitorUpdateBlockingAction > > ,
557
597
/// The peer is currently connected (i.e. we've seen a
558
598
/// [`ChannelMessageHandler::peer_connected`] and no corresponding
559
599
/// [`ChannelMessageHandler::peer_disconnected`].
@@ -4386,23 +4426,24 @@ where
4386
4426
} ,
4387
4427
HTLCSource :: PreviousHopData ( hop_data) => {
4388
4428
let prev_outpoint = hop_data. outpoint ;
4429
+ let completed_blocker = RAAMonitorUpdateBlockingAction :: from_prev_hop_data ( & hop_data) ;
4389
4430
let res = self . claim_funds_from_hop ( hop_data, payment_preimage,
4390
4431
|htlc_claim_value_msat| {
4391
4432
if let Some ( forwarded_htlc_value) = forwarded_htlc_value_msat {
4392
4433
let fee_earned_msat = if let Some ( claimed_htlc_value) = htlc_claim_value_msat {
4393
4434
Some ( claimed_htlc_value - forwarded_htlc_value)
4394
4435
} else { None } ;
4395
4436
4396
- let prev_channel_id = Some ( prev_outpoint . to_channel_id ( ) ) ;
4397
- let next_channel_id = Some ( next_channel_id ) ;
4398
-
4399
- Some ( MonitorUpdateCompletionAction :: EmitEvent { event : events :: Event :: PaymentForwarded {
4400
- fee_earned_msat ,
4401
- claim_from_onchain_tx : from_onchain ,
4402
- prev_channel_id ,
4403
- next_channel_id ,
4404
- outbound_amount_forwarded_msat : forwarded_htlc_value_msat ,
4405
- } } )
4437
+ Some ( MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
4438
+ event : events :: Event :: PaymentForwarded {
4439
+ fee_earned_msat ,
4440
+ claim_from_onchain_tx : from_onchain ,
4441
+ prev_channel_id : Some ( prev_outpoint . to_channel_id ( ) ) ,
4442
+ next_channel_id : Some ( next_channel_id ) ,
4443
+ outbound_amount_forwarded_msat : forwarded_htlc_value_msat ,
4444
+ } ,
4445
+ downstream_counterparty_and_funding_outpoint : None ,
4446
+ } )
4406
4447
} else { None }
4407
4448
} ) ;
4408
4449
if let Err ( ( pk, err) ) = res {
@@ -4429,8 +4470,13 @@ where
4429
4470
} , None ) ) ;
4430
4471
}
4431
4472
} ,
4432
- MonitorUpdateCompletionAction :: EmitEvent { event } => {
4473
+ MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
4474
+ event, downstream_counterparty_and_funding_outpoint
4475
+ } => {
4433
4476
self . pending_events . lock ( ) . unwrap ( ) . push_back ( ( event, None ) ) ;
4477
+ if let Some ( ( node_id, funding_outpoint, blocker) ) = downstream_counterparty_and_funding_outpoint {
4478
+ self . handle_monitor_update_release ( node_id, funding_outpoint, Some ( blocker) ) ;
4479
+ }
4434
4480
} ,
4435
4481
}
4436
4482
}
@@ -5277,6 +5323,36 @@ where
5277
5323
}
5278
5324
}
5279
5325
5326
+ fn raa_monitor_updates_held ( & self ,
5327
+ actions_blocking_raa_monitor_updates : & BTreeMap < [ u8 ; 32 ] , Vec < RAAMonitorUpdateBlockingAction > > ,
5328
+ channel_funding_outpoint : OutPoint , counterparty_node_id : PublicKey
5329
+ ) -> bool {
5330
+ actions_blocking_raa_monitor_updates
5331
+ . get ( & channel_funding_outpoint. to_channel_id ( ) ) . map ( |v| !v. is_empty ( ) ) . unwrap_or ( false )
5332
+ || self . pending_events . lock ( ) . unwrap ( ) . iter ( ) . any ( |( _, action) | {
5333
+ action == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
5334
+ channel_funding_outpoint,
5335
+ counterparty_node_id,
5336
+ } )
5337
+ } )
5338
+ }
5339
+
5340
+ pub ( crate ) fn test_raa_monitor_updates_held ( & self , counterparty_node_id : PublicKey ,
5341
+ channel_id : [ u8 ; 32 ] )
5342
+ -> bool {
5343
+ let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5344
+ if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
5345
+ let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
5346
+ let peer_state = & mut * peer_state_lck;
5347
+
5348
+ if let Some ( chan) = peer_state. channel_by_id . get ( & channel_id) {
5349
+ return self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
5350
+ chan. get_funding_txo ( ) . unwrap ( ) , counterparty_node_id) ;
5351
+ }
5352
+ }
5353
+ false
5354
+ }
5355
+
5280
5356
fn internal_revoke_and_ack ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) -> Result < ( ) , MsgHandleErrInternal > {
5281
5357
let ( htlcs_to_fail, res) = {
5282
5358
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
@@ -5939,24 +6015,28 @@ where
5939
6015
self . pending_outbound_payments . clear_pending_payments ( )
5940
6016
}
5941
6017
5942
- fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint ) {
6018
+ fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint , completed_blocker : Option < RAAMonitorUpdateBlockingAction > ) {
5943
6019
loop {
5944
6020
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5945
6021
if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
5946
6022
let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
5947
6023
let peer_state = & mut * peer_state_lck;
5948
- if self . pending_events . lock ( ) . unwrap ( ) . iter ( )
5949
- . any ( |( _ev, action_opt) | action_opt == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
5950
- channel_funding_outpoint, counterparty_node_id
5951
- } ) )
5952
- {
5953
- // Check that, while holding the peer lock, we don't have another event
5954
- // blocking any monitor updates for this channel. If we do, let those
5955
- // events be the ones that ultimately release the monitor update(s).
5956
- log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another event is pending" ,
6024
+
6025
+ if let Some ( blocker) = & completed_blocker {
6026
+ if let Some ( blockers) = peer_state. actions_blocking_raa_monitor_updates
6027
+ . get_mut ( & channel_funding_outpoint. to_channel_id ( ) )
6028
+ {
6029
+ blockers. retain ( |iter| iter != blocker) ;
6030
+ }
6031
+ }
6032
+
6033
+ if self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
6034
+ channel_funding_outpoint, counterparty_node_id) {
6035
+ log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another channel's mon update needs to complete first" ,
5957
6036
log_bytes!( & channel_funding_outpoint. to_channel_id( ) [ ..] ) ) ;
5958
6037
return ;
5959
6038
}
6039
+
5960
6040
if let hash_map:: Entry :: Occupied ( mut chan) = peer_state. channel_by_id . entry ( channel_funding_outpoint. to_channel_id ( ) ) {
5961
6041
debug_assert_eq ! ( chan. get( ) . get_funding_txo( ) . unwrap( ) , channel_funding_outpoint) ;
5962
6042
if let Some ( ( monitor_update, further_update_exists) ) = chan. get_mut ( ) . unblock_next_blocked_monitor_update ( ) {
@@ -5993,7 +6073,7 @@ where
5993
6073
EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
5994
6074
channel_funding_outpoint, counterparty_node_id
5995
6075
} => {
5996
- self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint) ;
6076
+ self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint, None ) ;
5997
6077
}
5998
6078
}
5999
6079
}
@@ -6644,6 +6724,7 @@ where
6644
6724
latest_features : init_msg. features . clone ( ) ,
6645
6725
pending_msg_events : Vec :: new ( ) ,
6646
6726
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
6727
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
6647
6728
is_connected : true ,
6648
6729
} ) ) ;
6649
6730
} ,
@@ -7770,6 +7851,7 @@ where
7770
7851
latest_features : Readable :: read ( reader) ?,
7771
7852
pending_msg_events : Vec :: new ( ) ,
7772
7853
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
7854
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
7773
7855
is_connected : false ,
7774
7856
} ;
7775
7857
per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
@@ -7852,7 +7934,7 @@ where
7852
7934
let mut probing_cookie_secret: Option < [ u8 ; 32 ] > = None ;
7853
7935
let mut claimable_htlc_purposes = None ;
7854
7936
let mut pending_claiming_payments = Some ( HashMap :: new ( ) ) ;
7855
- let mut monitor_update_blocked_actions_per_peer = Some ( Vec :: new ( ) ) ;
7937
+ let mut monitor_update_blocked_actions_per_peer: Option < Vec < ( _ , BTreeMap < _ , Vec < _ > > ) > > = Some ( Vec :: new ( ) ) ;
7856
7938
let mut events_override = None ;
7857
7939
read_tlv_fields ! ( reader, {
7858
7940
( 1 , pending_outbound_payments_no_retry, option) ,
@@ -8156,7 +8238,21 @@ where
8156
8238
}
8157
8239
8158
8240
for ( node_id, monitor_update_blocked_actions) in monitor_update_blocked_actions_per_peer. unwrap ( ) {
8159
- if let Some ( peer_state) = per_peer_state. get_mut ( & node_id) {
8241
+ if let Some ( peer_state) = per_peer_state. get ( & node_id) {
8242
+ for ( _, actions) in monitor_update_blocked_actions. iter ( ) {
8243
+ for action in actions. iter ( ) {
8244
+ if let MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
8245
+ downstream_counterparty_and_funding_outpoint :
8246
+ Some ( ( blocked_node_id, blocked_channel_outpoint, blocking_action) ) , ..
8247
+ } = action {
8248
+ if let Some ( blocked_peer_state) = per_peer_state. get ( & blocked_node_id) {
8249
+ blocked_peer_state. lock ( ) . unwrap ( ) . actions_blocking_raa_monitor_updates
8250
+ . entry ( blocked_channel_outpoint. to_channel_id ( ) )
8251
+ . or_insert_with ( Vec :: new) . push ( blocking_action. clone ( ) ) ;
8252
+ }
8253
+ }
8254
+ }
8255
+ }
8160
8256
peer_state. lock ( ) . unwrap ( ) . monitor_update_blocked_actions = monitor_update_blocked_actions;
8161
8257
} else {
8162
8258
log_error ! ( args. logger, "Got blocked actions without a per-peer-state for {}" , node_id) ;
0 commit comments