@@ -244,8 +244,6 @@ enum ChannelState {
244
244
RemoteShutdownSent = 1 << 10 ,
245
245
/// Flag which is set on ChannelFunded or FundingSent after sending a shutdown message. At this
246
246
/// point, we may not add any new HTLCs to the channel.
247
- /// TODO: Investigate some kind of timeout mechanism by which point the remote end must provide
248
- /// us their shutdown.
249
247
LocalShutdownSent = 1 << 11 ,
250
248
/// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
251
249
/// to drop us, but we store this anyway.
@@ -473,6 +471,11 @@ pub(super) struct Channel<Signer: Sign> {
473
471
commitment_secrets : CounterpartyCommitmentSecrets ,
474
472
475
473
channel_update_status : ChannelUpdateStatus ,
474
+ /// Once we reach `closing_negotiation_ready`, we set this, indicating if closing_signed does
475
+ /// not complete within a single timer tick (one minte), we should force-close the channel.
476
+ /// Note that this field is reset to false on deserialization to give us a chance to connect to
477
+ /// our peer and start the closing_signed negotiation fresh.
478
+ closing_signed_in_flight : bool ,
476
479
477
480
/// Our counterparty's channel_announcement signatures provided in announcement_signatures.
478
481
/// This can be used to rebroadcast the channel_announcement message later.
@@ -699,6 +702,7 @@ impl<Signer: Sign> Channel<Signer> {
699
702
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
700
703
701
704
channel_update_status : ChannelUpdateStatus :: Enabled ,
705
+ closing_signed_in_flight : false ,
702
706
703
707
announcement_sigs : None ,
704
708
@@ -949,6 +953,7 @@ impl<Signer: Sign> Channel<Signer> {
949
953
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
950
954
951
955
channel_update_status : ChannelUpdateStatus :: Enabled ,
956
+ closing_signed_in_flight : false ,
952
957
953
958
announcement_sigs : None ,
954
959
@@ -3275,16 +3280,38 @@ impl<Signer: Sign> Channel<Signer> {
3275
3280
self . closing_fee_limits = Some ( ( proposed_total_fee_satoshis, proposed_max_total_fee_satoshis) ) ;
3276
3281
}
3277
3282
3283
+ /// Returns true if we're ready to commence the closing_signed negotiation phase. This is true
3284
+ /// after both sides have exchanged a `shutdown` message and all HTLCs have been drained. At
3285
+ /// this point if we're the funder we should send the initial closing_signed, and in any case
3286
+ /// shutdown should complete within a reasonable timeframe.
3287
+ fn closing_negotiation_ready ( & self ) -> bool {
3288
+ self . pending_inbound_htlcs . is_empty ( ) && self . pending_outbound_htlcs . is_empty ( ) &&
3289
+ self . channel_state &
3290
+ ( BOTH_SIDES_SHUTDOWN_MASK | ChannelState :: AwaitingRemoteRevoke as u32 |
3291
+ ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateFailed as u32 )
3292
+ == BOTH_SIDES_SHUTDOWN_MASK &&
3293
+ self . pending_update_fee . is_none ( )
3294
+ }
3295
+
3296
+ /// Checks if the closing_signed negotiation is making appropriate progress, possibly returning
3297
+ /// an Err if no progress is being made and the channel should be force-closed instead.
3298
+ /// Should be called on a one-minute timer.
3299
+ pub fn timer_check_closing_negotiation_progress ( & mut self ) -> Result < ( ) , ChannelError > {
3300
+ if self . closing_negotiation_ready ( ) {
3301
+ if self . closing_signed_in_flight {
3302
+ return Err ( ChannelError :: Close ( "closing_signed negotiation failed to finish within one minute" . to_owned ( ) ) ) ;
3303
+ } else {
3304
+ self . closing_signed_in_flight = true ;
3305
+ }
3306
+ }
3307
+ Ok ( ( ) )
3308
+ }
3309
+
3278
3310
pub fn maybe_propose_first_closing_signed < F : Deref , L : Deref > ( & mut self , fee_estimator : & F , logger : & L )
3279
3311
-> Result < Option < msgs:: ClosingSigned > , ChannelError >
3280
3312
where F :: Target : FeeEstimator , L :: Target : Logger
3281
3313
{
3282
- if !self . is_outbound ( ) || !self . pending_inbound_htlcs . is_empty ( ) || !self . pending_outbound_htlcs . is_empty ( ) ||
3283
- self . channel_state &
3284
- ( BOTH_SIDES_SHUTDOWN_MASK | ChannelState :: AwaitingRemoteRevoke as u32 |
3285
- ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateFailed as u32 )
3286
- != BOTH_SIDES_SHUTDOWN_MASK ||
3287
- self . last_sent_closing_fee . is_some ( ) || self . pending_update_fee . is_some ( ) {
3314
+ if !self . is_outbound ( ) || self . last_sent_closing_fee . is_some ( ) || !self . closing_negotiation_ready ( ) {
3288
3315
return Ok ( None ) ;
3289
3316
}
3290
3317
@@ -5228,6 +5255,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5228
5255
commitment_secrets,
5229
5256
5230
5257
channel_update_status,
5258
+ closing_signed_in_flight : false ,
5231
5259
5232
5260
announcement_sigs,
5233
5261
0 commit comments