Skip to content

Commit e06b59c

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 6f053e4 commit e06b59c

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
@@ -2050,10 +2050,10 @@ impl<Signer: Sign> Channel<Signer> {
20502050

20512051
// Get the fee cost of a commitment tx with a given number of HTLC outputs.
20522052
// Note that num_htlcs should not include dust HTLCs.
2053-
fn commit_tx_fee_msat(&self, num_htlcs: usize) -> u64 {
2053+
fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize) -> u64 {
20542054
// Note that we need to divide before multiplying to round properly,
20552055
// since the lowest denomination of bitcoin on-chain is the satoshi.
2056-
(COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * self.feerate_per_kw as u64 / 1000 * 1000
2056+
(COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate_per_kw as u64 / 1000 * 1000
20572057
}
20582058

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

21222122
let num_htlcs = included_htlcs + addl_htlcs;
2123-
let res = self.commit_tx_fee_msat(num_htlcs);
2123+
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs);
21242124
#[cfg(any(test, feature = "fuzztarget"))]
21252125
{
21262126
let mut fee = res;
21272127
if fee_spike_buffer_htlc.is_some() {
2128-
fee = self.commit_tx_fee_msat(num_htlcs - 1);
2128+
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1);
21292129
}
21302130
let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len()
21312131
+ self.holding_cell_htlc_updates.len();
@@ -2198,12 +2198,12 @@ impl<Signer: Sign> Channel<Signer> {
21982198
}
21992199

22002200
let num_htlcs = included_htlcs + addl_htlcs;
2201-
let res = self.commit_tx_fee_msat(num_htlcs);
2201+
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs);
22022202
#[cfg(any(test, feature = "fuzztarget"))]
22032203
{
22042204
let mut fee = res;
22052205
if fee_spike_buffer_htlc.is_some() {
2206-
fee = self.commit_tx_fee_msat(num_htlcs - 1);
2206+
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1);
22072207
}
22082208
let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
22092209
let commitment_tx_info = CommitmentTxInfoCached {
@@ -4805,7 +4805,7 @@ impl<Signer: Sign> Channel<Signer> {
48054805
&& info.next_holder_htlc_id == self.next_holder_htlc_id
48064806
&& info.next_counterparty_htlc_id == self.next_counterparty_htlc_id
48074807
&& info.feerate == self.feerate_per_kw {
4808-
let actual_fee = self.commit_tx_fee_msat(counterparty_commitment_tx.2);
4808+
let actual_fee = Self::commit_tx_fee_msat(self.feerate_per_kw, counterparty_commitment_tx.2);
48094809
assert_eq!(actual_fee, info.fee);
48104810
}
48114811
}
@@ -5818,13 +5818,13 @@ mod tests {
58185818
// the dust limit check.
58195819
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
58205820
let local_commit_tx_fee = node_a_chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
5821-
let local_commit_fee_0_htlcs = node_a_chan.commit_tx_fee_msat(0);
5821+
let local_commit_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 0);
58225822
assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
58235823

58245824
// Finally, make sure that when Node A calculates the remote's commitment transaction fees, all
58255825
// of the HTLCs are seen to be above the dust limit.
58265826
node_a_chan.channel_transaction_parameters.is_outbound_from_holder = false;
5827-
let remote_commit_fee_3_htlcs = node_a_chan.commit_tx_fee_msat(3);
5827+
let remote_commit_fee_3_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 3);
58285828
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
58295829
let remote_commit_tx_fee = node_a_chan.next_remote_commit_tx_fee_msat(htlc_candidate, None);
58305830
assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
@@ -5846,8 +5846,8 @@ mod tests {
58465846
let config = UserConfig::default();
58475847
let mut chan = Channel::<EnforcingSigner>::new_outbound(&&fee_est, &&keys_provider, node_id, &InitFeatures::known(), 10000000, 100000, 42, &config).unwrap();
58485848

5849-
let commitment_tx_fee_0_htlcs = chan.commit_tx_fee_msat(0);
5850-
let commitment_tx_fee_1_htlc = chan.commit_tx_fee_msat(1);
5849+
let commitment_tx_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.feerate_per_kw, 0);
5850+
let commitment_tx_fee_1_htlc = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.feerate_per_kw, 1);
58515851

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

0 commit comments

Comments
 (0)