@@ -32,7 +32,14 @@ use crate::ln::msgs::DecodeError;
32
32
use crate::ln::script::{self, ShutdownScript};
33
33
use crate::ln::channel_state::{ChannelShutdownState, CounterpartyForwardingInfo, InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails, OutboundHTLCStateDetails};
34
34
use crate::ln::channelmanager::{self, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
35
- use crate::ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, htlc_success_tx_weight, htlc_timeout_tx_weight, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor, ClosingTransaction};
35
+ use crate::ln::chan_utils::{
36
+ CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, htlc_success_tx_weight,
37
+ htlc_timeout_tx_weight, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction,
38
+ HolderCommitmentTransaction, ChannelTransactionParameters,
39
+ CounterpartyChannelTransactionParameters, MAX_HTLCS,
40
+ get_commitment_transaction_number_obscure_factor,
41
+ ClosingTransaction, commit_tx_fee_sat, per_outbound_htlc_counterparty_commit_tx_fee_msat,
42
+ };
36
43
use crate::ln::chan_utils;
37
44
use crate::ln::onion_utils::HTLCFailReason;
38
45
use crate::chain::BestBlock;
@@ -655,17 +662,6 @@ pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
655
662
656
663
pub const DEFAULT_MAX_HTLCS: u16 = 50;
657
664
658
- pub(crate) fn commitment_tx_base_weight(channel_type_features: &ChannelTypeFeatures) -> u64 {
659
- const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
660
- const COMMITMENT_TX_BASE_ANCHOR_WEIGHT: u64 = 1124;
661
- if channel_type_features.supports_anchors_zero_fee_htlc_tx() { COMMITMENT_TX_BASE_ANCHOR_WEIGHT } else { COMMITMENT_TX_BASE_WEIGHT }
662
- }
663
-
664
- #[cfg(not(test))]
665
- const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
666
- #[cfg(test)]
667
- pub const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
668
-
669
665
pub const ANCHOR_OUTPUT_VALUE_SATOSHI: u64 = 330;
670
666
671
667
/// The percentage of the channel value `holder_max_htlc_value_in_flight_msat` used to be set to,
@@ -1621,7 +1617,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1621
1617
0
1622
1618
};
1623
1619
let funders_amount_msat = open_channel_fields.funding_satoshis * 1000 - msg_push_msat;
1624
- let commitment_tx_fee = commit_tx_fee_msat (open_channel_fields.commitment_feerate_sat_per_1000_weight, MIN_AFFORDABLE_HTLC_COUNT, &channel_type) / 1000 ;
1620
+ let commitment_tx_fee = commit_tx_fee_sat (open_channel_fields.commitment_feerate_sat_per_1000_weight, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
1625
1621
if (funders_amount_msat / 1000).saturating_sub(anchor_outputs_value) < commitment_tx_fee {
1626
1622
return Err(ChannelError::close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", (funders_amount_msat / 1000).saturating_sub(anchor_outputs_value), commitment_tx_fee)));
1627
1623
}
@@ -1887,7 +1883,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1887
1883
let commitment_feerate = fee_estimator.bounded_sat_per_1000_weight(commitment_conf_target);
1888
1884
1889
1885
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
1890
- let commitment_tx_fee = commit_tx_fee_msat (commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type);
1886
+ let commitment_tx_fee = commit_tx_fee_sat (commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type) * 1000 ;
1891
1887
if value_to_self_msat.saturating_sub(anchor_outputs_value_msat) < commitment_tx_fee {
1892
1888
return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", value_to_self_msat / 1000, commitment_tx_fee / 1000) });
1893
1889
}
@@ -2948,7 +2944,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2948
2944
let on_counterparty_tx_nondust_htlcs =
2949
2945
on_counterparty_tx_accepted_nondust_htlcs + on_counterparty_tx_offered_nondust_htlcs;
2950
2946
on_counterparty_tx_dust_exposure_msat +=
2951
- commit_tx_fee_msat (excess_feerate, on_counterparty_tx_nondust_htlcs, &self.channel_type);
2947
+ commit_tx_fee_sat (excess_feerate, on_counterparty_tx_nondust_htlcs, &self.channel_type) * 1000 ;
2952
2948
if !self.channel_type.supports_anchors_zero_fee_htlc_tx() {
2953
2949
on_counterparty_tx_dust_exposure_msat +=
2954
2950
on_counterparty_tx_accepted_nondust_htlcs as u64 * htlc_success_tx_weight(&self.channel_type)
@@ -3313,12 +3309,12 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3313
3309
}
3314
3310
3315
3311
let num_htlcs = included_htlcs + addl_htlcs;
3316
- let res = commit_tx_fee_msat (context.feerate_per_kw, num_htlcs, &context.channel_type);
3312
+ let res = commit_tx_fee_sat (context.feerate_per_kw, num_htlcs, &context.channel_type) * 1000 ;
3317
3313
#[cfg(any(test, fuzzing))]
3318
3314
{
3319
3315
let mut fee = res;
3320
3316
if fee_spike_buffer_htlc.is_some() {
3321
- fee = commit_tx_fee_msat (context.feerate_per_kw, num_htlcs - 1, &context.channel_type);
3317
+ fee = commit_tx_fee_sat (context.feerate_per_kw, num_htlcs - 1, &context.channel_type) * 1000 ;
3322
3318
}
3323
3319
let total_pending_htlcs = context.pending_inbound_htlcs.len() + context.pending_outbound_htlcs.len()
3324
3320
+ context.holding_cell_htlc_updates.len();
@@ -3404,12 +3400,12 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3404
3400
}
3405
3401
3406
3402
let num_htlcs = included_htlcs + addl_htlcs;
3407
- let res = commit_tx_fee_msat (context.feerate_per_kw, num_htlcs, &context.channel_type);
3403
+ let res = commit_tx_fee_sat (context.feerate_per_kw, num_htlcs, &context.channel_type) * 1000 ;
3408
3404
#[cfg(any(test, fuzzing))]
3409
3405
{
3410
3406
let mut fee = res;
3411
3407
if fee_spike_buffer_htlc.is_some() {
3412
- fee = commit_tx_fee_msat (context.feerate_per_kw, num_htlcs - 1, &context.channel_type);
3408
+ fee = commit_tx_fee_sat (context.feerate_per_kw, num_htlcs - 1, &context.channel_type) * 1000 ;
3413
3409
}
3414
3410
let total_pending_htlcs = context.pending_inbound_htlcs.len() + context.pending_outbound_htlcs.len();
3415
3411
let commitment_tx_info = CommitmentTxInfoCached {
@@ -3675,32 +3671,6 @@ fn get_v2_channel_reserve_satoshis(channel_value_satoshis: u64, dust_limit_satos
3675
3671
cmp::min(channel_value_satoshis, cmp::max(q, dust_limit_satoshis))
3676
3672
}
3677
3673
3678
- // Get the fee cost in SATS of a commitment tx with a given number of HTLC outputs.
3679
- // Note that num_htlcs should not include dust HTLCs.
3680
- #[inline]
3681
- fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
3682
- feerate_per_kw as u64 * (commitment_tx_base_weight(channel_type_features) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
3683
- }
3684
-
3685
- // Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
3686
- // Note that num_htlcs should not include dust HTLCs.
3687
- pub(crate) fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize, channel_type_features: &ChannelTypeFeatures) -> u64 {
3688
- // Note that we need to divide before multiplying to round properly,
3689
- // since the lowest denomination of bitcoin on-chain is the satoshi.
3690
- (commitment_tx_base_weight(channel_type_features) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate_per_kw as u64 / 1000 * 1000
3691
- }
3692
-
3693
- pub(crate) fn per_outbound_htlc_counterparty_commit_tx_fee_msat(feerate_per_kw: u32, channel_type_features: &ChannelTypeFeatures) -> u64 {
3694
- // Note that we need to divide before multiplying to round properly,
3695
- // since the lowest denomination of bitcoin on-chain is the satoshi.
3696
- let commitment_tx_fee = COMMITMENT_TX_WEIGHT_PER_HTLC * feerate_per_kw as u64 / 1000 * 1000;
3697
- if channel_type_features.supports_anchors_zero_fee_htlc_tx() {
3698
- commitment_tx_fee + htlc_success_tx_weight(channel_type_features) * feerate_per_kw as u64 / 1000
3699
- } else {
3700
- commitment_tx_fee
3701
- }
3702
- }
3703
-
3704
3674
/// Context for dual-funded channels.
3705
3675
#[cfg(any(dual_funding, splicing))]
3706
3676
pub(super) struct DualFundingChannelContext {
@@ -7395,7 +7365,7 @@ impl<SP: Deref> Channel<SP> where
7395
7365
&& info.next_holder_htlc_id == self.context.next_holder_htlc_id
7396
7366
&& info.next_counterparty_htlc_id == self.context.next_counterparty_htlc_id
7397
7367
&& info.feerate == self.context.feerate_per_kw {
7398
- let actual_fee = commit_tx_fee_msat (self.context.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.context.get_channel_type());
7368
+ let actual_fee = commit_tx_fee_sat (self.context.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.context.get_channel_type()) * 1000 ;
7399
7369
assert_eq!(actual_fee, info.fee);
7400
7370
}
7401
7371
}
@@ -9596,7 +9566,7 @@ mod tests {
9596
9566
use crate::ln::channel_keys::{RevocationKey, RevocationBasepoint};
9597
9567
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
9598
9568
use crate::ln::channel::InitFeatures;
9599
- use crate::ln::channel::{AwaitingChannelReadyFlags, Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_msat };
9569
+ use crate::ln::channel::{AwaitingChannelReadyFlags, Channel, ChannelState, InboundHTLCOutput, OutboundV1Channel, InboundV1Channel, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, HTLCUpdateAwaitingACK, commit_tx_fee_sat };
9600
9570
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
9601
9571
use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
9602
9572
use crate::ln::msgs;
@@ -9819,13 +9789,13 @@ mod tests {
9819
9789
// the dust limit check.
9820
9790
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
9821
9791
let local_commit_tx_fee = node_a_chan.context.next_local_commit_tx_fee_msat(htlc_candidate, None);
9822
- let local_commit_fee_0_htlcs = commit_tx_fee_msat (node_a_chan.context.feerate_per_kw, 0, node_a_chan.context.get_channel_type());
9792
+ let local_commit_fee_0_htlcs = commit_tx_fee_sat (node_a_chan.context.feerate_per_kw, 0, node_a_chan.context.get_channel_type()) * 1000 ;
9823
9793
assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
9824
9794
9825
9795
// Finally, make sure that when Node A calculates the remote's commitment transaction fees, all
9826
9796
// of the HTLCs are seen to be above the dust limit.
9827
9797
node_a_chan.context.channel_transaction_parameters.is_outbound_from_holder = false;
9828
- let remote_commit_fee_3_htlcs = commit_tx_fee_msat (node_a_chan.context.feerate_per_kw, 3, node_a_chan.context.get_channel_type());
9798
+ let remote_commit_fee_3_htlcs = commit_tx_fee_sat (node_a_chan.context.feerate_per_kw, 3, node_a_chan.context.get_channel_type()) * 1000 ;
9829
9799
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
9830
9800
let remote_commit_tx_fee = node_a_chan.context.next_remote_commit_tx_fee_msat(htlc_candidate, None);
9831
9801
assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
@@ -9848,8 +9818,8 @@ mod tests {
9848
9818
let config = UserConfig::default();
9849
9819
let mut chan = OutboundV1Channel::<&TestKeysInterface>::new(&fee_est, &&keys_provider, &&keys_provider, node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42, None, &logger).unwrap();
9850
9820
9851
- let commitment_tx_fee_0_htlcs = commit_tx_fee_msat (chan.context.feerate_per_kw, 0, chan.context.get_channel_type());
9852
- let commitment_tx_fee_1_htlc = commit_tx_fee_msat (chan.context.feerate_per_kw, 1, chan.context.get_channel_type());
9821
+ let commitment_tx_fee_0_htlcs = commit_tx_fee_sat (chan.context.feerate_per_kw, 0, chan.context.get_channel_type()) * 1000 ;
9822
+ let commitment_tx_fee_1_htlc = commit_tx_fee_sat (chan.context.feerate_per_kw, 1, chan.context.get_channel_type()) * 1000 ;
9853
9823
9854
9824
// If HTLC_SUCCESS_TX_WEIGHT and HTLC_TIMEOUT_TX_WEIGHT were swapped: then this HTLC would be
9855
9825
// counted as dust when it shouldn't be.
0 commit comments