Skip to content

Commit 12fd6c3

Browse files
committed
f: Tweak next commit tx fee msat parameters
1 parent e538b7a commit 12fd6c3

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

lightning/src/ln/channel.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,6 @@ pub enum AnnouncementSigsState {
10591059

10601060
/// An enum indicating whether the local or remote side offered a given HTLC.
10611061
enum HTLCInitiator {
1062-
LocalOffered,
10631062
RemoteOffered,
10641063
}
10651064

@@ -4145,7 +4144,7 @@ where
41454144
{
41464145
let remote_commit_tx_fee_msat = if funding.is_outbound() { 0 } else {
41474146
let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
4148-
self.next_remote_commit_tx_fee_msat(funding, Some(htlc_candidate), None) // Don't include the extra fee spike buffer HTLC in calculations
4147+
self.next_remote_commit_tx_fee_msat(funding, Some(htlc_candidate), 0) // Don't include the extra fee spike buffer HTLC in calculations
41494148
};
41504149
if remote_balance_before_fee_msat.saturating_sub(msg.amount_msat) < remote_commit_tx_fee_msat {
41514150
return Err(ChannelError::close("Remote HTLC add would not leave enough to pay for fees".to_owned()));
@@ -4158,7 +4157,7 @@ where
41584157
if funding.is_outbound() {
41594158
// Check that they won't violate our local required channel reserve by adding this HTLC.
41604159
let htlc_candidate = HTLCCandidate::new(msg.amount_msat, HTLCInitiator::RemoteOffered);
4161-
let local_commit_tx_fee_msat = self.next_local_commit_tx_fee_msat(funding, htlc_candidate, None);
4160+
let local_commit_tx_fee_msat = self.next_local_commit_tx_fee_msat(funding, Some(htlc_candidate), 0);
41624161
if local_balance_before_fee_msat < funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 + local_commit_tx_fee_msat {
41634162
return Err(ChannelError::close("Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value".to_owned()));
41644163
}
@@ -4372,7 +4371,7 @@ where
43724371
//
43734372
// A `None` `HTLCCandidate` is used as in this case because we're already accounting for
43744373
// the incoming HTLC as it has been fully committed by both sides.
4375-
let mut remote_fee_cost_incl_stuck_buffer_msat = self.next_remote_commit_tx_fee_msat(funding, None, Some(()));
4374+
let mut remote_fee_cost_incl_stuck_buffer_msat = self.next_remote_commit_tx_fee_msat(funding, None, 1);
43764375
if !funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
43774376
remote_fee_cost_incl_stuck_buffer_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
43784377
}
@@ -4865,10 +4864,8 @@ where
48654864
// dependency.
48664865
// This complicates the computation around dust-values, up to the one-htlc-value.
48674866

4868-
let htlc_above_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000, HTLCInitiator::LocalOffered);
4869-
let mut max_reserved_commit_tx_fee_msat = context.next_local_commit_tx_fee_msat(funding, htlc_above_dust, Some(()));
4870-
let htlc_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000 - 1, HTLCInitiator::LocalOffered);
4871-
let mut min_reserved_commit_tx_fee_msat = context.next_local_commit_tx_fee_msat(funding, htlc_dust, Some(()));
4867+
let mut max_reserved_commit_tx_fee_msat = context.next_local_commit_tx_fee_msat(funding, None, 2);
4868+
let mut min_reserved_commit_tx_fee_msat = context.next_local_commit_tx_fee_msat(funding, None, 1);
48724869
if !funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
48734870
max_reserved_commit_tx_fee_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
48744871
min_reserved_commit_tx_fee_msat *= FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
@@ -4891,8 +4888,7 @@ where
48914888
} else {
48924889
// If the channel is inbound (i.e. counterparty pays the fee), we need to make sure
48934890
// sending a new HTLC won't reduce their balance below our reserve threshold.
4894-
let htlc_above_dust = HTLCCandidate::new(real_dust_limit_success_sat * 1000, HTLCInitiator::LocalOffered);
4895-
let max_reserved_commit_tx_fee_msat = context.next_remote_commit_tx_fee_msat(funding, Some(htlc_above_dust), None);
4891+
let max_reserved_commit_tx_fee_msat = context.next_remote_commit_tx_fee_msat(funding, None, 1);
48964892

48974893
let holder_selected_chan_reserve_msat = funding.holder_selected_channel_reserve_satoshis * 1000;
48984894
if remote_balance_before_fee_msat < max_reserved_commit_tx_fee_msat + holder_selected_chan_reserve_msat {
@@ -4975,18 +4971,22 @@ where
49754971
/// Dust HTLCs are excluded.
49764972
#[rustfmt::skip]
49774973
fn next_local_commit_tx_fee_msat(
4978-
&self, funding: &FundingScope, htlc: HTLCCandidate, fee_spike_buffer_htlc: Option<()>,
4974+
&self, funding: &FundingScope, htlc: Option<HTLCCandidate>, addl_nondust_htlc: usize,
49794975
) -> u64 {
4976+
debug_assert!(htlc.is_some() || addl_nondust_htlc != 0, "At least one of the options must be set");
4977+
49804978
let context = self;
49814979
assert!(funding.is_outbound());
49824980
let mut htlcs: Vec<HTLCAmountDirection> = Vec::new();
49834981

4984-
match htlc.origin {
4985-
HTLCInitiator::LocalOffered => {
4986-
htlcs.push(HTLCAmountDirection { offered: true, amount_msat: htlc.amount_msat });
4987-
},
4988-
HTLCInitiator::RemoteOffered => {
4989-
htlcs.push(HTLCAmountDirection { offered: false, amount_msat: htlc.amount_msat });
4982+
if let Some(htlc) = &htlc {
4983+
match htlc.origin {
4984+
HTLCInitiator::LocalOffered => {
4985+
htlcs.push(HTLCAmountDirection { offered: true, amount_msat: htlc.amount_msat });
4986+
},
4987+
HTLCInitiator::RemoteOffered => {
4988+
htlcs.push(HTLCAmountDirection { offered: false, amount_msat: htlc.amount_msat });
4989+
}
49904990
}
49914991
}
49924992

@@ -5017,13 +5017,13 @@ where
50175017
}
50185018

50195019
#[cfg(any(test, fuzzing))]
5020-
let commit_tx_fee_msat = SpecTxBuilder {}.commit_tx_fee_sat(context.holder_dust_limit_satoshis, context.feerate_per_kw, &htlcs, fee_spike_buffer_htlc.map(|_| 1).unwrap_or(0), funding.get_channel_type()) * 1000;
5020+
let commit_tx_fee_msat = SpecTxBuilder {}.commit_tx_fee_sat(context.holder_dust_limit_satoshis, context.feerate_per_kw, &htlcs, addl_nondust_htlc, funding.get_channel_type()) * 1000;
50215021
#[cfg(not(any(test, fuzzing)))]
5022-
let commit_tx_fee_msat = SpecTxBuilder {}.commit_tx_fee_sat(context.holder_dust_limit_satoshis, context.feerate_per_kw, &htlcs, fee_spike_buffer_htlc.map(|_| 1).unwrap_or(0), funding.get_channel_type()) * 1000;
5022+
let commit_tx_fee_msat = SpecTxBuilder {}.commit_tx_fee_sat(context.holder_dust_limit_satoshis, context.feerate_per_kw, &htlcs, addl_nondust_htlc, funding.get_channel_type()) * 1000;
50235023
#[cfg(any(test, fuzzing))]
5024-
{
5024+
if let Some(htlc) = htlc {
50255025
let mut fee = commit_tx_fee_msat;
5026-
if fee_spike_buffer_htlc.is_some() {
5026+
if addl_nondust_htlc != 0 {
50275027
fee = SpecTxBuilder {}.commit_tx_fee_sat(context.holder_dust_limit_satoshis, context.feerate_per_kw, &htlcs, 0, funding.get_channel_type()) * 1000;
50285028
}
50295029
let total_pending_htlcs = context.pending_inbound_htlcs.len() + context.pending_outbound_htlcs.len()
@@ -5058,9 +5058,9 @@ where
50585058
/// Dust HTLCs are excluded.
50595059
#[rustfmt::skip]
50605060
fn next_remote_commit_tx_fee_msat(
5061-
&self, funding: &FundingScope, htlc: Option<HTLCCandidate>, fee_spike_buffer_htlc: Option<()>,
5061+
&self, funding: &FundingScope, htlc: Option<HTLCCandidate>, addl_nondust_htlc: usize,
50625062
) -> u64 {
5063-
debug_assert!(htlc.is_some() || fee_spike_buffer_htlc.is_some(), "At least one of the options must be set");
5063+
debug_assert!(htlc.is_some() || addl_nondust_htlc != 0, "At least one of the options must be set");
50645064

50655065
let context = self;
50665066
assert!(!funding.is_outbound());
@@ -5097,13 +5097,13 @@ where
50975097
}
50985098

50995099
#[cfg(any(test, fuzzing))]
5100-
let commit_tx_fee_msat = SpecTxBuilder {}.commit_tx_fee_sat(context.counterparty_dust_limit_satoshis, context.feerate_per_kw, &htlcs, fee_spike_buffer_htlc.map(|_| 1).unwrap_or(0), funding.get_channel_type()) * 1000;
5100+
let commit_tx_fee_msat = SpecTxBuilder {}.commit_tx_fee_sat(context.counterparty_dust_limit_satoshis, context.feerate_per_kw, &htlcs, addl_nondust_htlc, funding.get_channel_type()) * 1000;
51015101
#[cfg(not(any(test, fuzzing)))]
5102-
let commit_tx_fee_msat = SpecTxBuilder {}.commit_tx_fee_sat(context.counterparty_dust_limit_satoshis, context.feerate_per_kw, &htlcs, fee_spike_buffer_htlc.map(|_| 1).unwrap_or(0), funding.get_channel_type()) * 1000;
5102+
let commit_tx_fee_msat = SpecTxBuilder {}.commit_tx_fee_sat(context.counterparty_dust_limit_satoshis, context.feerate_per_kw, &htlcs, addl_nondust_htlc, funding.get_channel_type()) * 1000;
51035103
#[cfg(any(test, fuzzing))]
51045104
if let Some(htlc) = &htlc {
51055105
let mut fee = commit_tx_fee_msat;
5106-
if fee_spike_buffer_htlc.is_some() {
5106+
if addl_nondust_htlc != 0 {
51075107
fee = SpecTxBuilder {}.commit_tx_fee_sat(context.counterparty_dust_limit_satoshis, context.feerate_per_kw, &htlcs, 0, funding.get_channel_type()) * 1000;
51085108
}
51095109
let total_pending_htlcs = context.pending_inbound_htlcs.len() + context.pending_outbound_htlcs.len();
@@ -13450,7 +13450,7 @@ mod tests {
1345013450
// Make sure when Node A calculates their local commitment transaction, none of the HTLCs pass
1345113451
// the dust limit check.
1345213452
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
13453-
let local_commit_tx_fee = node_a_chan.context.next_local_commit_tx_fee_msat(&node_a_chan.funding, htlc_candidate, None);
13453+
let local_commit_tx_fee = node_a_chan.context.next_local_commit_tx_fee_msat(&node_a_chan.funding, Some(htlc_candidate), 0);
1345413454
let local_commit_fee_0_htlcs = commit_tx_fee_sat(node_a_chan.context.feerate_per_kw, 0, node_a_chan.funding.get_channel_type()) * 1000;
1345513455
assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
1345613456

@@ -13459,7 +13459,7 @@ mod tests {
1345913459
node_a_chan.funding.channel_transaction_parameters.is_outbound_from_holder = false;
1346013460
let remote_commit_fee_3_htlcs = commit_tx_fee_sat(node_a_chan.context.feerate_per_kw, 3, node_a_chan.funding.get_channel_type()) * 1000;
1346113461
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
13462-
let remote_commit_tx_fee = node_a_chan.context.next_remote_commit_tx_fee_msat(&node_a_chan.funding, Some(htlc_candidate), None);
13462+
let remote_commit_tx_fee = node_a_chan.context.next_remote_commit_tx_fee_msat(&node_a_chan.funding, Some(htlc_candidate), 0);
1346313463
assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
1346413464
}
1346513465

@@ -13489,27 +13489,27 @@ mod tests {
1348913489
// counted as dust when it shouldn't be.
1349013490
let htlc_amt_above_timeout = ((253 * htlc_timeout_tx_weight(chan.funding.get_channel_type()) / 1000) + chan.context.holder_dust_limit_satoshis + 1) * 1000;
1349113491
let htlc_candidate = HTLCCandidate::new(htlc_amt_above_timeout, HTLCInitiator::LocalOffered);
13492-
let commitment_tx_fee = chan.context.next_local_commit_tx_fee_msat(&chan.funding, htlc_candidate, None);
13492+
let commitment_tx_fee = chan.context.next_local_commit_tx_fee_msat(&chan.funding, Some(htlc_candidate), 0);
1349313493
assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
1349413494

1349513495
// If swapped: this HTLC would be counted as non-dust when it shouldn't be.
1349613496
let dust_htlc_amt_below_success = ((253 * htlc_success_tx_weight(chan.funding.get_channel_type()) / 1000) + chan.context.holder_dust_limit_satoshis - 1) * 1000;
1349713497
let htlc_candidate = HTLCCandidate::new(dust_htlc_amt_below_success, HTLCInitiator::RemoteOffered);
13498-
let commitment_tx_fee = chan.context.next_local_commit_tx_fee_msat(&chan.funding, htlc_candidate, None);
13498+
let commitment_tx_fee = chan.context.next_local_commit_tx_fee_msat(&chan.funding, Some(htlc_candidate), 0);
1349913499
assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
1350013500

1350113501
chan.funding.channel_transaction_parameters.is_outbound_from_holder = false;
1350213502

1350313503
// If swapped: this HTLC would be counted as non-dust when it shouldn't be.
1350413504
let dust_htlc_amt_above_timeout = ((253 * htlc_timeout_tx_weight(chan.funding.get_channel_type()) / 1000) + chan.context.counterparty_dust_limit_satoshis + 1) * 1000;
1350513505
let htlc_candidate = HTLCCandidate::new(dust_htlc_amt_above_timeout, HTLCInitiator::LocalOffered);
13506-
let commitment_tx_fee = chan.context.next_remote_commit_tx_fee_msat(&chan.funding, Some(htlc_candidate), None);
13506+
let commitment_tx_fee = chan.context.next_remote_commit_tx_fee_msat(&chan.funding, Some(htlc_candidate), 0);
1350713507
assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
1350813508

1350913509
// If swapped: this HTLC would be counted as dust when it shouldn't be.
1351013510
let htlc_amt_below_success = ((253 * htlc_success_tx_weight(chan.funding.get_channel_type()) / 1000) + chan.context.counterparty_dust_limit_satoshis - 1) * 1000;
1351113511
let htlc_candidate = HTLCCandidate::new(htlc_amt_below_success, HTLCInitiator::RemoteOffered);
13512-
let commitment_tx_fee = chan.context.next_remote_commit_tx_fee_msat(&chan.funding, Some(htlc_candidate), None);
13512+
let commitment_tx_fee = chan.context.next_remote_commit_tx_fee_msat(&chan.funding, Some(htlc_candidate), 0);
1351313513
assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
1351413514
}
1351513515

0 commit comments

Comments
 (0)