@@ -3161,16 +3161,16 @@ impl<Signer: Sign> Channel<Signer> {
3161
3161
/// Public version of the below, checking relevant preconditions first.
3162
3162
/// If we're not in a state where freeing the holding cell makes sense, this is a no-op and
3163
3163
/// returns `(None, Vec::new())`.
3164
- pub fn maybe_free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> Result < ( Option < ( msgs :: CommitmentUpdate , ChannelMonitorUpdate ) > , Vec < ( HTLCSource , PaymentHash ) > ) , ChannelError > where L :: Target : Logger {
3164
+ pub fn maybe_free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> ( Option < & ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) where L :: Target : Logger {
3165
3165
if self . channel_state >= ChannelState :: ChannelReady as u32 &&
3166
3166
( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateInProgress as u32 ) ) == 0 {
3167
3167
self . free_holding_cell_htlcs ( logger)
3168
- } else { Ok ( ( None , Vec :: new ( ) ) ) }
3168
+ } else { ( None , Vec :: new ( ) ) }
3169
3169
}
3170
3170
3171
3171
/// Frees any pending commitment updates in the holding cell, generating the relevant messages
3172
3172
/// for our counterparty.
3173
- fn free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> Result < ( Option < ( msgs :: CommitmentUpdate , ChannelMonitorUpdate ) > , Vec < ( HTLCSource , PaymentHash ) > ) , ChannelError > where L :: Target : Logger {
3173
+ fn free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> ( Option < & ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) where L :: Target : Logger {
3174
3174
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 , 0 ) ;
3175
3175
if self . holding_cell_htlc_updates . len ( ) != 0 || self . holding_cell_update_fee . is_some ( ) {
3176
3176
log_trace ! ( logger, "Freeing holding cell with {} HTLC updates{} in channel {}" , self . holding_cell_htlc_updates. len( ) ,
@@ -3251,16 +3251,16 @@ impl<Signer: Sign> Channel<Signer> {
3251
3251
}
3252
3252
}
3253
3253
if update_add_htlcs. is_empty ( ) && update_fulfill_htlcs. is_empty ( ) && update_fail_htlcs. is_empty ( ) && self . holding_cell_update_fee . is_none ( ) {
3254
- return Ok ( ( None , htlcs_to_fail) ) ;
3254
+ return ( None , htlcs_to_fail) ;
3255
3255
}
3256
3256
let update_fee = if let Some ( feerate) = self . holding_cell_update_fee . take ( ) {
3257
3257
self . send_update_fee ( feerate, false , logger)
3258
3258
} else {
3259
3259
None
3260
3260
} ;
3261
3261
3262
- let ( commitment_signed , mut additional_update) = self . send_commitment_no_status_check ( logger) ? ;
3263
- // send_commitment_no_status_check and get_update_fulfill_htlc may bump latest_monitor_id
3262
+ let mut additional_update = self . build_commitment_no_status_check ( logger) ;
3263
+ // build_commitment_no_status_check and get_update_fulfill_htlc may bump latest_monitor_id
3264
3264
// but we want them to be strictly increasing by one, so reset it here.
3265
3265
self . latest_monitor_update_id = monitor_update. update_id ;
3266
3266
monitor_update. updates . append ( & mut additional_update. updates ) ;
@@ -3269,16 +3269,11 @@ impl<Signer: Sign> Channel<Signer> {
3269
3269
log_bytes!( self . channel_id( ) ) , if update_fee. is_some( ) { "a fee update, " } else { "" } ,
3270
3270
update_add_htlcs. len( ) , update_fulfill_htlcs. len( ) , update_fail_htlcs. len( ) ) ;
3271
3271
3272
- Ok ( ( Some ( ( msgs:: CommitmentUpdate {
3273
- update_add_htlcs,
3274
- update_fulfill_htlcs,
3275
- update_fail_htlcs,
3276
- update_fail_malformed_htlcs : Vec :: new ( ) ,
3277
- update_fee,
3278
- commitment_signed,
3279
- } , monitor_update) ) , htlcs_to_fail) )
3272
+ self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3273
+ self . pending_monitor_updates . push ( monitor_update) ;
3274
+ ( Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) , htlcs_to_fail)
3280
3275
} else {
3281
- Ok ( ( None , Vec :: new ( ) ) )
3276
+ ( None , Vec :: new ( ) )
3282
3277
}
3283
3278
}
3284
3279
@@ -3488,17 +3483,9 @@ impl<Signer: Sign> Channel<Signer> {
3488
3483
return Ok ( ( Vec :: new ( ) , self . pending_monitor_updates . last ( ) . unwrap ( ) ) ) ;
3489
3484
}
3490
3485
3491
- match self . free_holding_cell_htlcs ( logger) ? {
3492
- ( Some ( ( mut commitment_update, mut additional_update) ) , htlcs_to_fail) => {
3493
- commitment_update. update_fail_htlcs . reserve ( update_fail_htlcs. len ( ) ) ;
3494
- for fail_msg in update_fail_htlcs. drain ( ..) {
3495
- commitment_update. update_fail_htlcs . push ( fail_msg) ;
3496
- }
3497
- commitment_update. update_fail_malformed_htlcs . reserve ( update_fail_malformed_htlcs. len ( ) ) ;
3498
- for fail_msg in update_fail_malformed_htlcs. drain ( ..) {
3499
- commitment_update. update_fail_malformed_htlcs . push ( fail_msg) ;
3500
- }
3501
-
3486
+ match self . free_holding_cell_htlcs ( logger) {
3487
+ ( Some ( _) , htlcs_to_fail) => {
3488
+ let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) ;
3502
3489
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
3503
3490
// strictly increasing by one, so decrement it here.
3504
3491
self . latest_monitor_update_id = monitor_update. update_id ;
@@ -5815,8 +5802,11 @@ impl<Signer: Sign> Channel<Signer> {
5815
5802
5816
5803
/// Begins the shutdown process, getting a message for the remote peer and returning all
5817
5804
/// holding cell HTLCs for payment failure.
5805
+ ///
5806
+ /// May jump to the channel being fully shutdown (see [`Self::is_shutdown`]) in which case no
5807
+ /// [`ChannelMonitorUpdate`] will be returned).
5818
5808
pub fn get_shutdown < K : Deref > ( & mut self , keys_provider : & K , their_features : & InitFeatures , target_feerate_sats_per_kw : Option < u32 > )
5819
- -> Result < ( msgs:: Shutdown , Option < ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) , APIError >
5809
+ -> Result < ( msgs:: Shutdown , Option < & ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) , APIError >
5820
5810
where K :: Target : KeysInterface {
5821
5811
for htlc in self . pending_outbound_htlcs . iter ( ) {
5822
5812
if let OutboundHTLCState :: LocalAnnounced ( _) = htlc. state {
@@ -5859,12 +5849,15 @@ impl<Signer: Sign> Channel<Signer> {
5859
5849
5860
5850
let monitor_update = if update_shutdown_script {
5861
5851
self . latest_monitor_update_id += 1 ;
5862
- Some ( ChannelMonitorUpdate {
5852
+ let monitor_update = ChannelMonitorUpdate {
5863
5853
update_id : self . latest_monitor_update_id ,
5864
5854
updates : vec ! [ ChannelMonitorUpdateStep :: ShutdownScript {
5865
5855
scriptpubkey: self . get_closing_scriptpubkey( ) ,
5866
5856
} ] ,
5867
- } )
5857
+ } ;
5858
+ self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
5859
+ self . pending_monitor_updates . push ( monitor_update) ;
5860
+ Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) )
5868
5861
} else { None } ;
5869
5862
let shutdown = msgs:: Shutdown {
5870
5863
channel_id : self . channel_id ,
@@ -5885,6 +5878,9 @@ impl<Signer: Sign> Channel<Signer> {
5885
5878
}
5886
5879
} ) ;
5887
5880
5881
+ debug_assert ! ( !self . is_shutdown( ) || monitor_update. is_none( ) ,
5882
+ "we can't both complete shutdown and return a monitor update" ) ;
5883
+
5888
5884
Ok ( ( shutdown, monitor_update, dropped_outbound_htlcs) )
5889
5885
}
5890
5886
0 commit comments