@@ -28,6 +28,7 @@ use crate::ln::types::PaymentPreimage;
2828use crate :: ln:: chan_utils:: { self , TxCreationKeys , HTLCOutputInCommitment } ;
2929use crate :: ln:: features:: ChannelTypeFeatures ;
3030use crate :: ln:: channel_keys:: { DelayedPaymentBasepoint , HtlcBasepoint } ;
31+ use crate :: ln:: channelmanager:: MIN_CLTV_EXPIRY_DELTA ;
3132use crate :: ln:: msgs:: DecodeError ;
3233use crate :: chain:: chaininterface:: { FeeEstimator , ConfirmationTarget , MIN_RELAY_FEE_SAT_PER_1000_WEIGHT , compute_feerate_sat_per_1000_weight, FEERATE_FLOOR_SATS_PER_KW } ;
3334use crate :: chain:: transaction:: MaybeSignedTransaction ;
@@ -104,8 +105,13 @@ pub(crate) fn verify_channel_type_features(channel_type_features: &Option<Channe
104105// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
105106pub ( crate ) const WEIGHT_REVOKED_OUTPUT : u64 = 1 + 1 + 73 + 1 + 1 + 1 + 77 ;
106107
108+ #[ cfg( not( test) ) ]
107109/// Height delay at which transactions are fee-bumped/rebroadcasted with a low priority.
108110const LOW_FREQUENCY_BUMP_INTERVAL : u32 = 15 ;
111+ #[ cfg( test) ]
112+ /// Height delay at which transactions are fee-bumped/rebroadcasted with a low priority.
113+ pub ( crate ) const LOW_FREQUENCY_BUMP_INTERVAL : u32 = 15 ;
114+
109115/// Height delay at which transactions are fee-bumped/rebroadcasted with a middle priority.
110116const MIDDLE_FREQUENCY_BUMP_INTERVAL : u32 = 3 ;
111117/// Height delay at which transactions are fee-bumped/rebroadcasted with a high priority.
@@ -945,18 +951,83 @@ impl PackageTemplate {
945951 return None ;
946952 } else { panic ! ( "API Error: Package must not be inputs empty" ) ; }
947953 }
948- /// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
949- /// output detection, we generate a first version of a claim tx and associate to it a height timer. A height timer is an absolute block
950- /// height that once reached we should generate a new bumped "version" of the claim tx to be sure that we safely claim outputs before
951- /// that our counterparty can do so. If timelock expires soon, height timer is going to be scaled down in consequence to increase
952- /// frequency of the bump and so increase our bets of success .
954+ /// Gets the next height at which we should fee-bump this package, assuming we can do so and
955+ /// the package is last fee-bumped at `current_height`.
956+ ///
957+ /// As the deadline with which to get a claim confirmed approaches, the rate at which the timer
958+ /// ticks increases .
953959 pub ( crate ) fn get_height_timer ( & self , current_height : u32 ) -> u32 {
954- if self . soonest_conf_deadline <= current_height + MIDDLE_FREQUENCY_BUMP_INTERVAL {
955- return current_height + HIGH_FREQUENCY_BUMP_INTERVAL
956- } else if self . soonest_conf_deadline - current_height <= LOW_FREQUENCY_BUMP_INTERVAL {
957- return current_height + MIDDLE_FREQUENCY_BUMP_INTERVAL
960+ let mut height_timer = current_height + LOW_FREQUENCY_BUMP_INTERVAL ;
961+ let timer_for_target_conf = |target_conf| -> u32 {
962+ if target_conf <= current_height + MIDDLE_FREQUENCY_BUMP_INTERVAL {
963+ current_height + HIGH_FREQUENCY_BUMP_INTERVAL
964+ } else if target_conf <= current_height + LOW_FREQUENCY_BUMP_INTERVAL {
965+ current_height + MIDDLE_FREQUENCY_BUMP_INTERVAL
966+ } else {
967+ current_height + LOW_FREQUENCY_BUMP_INTERVAL
968+ }
969+ } ;
970+ for ( _, input) in self . inputs . iter ( ) {
971+ match input {
972+ PackageSolvingData :: RevokedOutput ( _) => {
973+ // Revoked Outputs will become spendable by our counterparty at the height
974+ // where the CSV expires, which is also our `soonest_conf_deadline`.
975+ height_timer = cmp:: min (
976+ height_timer,
977+ timer_for_target_conf ( self . soonest_conf_deadline ) ,
978+ ) ;
979+ } ,
980+ PackageSolvingData :: RevokedHTLCOutput ( _) => {
981+ // Revoked HTLC Outputs may be spendable by our counterparty right now, but
982+ // after they spend them they still have to wait for an additional CSV delta
983+ // before they can claim the full funds. Thus, we leave the timer at
984+ // `LOW_FREQUENCY_BUMP_INTERVAL` until the HTLC output is spent, creating a
985+ // `RevokedOutput`.
986+ } ,
987+ PackageSolvingData :: CounterpartyOfferedHTLCOutput ( outp) => {
988+ // Incoming HTLCs being claimed by preimage should be claimed by the time their
989+ // CLTV unlocks.
990+ height_timer = cmp:: min (
991+ height_timer,
992+ timer_for_target_conf ( outp. htlc . cltv_expiry ) ,
993+ ) ;
994+ } ,
995+ PackageSolvingData :: HolderHTLCOutput ( outp) if outp. preimage . is_some ( ) => {
996+ // We have the same deadline here as for `CounterpartyOfferedHTLCOutput`. Note
997+ // that `outp.cltv_expiry` is always 0 in this case, but
998+ // `soonest_conf_deadline` holds the real HTLC expiry.
999+ height_timer = cmp:: min (
1000+ height_timer,
1001+ timer_for_target_conf ( self . soonest_conf_deadline ) ,
1002+ ) ;
1003+ } ,
1004+ PackageSolvingData :: CounterpartyReceivedHTLCOutput ( outp) => {
1005+ // Outgoing HTLCs being claimed through their timeout should be claimed fast
1006+ // enough to allow us to claim before the CLTV lock expires on the inbound
1007+ // edge (assuming the HTLC was forwarded).
1008+ height_timer = cmp:: min (
1009+ height_timer,
1010+ timer_for_target_conf ( outp. htlc . cltv_expiry + MIN_CLTV_EXPIRY_DELTA as u32 ) ,
1011+ ) ;
1012+ } ,
1013+ PackageSolvingData :: HolderHTLCOutput ( outp) => {
1014+ // We have the same deadline for holder timeout claims as for
1015+ // `CounterpartyReceivedHTLCOutput`
1016+ height_timer = cmp:: min (
1017+ height_timer,
1018+ timer_for_target_conf ( outp. cltv_expiry + MIN_CLTV_EXPIRY_DELTA as u32 ) ,
1019+ ) ;
1020+ } ,
1021+ PackageSolvingData :: HolderFundingOutput ( _) => {
1022+ // We should apply a smart heuristic here based on the HTLCs in the commitment
1023+ // transaction, but we don't currently have that information available so
1024+ // instead we just bump once per block.
1025+ height_timer =
1026+ cmp:: min ( height_timer, current_height + HIGH_FREQUENCY_BUMP_INTERVAL ) ;
1027+ } ,
1028+ }
9581029 }
959- current_height + LOW_FREQUENCY_BUMP_INTERVAL
1030+ height_timer
9601031 }
9611032
9621033 /// Returns value in satoshis to be included as package outgoing output amount and feerate
0 commit comments