@@ -187,6 +187,21 @@ enum HTLCUpdateAwaitingACK {
187
187
} ,
188
188
}
189
189
190
+ /// A struct capturing the set of things we may need to do after a reconnect or after monitor
191
+ /// updating has been restored.
192
+ pub ( super ) struct ChannelRestoredUpdates {
193
+ pub ( super ) revoke_and_ack : Option < msgs:: RevokeAndACK > ,
194
+ pub ( super ) commitment_update : Option < msgs:: CommitmentUpdate > ,
195
+ pub ( super ) raa_commitment_order : RAACommitmentOrder ,
196
+ pub ( super ) chanmon_update : Option < ChannelMonitorUpdate > ,
197
+ pub ( super ) pending_forwards : Vec < ( PendingHTLCInfo , u64 ) > ,
198
+ pub ( super ) pending_failures : Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > ,
199
+ pub ( super ) forwarding_failures : Vec < ( HTLCSource , PaymentHash ) > ,
200
+ pub ( super ) needs_broadcast_safe : bool ,
201
+ pub ( super ) funding_locked : Option < msgs:: FundingLocked > ,
202
+ pub ( super ) shutdown : Option < msgs:: Shutdown > ,
203
+ }
204
+
190
205
/// There are a few "states" and then a number of flags which can be applied:
191
206
/// We first move through init with OurInitSent -> TheirInitSent -> FundingCreated -> FundingSent.
192
207
/// TheirFundingLocked and OurFundingLocked then get set on FundingSent, and when both are set we
@@ -2739,10 +2754,7 @@ impl<Signer: Sign> Channel<Signer> {
2739
2754
/// Indicates that the latest ChannelMonitor update has been committed by the client
2740
2755
/// successfully and we should restore normal operation. Returns messages which should be sent
2741
2756
/// to the remote side.
2742
- pub fn monitor_updating_restored < L : Deref > ( & mut self , logger : & L ) -> (
2743
- Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , RAACommitmentOrder , Option < ChannelMonitorUpdate > ,
2744
- Vec < ( PendingHTLCInfo , u64 ) > , Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > , Vec < ( HTLCSource , PaymentHash ) > ,
2745
- bool , Option < msgs:: FundingLocked > ) where L :: Target : Logger {
2757
+ pub fn monitor_updating_restored < L : Deref > ( & mut self , logger : & L ) -> ChannelRestoredUpdates where L :: Target : Logger {
2746
2758
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 , ChannelState :: MonitorUpdateFailed as u32 ) ;
2747
2759
self . channel_state &= !( ChannelState :: MonitorUpdateFailed as u32 ) ;
2748
2760
@@ -2764,35 +2776,46 @@ impl<Signer: Sign> Channel<Signer> {
2764
2776
} )
2765
2777
} else { None } ;
2766
2778
2767
- let mut forwards = Vec :: new ( ) ;
2768
- mem:: swap ( & mut forwards , & mut self . monitor_pending_forwards ) ;
2769
- let mut failures = Vec :: new ( ) ;
2770
- mem:: swap ( & mut failures , & mut self . monitor_pending_failures ) ;
2779
+ let mut pending_forwards = Vec :: new ( ) ;
2780
+ mem:: swap ( & mut pending_forwards , & mut self . monitor_pending_forwards ) ;
2781
+ let mut pending_failures = Vec :: new ( ) ;
2782
+ mem:: swap ( & mut pending_failures , & mut self . monitor_pending_failures ) ;
2771
2783
2772
2784
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) != 0 {
2773
2785
self . monitor_pending_revoke_and_ack = false ;
2774
2786
self . monitor_pending_commitment_signed = false ;
2775
- return ( None , None , RAACommitmentOrder :: RevokeAndACKFirst , None , forwards, failures, Vec :: new ( ) , needs_broadcast_safe, funding_locked) ;
2787
+ return ChannelRestoredUpdates {
2788
+ revoke_and_ack : None ,
2789
+ commitment_update : None ,
2790
+ raa_commitment_order : RAACommitmentOrder :: RevokeAndACKFirst ,
2791
+ chanmon_update : None ,
2792
+ pending_forwards,
2793
+ pending_failures,
2794
+ forwarding_failures : Vec :: new ( ) ,
2795
+ needs_broadcast_safe,
2796
+ funding_locked,
2797
+ shutdown : None ,
2798
+ }
2776
2799
}
2777
2800
2778
- let raa = if self . monitor_pending_revoke_and_ack {
2801
+ let revoke_and_ack = if self . monitor_pending_revoke_and_ack {
2779
2802
Some ( self . get_last_revoke_and_ack ( ) )
2780
2803
} else { None } ;
2781
2804
let mut commitment_update = if self . monitor_pending_commitment_signed {
2782
2805
Some ( self . get_last_commitment_update ( logger) )
2783
2806
} else { None } ;
2784
2807
2785
- let mut order = self . resend_order . clone ( ) ;
2808
+ let mut raa_commitment_order = self . resend_order . clone ( ) ;
2786
2809
self . monitor_pending_revoke_and_ack = false ;
2787
2810
self . monitor_pending_commitment_signed = false ;
2788
2811
2789
- let mut htlcs_failed_to_forward = Vec :: new ( ) ;
2812
+ let mut forwarding_failures = Vec :: new ( ) ;
2790
2813
let mut chanmon_update = None ;
2791
2814
if commitment_update. is_none ( ) && self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 ) == 0 {
2792
- order = RAACommitmentOrder :: RevokeAndACKFirst ;
2815
+ raa_commitment_order = RAACommitmentOrder :: RevokeAndACKFirst ;
2793
2816
2794
2817
let ( update_opt, mut failed_htlcs) = self . free_holding_cell_htlcs ( logger) . unwrap ( ) ;
2795
- htlcs_failed_to_forward . append ( & mut failed_htlcs) ;
2818
+ forwarding_failures . append ( & mut failed_htlcs) ;
2796
2819
if let Some ( ( com_update, mon_update) ) = update_opt {
2797
2820
commitment_update = Some ( com_update) ;
2798
2821
chanmon_update = Some ( mon_update) ;
@@ -2802,9 +2825,21 @@ impl<Signer: Sign> Channel<Signer> {
2802
2825
log_trace ! ( logger, "Restored monitor updating resulting in {}{} commitment update and {} RAA, with {} first" ,
2803
2826
if needs_broadcast_safe { "a funding broadcast safe, " } else { "" } ,
2804
2827
if commitment_update. is_some( ) { "a" } else { "no" } ,
2805
- if raa. is_some( ) { "an" } else { "no" } ,
2806
- match order { RAACommitmentOrder :: CommitmentFirst => "commitment" , RAACommitmentOrder :: RevokeAndACKFirst => "RAA" } ) ;
2807
- ( raa, commitment_update, order, chanmon_update, forwards, failures, htlcs_failed_to_forward, needs_broadcast_safe, funding_locked)
2828
+ if revoke_and_ack. is_some( ) { "an" } else { "no" } ,
2829
+ match raa_commitment_order { RAACommitmentOrder :: CommitmentFirst => "commitment" , RAACommitmentOrder :: RevokeAndACKFirst => "RAA" } ) ;
2830
+
2831
+ ChannelRestoredUpdates {
2832
+ revoke_and_ack,
2833
+ commitment_update,
2834
+ raa_commitment_order,
2835
+ chanmon_update,
2836
+ pending_forwards,
2837
+ pending_failures,
2838
+ forwarding_failures,
2839
+ needs_broadcast_safe,
2840
+ funding_locked,
2841
+ shutdown : None ,
2842
+ }
2808
2843
}
2809
2844
2810
2845
pub fn update_fee < F : Deref > ( & mut self , fee_estimator : & F , msg : & msgs:: UpdateFee ) -> Result < ( ) , ChannelError >
@@ -2891,7 +2926,7 @@ impl<Signer: Sign> Channel<Signer> {
2891
2926
2892
2927
/// May panic if some calls other than message-handling calls (which will all Err immediately)
2893
2928
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
2894
- pub fn channel_reestablish < L : Deref > ( & mut self , msg : & msgs:: ChannelReestablish , logger : & L ) -> Result < ( Option < msgs :: FundingLocked > , Option < msgs :: RevokeAndACK > , Option < msgs :: CommitmentUpdate > , Option < ChannelMonitorUpdate > , RAACommitmentOrder , Vec < ( HTLCSource , PaymentHash ) > , Option < msgs :: Shutdown > ) , ChannelError > where L :: Target : Logger {
2929
+ pub fn channel_reestablish < L : Deref > ( & mut self , msg : & msgs:: ChannelReestablish , logger : & L ) -> Result < ChannelRestoredUpdates , ChannelError > where L :: Target : Logger {
2895
2930
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == 0 {
2896
2931
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
2897
2932
// almost certainly indicates we are going to end up out-of-sync in some way, so we
@@ -2942,15 +2977,38 @@ impl<Signer: Sign> Channel<Signer> {
2942
2977
return Err ( ChannelError :: Close ( "Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet" . to_owned ( ) ) ) ;
2943
2978
}
2944
2979
// Short circuit the whole handler as there is nothing we can resend them
2945
- return Ok ( ( None , None , None , None , RAACommitmentOrder :: CommitmentFirst , Vec :: new ( ) , shutdown_msg) ) ;
2980
+ return Ok ( ChannelRestoredUpdates {
2981
+ revoke_and_ack : None ,
2982
+ commitment_update : None ,
2983
+ raa_commitment_order : RAACommitmentOrder :: CommitmentFirst ,
2984
+ chanmon_update : None ,
2985
+ pending_forwards : Vec :: new ( ) ,
2986
+ pending_failures : Vec :: new ( ) ,
2987
+ forwarding_failures : Vec :: new ( ) ,
2988
+ needs_broadcast_safe : false ,
2989
+ funding_locked : None ,
2990
+ shutdown : shutdown_msg,
2991
+ } ) ;
2946
2992
}
2947
2993
2948
2994
// We have OurFundingLocked set!
2949
2995
let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
2950
- return Ok ( ( Some ( msgs:: FundingLocked {
2951
- channel_id : self . channel_id ( ) ,
2952
- next_per_commitment_point,
2953
- } ) , None , None , None , RAACommitmentOrder :: CommitmentFirst , Vec :: new ( ) , shutdown_msg) ) ;
2996
+
2997
+ return Ok ( ChannelRestoredUpdates {
2998
+ revoke_and_ack : None ,
2999
+ commitment_update : None ,
3000
+ raa_commitment_order : RAACommitmentOrder :: CommitmentFirst ,
3001
+ chanmon_update : None ,
3002
+ pending_forwards : Vec :: new ( ) ,
3003
+ pending_failures : Vec :: new ( ) ,
3004
+ forwarding_failures : Vec :: new ( ) ,
3005
+ needs_broadcast_safe : false ,
3006
+ funding_locked : Some ( msgs:: FundingLocked {
3007
+ channel_id : self . channel_id ( ) ,
3008
+ next_per_commitment_point,
3009
+ } ) ,
3010
+ shutdown : shutdown_msg,
3011
+ } ) ;
2954
3012
}
2955
3013
2956
3014
let required_revoke = if msg. next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self . cur_holder_commitment_transaction_number {
@@ -2999,14 +3057,47 @@ impl<Signer: Sign> Channel<Signer> {
2999
3057
Err ( ChannelError :: Close ( msg) ) => return Err ( ChannelError :: Close ( msg) ) ,
3000
3058
Err ( ChannelError :: Ignore ( _) ) | Err ( ChannelError :: CloseDelayBroadcast ( _) ) => panic ! ( "Got non-channel-failing result from free_holding_cell_htlcs" ) ,
3001
3059
Ok ( ( Some ( ( commitment_update, monitor_update) ) , htlcs_to_fail) ) => {
3002
- return Ok ( ( resend_funding_locked, required_revoke, Some ( commitment_update) , Some ( monitor_update) , self . resend_order . clone ( ) , htlcs_to_fail, shutdown_msg) ) ;
3060
+ return Ok ( ChannelRestoredUpdates {
3061
+ revoke_and_ack : required_revoke,
3062
+ commitment_update : Some ( commitment_update) ,
3063
+ raa_commitment_order : self . resend_order . clone ( ) ,
3064
+ chanmon_update : Some ( monitor_update) ,
3065
+ pending_forwards : Vec :: new ( ) ,
3066
+ pending_failures : Vec :: new ( ) ,
3067
+ forwarding_failures : htlcs_to_fail,
3068
+ needs_broadcast_safe : false ,
3069
+ funding_locked : resend_funding_locked,
3070
+ shutdown : shutdown_msg,
3071
+ } ) ;
3003
3072
} ,
3004
3073
Ok ( ( None , htlcs_to_fail) ) => {
3005
- return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , htlcs_to_fail, shutdown_msg) ) ;
3074
+ return Ok ( ChannelRestoredUpdates {
3075
+ revoke_and_ack : required_revoke,
3076
+ commitment_update : None ,
3077
+ raa_commitment_order : self . resend_order . clone ( ) ,
3078
+ chanmon_update : None ,
3079
+ pending_forwards : Vec :: new ( ) ,
3080
+ pending_failures : Vec :: new ( ) ,
3081
+ forwarding_failures : htlcs_to_fail,
3082
+ needs_broadcast_safe : false ,
3083
+ funding_locked : resend_funding_locked,
3084
+ shutdown : shutdown_msg,
3085
+ } ) ;
3006
3086
} ,
3007
3087
}
3008
3088
} else {
3009
- return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , Vec :: new ( ) , shutdown_msg) ) ;
3089
+ return Ok ( ChannelRestoredUpdates {
3090
+ revoke_and_ack : required_revoke,
3091
+ commitment_update : None ,
3092
+ raa_commitment_order : self . resend_order . clone ( ) ,
3093
+ chanmon_update : None ,
3094
+ pending_forwards : Vec :: new ( ) ,
3095
+ pending_failures : Vec :: new ( ) ,
3096
+ forwarding_failures : Vec :: new ( ) ,
3097
+ needs_broadcast_safe : false ,
3098
+ funding_locked : resend_funding_locked,
3099
+ shutdown : shutdown_msg,
3100
+ } ) ;
3010
3101
}
3011
3102
} else if msg. next_local_commitment_number == next_counterparty_commitment_number - 1 {
3012
3103
if required_revoke. is_some ( ) {
@@ -3017,10 +3108,32 @@ impl<Signer: Sign> Channel<Signer> {
3017
3108
3018
3109
if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
3019
3110
self . monitor_pending_commitment_signed = true ;
3020
- return Ok ( ( resend_funding_locked, None , None , None , self . resend_order . clone ( ) , Vec :: new ( ) , shutdown_msg) ) ;
3111
+ return Ok ( ChannelRestoredUpdates {
3112
+ revoke_and_ack : None ,
3113
+ commitment_update : None ,
3114
+ raa_commitment_order : self . resend_order . clone ( ) ,
3115
+ chanmon_update : None ,
3116
+ pending_forwards : Vec :: new ( ) ,
3117
+ pending_failures : Vec :: new ( ) ,
3118
+ forwarding_failures : Vec :: new ( ) ,
3119
+ needs_broadcast_safe : false ,
3120
+ funding_locked : resend_funding_locked,
3121
+ shutdown : shutdown_msg,
3122
+ } ) ;
3021
3123
}
3022
3124
3023
- return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( logger) ) , None , self . resend_order . clone ( ) , Vec :: new ( ) , shutdown_msg) ) ;
3125
+ return Ok ( ChannelRestoredUpdates {
3126
+ revoke_and_ack : required_revoke,
3127
+ commitment_update : Some ( self . get_last_commitment_update ( logger) ) ,
3128
+ raa_commitment_order : self . resend_order . clone ( ) ,
3129
+ chanmon_update : None ,
3130
+ pending_forwards : Vec :: new ( ) ,
3131
+ pending_failures : Vec :: new ( ) ,
3132
+ forwarding_failures : Vec :: new ( ) ,
3133
+ needs_broadcast_safe : false ,
3134
+ funding_locked : resend_funding_locked,
3135
+ shutdown : shutdown_msg,
3136
+ } ) ;
3024
3137
} else {
3025
3138
return Err ( ChannelError :: Close ( "Peer attempted to reestablish channel with a very old remote commitment transaction" . to_owned ( ) ) ) ;
3026
3139
}
0 commit comments