@@ -609,7 +609,32 @@ macro_rules! try_chan_entry {
609
609
return Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, shutdown_res, $self. get_channel_update( & chan) . ok( ) ) )
610
610
}
611
611
}
612
- }
612
+ } ;
613
+ ( $self: ident, $res: expr, $channel_state: expr, $entry: expr, $optional: expr) => {
614
+ match $res {
615
+ Ok ( res) => res,
616
+ Err ( ChannelError :: Ignore ( msg) ) => {
617
+ return ( $optional, Err ( MsgHandleErrInternal :: from_chan_no_close( ChannelError :: Ignore ( msg) , $entry. key( ) . clone( ) ) ) )
618
+ } ,
619
+ Err ( ChannelError :: Close ( msg) ) => {
620
+ log_trace!( $self. logger, "Closing channel {} due to Close-required error: {}" , log_bytes!( $entry. key( ) [ ..] ) , msg) ;
621
+ let ( channel_id, mut chan) = $entry. remove_entry( ) ;
622
+ if let Some ( short_id) = chan. get_short_channel_id( ) {
623
+ $channel_state. short_to_id. remove( & short_id) ;
624
+ }
625
+ return ( $optional, Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, chan. force_shutdown( true ) , $self. get_channel_update( & chan) . ok( ) ) ) )
626
+ } ,
627
+ Err ( ChannelError :: CloseDelayBroadcast ( msg) ) => {
628
+ log_error!( $self. logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}" , log_bytes!( $entry. key( ) [ ..] ) , msg) ;
629
+ let ( channel_id, mut chan) = $entry. remove_entry( ) ;
630
+ if let Some ( short_id) = chan. get_short_channel_id( ) {
631
+ $channel_state. short_to_id. remove( & short_id) ;
632
+ }
633
+ let shutdown_res = chan. force_shutdown( false ) ;
634
+ return ( $optional, Err ( MsgHandleErrInternal :: from_finish_shutdown( msg, channel_id, shutdown_res, $self. get_channel_update( & chan) . ok( ) ) ) )
635
+ }
636
+ }
637
+ } ;
613
638
}
614
639
615
640
macro_rules! handle_monitor_err {
@@ -668,6 +693,9 @@ macro_rules! return_monitor_err {
668
693
} ;
669
694
( $self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr) => {
670
695
return handle_monitor_err!( $self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails) ;
696
+ } ;
697
+ ( $self: ident, $err: expr, $channel_state: expr, $entry: expr, $action_type: path, $resend_raa: expr, $resend_commitment: expr, $failed_forwards: expr, $failed_fails: expr, $extra_rv: expr) => {
698
+ return ( $extra_rv, handle_monitor_err!( $self, $err, $channel_state, $entry, $action_type, $resend_raa, $resend_commitment, $failed_forwards, $failed_fails) ) ;
671
699
}
672
700
}
673
701
@@ -1825,7 +1853,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
1825
1853
// Fail a list of HTLCs that were just freed from the holding cell. The HTLCs need to be
1826
1854
// failed backwards or, if they were one of our outgoing HTLCs, then their failure needs to
1827
1855
// be surfaced to the user.
1828
- fn fail_holding_cell_htlcs ( & self , mut htlcs_to_fail : Vec < ( HTLCSource , PaymentHash ) > , channel_id : [ u8 ; 32 ] ) -> Result < ( ) , MsgHandleErrInternal > {
1856
+ fn fail_holding_cell_htlcs ( & self , mut htlcs_to_fail : Vec < ( HTLCSource , PaymentHash ) > , channel_id : [ u8 ; 32 ] ) {
1829
1857
for ( htlc_src, payment_hash) in htlcs_to_fail. drain ( ..) {
1830
1858
match htlc_src {
1831
1859
HTLCSource :: PreviousHopData ( HTLCPreviousHopData { .. } ) => {
@@ -1858,7 +1886,6 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
1858
1886
} ,
1859
1887
} ;
1860
1888
}
1861
- Ok ( ( ) )
1862
1889
}
1863
1890
1864
1891
/// Fails an HTLC backwards to the sender of it to us.
@@ -2708,40 +2735,24 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
2708
2735
}
2709
2736
}
2710
2737
2711
- fn internal_revoke_and_ack ( & self , their_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) -> Result < ( ) , MsgHandleErrInternal > {
2712
- let ( commitment_update , pending_forwards, mut pending_failures, closing_signed , monitor_update , htlcs_to_fail) = {
2738
+ fn internal_revoke_and_ack ( & self , their_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) -> ( Vec < ( HTLCSource , PaymentHash ) > , Result < ( ) , MsgHandleErrInternal > ) {
2739
+ let ( pending_forwards, mut pending_failures, short_channel_id , htlcs_to_fail) = {
2713
2740
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2714
2741
let channel_state = & mut * channel_state_lock;
2715
2742
match channel_state. by_id . entry ( msg. channel_id ) {
2716
2743
hash_map:: Entry :: Occupied ( mut chan) => {
2717
2744
if chan. get ( ) . get_their_node_id ( ) != * their_node_id {
2718
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" . to_owned ( ) , msg. channel_id ) ) ;
2745
+ return ( Vec :: new ( ) , Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" . to_owned ( ) , msg. channel_id ) ) ) ;
2719
2746
}
2720
- try_chan_entry ! ( self , chan. get_mut( ) . revoke_and_ack( & msg, & self . fee_estimator, & self . logger) , channel_state, chan)
2721
- } ,
2722
- hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" . to_owned ( ) , msg. channel_id ) )
2723
- }
2724
- } ;
2725
-
2726
- // Failing back holding cell HTLCs requires acquiring the channel state
2727
- // lock, hence why this processing can't be done above.
2728
- if let Err ( e) = self . fail_holding_cell_htlcs ( htlcs_to_fail, msg. channel_id ) { return Err ( e) } ;
2729
-
2730
- // Updating the corresponding ChannelMonitor may result in returning early,
2731
- // hence why we couldn't do this processing when we originally acquired the
2732
- // lock above.
2733
- let short_channel_id = {
2734
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2735
- let channel_state = & mut * channel_state_lock;
2736
- match channel_state. by_id . entry ( msg. channel_id ) {
2737
- hash_map:: Entry :: Occupied ( mut chan) => {
2738
2747
let was_frozen_for_monitor = chan. get ( ) . is_awaiting_monitor_update ( ) ;
2748
+ let ( commitment_update, pending_forwards, pending_failures, closing_signed, monitor_update, htlcs_to_fail) =
2749
+ try_chan_entry ! ( self , chan. get_mut( ) . revoke_and_ack( & msg, & self . fee_estimator, & self . logger) , channel_state, chan, Vec :: new( ) ) ;
2739
2750
if let Err ( e) = self . monitor . update_monitor ( chan. get ( ) . get_funding_txo ( ) . unwrap ( ) , monitor_update) {
2740
2751
if was_frozen_for_monitor {
2741
2752
assert ! ( commitment_update. is_none( ) && closing_signed. is_none( ) && pending_forwards. is_empty( ) && pending_failures. is_empty( ) ) ;
2742
- return Err ( MsgHandleErrInternal :: ignore_no_close ( "Previous monitor update failure prevented responses to RAA" . to_owned ( ) ) ) ;
2753
+ return ( htlcs_to_fail , Err ( MsgHandleErrInternal :: ignore_no_close ( "Previous monitor update failure prevented responses to RAA" . to_owned ( ) ) ) ) ;
2743
2754
} else {
2744
- return_monitor_err ! ( self , e, channel_state, chan, RAACommitmentOrder :: CommitmentFirst , false , commitment_update. is_some( ) , pending_forwards, pending_failures) ;
2755
+ return_monitor_err ! ( self , e, channel_state, chan, RAACommitmentOrder :: CommitmentFirst , false , commitment_update. is_some( ) , pending_forwards, pending_failures, htlcs_to_fail ) ;
2745
2756
}
2746
2757
}
2747
2758
if let Some ( updates) = commitment_update {
@@ -2756,17 +2767,17 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
2756
2767
msg,
2757
2768
} ) ;
2758
2769
}
2759
- chan. get ( ) . get_short_channel_id ( ) . expect ( "RAA should only work on a short-id-available channel" )
2770
+ ( pending_forwards , pending_failures , chan. get ( ) . get_short_channel_id ( ) . expect ( "RAA should only work on a short-id-available channel" ) , htlcs_to_fail )
2760
2771
} ,
2761
- hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" . to_owned ( ) , msg. channel_id ) )
2772
+ hash_map:: Entry :: Vacant ( _) => return ( Vec :: new ( ) , Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" . to_owned ( ) , msg. channel_id ) ) )
2762
2773
}
2763
2774
} ;
2764
2775
for failure in pending_failures. drain ( ..) {
2765
2776
self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , failure. 0 , & failure. 1 , failure. 2 ) ;
2766
2777
}
2767
2778
self . forward_htlcs ( & mut [ ( short_channel_id, pending_forwards) ] ) ;
2768
2779
2769
- Ok ( ( ) )
2780
+ ( htlcs_to_fail , Ok ( ( ) ) )
2770
2781
}
2771
2782
2772
2783
fn internal_update_fee ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFee ) -> Result < ( ) , MsgHandleErrInternal > {
@@ -3267,7 +3278,9 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
3267
3278
3268
3279
fn handle_revoke_and_ack ( & self , their_node_id : & PublicKey , msg : & msgs:: RevokeAndACK ) {
3269
3280
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
3270
- let _ = handle_error ! ( self , self . internal_revoke_and_ack( their_node_id, msg) , * their_node_id) ;
3281
+ let ( htlcs_to_fail, res) = self . internal_revoke_and_ack ( their_node_id, msg) ;
3282
+ self . fail_holding_cell_htlcs ( htlcs_to_fail, msg. channel_id ) ;
3283
+ let _ = handle_error ! ( self , res, * their_node_id) ;
3271
3284
}
3272
3285
3273
3286
fn handle_update_fee ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFee ) {
0 commit comments