@@ -786,20 +786,60 @@ macro_rules! maybe_break_monitor_err {
786
786
}
787
787
788
788
macro_rules! handle_chan_restoration_locked {
789
- ( $self: expr , $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
789
+ ( $self: ident , $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
790
790
$raa: expr, $commitment_update: expr, $order: expr,
791
+ $pending_forwards: expr, $broadcast_safe: expr, $funding_locked: expr) => { {
792
+ let res = handle_chan_restoration_locked!( $self, $channel_lock, $channel_state, $channel_entry,
793
+ $raa, $commitment_update, $order, None , $pending_forwards, $broadcast_safe, $funding_locked) ;
794
+ // If there was no ChannelMonitorUpdate, we should never generate an Err in the res loop
795
+ // below. Doing so would imply calling handle_err!() from channel_monitor_updated() which
796
+ // should *never* end up calling back to `chain_monitor.update_channel()`.
797
+ assert!( res. 2 . is_ok( ) ) ;
798
+ res
799
+ } } ;
800
+ ( $self: ident, $channel_lock: expr, $channel_state: expr, $channel_entry: expr,
801
+ $raa: expr, $commitment_update: expr, $order: expr, $chanmon_update: expr,
791
802
$pending_forwards: expr, $broadcast_safe: expr, $funding_locked: expr) => { {
792
803
let mut htlc_forwards = None ;
793
804
let mut funding_broadcast_safe = None ;
794
805
let counterparty_node_id = $channel_entry. get( ) . get_counterparty_node_id( ) ;
795
806
796
- {
797
- if !$pending_forwards. is_empty( ) {
807
+ let res = loop {
808
+ let forwards: Vec <( PendingHTLCInfo , u64 ) > = $pending_forwards; // Force type-checking to resolve
809
+ if !forwards. is_empty( ) {
798
810
htlc_forwards = Some ( ( $channel_entry. get( ) . get_short_channel_id( ) . expect( "We can't have pending forwards before funding confirmation" ) ,
799
- $channel_entry. get( ) . get_funding_txo( ) . unwrap( ) , $pending_forwards) ) ;
811
+ $channel_entry. get( ) . get_funding_txo( ) . unwrap( ) , forwards) ) ;
812
+ }
813
+
814
+ let chanmon_update: Option <ChannelMonitorUpdate > = $chanmon_update; // Force type-checking to resolve
815
+ if chanmon_update. is_some( ) {
816
+ assert!( $commitment_update. is_some( ) ) ;
817
+ assert!( $funding_locked. is_none( ) ) ;
818
+ }
819
+
820
+ if let Some ( msg) = $funding_locked {
821
+ $channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendFundingLocked {
822
+ node_id: counterparty_node_id,
823
+ msg,
824
+ } ) ;
825
+ if let Some ( announcement_sigs) = $self. get_announcement_sigs( $channel_entry. get( ) ) {
826
+ $channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendAnnouncementSignatures {
827
+ node_id: counterparty_node_id,
828
+ msg: announcement_sigs,
829
+ } ) ;
830
+ }
831
+ $channel_state. short_to_id. insert( $channel_entry. get( ) . get_short_channel_id( ) . unwrap( ) , $channel_entry. get( ) . channel_id( ) ) ;
800
832
}
801
833
802
834
macro_rules! handle_cs { ( ) => {
835
+ if let Some ( monitor_update) = chanmon_update {
836
+ assert!( $order == RAACommitmentOrder :: RevokeAndACKFirst ) ;
837
+ assert!( !$broadcast_safe) ;
838
+ assert!( $commitment_update. is_some( ) ) ;
839
+ if let Err ( e) = $self. chain_monitor. update_channel( $channel_entry. get( ) . get_funding_txo( ) . unwrap( ) , monitor_update) {
840
+ break handle_monitor_err!( $self, e, $channel_state, $channel_entry, RAACommitmentOrder :: CommitmentFirst , false , true ) ;
841
+ }
842
+ }
803
843
if let Some ( update) = $commitment_update {
804
844
$channel_state. pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
805
845
node_id: counterparty_node_id,
@@ -831,27 +871,18 @@ macro_rules! handle_chan_restoration_locked {
831
871
user_channel_id: $channel_entry. get( ) . get_user_id( ) ,
832
872
} ) ;
833
873
}
834
- if let Some ( msg) = $funding_locked {
835
- $channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendFundingLocked {
836
- node_id: counterparty_node_id,
837
- msg,
838
- } ) ;
839
- if let Some ( announcement_sigs) = $self. get_announcement_sigs( $channel_entry. get( ) ) {
840
- $channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendAnnouncementSignatures {
841
- node_id: counterparty_node_id,
842
- msg: announcement_sigs,
843
- } ) ;
844
- }
845
- $channel_state. short_to_id. insert( $channel_entry. get( ) . get_short_channel_id( ) . unwrap( ) , $channel_entry. get( ) . channel_id( ) ) ;
846
- }
847
- }
848
- ( htlc_forwards, funding_broadcast_safe)
874
+ break Ok ( ( ) ) ;
875
+ } ;
876
+
877
+ ( htlc_forwards, funding_broadcast_safe, res, counterparty_node_id)
849
878
} }
850
879
}
851
880
852
881
macro_rules! post_handle_chan_restoration {
853
- ( $self: expr, $locked_res: expr) => { {
854
- let ( htlc_forwards, funding_broadcast_safe) = $locked_res;
882
+ ( $self: ident, $locked_res: expr) => { {
883
+ let ( htlc_forwards, funding_broadcast_safe, res, counterparty_node_id) = $locked_res;
884
+
885
+ let _ = handle_error!( $self, res, counterparty_node_id) ;
855
886
856
887
if let Some ( ev) = funding_broadcast_safe {
857
888
$self. pending_events. lock( ) . unwrap( ) . push( ev) ;
@@ -3045,77 +3076,34 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3045
3076
}
3046
3077
3047
3078
fn internal_channel_reestablish ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( ) , MsgHandleErrInternal > {
3048
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3049
- let channel_state = & mut * channel_state_lock;
3079
+ let chan_restoration_res = {
3080
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3081
+ let channel_state = & mut * channel_state_lock;
3050
3082
3051
- match channel_state. by_id . entry ( msg. channel_id ) {
3052
- hash_map:: Entry :: Occupied ( mut chan) => {
3053
- if chan. get ( ) . get_counterparty_node_id ( ) != * counterparty_node_id {
3054
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" . to_owned ( ) , msg. channel_id ) ) ;
3055
- }
3056
- // Currently, we expect all holding cell update_adds to be dropped on peer
3057
- // disconnect, so Channel's reestablish will never hand us any holding cell
3058
- // freed HTLCs to fail backwards. If in the future we no longer drop pending
3059
- // add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
3060
- let ( funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, mut order, shutdown) =
3061
- try_chan_entry ! ( self , chan. get_mut( ) . channel_reestablish( msg, & self . logger) , channel_state, chan) ;
3062
- if let Some ( monitor_update) = monitor_update_opt {
3063
- if let Err ( e) = self . chain_monitor . update_channel ( chan. get ( ) . get_funding_txo ( ) . unwrap ( ) , monitor_update) {
3064
- // channel_reestablish doesn't guarantee the order it returns is sensical
3065
- // for the messages it returns, but if we're setting what messages to
3066
- // re-transmit on monitor update success, we need to make sure it is sane.
3067
- if revoke_and_ack. is_none ( ) {
3068
- order = RAACommitmentOrder :: CommitmentFirst ;
3069
- }
3070
- if commitment_update. is_none ( ) {
3071
- order = RAACommitmentOrder :: RevokeAndACKFirst ;
3072
- }
3073
- return_monitor_err ! ( self , e, channel_state, chan, order, revoke_and_ack. is_some( ) , commitment_update. is_some( ) ) ;
3074
- //TODO: Resend the funding_locked if needed once we get the monitor running again
3075
- }
3076
- }
3077
- if let Some ( msg) = funding_locked {
3078
- channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingLocked {
3079
- node_id : counterparty_node_id. clone ( ) ,
3080
- msg
3081
- } ) ;
3082
- }
3083
- macro_rules! send_raa { ( ) => {
3084
- if let Some ( msg) = revoke_and_ack {
3085
- channel_state. pending_msg_events. push( events:: MessageSendEvent :: SendRevokeAndACK {
3086
- node_id: counterparty_node_id. clone( ) ,
3087
- msg
3088
- } ) ;
3083
+ match channel_state. by_id . entry ( msg. channel_id ) {
3084
+ hash_map:: Entry :: Occupied ( mut chan) => {
3085
+ if chan. get ( ) . get_counterparty_node_id ( ) != * counterparty_node_id {
3086
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" . to_owned ( ) , msg. channel_id ) ) ;
3089
3087
}
3090
- } }
3091
- macro_rules! send_cu { ( ) => {
3092
- if let Some ( updates) = commitment_update {
3093
- channel_state. pending_msg_events. push( events:: MessageSendEvent :: UpdateHTLCs {
3088
+ // Currently, we expect all holding cell update_adds to be dropped on peer
3089
+ // disconnect, so Channel's reestablish will never hand us any holding cell
3090
+ // freed HTLCs to fail backwards. If in the future we no longer drop pending
3091
+ // add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
3092
+ let ( funding_locked, revoke_and_ack, commitment_update, monitor_update_opt, order, shutdown) =
3093
+ try_chan_entry ! ( self , chan. get_mut( ) . channel_reestablish( msg, & self . logger) , channel_state, chan) ;
3094
+ if let Some ( msg) = shutdown {
3095
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendShutdown {
3094
3096
node_id : counterparty_node_id. clone ( ) ,
3095
- updates
3097
+ msg ,
3096
3098
} ) ;
3097
3099
}
3098
- } }
3099
- match order {
3100
- RAACommitmentOrder :: RevokeAndACKFirst => {
3101
- send_raa ! ( ) ;
3102
- send_cu ! ( ) ;
3103
- } ,
3104
- RAACommitmentOrder :: CommitmentFirst => {
3105
- send_cu ! ( ) ;
3106
- send_raa ! ( ) ;
3107
- } ,
3108
- }
3109
- if let Some ( msg) = shutdown {
3110
- channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendShutdown {
3111
- node_id : counterparty_node_id. clone ( ) ,
3112
- msg,
3113
- } ) ;
3114
- }
3115
- Ok ( ( ) )
3116
- } ,
3117
- hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" . to_owned ( ) , msg. channel_id ) )
3118
- }
3100
+ handle_chan_restoration_locked ! ( self , channel_state_lock, channel_state, chan, revoke_and_ack, commitment_update, order, monitor_update_opt, Vec :: new( ) , false , funding_locked)
3101
+ } ,
3102
+ hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" . to_owned ( ) , msg. channel_id ) )
3103
+ }
3104
+ } ;
3105
+ post_handle_chan_restoration ! ( self , chan_restoration_res) ;
3106
+ Ok ( ( ) )
3119
3107
}
3120
3108
3121
3109
/// Begin Update fee process. Allowed only on an outbound channel.
0 commit comments