@@ -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`].
@@ -4397,20 +4437,24 @@ where
4397
4437
} ,
4398
4438
HTLCSource :: PreviousHopData ( hop_data) => {
4399
4439
let prev_outpoint = hop_data. outpoint ;
4440
+ let completed_blocker = RAAMonitorUpdateBlockingAction :: from_prev_hop_data ( & hop_data) ;
4400
4441
let res = self . claim_funds_from_hop ( hop_data, payment_preimage,
4401
4442
|htlc_claim_value_msat| {
4402
4443
if let Some ( forwarded_htlc_value) = forwarded_htlc_value_msat {
4403
4444
let fee_earned_msat = if let Some ( claimed_htlc_value) = htlc_claim_value_msat {
4404
4445
Some ( claimed_htlc_value - forwarded_htlc_value)
4405
4446
} else { None } ;
4406
4447
4407
- Some ( MonitorUpdateCompletionAction :: EmitEvent { event : events:: Event :: PaymentForwarded {
4408
- fee_earned_msat,
4409
- claim_from_onchain_tx : from_onchain,
4410
- prev_channel_id : Some ( prev_outpoint. to_channel_id ( ) ) ,
4411
- next_channel_id : Some ( next_channel_outpoint. to_channel_id ( ) ) ,
4412
- outbound_amount_forwarded_msat : forwarded_htlc_value_msat,
4413
- } } )
4448
+ Some ( MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
4449
+ event : events:: Event :: PaymentForwarded {
4450
+ fee_earned_msat,
4451
+ claim_from_onchain_tx : from_onchain,
4452
+ prev_channel_id : Some ( prev_outpoint. to_channel_id ( ) ) ,
4453
+ next_channel_id : Some ( next_channel_outpoint. to_channel_id ( ) ) ,
4454
+ outbound_amount_forwarded_msat : forwarded_htlc_value_msat,
4455
+ } ,
4456
+ downstream_counterparty_and_funding_outpoint : None ,
4457
+ } )
4414
4458
} else { None }
4415
4459
} ) ;
4416
4460
if let Err ( ( pk, err) ) = res {
@@ -4437,8 +4481,13 @@ where
4437
4481
} , None ) ) ;
4438
4482
}
4439
4483
} ,
4440
- MonitorUpdateCompletionAction :: EmitEvent { event } => {
4484
+ MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
4485
+ event, downstream_counterparty_and_funding_outpoint
4486
+ } => {
4441
4487
self . pending_events . lock ( ) . unwrap ( ) . push_back ( ( event, None ) ) ;
4488
+ if let Some ( ( node_id, funding_outpoint, blocker) ) = downstream_counterparty_and_funding_outpoint {
4489
+ self . handle_monitor_update_release ( node_id, funding_outpoint, Some ( blocker) ) ;
4490
+ }
4442
4491
} ,
4443
4492
}
4444
4493
}
@@ -5288,6 +5337,36 @@ where
5288
5337
}
5289
5338
}
5290
5339
5340
+ fn raa_monitor_updates_held ( & self ,
5341
+ actions_blocking_raa_monitor_updates : & BTreeMap < [ u8 ; 32 ] , Vec < RAAMonitorUpdateBlockingAction > > ,
5342
+ channel_funding_outpoint : OutPoint , counterparty_node_id : PublicKey
5343
+ ) -> bool {
5344
+ actions_blocking_raa_monitor_updates
5345
+ . get ( & channel_funding_outpoint. to_channel_id ( ) ) . map ( |v| !v. is_empty ( ) ) . unwrap_or ( false )
5346
+ || self . pending_events . lock ( ) . unwrap ( ) . iter ( ) . any ( |( _, action) | {
5347
+ action == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
5348
+ channel_funding_outpoint,
5349
+ counterparty_node_id,
5350
+ } )
5351
+ } )
5352
+ }
5353
+
5354
+ pub ( crate ) fn test_raa_monitor_updates_held ( & self , counterparty_node_id : PublicKey ,
5355
+ channel_id : [ u8 ; 32 ] )
5356
+ -> bool {
5357
+ let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5358
+ if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
5359
+ let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
5360
+ let peer_state = & mut * peer_state_lck;
5361
+
5362
+ if let Some ( chan) = peer_state. channel_by_id . get ( & channel_id) {
5363
+ return self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
5364
+ chan. get_funding_txo ( ) . unwrap ( ) , counterparty_node_id) ;
5365
+ }
5366
+ }
5367
+ false
5368
+ }
5369
+
5291
5370
fn internal_revoke_and_ack ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) -> Result < ( ) , MsgHandleErrInternal > {
5292
5371
let ( htlcs_to_fail, res) = {
5293
5372
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
@@ -5957,24 +6036,28 @@ where
5957
6036
self . pending_outbound_payments . clear_pending_payments ( )
5958
6037
}
5959
6038
5960
- fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint ) {
6039
+ fn handle_monitor_update_release ( & self , counterparty_node_id : PublicKey , channel_funding_outpoint : OutPoint , completed_blocker : Option < RAAMonitorUpdateBlockingAction > ) {
5961
6040
loop {
5962
6041
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5963
6042
if let Some ( peer_state_mtx) = per_peer_state. get ( & counterparty_node_id) {
5964
6043
let mut peer_state_lck = peer_state_mtx. lock ( ) . unwrap ( ) ;
5965
6044
let peer_state = & mut * peer_state_lck;
5966
- if self . pending_events . lock ( ) . unwrap ( ) . iter ( )
5967
- . any ( |( _ev, action_opt) | action_opt == & Some ( EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
5968
- channel_funding_outpoint, counterparty_node_id
5969
- } ) )
5970
- {
5971
- // Check that, while holding the peer lock, we don't have another event
5972
- // blocking any monitor updates for this channel. If we do, let those
5973
- // events be the ones that ultimately release the monitor update(s).
5974
- log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another event is pending" ,
6045
+
6046
+ if let Some ( blocker) = & completed_blocker {
6047
+ if let Some ( blockers) = peer_state. actions_blocking_raa_monitor_updates
6048
+ . get_mut ( & channel_funding_outpoint. to_channel_id ( ) )
6049
+ {
6050
+ blockers. retain ( |iter| iter != blocker) ;
6051
+ }
6052
+ }
6053
+
6054
+ if self . raa_monitor_updates_held ( & peer_state. actions_blocking_raa_monitor_updates ,
6055
+ channel_funding_outpoint, counterparty_node_id) {
6056
+ log_trace ! ( self . logger, "Delaying monitor unlock for channel {} as another channel's mon update needs to complete first" ,
5975
6057
log_bytes!( & channel_funding_outpoint. to_channel_id( ) [ ..] ) ) ;
5976
6058
return ;
5977
6059
}
6060
+
5978
6061
if let hash_map:: Entry :: Occupied ( mut chan) = peer_state. channel_by_id . entry ( channel_funding_outpoint. to_channel_id ( ) ) {
5979
6062
debug_assert_eq ! ( chan. get( ) . get_funding_txo( ) . unwrap( ) , channel_funding_outpoint) ;
5980
6063
if let Some ( ( monitor_update, further_update_exists) ) = chan. get_mut ( ) . unblock_next_blocked_monitor_update ( ) {
@@ -6011,7 +6094,7 @@ where
6011
6094
EventCompletionAction :: ReleaseRAAChannelMonitorUpdate {
6012
6095
channel_funding_outpoint, counterparty_node_id
6013
6096
} => {
6014
- self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint) ;
6097
+ self . handle_monitor_update_release ( counterparty_node_id, channel_funding_outpoint, None ) ;
6015
6098
}
6016
6099
}
6017
6100
}
@@ -6662,6 +6745,7 @@ where
6662
6745
latest_features : init_msg. features . clone ( ) ,
6663
6746
pending_msg_events : Vec :: new ( ) ,
6664
6747
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
6748
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
6665
6749
is_connected : true ,
6666
6750
} ) ) ;
6667
6751
} ,
@@ -7788,6 +7872,7 @@ where
7788
7872
latest_features : Readable :: read ( reader) ?,
7789
7873
pending_msg_events : Vec :: new ( ) ,
7790
7874
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
7875
+ actions_blocking_raa_monitor_updates : BTreeMap :: new ( ) ,
7791
7876
is_connected : false ,
7792
7877
} ;
7793
7878
per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
@@ -7870,7 +7955,7 @@ where
7870
7955
let mut probing_cookie_secret: Option < [ u8 ; 32 ] > = None ;
7871
7956
let mut claimable_htlc_purposes = None ;
7872
7957
let mut pending_claiming_payments = Some ( HashMap :: new ( ) ) ;
7873
- let mut monitor_update_blocked_actions_per_peer = Some ( Vec :: new ( ) ) ;
7958
+ let mut monitor_update_blocked_actions_per_peer: Option < Vec < ( _ , BTreeMap < _ , Vec < _ > > ) > > = Some ( Vec :: new ( ) ) ;
7874
7959
let mut events_override = None ;
7875
7960
read_tlv_fields ! ( reader, {
7876
7961
( 1 , pending_outbound_payments_no_retry, option) ,
@@ -8180,7 +8265,21 @@ where
8180
8265
}
8181
8266
8182
8267
for ( node_id, monitor_update_blocked_actions) in monitor_update_blocked_actions_per_peer. unwrap ( ) {
8183
- if let Some ( peer_state) = per_peer_state. get_mut ( & node_id) {
8268
+ if let Some ( peer_state) = per_peer_state. get ( & node_id) {
8269
+ for ( _, actions) in monitor_update_blocked_actions. iter ( ) {
8270
+ for action in actions. iter ( ) {
8271
+ if let MonitorUpdateCompletionAction :: EmitEventAndFreeOtherChannel {
8272
+ downstream_counterparty_and_funding_outpoint :
8273
+ Some ( ( blocked_node_id, blocked_channel_outpoint, blocking_action) ) , ..
8274
+ } = action {
8275
+ if let Some ( blocked_peer_state) = per_peer_state. get ( & blocked_node_id) {
8276
+ blocked_peer_state. lock ( ) . unwrap ( ) . actions_blocking_raa_monitor_updates
8277
+ . entry ( blocked_channel_outpoint. to_channel_id ( ) )
8278
+ . or_insert_with ( Vec :: new) . push ( blocking_action. clone ( ) ) ;
8279
+ }
8280
+ }
8281
+ }
8282
+ }
8184
8283
peer_state. lock ( ) . unwrap ( ) . monitor_update_blocked_actions = monitor_update_blocked_actions;
8185
8284
} else {
8186
8285
log_error ! ( args. logger, "Got blocked actions without a per-peer-state for {}" , node_id) ;
0 commit comments