@@ -40,7 +40,7 @@ use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLC
40
40
use ln:: channelmanager:: HTLCSource ;
41
41
use chain;
42
42
use chain:: { BestBlock , WatchedOutput } ;
43
- use chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
43
+ use chain:: chaininterface:: { BroadcasterInterface , ConfirmationTarget , FeeEstimator } ;
44
44
use chain:: transaction:: { OutPoint , TransactionData } ;
45
45
use chain:: keysinterface:: { SpendableOutputDescriptor , StaticPaymentOutputDescriptor , DelayedPaymentOutputDescriptor , Sign , KeysInterface } ;
46
46
use chain:: onchaintx:: OnchainTxHandler ;
@@ -701,6 +701,8 @@ pub(crate) struct ChannelMonitorImpl<Signer: Sign> {
701
701
/// spending CSV for revocable outputs).
702
702
htlcs_resolved_on_chain : Vec < IrrevocablyResolvedHTLC > ,
703
703
704
+ spikes_force_close_rate : u32 ,
705
+
704
706
// We simply modify best_block in Channel's block_connected so that serialization is
705
707
// consistent but hopefully the users' copy handles block_connected in a consistent way.
706
708
// (we do *not*, however, update them in update_monitor to ensure any local user copies keep
@@ -935,6 +937,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
935
937
( 1 , self . funding_spend_confirmed, option) ,
936
938
( 3 , self . htlcs_resolved_on_chain, vec_type) ,
937
939
( 5 , self . pending_monitor_events, vec_type) ,
940
+ ( 7 , self . spikes_force_close_rate, required) ,
938
941
} ) ;
939
942
940
943
Ok ( ( ) )
@@ -948,7 +951,8 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
948
951
funding_redeemscript : Script , channel_value_satoshis : u64 ,
949
952
commitment_transaction_number_obscure_factor : u64 ,
950
953
initial_holder_commitment_tx : HolderCommitmentTransaction ,
951
- best_block : BestBlock ) -> ChannelMonitor < Signer > {
954
+ best_block : BestBlock ,
955
+ spikes_force_close_rate : u32 ) -> ChannelMonitor < Signer > {
952
956
953
957
assert ! ( commitment_transaction_number_obscure_factor <= ( 1 << 48 ) ) ;
954
958
let payment_key_hash = WPubkeyHash :: hash ( & keys. pubkeys ( ) . payment_point . serialize ( ) ) ;
@@ -1036,6 +1040,8 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
1036
1040
funding_spend_confirmed : None ,
1037
1041
htlcs_resolved_on_chain : Vec :: new ( ) ,
1038
1042
1043
+ spikes_force_close_rate,
1044
+
1039
1045
best_block,
1040
1046
1041
1047
secp_ctx,
@@ -2431,7 +2437,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2431
2437
log_trace ! ( logger, "Processing {} matched transactions for block at height {}." , txn_matched. len( ) , conf_height) ;
2432
2438
debug_assert ! ( self . best_block. height( ) >= conf_height) ;
2433
2439
2434
- let should_broadcast = self . should_broadcast_holder_commitment_txn ( logger) ;
2440
+ let should_broadcast = self . should_broadcast_holder_commitment_txn ( if self . should_force_close_fee_spikes ( fee_estimator , self . current_holder_commitment_tx . feerate_per_kw ) { true } else { false } , logger) ;
2435
2441
if should_broadcast {
2436
2442
let funding_outp = HolderFundingOutput :: build ( self . funding_redeemscript . clone ( ) ) ;
2437
2443
let commitment_package = PackageTemplate :: build_package ( self . funding_info . 0 . txid . clone ( ) , self . funding_info . 0 . index as u32 , PackageSolvingData :: HolderFundingOutput ( funding_outp) , self . best_block . height ( ) , false , self . best_block . height ( ) ) ;
@@ -2622,7 +2628,18 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2622
2628
false
2623
2629
}
2624
2630
2625
- fn should_broadcast_holder_commitment_txn < L : Deref > ( & self , logger : & L ) -> bool where L :: Target : Logger {
2631
+ fn should_force_close_fee_spikes < F : Deref > ( & self , fee_estimator : & F , current_feerate : u32 ) -> bool where F :: Target : FeeEstimator {
2632
+
2633
+ let new_feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Normal ) ;
2634
+ // Verify the new feerate is at least superior by 30% of current feerate before to start
2635
+ // a autoclose timer.
2636
+ if new_feerate < current_feerate * self . spikes_force_close_rate / 1000 {
2637
+ return true ;
2638
+ }
2639
+ false
2640
+ }
2641
+
2642
+ fn should_broadcast_holder_commitment_txn < L : Deref > ( & self , spikes_force_close : bool , logger : & L ) -> bool where L :: Target : Logger {
2626
2643
// We need to consider all HTLCs which are:
2627
2644
// * in any unrevoked counterparty commitment transaction, as they could broadcast said
2628
2645
// transactions and we'd end up in a race, or
@@ -2662,7 +2679,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
2662
2679
// with CHECK_CLTV_EXPIRY_SANITY_2.
2663
2680
let htlc_outbound = $holder_tx == htlc. offered;
2664
2681
if ( htlc_outbound && htlc. cltv_expiry + LATENCY_GRACE_PERIOD_BLOCKS <= height) ||
2665
- ( !htlc_outbound && htlc. cltv_expiry <= height + CLTV_CLAIM_BUFFER && self . payment_preimages. contains_key( & htlc. payment_hash) ) {
2682
+ ( ( !htlc_outbound && ( htlc. cltv_expiry <= height + CLTV_CLAIM_BUFFER || spikes_force_close ) ) && self . payment_preimages. contains_key( & htlc. payment_hash) ) { //TODO: don't force-close if dust satoshis
2666
2683
log_info!( logger, "Force-closing channel due to {} HTLC timeout, HTLC expiry is {}" , if htlc_outbound { "outbound" } else { "inbound " } , htlc. cltv_expiry) ;
2667
2684
return true ;
2668
2685
}
@@ -3206,10 +3223,12 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
3206
3223
3207
3224
let mut funding_spend_confirmed = None ;
3208
3225
let mut htlcs_resolved_on_chain = Some ( Vec :: new ( ) ) ;
3226
+ let mut spikes_force_close_rate = 0 ;
3209
3227
read_tlv_fields ! ( reader, {
3210
3228
( 1 , funding_spend_confirmed, option) ,
3211
3229
( 3 , htlcs_resolved_on_chain, vec_type) ,
3212
3230
( 5 , pending_monitor_events, vec_type) ,
3231
+ ( 7 , spikes_force_close_rate, required) ,
3213
3232
} ) ;
3214
3233
3215
3234
let mut secp_ctx = Secp256k1 :: new ( ) ;
@@ -3262,6 +3281,8 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
3262
3281
funding_spend_confirmed,
3263
3282
htlcs_resolved_on_chain : htlcs_resolved_on_chain. unwrap ( ) ,
3264
3283
3284
+ spikes_force_close_rate,
3285
+
3265
3286
best_block,
3266
3287
3267
3288
secp_ctx,
@@ -3392,7 +3413,8 @@ mod tests {
3392
3413
( OutPoint { txid : Txid :: from_slice ( & [ 43 ; 32 ] ) . unwrap ( ) , index : 0 } , Script :: new ( ) ) ,
3393
3414
& channel_parameters,
3394
3415
Script :: new ( ) , 46 , 0 ,
3395
- HolderCommitmentTransaction :: dummy ( ) , best_block) ;
3416
+ HolderCommitmentTransaction :: dummy ( ) , best_block,
3417
+ 1300 ) ;
3396
3418
3397
3419
monitor. provide_latest_holder_commitment_tx ( HolderCommitmentTransaction :: dummy ( ) , preimages_to_holder_htlcs ! ( preimages[ 0 ..10 ] ) ) . unwrap ( ) ;
3398
3420
let dummy_txid = dummy_tx. txid ( ) ;
0 commit comments