Skip to content

Commit 694d72b

Browse files
committed
Delete ChannelContext::next_{local, remote}_commit_tx_fee_msat
1 parent 176ef10 commit 694d72b

File tree

1 file changed

+23
-198
lines changed

1 file changed

+23
-198
lines changed

lightning/src/ln/channel.rs

Lines changed: 23 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,13 +1100,6 @@ pub enum AnnouncementSigsState {
11001100
PeerReceived,
11011101
}
11021102

1103-
/// An enum indicating whether the local or remote side offered a given HTLC.
1104-
enum HTLCInitiator {
1105-
LocalOffered,
1106-
#[allow(dead_code)]
1107-
RemoteOffered,
1108-
}
1109-
11101103
/// A struct gathering data on a commitment, either local or remote.
11111104
struct CommitmentData<'a> {
11121105
tx: CommitmentTransaction,
@@ -1127,18 +1120,6 @@ pub(crate) struct CommitmentStats {
11271120
pub remote_balance_before_fee_msat: u64,
11281121
}
11291122

1130-
/// Used when calculating whether we or the remote can afford an additional HTLC.
1131-
struct HTLCCandidate {
1132-
amount_msat: u64,
1133-
origin: HTLCInitiator,
1134-
}
1135-
1136-
impl HTLCCandidate {
1137-
fn new(amount_msat: u64, origin: HTLCInitiator) -> Self {
1138-
Self { amount_msat, origin }
1139-
}
1140-
}
1141-
11421123
/// A return value enum for get_update_fulfill_htlc. See UpdateFulfillCommitFetch variants for
11431124
/// description
11441125
enum UpdateFulfillFetch {
@@ -5004,169 +4985,6 @@ where
50044985
)
50054986
}
50064987

5007-
/// Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
5008-
/// number of pending HTLCs that are on track to be in our next commitment tx.
5009-
///
5010-
/// Includes the `HTLCCandidate` given by `htlc` and an additional non-dust HTLC if
5011-
/// `fee_spike_buffer_htlc` is `Some`.
5012-
///
5013-
/// The first extra HTLC is useful for determining whether we can accept a further HTLC, the
5014-
/// second allows for creating a buffer to ensure a further HTLC can always be accepted/added.
5015-
///
5016-
/// Dust HTLCs are excluded.
5017-
#[rustfmt::skip]
5018-
fn next_local_commit_tx_fee_msat(
5019-
&self, funding: &FundingScope, htlc: HTLCCandidate, fee_spike_buffer_htlc: Option<()>,
5020-
) -> u64 {
5021-
let context = self;
5022-
assert!(funding.is_outbound());
5023-
5024-
if funding.get_channel_type().supports_anchor_zero_fee_commitments() {
5025-
debug_assert_eq!(context.feerate_per_kw, 0);
5026-
debug_assert!(fee_spike_buffer_htlc.is_none());
5027-
return 0;
5028-
}
5029-
5030-
let (htlc_success_tx_fee_sat, htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
5031-
funding.get_channel_type(), context.feerate_per_kw,
5032-
);
5033-
let real_dust_limit_success_sat = htlc_success_tx_fee_sat + context.holder_dust_limit_satoshis;
5034-
let real_dust_limit_timeout_sat = htlc_timeout_tx_fee_sat + context.holder_dust_limit_satoshis;
5035-
5036-
let mut addl_htlcs = 0;
5037-
if fee_spike_buffer_htlc.is_some() { addl_htlcs += 1; }
5038-
match htlc.origin {
5039-
HTLCInitiator::LocalOffered => {
5040-
if htlc.amount_msat / 1000 >= real_dust_limit_timeout_sat {
5041-
addl_htlcs += 1;
5042-
}
5043-
},
5044-
HTLCInitiator::RemoteOffered => {
5045-
if htlc.amount_msat / 1000 >= real_dust_limit_success_sat {
5046-
addl_htlcs += 1;
5047-
}
5048-
}
5049-
}
5050-
5051-
let mut included_htlcs = 0;
5052-
for ref htlc in context.pending_inbound_htlcs.iter() {
5053-
if htlc.amount_msat / 1000 < real_dust_limit_success_sat {
5054-
continue
5055-
}
5056-
// We include LocalRemoved HTLCs here because we may still need to broadcast a commitment
5057-
// transaction including this HTLC if it times out before they RAA.
5058-
included_htlcs += 1;
5059-
}
5060-
5061-
for ref htlc in context.pending_outbound_htlcs.iter() {
5062-
if htlc.amount_msat / 1000 < real_dust_limit_timeout_sat {
5063-
continue
5064-
}
5065-
match htlc.state {
5066-
OutboundHTLCState::LocalAnnounced {..} => included_htlcs += 1,
5067-
OutboundHTLCState::Committed => included_htlcs += 1,
5068-
OutboundHTLCState::RemoteRemoved {..} => included_htlcs += 1,
5069-
// We don't include AwaitingRemoteRevokeToRemove HTLCs because our next commitment
5070-
// transaction won't be generated until they send us their next RAA, which will mean
5071-
// dropping any HTLCs in this state.
5072-
_ => {},
5073-
}
5074-
}
5075-
5076-
for htlc in context.holding_cell_htlc_updates.iter() {
5077-
match htlc {
5078-
&HTLCUpdateAwaitingACK::AddHTLC { amount_msat, .. } => {
5079-
if amount_msat / 1000 < real_dust_limit_timeout_sat {
5080-
continue
5081-
}
5082-
included_htlcs += 1
5083-
},
5084-
_ => {}, // Don't include claims/fails that are awaiting ack, because once we get the
5085-
// ack we're guaranteed to never include them in commitment txs anymore.
5086-
}
5087-
}
5088-
5089-
let num_htlcs = included_htlcs + addl_htlcs;
5090-
SpecTxBuilder {}.commit_tx_fee_sat(context.feerate_per_kw, num_htlcs, funding.get_channel_type()) * 1000
5091-
}
5092-
5093-
/// Get the commitment tx fee for the remote's next commitment transaction based on the number of
5094-
/// pending HTLCs that are on track to be in their next commitment tx
5095-
///
5096-
/// Optionally includes the `HTLCCandidate` given by `htlc` and an additional non-dust HTLC if
5097-
/// `fee_spike_buffer_htlc` is `Some`.
5098-
///
5099-
/// The first extra HTLC is useful for determining whether we can accept a further HTLC, the
5100-
/// second allows for creating a buffer to ensure a further HTLC can always be accepted/added.
5101-
///
5102-
/// Dust HTLCs are excluded.
5103-
#[rustfmt::skip]
5104-
fn next_remote_commit_tx_fee_msat(
5105-
&self, funding: &FundingScope, htlc: Option<HTLCCandidate>, fee_spike_buffer_htlc: Option<()>,
5106-
) -> u64 {
5107-
let context = self;
5108-
assert!(!funding.is_outbound());
5109-
5110-
if funding.get_channel_type().supports_anchor_zero_fee_commitments() {
5111-
debug_assert_eq!(context.feerate_per_kw, 0);
5112-
debug_assert!(fee_spike_buffer_htlc.is_none());
5113-
return 0
5114-
}
5115-
5116-
debug_assert!(htlc.is_some() || fee_spike_buffer_htlc.is_some(), "At least one of the options must be set");
5117-
5118-
let (htlc_success_tx_fee_sat, htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
5119-
funding.get_channel_type(), context.feerate_per_kw,
5120-
);
5121-
let real_dust_limit_success_sat = htlc_success_tx_fee_sat + context.counterparty_dust_limit_satoshis;
5122-
let real_dust_limit_timeout_sat = htlc_timeout_tx_fee_sat + context.counterparty_dust_limit_satoshis;
5123-
5124-
let mut addl_htlcs = 0;
5125-
if fee_spike_buffer_htlc.is_some() { addl_htlcs += 1; }
5126-
if let Some(htlc) = &htlc {
5127-
match htlc.origin {
5128-
HTLCInitiator::LocalOffered => {
5129-
if htlc.amount_msat / 1000 >= real_dust_limit_success_sat {
5130-
addl_htlcs += 1;
5131-
}
5132-
},
5133-
HTLCInitiator::RemoteOffered => {
5134-
if htlc.amount_msat / 1000 >= real_dust_limit_timeout_sat {
5135-
addl_htlcs += 1;
5136-
}
5137-
}
5138-
}
5139-
}
5140-
5141-
// When calculating the set of HTLCs which will be included in their next commitment_signed, all
5142-
// non-dust inbound HTLCs are included (as all states imply it will be included) and only
5143-
// committed outbound HTLCs, see below.
5144-
let mut included_htlcs = 0;
5145-
for ref htlc in context.pending_inbound_htlcs.iter() {
5146-
if htlc.amount_msat / 1000 < real_dust_limit_timeout_sat {
5147-
continue
5148-
}
5149-
included_htlcs += 1;
5150-
}
5151-
5152-
for ref htlc in context.pending_outbound_htlcs.iter() {
5153-
if htlc.amount_msat / 1000 < real_dust_limit_success_sat {
5154-
continue
5155-
}
5156-
// We only include outbound HTLCs if it will not be included in their next commitment_signed,
5157-
// i.e. if they've responded to us with an RAA after announcement.
5158-
match htlc.state {
5159-
OutboundHTLCState::Committed => included_htlcs += 1,
5160-
OutboundHTLCState::RemoteRemoved {..} => included_htlcs += 1,
5161-
OutboundHTLCState::LocalAnnounced { .. } => included_htlcs += 1,
5162-
_ => {},
5163-
}
5164-
}
5165-
5166-
let num_htlcs = included_htlcs + addl_htlcs;
5167-
SpecTxBuilder {}.commit_tx_fee_sat(context.feerate_per_kw, num_htlcs, funding.get_channel_type()) * 1000
5168-
}
5169-
51704988
#[rustfmt::skip]
51714989
fn if_unbroadcasted_funding<F, O>(&self, f: F) -> Option<O> where F: Fn() -> Option<O> {
51724990
match self.channel_state {
@@ -14152,9 +13970,9 @@ mod tests {
1415213970
use crate::ln::chan_utils::ChannelTransactionParameters;
1415313971
use crate::ln::chan_utils::{self, commit_tx_fee_sat};
1415413972
use crate::ln::channel::{
14155-
AwaitingChannelReadyFlags, ChannelState, FundedChannel, HTLCCandidate, HTLCInitiator,
14156-
HTLCUpdateAwaitingACK, InboundHTLCOutput, InboundHTLCState, InboundV1Channel,
14157-
OutboundHTLCOutput, OutboundHTLCState, OutboundV1Channel,
13973+
AwaitingChannelReadyFlags, ChannelState, FundedChannel, HTLCUpdateAwaitingACK,
13974+
InboundHTLCOutput, InboundHTLCState, InboundV1Channel, OutboundHTLCOutput,
13975+
OutboundHTLCState, OutboundV1Channel,
1415813976
};
1415913977
use crate::ln::channel::{
1416013978
MAX_FUNDING_SATOSHIS_NO_WUMBO, MIN_THEIR_CHAN_RESERVE_SATOSHIS,
@@ -14169,6 +13987,7 @@ mod tests {
1416913987
use crate::ln::script::ShutdownScript;
1417013988
use crate::prelude::*;
1417113989
use crate::routing::router::{Path, RouteHop};
13990+
use crate::sign::tx_builder::HTLCAmountDirection;
1417213991
#[cfg(ldk_test_vectors)]
1417313992
use crate::sign::{ChannelSigner, EntropySource, InMemorySigner, SignerProvider};
1417413993
#[cfg(splicing)]
@@ -14432,24 +14251,26 @@ mod tests {
1443214251

1443314252
// Make sure when Node A calculates their local commitment transaction, none of the HTLCs pass
1443414253
// the dust limit check.
14435-
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
14436-
let local_commit_tx_fee = node_a_chan.context.next_local_commit_tx_fee_msat(&node_a_chan.funding, htlc_candidate, None);
14254+
let htlc_candidate = HTLCAmountDirection { amount_msat: htlc_amount_msat, outbound: true };
14255+
// We set dust_exposure_to_limiting_feerate to Some as the channel type is not zero fee, but we don't care for the value as it has no impact on the commitment transaction fee
14256+
let local_commit_tx_fee = node_a_chan.context.get_next_local_commitment_stats(&node_a_chan.funding, Some(htlc_candidate), true, 0, node_a_chan.context.feerate_per_kw, Some(0)).commit_tx_fee_sat * 1000;
1443714257
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;
1443814258
assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
1443914259

1444014260
// Finally, make sure that when Node A calculates the remote's commitment transaction fees, all
1444114261
// of the HTLCs are seen to be above the dust limit.
1444214262
node_a_chan.funding.channel_transaction_parameters.is_outbound_from_holder = false;
1444314263
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;
14444-
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
14445-
let remote_commit_tx_fee = node_a_chan.context.next_remote_commit_tx_fee_msat(&node_a_chan.funding, Some(htlc_candidate), None);
14264+
let htlc_candidate = HTLCAmountDirection { amount_msat: htlc_amount_msat, outbound: true };
14265+
// We set dust_exposure_to_limiting_feerate to Some as the channel type is not zero fee, but we don't care for the value as it has no impact on the commitment transaction fee
14266+
let remote_commit_tx_fee = node_a_chan.context.get_next_remote_commitment_stats(&node_a_chan.funding, Some(htlc_candidate), true, 0, node_a_chan.context.feerate_per_kw, Some(0)).commit_tx_fee_sat * 1000;
1444614267
assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
1444714268
}
1444814269

1444914270
#[test]
1445014271
#[rustfmt::skip]
1445114272
fn test_timeout_vs_success_htlc_dust_limit() {
14452-
// Make sure that when `next_remote_commit_tx_fee_msat` and `next_local_commit_tx_fee_msat`
14273+
// Make sure that when `get_next_local/remote_commitment_stats`
1445314274
// calculate the real dust limits for HTLCs (i.e. the dust limit given by the counterparty
1445414275
// *plus* the fees paid for the HTLC) they don't swap `HTLC_SUCCESS_TX_WEIGHT` for
1445514276
// `HTLC_TIMEOUT_TX_WEIGHT`, and vice versa.
@@ -14474,28 +14295,32 @@ mod tests {
1447414295
// If HTLC_SUCCESS_TX_WEIGHT and HTLC_TIMEOUT_TX_WEIGHT were swapped: then this HTLC would be
1447514296
// counted as dust when it shouldn't be.
1447614297
let htlc_amt_above_timeout = (htlc_timeout_tx_fee_sat + chan.context.holder_dust_limit_satoshis + 1) * 1000;
14477-
let htlc_candidate = HTLCCandidate::new(htlc_amt_above_timeout, HTLCInitiator::LocalOffered);
14478-
let commitment_tx_fee = chan.context.next_local_commit_tx_fee_msat(&chan.funding, htlc_candidate, None);
14298+
let htlc_candidate = HTLCAmountDirection { amount_msat: htlc_amt_above_timeout, outbound: true };
14299+
// We set dust_exposure_to_limiting_feerate to Some as the channel type is not zero fee, but we don't care for the value as it has no impact on the commitment transaction fee
14300+
let commitment_tx_fee = chan.context.get_next_local_commitment_stats(&chan.funding, Some(htlc_candidate), true, 0, chan.context.feerate_per_kw, Some(0)).commit_tx_fee_sat * 1000;
1447914301
assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
1448014302

1448114303
// If swapped: this HTLC would be counted as non-dust when it shouldn't be.
1448214304
let dust_htlc_amt_below_success = (htlc_success_tx_fee_sat + chan.context.holder_dust_limit_satoshis - 1) * 1000;
14483-
let htlc_candidate = HTLCCandidate::new(dust_htlc_amt_below_success, HTLCInitiator::RemoteOffered);
14484-
let commitment_tx_fee = chan.context.next_local_commit_tx_fee_msat(&chan.funding, htlc_candidate, None);
14305+
let htlc_candidate = HTLCAmountDirection { amount_msat: dust_htlc_amt_below_success, outbound: false };
14306+
// We set dust_exposure_to_limiting_feerate to Some as the channel type is not zero fee, but we don't care for the value as it has no impact on the commitment transaction fee
14307+
let commitment_tx_fee = chan.context.get_next_local_commitment_stats(&chan.funding, Some(htlc_candidate), true, 0, chan.context.feerate_per_kw, Some(0)).commit_tx_fee_sat * 1000;
1448514308
assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
1448614309

1448714310
chan.funding.channel_transaction_parameters.is_outbound_from_holder = false;
1448814311

1448914312
// If swapped: this HTLC would be counted as non-dust when it shouldn't be.
1449014313
let dust_htlc_amt_above_timeout = (htlc_timeout_tx_fee_sat + chan.context.counterparty_dust_limit_satoshis + 1) * 1000;
14491-
let htlc_candidate = HTLCCandidate::new(dust_htlc_amt_above_timeout, HTLCInitiator::LocalOffered);
14492-
let commitment_tx_fee = chan.context.next_remote_commit_tx_fee_msat(&chan.funding, Some(htlc_candidate), None);
14314+
let htlc_candidate = HTLCAmountDirection { amount_msat: dust_htlc_amt_above_timeout, outbound: true };
14315+
// We set dust_exposure_to_limiting_feerate to Some as the channel type is not zero fee, but we don't care for the value as it has no impact on the commitment transaction fee
14316+
let commitment_tx_fee = chan.context.get_next_remote_commitment_stats(&chan.funding, Some(htlc_candidate), false, 0, chan.context.feerate_per_kw, Some(0)).commit_tx_fee_sat * 1000;
1449314317
assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
1449414318

1449514319
// If swapped: this HTLC would be counted as dust when it shouldn't be.
1449614320
let htlc_amt_below_success = (htlc_success_tx_fee_sat + chan.context.counterparty_dust_limit_satoshis - 1) * 1000;
14497-
let htlc_candidate = HTLCCandidate::new(htlc_amt_below_success, HTLCInitiator::RemoteOffered);
14498-
let commitment_tx_fee = chan.context.next_remote_commit_tx_fee_msat(&chan.funding, Some(htlc_candidate), None);
14321+
let htlc_candidate = HTLCAmountDirection { amount_msat: htlc_amt_below_success, outbound: false };
14322+
// We set dust_exposure_to_limiting_feerate to Some as the channel type is not zero fee, but we don't care for the value as it has no impact on the commitment transaction fee
14323+
let commitment_tx_fee = chan.context.get_next_remote_commitment_stats(&chan.funding, Some(htlc_candidate), false, 0, chan.context.feerate_per_kw, Some(0)).commit_tx_fee_sat * 1000;
1449914324
assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
1450014325
}
1450114326

0 commit comments

Comments
 (0)