@@ -827,13 +827,12 @@ macro_rules! handle_monitor_err {
827
827
( $self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
828
828
handle_monitor_err!( $self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, Vec :: new( ) , Vec :: new( ) )
829
829
} ;
830
- ( $self: ident, $err: expr, $channel_state : expr, $entry : expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
830
+ ( $self: ident, $err: expr, $short_to_id : expr, $chan : expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr , $chan_id : expr) => {
831
831
match $err {
832
832
ChannelMonitorUpdateErr :: PermanentFailure => {
833
- log_error!( $self. logger, "Closing channel {} due to monitor update PermanentFailure" , log_bytes!( $entry. key( ) [ ..] ) ) ;
834
- let ( channel_id, mut chan) = $entry. remove_entry( ) ;
835
- if let Some ( short_id) = chan. get_short_channel_id( ) {
836
- $channel_state. short_to_id. remove( & short_id) ;
833
+ log_error!( $self. logger, "Closing channel {} due to monitor update ChannelMonitorUpdateErr::PermanentFailure" , log_bytes!( $chan_id[ ..] ) ) ;
834
+ if let Some ( short_id) = $chan. get_short_channel_id( ) {
835
+ $short_to_id. remove( & short_id) ;
837
836
}
838
837
// TODO: $failed_fails is dropped here, which will cause other channels to hit the
839
838
// chain in a confused state! We need to move them into the ChannelMonitor which
@@ -844,12 +843,12 @@ macro_rules! handle_monitor_err {
844
843
// splitting hairs we'd prefer to claim payments that were to us, but we haven't
845
844
// given up the preimage yet, so might as well just wait until the payment is
846
845
// retried, avoiding the on-chain fees.
847
- let res: Result <( ) , _> = Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" . to_owned( ) , channel_id , chan. force_shutdown( true ) , $self. get_channel_update( & chan) . ok( ) ) ) ;
848
- res
846
+ let res: Result <( ) , _> = Err ( MsgHandleErrInternal :: from_finish_shutdown( "ChannelMonitor storage failure" . to_owned( ) , * $chan_id , $ chan. force_shutdown( true ) , $self. get_channel_update( & $ chan) . ok( ) ) ) ;
847
+ ( res, true )
849
848
} ,
850
849
ChannelMonitorUpdateErr :: TemporaryFailure => {
851
850
log_info!( $self. logger, "Disabling channel {} due to monitor update TemporaryFailure. On restore will send {} and process {} forwards and {} fails" ,
852
- log_bytes!( $entry . key ( ) [ ..] ) ,
851
+ log_bytes!( $chan_id [ ..] ) ,
853
852
if $resend_commitment && $resend_raa {
854
853
match $action_type {
855
854
RAACommitmentOrder :: CommitmentFirst => { "commitment then RAA" } ,
@@ -866,11 +865,18 @@ macro_rules! handle_monitor_err {
866
865
if !$resend_raa {
867
866
debug_assert!( $action_type == RAACommitmentOrder :: CommitmentFirst || !$resend_commitment) ;
868
867
}
869
- $entry . get_mut ( ) . monitor_update_failed( $resend_raa, $resend_commitment, $failed_forwards, $failed_fails) ;
870
- Err ( MsgHandleErrInternal :: from_chan_no_close( ChannelError :: Ignore ( "Failed to update ChannelMonitor" . to_owned( ) ) , * $entry . key ( ) ) )
868
+ $chan . monitor_update_failed( $resend_raa, $resend_commitment, $failed_forwards, $failed_fails) ;
869
+ ( Err ( MsgHandleErrInternal :: from_chan_no_close( ChannelError :: Ignore ( "Failed to update ChannelMonitor" . to_owned( ) ) , * $chan_id ) ) , false )
871
870
} ,
872
871
}
873
- }
872
+ } ;
873
+ ( $self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => { {
874
+ let ( res, drop) = handle_monitor_err!( $self, $err, $channel_state. short_to_id, $entry. get_mut( ) , $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails, $entry. key( ) ) ;
875
+ if drop {
876
+ $entry. remove_entry( ) ;
877
+ }
878
+ res
879
+ } } ;
874
880
}
875
881
876
882
macro_rules! return_monitor_err {
@@ -3490,6 +3496,57 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3490
3496
}
3491
3497
}
3492
3498
3499
+ /// Check the holding cell in each channel and free any pending HTLCs in them if possible.
3500
+ /// This should only apply to HTLCs which were added to the holding cell because we were
3501
+ /// waiting on a monitor update to finish. In that case, we don't want to free the holding cell
3502
+ /// directly in `channel_monitor_updated` as it may introduce deadlocks calling back into user
3503
+ /// code to inform them of a channel monitor update.
3504
+ fn check_free_holding_cells ( & self ) {
3505
+ let mut failed_htlcs = Vec :: new ( ) ;
3506
+ let mut handle_errors = Vec :: new ( ) ;
3507
+ {
3508
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3509
+ let channel_state = & mut * channel_state_lock;
3510
+ let by_id = & mut channel_state. by_id ;
3511
+ let short_to_id = & mut channel_state. short_to_id ;
3512
+ let pending_msg_events = & mut channel_state. pending_msg_events ;
3513
+
3514
+ by_id. retain ( |channel_id, chan| {
3515
+ match chan. maybe_free_holding_cell_htlcs ( & self . logger ) {
3516
+ Ok ( ( None , ref htlcs) ) if htlcs. is_empty ( ) => true ,
3517
+ Ok ( ( commitment_opt, holding_cell_failed_htlcs) ) => {
3518
+ failed_htlcs. push ( ( holding_cell_failed_htlcs, * channel_id) ) ;
3519
+ if let Some ( ( commitment_update, monitor_update) ) = commitment_opt {
3520
+ if let Err ( e) = self . chain_monitor . update_channel ( chan. get_funding_txo ( ) . unwrap ( ) , monitor_update) {
3521
+ let ( res, close_channel) = handle_monitor_err ! ( self , e, short_to_id, chan, RAACommitmentOrder :: CommitmentFirst , false , true , Vec :: new( ) , Vec :: new( ) , channel_id) ;
3522
+ handle_errors. push ( ( chan. get_counterparty_node_id ( ) , res) ) ;
3523
+ if close_channel { return false ; }
3524
+ } else {
3525
+ pending_msg_events. push ( events:: MessageSendEvent :: UpdateHTLCs {
3526
+ node_id : chan. get_counterparty_node_id ( ) ,
3527
+ updates : commitment_update,
3528
+ } ) ;
3529
+ }
3530
+ }
3531
+ true
3532
+ } ,
3533
+ Err ( e) => {
3534
+ let ( close_channel, res) = convert_chan_err ! ( self , e, short_to_id, chan, channel_id) ;
3535
+ handle_errors. push ( ( chan. get_counterparty_node_id ( ) , Err ( res) ) ) ;
3536
+ !close_channel
3537
+ }
3538
+ }
3539
+ } ) ;
3540
+ }
3541
+ for ( failures, channel_id) in failed_htlcs. drain ( ..) {
3542
+ self . fail_holding_cell_htlcs ( failures, channel_id) ;
3543
+ }
3544
+
3545
+ for ( counterparty_node_id, err) in handle_errors. drain ( ..) {
3546
+ let _ = handle_error ! ( self , err, counterparty_node_id) ;
3547
+ }
3548
+ }
3549
+
3493
3550
/// Handle a list of channel failures during a block_connected or block_disconnected call,
3494
3551
/// pushing the channel monitor update (if any) to the background events queue and removing the
3495
3552
/// Channel object.
@@ -3626,6 +3683,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> MessageSend
3626
3683
// ChannelMonitors when clearing other events.
3627
3684
self . process_pending_monitor_events ( ) ;
3628
3685
3686
+ self . check_free_holding_cells ( ) ;
3687
+
3629
3688
let mut ret = Vec :: new ( ) ;
3630
3689
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
3631
3690
mem:: swap ( & mut ret, & mut channel_state. pending_msg_events ) ;
0 commit comments