Skip to content

Commit 47d4496

Browse files
committed
Make Channel::commit_tx_fee_msat static and take fee explicitly
This may avoid risk of bugs in the future as it requires the caller to think about the fee being used, not just blindly use the current (committed) channel feerate.
1 parent 2b4ca9e commit 47d4496

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

lightning/src/ln/channel.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,10 +2068,10 @@ impl<Signer: Sign> Channel<Signer> {
20682068

20692069
// Get the fee cost of a commitment tx with a given number of HTLC outputs.
20702070
// Note that num_htlcs should not include dust HTLCs.
2071-
fn commit_tx_fee_msat(&self, num_htlcs: usize) -> u64 {
2071+
fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize) -> u64 {
20722072
// Note that we need to divide before multiplying to round properly,
20732073
// since the lowest denomination of bitcoin on-chain is the satoshi.
2074-
(COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * self.feerate_per_kw as u64 / 1000 * 1000
2074+
(COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate_per_kw as u64 / 1000 * 1000
20752075
}
20762076

20772077
// Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
@@ -2138,12 +2138,12 @@ impl<Signer: Sign> Channel<Signer> {
21382138
}
21392139

21402140
let num_htlcs = included_htlcs + addl_htlcs;
2141-
let res = self.commit_tx_fee_msat(num_htlcs);
2141+
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs);
21422142
#[cfg(any(test, feature = "fuzztarget"))]
21432143
{
21442144
let mut fee = res;
21452145
if fee_spike_buffer_htlc.is_some() {
2146-
fee = self.commit_tx_fee_msat(num_htlcs - 1);
2146+
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1);
21472147
}
21482148
let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len()
21492149
+ self.holding_cell_htlc_updates.len();
@@ -2216,12 +2216,12 @@ impl<Signer: Sign> Channel<Signer> {
22162216
}
22172217

22182218
let num_htlcs = included_htlcs + addl_htlcs;
2219-
let res = self.commit_tx_fee_msat(num_htlcs);
2219+
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs);
22202220
#[cfg(any(test, feature = "fuzztarget"))]
22212221
{
22222222
let mut fee = res;
22232223
if fee_spike_buffer_htlc.is_some() {
2224-
fee = self.commit_tx_fee_msat(num_htlcs - 1);
2224+
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1);
22252225
}
22262226
let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
22272227
let commitment_tx_info = CommitmentTxInfoCached {
@@ -4827,7 +4827,7 @@ impl<Signer: Sign> Channel<Signer> {
48274827
&& info.next_holder_htlc_id == self.next_holder_htlc_id
48284828
&& info.next_counterparty_htlc_id == self.next_counterparty_htlc_id
48294829
&& info.feerate == self.feerate_per_kw {
4830-
let actual_fee = self.commit_tx_fee_msat(counterparty_commitment_tx.2);
4830+
let actual_fee = Self::commit_tx_fee_msat(self.feerate_per_kw, counterparty_commitment_tx.2);
48314831
assert_eq!(actual_fee, info.fee);
48324832
}
48334833
}
@@ -5852,13 +5852,13 @@ mod tests {
58525852
// the dust limit check.
58535853
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
58545854
let local_commit_tx_fee = node_a_chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
5855-
let local_commit_fee_0_htlcs = node_a_chan.commit_tx_fee_msat(0);
5855+
let local_commit_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 0);
58565856
assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
58575857

58585858
// Finally, make sure that when Node A calculates the remote's commitment transaction fees, all
58595859
// of the HTLCs are seen to be above the dust limit.
58605860
node_a_chan.channel_transaction_parameters.is_outbound_from_holder = false;
5861-
let remote_commit_fee_3_htlcs = node_a_chan.commit_tx_fee_msat(3);
5861+
let remote_commit_fee_3_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 3);
58625862
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
58635863
let remote_commit_tx_fee = node_a_chan.next_remote_commit_tx_fee_msat(htlc_candidate, None);
58645864
assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
@@ -5880,8 +5880,8 @@ mod tests {
58805880
let config = UserConfig::default();
58815881
let mut chan = Channel::<EnforcingSigner>::new_outbound(&&fee_est, &&keys_provider, node_id, &InitFeatures::known(), 10000000, 100000, 42, &config, 0).unwrap();
58825882

5883-
let commitment_tx_fee_0_htlcs = chan.commit_tx_fee_msat(0);
5884-
let commitment_tx_fee_1_htlc = chan.commit_tx_fee_msat(1);
5883+
let commitment_tx_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.feerate_per_kw, 0);
5884+
let commitment_tx_fee_1_htlc = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.feerate_per_kw, 1);
58855885

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

0 commit comments

Comments
 (0)