@@ -754,13 +754,12 @@ macro_rules! handle_monitor_err {
754
754
( $self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr) => {
755
755
handle_monitor_err!( $self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, Vec :: new( ) , Vec :: new( ) )
756
756
} ;
757
- ( $self: ident, $err: expr, $channel_state : expr, $entry : expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
757
+ ( $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) => {
758
758
match $err {
759
759
ChannelMonitorUpdateErr :: PermanentFailure => {
760
- log_error!( $self. logger, "Closing channel {} due to monitor update PermanentFailure" , log_bytes!( $entry. key( ) [ ..] ) ) ;
761
- let ( channel_id, mut chan) = $entry. remove_entry( ) ;
762
- if let Some ( short_id) = chan. get_short_channel_id( ) {
763
- $channel_state. short_to_id. remove( & short_id) ;
760
+ log_error!( $self. logger, "Closing channel {} due to monitor update ChannelMonitorUpdateErr::PermanentFailure" , log_bytes!( $chan_id[ ..] ) ) ;
761
+ if let Some ( short_id) = $chan. get_short_channel_id( ) {
762
+ $short_to_id. remove( & short_id) ;
764
763
}
765
764
// TODO: $failed_fails is dropped here, which will cause other channels to hit the
766
765
// chain in a confused state! We need to move them into the ChannelMonitor which
@@ -771,12 +770,12 @@ macro_rules! handle_monitor_err {
771
770
// splitting hairs we'd prefer to claim payments that were to us, but we haven't
772
771
// given up the preimage yet, so might as well just wait until the payment is
773
772
// retried, avoiding the on-chain fees.
774
- 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( ) ) ) ;
775
- res
773
+ 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( ) ) ) ;
774
+ ( res, true )
776
775
} ,
777
776
ChannelMonitorUpdateErr :: TemporaryFailure => {
778
777
log_info!( $self. logger, "Disabling channel {} due to monitor update TemporaryFailure. On restore will send {} and process {} forwards and {} fails" ,
779
- log_bytes!( $entry . key ( ) [ ..] ) ,
778
+ log_bytes!( $chan_id [ ..] ) ,
780
779
if $resend_commitment && $resend_raa {
781
780
match $action_type {
782
781
RAACommitmentOrder :: CommitmentFirst => { "commitment then RAA" } ,
@@ -793,11 +792,18 @@ macro_rules! handle_monitor_err {
793
792
if !$resend_raa {
794
793
debug_assert!( $action_type == RAACommitmentOrder :: CommitmentFirst || !$resend_commitment) ;
795
794
}
796
- $entry . get_mut ( ) . monitor_update_failed( $resend_raa, $resend_commitment, $failed_forwards, $failed_fails) ;
797
- Err ( MsgHandleErrInternal :: from_chan_no_close( ChannelError :: Ignore ( "Failed to update ChannelMonitor" . to_owned( ) ) , * $entry . key ( ) ) )
795
+ $chan . monitor_update_failed( $resend_raa, $resend_commitment, $failed_forwards, $failed_fails) ;
796
+ ( Err ( MsgHandleErrInternal :: from_chan_no_close( ChannelError :: Ignore ( "Failed to update ChannelMonitor" . to_owned( ) ) , * $chan_id ) ) , false )
798
797
} ,
799
798
}
800
- }
799
+ } ;
800
+ ( $self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => { {
801
+ 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( ) ) ;
802
+ if drop {
803
+ $entry. remove_entry( ) ;
804
+ }
805
+ res
806
+ } } ;
801
807
}
802
808
803
809
macro_rules! return_monitor_err {
@@ -3303,6 +3309,57 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3303
3309
}
3304
3310
}
3305
3311
3312
+ /// Check the holding cell in each channel and free any pending HTLCs in them if possible.
3313
+ /// This should only apply to HTLCs which were added to the holding cell because we were
3314
+ /// waiting on a monitor update to finish. In that case, we don't want to free the holding cell
3315
+ /// directly in `channel_monitor_updated` as it may introduce deadlocks calling back into user
3316
+ /// code to inform them of a channel monitor update.
3317
+ fn check_free_holding_cells ( & self ) {
3318
+ let mut forwarding_failed_htlcs = Vec :: new ( ) ;
3319
+ let mut handle_errors = Vec :: new ( ) ;
3320
+ {
3321
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3322
+ let channel_state = & mut * channel_state_lock;
3323
+ let by_id = & mut channel_state. by_id ;
3324
+ let short_to_id = & mut channel_state. short_to_id ;
3325
+ let pending_msg_events = & mut channel_state. pending_msg_events ;
3326
+
3327
+ by_id. retain ( |channel_id, chan| {
3328
+ match chan. maybe_free_holding_cell_htlcs ( & self . logger ) {
3329
+ Ok ( ( None , ref htlcs) ) if htlcs. is_empty ( ) => true ,
3330
+ Ok ( ( commitment_opt, failed_htlcs) ) => {
3331
+ forwarding_failed_htlcs. push ( ( failed_htlcs, * channel_id) ) ;
3332
+ if let Some ( ( commitment_update, monitor_update) ) = commitment_opt {
3333
+ if let Err ( e) = self . chain_monitor . update_channel ( chan. get_funding_txo ( ) . unwrap ( ) , monitor_update) {
3334
+ let ( res, close_channel) = handle_monitor_err ! ( self , e, short_to_id, chan, RAACommitmentOrder :: CommitmentFirst , false , true , Vec :: new( ) , Vec :: new( ) , channel_id) ;
3335
+ handle_errors. push ( ( chan. get_counterparty_node_id ( ) , res) ) ;
3336
+ if close_channel { return false ; }
3337
+ } else {
3338
+ pending_msg_events. push ( events:: MessageSendEvent :: UpdateHTLCs {
3339
+ node_id : chan. get_counterparty_node_id ( ) ,
3340
+ updates : commitment_update,
3341
+ } ) ;
3342
+ }
3343
+ }
3344
+ true
3345
+ } ,
3346
+ Err ( e) => {
3347
+ let ( close_channel, res) = convert_chan_err ! ( self , e, short_to_id, chan, channel_id) ;
3348
+ handle_errors. push ( ( chan. get_counterparty_node_id ( ) , Err ( res) ) ) ;
3349
+ !close_channel
3350
+ }
3351
+ }
3352
+ } ) ;
3353
+ }
3354
+ for ( failures, channel_id) in forwarding_failed_htlcs. drain ( ..) {
3355
+ self . fail_holding_cell_htlcs ( failures, channel_id) ;
3356
+ }
3357
+
3358
+ for ( counterparty_node_id, err) in handle_errors. drain ( ..) {
3359
+ let _ = handle_error ! ( self , err, counterparty_node_id) ;
3360
+ }
3361
+ }
3362
+
3306
3363
/// Handle a list of channel failures during a block_connected or block_disconnected call,
3307
3364
/// pushing the channel monitor update (if any) to the background events queue and removing the
3308
3365
/// Channel object.
@@ -3339,6 +3396,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> MessageSend
3339
3396
// ChannelMonitors when clearing other events.
3340
3397
self . process_pending_monitor_events ( ) ;
3341
3398
3399
+ self . check_free_holding_cells ( ) ;
3400
+
3342
3401
let mut ret = Vec :: new ( ) ;
3343
3402
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
3344
3403
mem:: swap ( & mut ret, & mut channel_state. pending_msg_events ) ;
0 commit comments