@@ -3234,16 +3234,16 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3234
3234
/// Public version of the below, checking relevant preconditions first.
3235
3235
/// If we're not in a state where freeing the holding cell makes sense, this is a no-op and
3236
3236
/// returns `(None, Vec::new())`.
3237
- 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 {
3237
+ pub fn maybe_free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> ( Option < & ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) where L :: Target : Logger {
3238
3238
if self . channel_state >= ChannelState :: ChannelReady as u32 &&
3239
3239
( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateInProgress as u32 ) ) == 0 {
3240
3240
self . free_holding_cell_htlcs ( logger)
3241
- } else { Ok ( ( None , Vec :: new ( ) ) ) }
3241
+ } else { ( None , Vec :: new ( ) ) }
3242
3242
}
3243
3243
3244
3244
/// Frees any pending commitment updates in the holding cell, generating the relevant messages
3245
3245
/// for our counterparty.
3246
- fn free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> Result < ( Option < ( msgs :: CommitmentUpdate , ChannelMonitorUpdate ) > , Vec < ( HTLCSource , PaymentHash ) > ) , ChannelError > where L :: Target : Logger {
3246
+ fn free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> ( Option < & ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) where L :: Target : Logger {
3247
3247
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 , 0 ) ;
3248
3248
if self . holding_cell_htlc_updates . len ( ) != 0 || self . holding_cell_update_fee . is_some ( ) {
3249
3249
log_trace ! ( logger, "Freeing holding cell with {} HTLC updates{} in channel {}" , self . holding_cell_htlc_updates. len( ) ,
@@ -3324,16 +3324,16 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3324
3324
}
3325
3325
}
3326
3326
if update_add_htlcs. is_empty ( ) && update_fulfill_htlcs. is_empty ( ) && update_fail_htlcs. is_empty ( ) && self . holding_cell_update_fee . is_none ( ) {
3327
- return Ok ( ( None , htlcs_to_fail) ) ;
3327
+ return ( None , htlcs_to_fail) ;
3328
3328
}
3329
3329
let update_fee = if let Some ( feerate) = self . holding_cell_update_fee . take ( ) {
3330
3330
self . send_update_fee ( feerate, false , logger)
3331
3331
} else {
3332
3332
None
3333
3333
} ;
3334
3334
3335
- let ( commitment_signed , mut additional_update) = self . send_commitment_no_status_check ( logger) ? ;
3336
- // send_commitment_no_status_check and get_update_fulfill_htlc may bump latest_monitor_id
3335
+ let mut additional_update = self . build_commitment_no_status_check ( logger) ;
3336
+ // build_commitment_no_status_check and get_update_fulfill_htlc may bump latest_monitor_id
3337
3337
// but we want them to be strictly increasing by one, so reset it here.
3338
3338
self . latest_monitor_update_id = monitor_update. update_id ;
3339
3339
monitor_update. updates . append ( & mut additional_update. updates ) ;
@@ -3342,16 +3342,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3342
3342
log_bytes!( self . channel_id( ) ) , if update_fee. is_some( ) { "a fee update, " } else { "" } ,
3343
3343
update_add_htlcs. len( ) , update_fulfill_htlcs. len( ) , update_fail_htlcs. len( ) ) ;
3344
3344
3345
- Ok ( ( Some ( ( msgs:: CommitmentUpdate {
3346
- update_add_htlcs,
3347
- update_fulfill_htlcs,
3348
- update_fail_htlcs,
3349
- update_fail_malformed_htlcs : Vec :: new ( ) ,
3350
- update_fee,
3351
- commitment_signed,
3352
- } , monitor_update) ) , htlcs_to_fail) )
3345
+ self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3346
+ self . pending_monitor_updates . push ( monitor_update) ;
3347
+ ( Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) , htlcs_to_fail)
3353
3348
} else {
3354
- Ok ( ( None , Vec :: new ( ) ) )
3349
+ ( None , Vec :: new ( ) )
3355
3350
}
3356
3351
}
3357
3352
@@ -3561,17 +3556,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3561
3556
return Ok ( ( Vec :: new ( ) , self . pending_monitor_updates . last ( ) . unwrap ( ) ) ) ;
3562
3557
}
3563
3558
3564
- match self . free_holding_cell_htlcs ( logger) ? {
3565
- ( Some ( ( mut commitment_update, mut additional_update) ) , htlcs_to_fail) => {
3566
- commitment_update. update_fail_htlcs . reserve ( update_fail_htlcs. len ( ) ) ;
3567
- for fail_msg in update_fail_htlcs. drain ( ..) {
3568
- commitment_update. update_fail_htlcs . push ( fail_msg) ;
3569
- }
3570
- commitment_update. update_fail_malformed_htlcs . reserve ( update_fail_malformed_htlcs. len ( ) ) ;
3571
- for fail_msg in update_fail_malformed_htlcs. drain ( ..) {
3572
- commitment_update. update_fail_malformed_htlcs . push ( fail_msg) ;
3573
- }
3574
-
3559
+ match self . free_holding_cell_htlcs ( logger) {
3560
+ ( Some ( _) , htlcs_to_fail) => {
3561
+ let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) ;
3575
3562
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
3576
3563
// strictly increasing by one, so decrement it here.
3577
3564
self . latest_monitor_update_id = monitor_update. update_id ;
@@ -5948,8 +5935,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5948
5935
5949
5936
/// Begins the shutdown process, getting a message for the remote peer and returning all
5950
5937
/// holding cell HTLCs for payment failure.
5951
- pub fn get_shutdown < SP : Deref > ( & mut self , signer_provider : & SP , their_features : & InitFeatures , target_feerate_sats_per_kw : Option < u32 > )
5952
- -> Result < ( msgs:: Shutdown , Option < ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) , APIError >
5938
+ ///
5939
+ /// May jump to the channel being fully shutdown (see [`Self::is_shutdown`]) in which case no
5940
+ /// [`ChannelMonitorUpdate`] will be returned).
5941
+ pub fn get_shutdown < SP : Deref > ( & mut self , signer_provider : & SP , their_features : & InitFeatures ,
5942
+ target_feerate_sats_per_kw : Option < u32 > )
5943
+ -> Result < ( msgs:: Shutdown , Option < & ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) , APIError >
5953
5944
where SP :: Target : SignerProvider {
5954
5945
for htlc in self . pending_outbound_htlcs . iter ( ) {
5955
5946
if let OutboundHTLCState :: LocalAnnounced ( _) = htlc. state {
@@ -5992,12 +5983,15 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5992
5983
5993
5984
let monitor_update = if update_shutdown_script {
5994
5985
self . latest_monitor_update_id += 1 ;
5995
- Some ( ChannelMonitorUpdate {
5986
+ let monitor_update = ChannelMonitorUpdate {
5996
5987
update_id : self . latest_monitor_update_id ,
5997
5988
updates : vec ! [ ChannelMonitorUpdateStep :: ShutdownScript {
5998
5989
scriptpubkey: self . get_closing_scriptpubkey( ) ,
5999
5990
} ] ,
6000
- } )
5991
+ } ;
5992
+ self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
5993
+ self . pending_monitor_updates . push ( monitor_update) ;
5994
+ Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) )
6001
5995
} else { None } ;
6002
5996
let shutdown = msgs:: Shutdown {
6003
5997
channel_id : self . channel_id ,
@@ -6018,6 +6012,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
6018
6012
}
6019
6013
} ) ;
6020
6014
6015
+ debug_assert ! ( !self . is_shutdown( ) || monitor_update. is_none( ) ,
6016
+ "we can't both complete shutdown and return a monitor update" ) ;
6017
+
6021
6018
Ok ( ( shutdown, monitor_update, dropped_outbound_htlcs) )
6022
6019
}
6023
6020
0 commit comments