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