@@ -246,8 +246,6 @@ enum ChannelState {
246
246
RemoteShutdownSent = 1 << 10 ,
247
247
/// Flag which is set on ChannelFunded or FundingSent after sending a shutdown message. At this
248
248
/// point, we may not add any new HTLCs to the channel.
249
- /// TODO: Investigate some kind of timeout mechanism by which point the remote end must provide
250
- /// us their shutdown.
251
249
LocalShutdownSent = 1 << 11 ,
252
250
/// We've successfully negotiated a closing_signed dance. At this point ChannelManager is about
253
251
/// to drop us, but we store this anyway.
@@ -486,6 +484,13 @@ pub(super) struct Channel<Signer: Sign> {
486
484
commitment_secrets : CounterpartyCommitmentSecrets ,
487
485
488
486
channel_update_status : ChannelUpdateStatus ,
487
+ /// Once we reach `closing_negotiation_ready`, we set this, indicating if closing_signed does
488
+ /// not complete within a single timer tick (one minute), we should force-close the channel.
489
+ /// This prevents us from keeping unusable channels around forever if our counterparty wishes
490
+ /// to DoS us.
491
+ /// Note that this field is reset to false on deserialization to give us a chance to connect to
492
+ /// our peer and start the closing_signed negotiation fresh.
493
+ closing_signed_in_flight : bool ,
489
494
490
495
/// Our counterparty's channel_announcement signatures provided in announcement_signatures.
491
496
/// This can be used to rebroadcast the channel_announcement message later.
@@ -723,6 +728,7 @@ impl<Signer: Sign> Channel<Signer> {
723
728
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
724
729
725
730
channel_update_status : ChannelUpdateStatus :: Enabled ,
731
+ closing_signed_in_flight : false ,
726
732
727
733
announcement_sigs : None ,
728
734
@@ -984,6 +990,7 @@ impl<Signer: Sign> Channel<Signer> {
984
990
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
985
991
986
992
channel_update_status : ChannelUpdateStatus :: Enabled ,
993
+ closing_signed_in_flight : false ,
987
994
988
995
announcement_sigs : None ,
989
996
@@ -3321,16 +3328,38 @@ impl<Signer: Sign> Channel<Signer> {
3321
3328
self . closing_fee_limits = Some ( ( proposed_total_fee_satoshis, proposed_max_total_fee_satoshis) ) ;
3322
3329
}
3323
3330
3331
+ /// Returns true if we're ready to commence the closing_signed negotiation phase. This is true
3332
+ /// after both sides have exchanged a `shutdown` message and all HTLCs have been drained. At
3333
+ /// this point if we're the funder we should send the initial closing_signed, and in any case
3334
+ /// shutdown should complete within a reasonable timeframe.
3335
+ fn closing_negotiation_ready ( & self ) -> bool {
3336
+ self . pending_inbound_htlcs . is_empty ( ) && self . pending_outbound_htlcs . is_empty ( ) &&
3337
+ self . channel_state &
3338
+ ( BOTH_SIDES_SHUTDOWN_MASK | ChannelState :: AwaitingRemoteRevoke as u32 |
3339
+ ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateFailed as u32 )
3340
+ == BOTH_SIDES_SHUTDOWN_MASK &&
3341
+ self . pending_update_fee . is_none ( )
3342
+ }
3343
+
3344
+ /// Checks if the closing_signed negotiation is making appropriate progress, possibly returning
3345
+ /// an Err if no progress is being made and the channel should be force-closed instead.
3346
+ /// Should be called on a one-minute timer.
3347
+ pub fn timer_check_closing_negotiation_progress ( & mut self ) -> Result < ( ) , ChannelError > {
3348
+ if self . closing_negotiation_ready ( ) {
3349
+ if self . closing_signed_in_flight {
3350
+ return Err ( ChannelError :: Close ( "closing_signed negotiation failed to finish within one minute" . to_owned ( ) ) ) ;
3351
+ } else {
3352
+ self . closing_signed_in_flight = true ;
3353
+ }
3354
+ }
3355
+ Ok ( ( ) )
3356
+ }
3357
+
3324
3358
pub fn maybe_propose_first_closing_signed < F : Deref , L : Deref > ( & mut self , fee_estimator : & F , logger : & L )
3325
3359
-> Result < ( Option < msgs:: ClosingSigned > , Option < Transaction > ) , ChannelError >
3326
3360
where F :: Target : FeeEstimator , L :: Target : Logger
3327
3361
{
3328
- if !self . pending_inbound_htlcs . is_empty ( ) || !self . pending_outbound_htlcs . is_empty ( ) ||
3329
- self . channel_state &
3330
- ( BOTH_SIDES_SHUTDOWN_MASK | ChannelState :: AwaitingRemoteRevoke as u32 |
3331
- ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateFailed as u32 )
3332
- != BOTH_SIDES_SHUTDOWN_MASK ||
3333
- self . last_sent_closing_fee . is_some ( ) || self . pending_update_fee . is_some ( ) {
3362
+ if self . last_sent_closing_fee . is_some ( ) || !self . closing_negotiation_ready ( ) {
3334
3363
return Ok ( ( None , None ) ) ;
3335
3364
}
3336
3365
@@ -5338,6 +5367,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5338
5367
commitment_secrets,
5339
5368
5340
5369
channel_update_status,
5370
+ closing_signed_in_flight : false ,
5341
5371
5342
5372
announcement_sigs,
5343
5373
0 commit comments