Skip to content

Commit 08ee72b

Browse files
committed
Move Channel::commit_tx_fee_msat to file-level utilities
1 parent ed6a5bb commit 08ee72b

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

lightning/src/ln/channel.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,14 @@ fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, opt_anchors: bool) -
15011501
feerate_per_kw as u64 * (commitment_tx_base_weight(opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
15021502
}
15031503

1504+
// Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
1505+
// Note that num_htlcs should not include dust HTLCs.
1506+
fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize, opt_anchors: bool) -> u64 {
1507+
// Note that we need to divide before multiplying to round properly,
1508+
// since the lowest denomination of bitcoin on-chain is the satoshi.
1509+
(commitment_tx_base_weight(opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate_per_kw as u64 / 1000 * 1000
1510+
}
1511+
15041512
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
15051513
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
15061514
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
@@ -1698,7 +1706,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
16981706
let feerate = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
16991707

17001708
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
1701-
let commitment_tx_fee = Self::commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT, channel_type.requires_anchors_zero_fee_htlc_tx());
1709+
let commitment_tx_fee = commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT, channel_type.requires_anchors_zero_fee_htlc_tx());
17021710
if value_to_self_msat < commitment_tx_fee {
17031711
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) });
17041712
}
@@ -2032,7 +2040,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
20322040
// check if the funder's amount for the initial commitment tx is sufficient
20332041
// for full fee payment plus a few HTLCs to ensure the channel will be useful.
20342042
let funders_amount_msat = msg.funding_satoshis * 1000 - msg.push_msat;
2035-
let commitment_tx_fee = Self::commit_tx_fee_msat(msg.feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT, opt_anchors) / 1000;
2043+
let commitment_tx_fee = commit_tx_fee_msat(msg.feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT, opt_anchors) / 1000;
20362044
if funders_amount_msat / 1000 < commitment_tx_fee {
20372045
return Err(ChannelError::Close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", funders_amount_msat / 1000, commitment_tx_fee)));
20382046
}
@@ -3157,14 +3165,6 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
31573165
(context.holder_selected_channel_reserve_satoshis, context.counterparty_selected_channel_reserve_satoshis)
31583166
}
31593167

3160-
// Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
3161-
// Note that num_htlcs should not include dust HTLCs.
3162-
fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize, opt_anchors: bool) -> u64 {
3163-
// Note that we need to divide before multiplying to round properly,
3164-
// since the lowest denomination of bitcoin on-chain is the satoshi.
3165-
(commitment_tx_base_weight(opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate_per_kw as u64 / 1000 * 1000
3166-
}
3167-
31683168
/// Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
31693169
/// number of pending HTLCs that are on track to be in our next commitment tx.
31703170
///
@@ -3242,12 +3242,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
32423242
}
32433243

32443244
let num_htlcs = included_htlcs + addl_htlcs;
3245-
let res = Self::commit_tx_fee_msat(context.feerate_per_kw, num_htlcs, context.opt_anchors());
3245+
let res = commit_tx_fee_msat(context.feerate_per_kw, num_htlcs, context.opt_anchors());
32463246
#[cfg(any(test, fuzzing))]
32473247
{
32483248
let mut fee = res;
32493249
if fee_spike_buffer_htlc.is_some() {
3250-
fee = Self::commit_tx_fee_msat(context.feerate_per_kw, num_htlcs - 1, context.opt_anchors());
3250+
fee = commit_tx_fee_msat(context.feerate_per_kw, num_htlcs - 1, context.opt_anchors());
32513251
}
32523252
let total_pending_htlcs = context.pending_inbound_htlcs.len() + context.pending_outbound_htlcs.len()
32533253
+ context.holding_cell_htlc_updates.len();
@@ -3333,12 +3333,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
33333333
}
33343334

33353335
let num_htlcs = included_htlcs + addl_htlcs;
3336-
let res = Self::commit_tx_fee_msat(context.feerate_per_kw, num_htlcs, context.opt_anchors());
3336+
let res = commit_tx_fee_msat(context.feerate_per_kw, num_htlcs, context.opt_anchors());
33373337
#[cfg(any(test, fuzzing))]
33383338
{
33393339
let mut fee = res;
33403340
if fee_spike_buffer_htlc.is_some() {
3341-
fee = Self::commit_tx_fee_msat(context.feerate_per_kw, num_htlcs - 1, context.opt_anchors());
3341+
fee = commit_tx_fee_msat(context.feerate_per_kw, num_htlcs - 1, context.opt_anchors());
33423342
}
33433343
let total_pending_htlcs = context.pending_inbound_htlcs.len() + context.pending_outbound_htlcs.len();
33443344
let commitment_tx_info = CommitmentTxInfoCached {
@@ -6184,7 +6184,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
61846184
&& info.next_holder_htlc_id == self.context.next_holder_htlc_id
61856185
&& info.next_counterparty_htlc_id == self.context.next_counterparty_htlc_id
61866186
&& info.feerate == self.context.feerate_per_kw {
6187-
let actual_fee = Self::commit_tx_fee_msat(self.context.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.context.opt_anchors());
6187+
let actual_fee = commit_tx_fee_msat(self.context.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.context.opt_anchors());
61886188
assert_eq!(actual_fee, info.fee);
61896189
}
61906190
}
@@ -7280,7 +7280,7 @@ mod tests {
72807280
use crate::ln::channelmanager::{self, HTLCSource, PaymentId};
72817281
#[cfg(anchors)]
72827282
use crate::ln::channel::InitFeatures;
7283-
use crate::ln::channel::{Channel, InboundHTLCOutput, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator};
7283+
use crate::ln::channel::{Channel, InboundHTLCOutput, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator, commit_tx_fee_msat};
72847284
use crate::ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS, MIN_THEIR_CHAN_RESERVE_SATOSHIS};
72857285
use crate::ln::features::ChannelTypeFeatures;
72867286
use crate::ln::msgs::{ChannelUpdate, DecodeError, UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -7480,13 +7480,13 @@ mod tests {
74807480
// the dust limit check.
74817481
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
74827482
let local_commit_tx_fee = node_a_chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
7483-
let local_commit_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.context.feerate_per_kw, 0, node_a_chan.context.opt_anchors());
7483+
let local_commit_fee_0_htlcs = commit_tx_fee_msat(node_a_chan.context.feerate_per_kw, 0, node_a_chan.context.opt_anchors());
74847484
assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
74857485

74867486
// Finally, make sure that when Node A calculates the remote's commitment transaction fees, all
74877487
// of the HTLCs are seen to be above the dust limit.
74887488
node_a_chan.context.channel_transaction_parameters.is_outbound_from_holder = false;
7489-
let remote_commit_fee_3_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.context.feerate_per_kw, 3, node_a_chan.context.opt_anchors());
7489+
let remote_commit_fee_3_htlcs = commit_tx_fee_msat(node_a_chan.context.feerate_per_kw, 3, node_a_chan.context.opt_anchors());
74907490
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
74917491
let remote_commit_tx_fee = node_a_chan.next_remote_commit_tx_fee_msat(htlc_candidate, None);
74927492
assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
@@ -7508,8 +7508,8 @@ mod tests {
75087508
let config = UserConfig::default();
75097509
let mut chan = Channel::<EnforcingSigner>::new_outbound(&fee_est, &&keys_provider, &&keys_provider, node_id, &channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42).unwrap();
75107510

7511-
let commitment_tx_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.context.feerate_per_kw, 0, chan.context.opt_anchors());
7512-
let commitment_tx_fee_1_htlc = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.context.feerate_per_kw, 1, chan.context.opt_anchors());
7511+
let commitment_tx_fee_0_htlcs = commit_tx_fee_msat(chan.context.feerate_per_kw, 0, chan.context.opt_anchors());
7512+
let commitment_tx_fee_1_htlc = commit_tx_fee_msat(chan.context.feerate_per_kw, 1, chan.context.opt_anchors());
75137513

75147514
// If HTLC_SUCCESS_TX_WEIGHT and HTLC_TIMEOUT_TX_WEIGHT were swapped: then this HTLC would be
75157515
// counted as dust when it shouldn't be.

0 commit comments

Comments
 (0)