@@ -370,6 +370,12 @@ pub const FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE: u64 = 2;
370
370
#[ cfg( not( fuzzing) ) ]
371
371
const FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE : u64 = 2 ;
372
372
373
+ /// If after this value tick periods, the channel feerate isn't satisfying, we auto-close the
374
+ /// channel and goes on-chain to avoid unsafe situations where a commitment transaction with
375
+ /// time-sensitive outputs won't confirm due to staling feerate too far away from the upper feerate
376
+ /// groups of network mempools.
377
+ const AUTOCLOSE_TIMEOUT : u16 = 6 ;
378
+
373
379
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
374
380
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
375
381
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
@@ -392,6 +398,10 @@ pub(super) struct Channel<Signer: Sign> {
392
398
393
399
latest_monitor_update_id : u64 ,
394
400
401
+ // Auto-close timer, if the channel is outbound, and we didn't receive a RAA for counterparty
402
+ // commitment transaction after `AUTOCLOSE_TIMEOUT` periods, this channel must be force-closed.
403
+ autoclose_timer : u16 ,
404
+
395
405
holder_signer : Signer ,
396
406
shutdown_scriptpubkey : Option < ShutdownScript > ,
397
407
destination_script : Script ,
@@ -682,6 +692,8 @@ impl<Signer: Sign> Channel<Signer> {
682
692
683
693
latest_monitor_update_id : 0 ,
684
694
695
+ autoclose_timer : 0 ,
696
+
685
697
holder_signer,
686
698
shutdown_scriptpubkey,
687
699
destination_script : keys_provider. get_destination_script ( ) ,
@@ -945,6 +957,8 @@ impl<Signer: Sign> Channel<Signer> {
945
957
946
958
latest_monitor_update_id : 0 ,
947
959
960
+ autoclose_timer : 0 ,
961
+
948
962
holder_signer,
949
963
shutdown_scriptpubkey,
950
964
destination_script : keys_provider. get_destination_script ( ) ,
@@ -2720,6 +2734,18 @@ impl<Signer: Sign> Channel<Signer> {
2720
2734
}
2721
2735
}
2722
2736
2737
+ /// Trigger the autoclose timer if it's in the starting position
2738
+ fn maybe_trigger_autoclose_timer ( & mut self ) {
2739
+ // Start an auto-close timer, if the channel feerate doesn't increase before its
2740
+ // expiration (i.e this outbound feerate update has been committed on both sides),
2741
+ // the channel will be marked as unsafe and force-closed.
2742
+ // If a timer is already pending, no-op, as a higher-feerate `update_fee` will
2743
+ // implicitly override a lower-feerate `update_fee` part of the same update sequence.
2744
+ if self . autoclose_timer == 0 {
2745
+ self . autoclose_timer = 1 ;
2746
+ }
2747
+ }
2748
+
2723
2749
/// Handles receiving a remote's revoke_and_ack. Note that we may return a new
2724
2750
/// commitment_signed message here in case we had pending outbound HTLCs to add which were
2725
2751
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
@@ -2878,6 +2904,7 @@ impl<Signer: Sign> Channel<Signer> {
2878
2904
log_trace ! ( logger, " ...promoting outbound fee update {} to Committed" , feerate) ;
2879
2905
self . feerate_per_kw = feerate;
2880
2906
self . pending_update_fee = None ;
2907
+ self . autoclose_timer = 0 ;
2881
2908
} ,
2882
2909
FeeUpdateState :: RemoteAnnounced => { debug_assert ! ( !self . is_outbound( ) ) ; } ,
2883
2910
FeeUpdateState :: AwaitingRemoteRevokeToAnnounce => {
@@ -2974,6 +3001,8 @@ impl<Signer: Sign> Channel<Signer> {
2974
3001
return None ;
2975
3002
}
2976
3003
3004
+ self . maybe_trigger_autoclose_timer ( ) ;
3005
+
2977
3006
debug_assert ! ( self . pending_update_fee. is_none( ) ) ;
2978
3007
self . pending_update_fee = Some ( ( feerate_per_kw, FeeUpdateState :: Outbound ) ) ;
2979
3008
@@ -3163,6 +3192,22 @@ impl<Signer: Sign> Channel<Signer> {
3163
3192
Ok ( ( ) )
3164
3193
}
3165
3194
3195
+ /// If the auto-close timer is reached following the triggering of a auto-close condition
3196
+ /// (i.e a non-satisfying feerate to ensure efficient confirmation), we force-close
3197
+ /// channel, hopefully narrowing the safety risks for the user funds.
3198
+ pub fn check_autoclose ( & mut self ) -> Result < ( ) , ChannelError > {
3199
+ if self . autoclose_timer > 0 && self . autoclose_timer < AUTOCLOSE_TIMEOUT {
3200
+ self . autoclose_timer += 1 ;
3201
+ }
3202
+ if self . autoclose_timer == AUTOCLOSE_TIMEOUT {
3203
+ // If the channel doesn't have pending HTLC outputs to claim on-chain
3204
+ if self . pending_inbound_htlcs . len ( ) + self . pending_outbound_htlcs . len ( ) > 0 {
3205
+ return Err ( ChannelError :: Close ( "Channel has time-sensitive outputs and the auto-close timer has been reached" . to_owned ( ) ) ) ;
3206
+ }
3207
+ }
3208
+ Ok ( ( ) )
3209
+ }
3210
+
3166
3211
fn get_last_revoke_and_ack ( & self ) -> msgs:: RevokeAndACK {
3167
3212
let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
3168
3213
let per_commitment_secret = self . holder_signer . release_commitment_secret ( self . cur_holder_commitment_transaction_number + 2 ) ;
@@ -5178,6 +5223,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
5178
5223
( 5 , self . config, required) ,
5179
5224
( 7 , self . shutdown_scriptpubkey, option) ,
5180
5225
( 9 , self . target_closing_feerate_sats_per_kw, option) ,
5226
+ ( 11 , self . autoclose_timer, required) ,
5181
5227
} ) ;
5182
5228
5183
5229
Ok ( ( ) )
@@ -5411,13 +5457,15 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5411
5457
5412
5458
let mut announcement_sigs = None ;
5413
5459
let mut target_closing_feerate_sats_per_kw = None ;
5460
+ let mut autoclose_timer = 0 ;
5414
5461
read_tlv_fields ! ( reader, {
5415
5462
( 0 , announcement_sigs, option) ,
5416
5463
( 1 , minimum_depth, option) ,
5417
5464
( 3 , counterparty_selected_channel_reserve_satoshis, option) ,
5418
5465
( 5 , config, option) , // Note that if none is provided we will *not* overwrite the existing one.
5419
5466
( 7 , shutdown_scriptpubkey, option) ,
5420
5467
( 9 , target_closing_feerate_sats_per_kw, option) ,
5468
+ ( 11 , autoclose_timer, required) ,
5421
5469
} ) ;
5422
5470
5423
5471
let mut secp_ctx = Secp256k1 :: new ( ) ;
@@ -5434,6 +5482,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
5434
5482
5435
5483
latest_monitor_update_id,
5436
5484
5485
+ autoclose_timer,
5486
+
5437
5487
holder_signer,
5438
5488
shutdown_scriptpubkey,
5439
5489
destination_script,
0 commit comments