@@ -3188,16 +3188,16 @@ impl<Signer: Sign> Channel<Signer> {
3188
3188
/// Public version of the below, checking relevant preconditions first.
3189
3189
/// If we're not in a state where freeing the holding cell makes sense, this is a no-op and
3190
3190
/// returns `(None, Vec::new())`.
3191
- 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 {
3191
+ pub fn maybe_free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> ( Option < & ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) where L :: Target : Logger {
3192
3192
if self . channel_state >= ChannelState :: ChannelReady as u32 &&
3193
3193
( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateInProgress as u32 ) ) == 0 {
3194
3194
self . free_holding_cell_htlcs ( logger)
3195
- } else { Ok ( ( None , Vec :: new ( ) ) ) }
3195
+ } else { ( None , Vec :: new ( ) ) }
3196
3196
}
3197
3197
3198
3198
/// Frees any pending commitment updates in the holding cell, generating the relevant messages
3199
3199
/// for our counterparty.
3200
- fn free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> Result < ( Option < ( msgs :: CommitmentUpdate , ChannelMonitorUpdate ) > , Vec < ( HTLCSource , PaymentHash ) > ) , ChannelError > where L :: Target : Logger {
3200
+ fn free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> ( Option < & ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) where L :: Target : Logger {
3201
3201
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateInProgress as u32 , 0 ) ;
3202
3202
if self . holding_cell_htlc_updates . len ( ) != 0 || self . holding_cell_update_fee . is_some ( ) {
3203
3203
log_trace ! ( logger, "Freeing holding cell with {} HTLC updates{} in channel {}" , self . holding_cell_htlc_updates. len( ) ,
@@ -3278,16 +3278,16 @@ impl<Signer: Sign> Channel<Signer> {
3278
3278
}
3279
3279
}
3280
3280
if update_add_htlcs. is_empty ( ) && update_fulfill_htlcs. is_empty ( ) && update_fail_htlcs. is_empty ( ) && self . holding_cell_update_fee . is_none ( ) {
3281
- return Ok ( ( None , htlcs_to_fail) ) ;
3281
+ return ( None , htlcs_to_fail) ;
3282
3282
}
3283
3283
let update_fee = if let Some ( feerate) = self . holding_cell_update_fee . take ( ) {
3284
3284
self . send_update_fee ( feerate, false , logger)
3285
3285
} else {
3286
3286
None
3287
3287
} ;
3288
3288
3289
- let ( commitment_signed , mut additional_update) = self . send_commitment_no_status_check ( logger) ? ;
3290
- // send_commitment_no_status_check and get_update_fulfill_htlc may bump latest_monitor_id
3289
+ let mut additional_update = self . build_commitment_no_status_check ( logger) ;
3290
+ // build_commitment_no_status_check and get_update_fulfill_htlc may bump latest_monitor_id
3291
3291
// but we want them to be strictly increasing by one, so reset it here.
3292
3292
self . latest_monitor_update_id = monitor_update. update_id ;
3293
3293
monitor_update. updates . append ( & mut additional_update. updates ) ;
@@ -3296,16 +3296,11 @@ impl<Signer: Sign> Channel<Signer> {
3296
3296
log_bytes!( self . channel_id( ) ) , if update_fee. is_some( ) { "a fee update, " } else { "" } ,
3297
3297
update_add_htlcs. len( ) , update_fulfill_htlcs. len( ) , update_fail_htlcs. len( ) ) ;
3298
3298
3299
- Ok ( ( Some ( ( msgs:: CommitmentUpdate {
3300
- update_add_htlcs,
3301
- update_fulfill_htlcs,
3302
- update_fail_htlcs,
3303
- update_fail_malformed_htlcs : Vec :: new ( ) ,
3304
- update_fee,
3305
- commitment_signed,
3306
- } , monitor_update) ) , htlcs_to_fail) )
3299
+ self . monitor_updating_paused ( false , true , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
3300
+ self . pending_monitor_updates . push ( monitor_update) ;
3301
+ ( Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) ) , htlcs_to_fail)
3307
3302
} else {
3308
- Ok ( ( None , Vec :: new ( ) ) )
3303
+ ( None , Vec :: new ( ) )
3309
3304
}
3310
3305
}
3311
3306
@@ -3515,17 +3510,9 @@ impl<Signer: Sign> Channel<Signer> {
3515
3510
return Ok ( ( Vec :: new ( ) , self . pending_monitor_updates . last ( ) . unwrap ( ) ) ) ;
3516
3511
}
3517
3512
3518
- match self . free_holding_cell_htlcs ( logger) ? {
3519
- ( Some ( ( mut commitment_update, mut additional_update) ) , htlcs_to_fail) => {
3520
- commitment_update. update_fail_htlcs . reserve ( update_fail_htlcs. len ( ) ) ;
3521
- for fail_msg in update_fail_htlcs. drain ( ..) {
3522
- commitment_update. update_fail_htlcs . push ( fail_msg) ;
3523
- }
3524
- commitment_update. update_fail_malformed_htlcs . reserve ( update_fail_malformed_htlcs. len ( ) ) ;
3525
- for fail_msg in update_fail_malformed_htlcs. drain ( ..) {
3526
- commitment_update. update_fail_malformed_htlcs . push ( fail_msg) ;
3527
- }
3528
-
3513
+ match self . free_holding_cell_htlcs ( logger) {
3514
+ ( Some ( _) , htlcs_to_fail) => {
3515
+ let mut additional_update = self . pending_monitor_updates . pop ( ) . unwrap ( ) ;
3529
3516
// free_holding_cell_htlcs may bump latest_monitor_id multiple times but we want them to be
3530
3517
// strictly increasing by one, so decrement it here.
3531
3518
self . latest_monitor_update_id = monitor_update. update_id ;
@@ -5845,8 +5832,12 @@ impl<Signer: Sign> Channel<Signer> {
5845
5832
5846
5833
/// Begins the shutdown process, getting a message for the remote peer and returning all
5847
5834
/// holding cell HTLCs for payment failure.
5848
- pub fn get_shutdown < SP : Deref > ( & mut self , signer_provider : & SP , their_features : & InitFeatures , target_feerate_sats_per_kw : Option < u32 > )
5849
- -> Result < ( msgs:: Shutdown , Option < ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) , APIError >
5835
+ ///
5836
+ /// May jump to the channel being fully shutdown (see [`Self::is_shutdown`]) in which case no
5837
+ /// [`ChannelMonitorUpdate`] will be returned).
5838
+ pub fn get_shutdown < SP : Deref > ( & mut self , signer_provider : & SP , their_features : & InitFeatures ,
5839
+ target_feerate_sats_per_kw : Option < u32 > )
5840
+ -> Result < ( msgs:: Shutdown , Option < & ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > ) , APIError >
5850
5841
where SP :: Target : SignerProvider {
5851
5842
for htlc in self . pending_outbound_htlcs . iter ( ) {
5852
5843
if let OutboundHTLCState :: LocalAnnounced ( _) = htlc. state {
@@ -5889,12 +5880,15 @@ impl<Signer: Sign> Channel<Signer> {
5889
5880
5890
5881
let monitor_update = if update_shutdown_script {
5891
5882
self . latest_monitor_update_id += 1 ;
5892
- Some ( ChannelMonitorUpdate {
5883
+ let monitor_update = ChannelMonitorUpdate {
5893
5884
update_id : self . latest_monitor_update_id ,
5894
5885
updates : vec ! [ ChannelMonitorUpdateStep :: ShutdownScript {
5895
5886
scriptpubkey: self . get_closing_scriptpubkey( ) ,
5896
5887
} ] ,
5897
- } )
5888
+ } ;
5889
+ self . monitor_updating_paused ( false , false , false , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
5890
+ self . pending_monitor_updates . push ( monitor_update) ;
5891
+ Some ( self . pending_monitor_updates . last ( ) . unwrap ( ) )
5898
5892
} else { None } ;
5899
5893
let shutdown = msgs:: Shutdown {
5900
5894
channel_id : self . channel_id ,
@@ -5915,6 +5909,9 @@ impl<Signer: Sign> Channel<Signer> {
5915
5909
}
5916
5910
} ) ;
5917
5911
5912
+ debug_assert ! ( !self . is_shutdown( ) || monitor_update. is_none( ) ,
5913
+ "we can't both complete shutdown and return a monitor update" ) ;
5914
+
5918
5915
Ok ( ( shutdown, monitor_update, dropped_outbound_htlcs) )
5919
5916
}
5920
5917
0 commit comments