Skip to content

Commit 3b4a077

Browse files
committed
Delete ChannelContext::next_{local, remote}_commit_tx_fee_msat
1 parent 19313c4 commit 3b4a077

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
@@ -1098,13 +1098,6 @@ pub enum AnnouncementSigsState {
10981098
PeerReceived,
10991099
}
11001100

1101-
/// An enum indicating whether the local or remote side offered a given HTLC.
1102-
enum HTLCInitiator {
1103-
LocalOffered,
1104-
#[allow(dead_code)]
1105-
RemoteOffered,
1106-
}
1107-
11081101
/// A struct gathering data on a commitment, either local or remote.
11091102
struct CommitmentData<'a> {
11101103
tx: CommitmentTransaction,
@@ -1125,18 +1118,6 @@ pub(crate) struct CommitmentStats {
11251118
pub remote_balance_before_fee_msat: u64,
11261119
}
11271120

1128-
/// Used when calculating whether we or the remote can afford an additional HTLC.
1129-
struct HTLCCandidate {
1130-
amount_msat: u64,
1131-
origin: HTLCInitiator,
1132-
}
1133-
1134-
impl HTLCCandidate {
1135-
fn new(amount_msat: u64, origin: HTLCInitiator) -> Self {
1136-
Self { amount_msat, origin }
1137-
}
1138-
}
1139-
11401121
/// A return value enum for get_update_fulfill_htlc. See UpdateFulfillCommitFetch variants for
11411122
/// description
11421123
enum UpdateFulfillFetch {
@@ -4999,169 +4980,6 @@ where
49994980
)
50004981
}
50014982

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

1425314072
// Make sure when Node A calculates their local commitment transaction, none of the HTLCs pass
1425414073
// the dust limit check.
14255-
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
14256-
let local_commit_tx_fee = node_a_chan.context.next_local_commit_tx_fee_msat(&node_a_chan.funding, htlc_candidate, None);
14074+
let htlc_candidate = HTLCAmountDirection { amount_msat: htlc_amount_msat, outbound: true };
14075+
// 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
14076+
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;
1425714077
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;
1425814078
assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
1425914079

1426014080
// Finally, make sure that when Node A calculates the remote's commitment transaction fees, all
1426114081
// of the HTLCs are seen to be above the dust limit.
1426214082
node_a_chan.funding.channel_transaction_parameters.is_outbound_from_holder = false;
1426314083
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;
14264-
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
14265-
let remote_commit_tx_fee = node_a_chan.context.next_remote_commit_tx_fee_msat(&node_a_chan.funding, Some(htlc_candidate), None);
14084+
let htlc_candidate = HTLCAmountDirection { amount_msat: htlc_amount_msat, outbound: true };
14085+
// 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
14086+
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;
1426614087
assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
1426714088
}
1426814089

1426914090
#[test]
1427014091
#[rustfmt::skip]
1427114092
fn test_timeout_vs_success_htlc_dust_limit() {
14272-
// Make sure that when `next_remote_commit_tx_fee_msat` and `next_local_commit_tx_fee_msat`
14093+
// Make sure that when `get_next_local/remote_commitment_stats`
1427314094
// calculate the real dust limits for HTLCs (i.e. the dust limit given by the counterparty
1427414095
// *plus* the fees paid for the HTLC) they don't swap `HTLC_SUCCESS_TX_WEIGHT` for
1427514096
// `HTLC_TIMEOUT_TX_WEIGHT`, and vice versa.
@@ -14294,28 +14115,32 @@ mod tests {
1429414115
// If HTLC_SUCCESS_TX_WEIGHT and HTLC_TIMEOUT_TX_WEIGHT were swapped: then this HTLC would be
1429514116
// counted as dust when it shouldn't be.
1429614117
let htlc_amt_above_timeout = (htlc_timeout_tx_fee_sat + chan.context.holder_dust_limit_satoshis + 1) * 1000;
14297-
let htlc_candidate = HTLCCandidate::new(htlc_amt_above_timeout, HTLCInitiator::LocalOffered);
14298-
let commitment_tx_fee = chan.context.next_local_commit_tx_fee_msat(&chan.funding, htlc_candidate, None);
14118+
let htlc_candidate = HTLCAmountDirection { amount_msat: htlc_amt_above_timeout, outbound: true };
14119+
// 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
14120+
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;
1429914121
assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
1430014122

1430114123
// If swapped: this HTLC would be counted as non-dust when it shouldn't be.
1430214124
let dust_htlc_amt_below_success = (htlc_success_tx_fee_sat + chan.context.holder_dust_limit_satoshis - 1) * 1000;
14303-
let htlc_candidate = HTLCCandidate::new(dust_htlc_amt_below_success, HTLCInitiator::RemoteOffered);
14304-
let commitment_tx_fee = chan.context.next_local_commit_tx_fee_msat(&chan.funding, htlc_candidate, None);
14125+
let htlc_candidate = HTLCAmountDirection { amount_msat: dust_htlc_amt_below_success, outbound: false };
14126+
// 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
14127+
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;
1430514128
assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
1430614129

1430714130
chan.funding.channel_transaction_parameters.is_outbound_from_holder = false;
1430814131

1430914132
// If swapped: this HTLC would be counted as non-dust when it shouldn't be.
1431014133
let dust_htlc_amt_above_timeout = (htlc_timeout_tx_fee_sat + chan.context.counterparty_dust_limit_satoshis + 1) * 1000;
14311-
let htlc_candidate = HTLCCandidate::new(dust_htlc_amt_above_timeout, HTLCInitiator::LocalOffered);
14312-
let commitment_tx_fee = chan.context.next_remote_commit_tx_fee_msat(&chan.funding, Some(htlc_candidate), None);
14134+
let htlc_candidate = HTLCAmountDirection { amount_msat: dust_htlc_amt_above_timeout, outbound: true };
14135+
// 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
14136+
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;
1431314137
assert_eq!(commitment_tx_fee, commitment_tx_fee_0_htlcs);
1431414138

1431514139
// If swapped: this HTLC would be counted as dust when it shouldn't be.
1431614140
let htlc_amt_below_success = (htlc_success_tx_fee_sat + chan.context.counterparty_dust_limit_satoshis - 1) * 1000;
14317-
let htlc_candidate = HTLCCandidate::new(htlc_amt_below_success, HTLCInitiator::RemoteOffered);
14318-
let commitment_tx_fee = chan.context.next_remote_commit_tx_fee_msat(&chan.funding, Some(htlc_candidate), None);
14141+
let htlc_candidate = HTLCAmountDirection { amount_msat: htlc_amt_below_success, outbound: false };
14142+
// 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
14143+
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;
1431914144
assert_eq!(commitment_tx_fee, commitment_tx_fee_1_htlc);
1432014145
}
1432114146

0 commit comments

Comments
 (0)