Skip to content

Commit 9566795

Browse files
committed
Convert COMMITMENT_TX_BASE_WEIGHT to anchor-aware function
1 parent c47d014 commit 9566795

File tree

4 files changed

+89
-53
lines changed

4 files changed

+89
-53
lines changed

lightning/src/ln/channel.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,12 @@ struct CommitmentTxInfoCached {
622622

623623
pub const OUR_MAX_HTLCS: u16 = 50; //TODO
624624

625-
#[cfg(not(test))]
626-
const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
627-
#[cfg(test)]
628-
pub const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
625+
pub(crate) fn commitment_tx_base_weight(opt_anchors: bool) -> u64 {
626+
const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
627+
const COMMITMENT_TX_BASE_ANCHOR_WEIGHT: u64 = 1124;
628+
if opt_anchors { COMMITMENT_TX_BASE_ANCHOR_WEIGHT } else { COMMITMENT_TX_BASE_WEIGHT }
629+
}
630+
629631
#[cfg(not(test))]
630632
const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
631633
#[cfg(test)]
@@ -717,6 +719,8 @@ impl<Signer: Sign> Channel<Signer> {
717719
where K::Target: KeysInterface<Signer = Signer>,
718720
F::Target: FeeEstimator,
719721
{
722+
let opt_anchors = false; // TODO - should be based on features
723+
720724
let holder_selected_contest_delay = config.own_channel_config.our_to_self_delay;
721725
let holder_signer = keys_provider.get_channel_signer(false, channel_value_satoshis);
722726
let pubkeys = holder_signer.pubkeys().clone();
@@ -739,7 +743,7 @@ impl<Signer: Sign> Channel<Signer> {
739743
let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
740744

741745
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
742-
let commitment_tx_fee = Self::commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT);
746+
let commitment_tx_fee = Self::commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT, opt_anchors);
743747
if value_to_self_msat < commitment_tx_fee {
744748
return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", value_to_self_msat / 1000, commitment_tx_fee / 1000) });
745749
}
@@ -829,7 +833,7 @@ impl<Signer: Sign> Channel<Signer> {
829833
is_outbound_from_holder: true,
830834
counterparty_parameters: None,
831835
funding_outpoint: None,
832-
opt_anchors: None,
836+
opt_anchors: if opt_anchors { Some(()) } else { None },
833837
},
834838
funding_transaction: None,
835839

@@ -896,6 +900,8 @@ impl<Signer: Sign> Channel<Signer> {
896900
F::Target: FeeEstimator,
897901
L::Target: Logger,
898902
{
903+
let opt_anchors = false; // TODO - should be based on features
904+
899905
// First check the channel type is known, failing before we do anything else if we don't
900906
// support this channel type.
901907
let channel_type = if let Some(channel_type) = &msg.channel_type {
@@ -1008,7 +1014,7 @@ impl<Signer: Sign> Channel<Signer> {
10081014
// check if the funder's amount for the initial commitment tx is sufficient
10091015
// for full fee payment plus a few HTLCs to ensure the channel will be useful.
10101016
let funders_amount_msat = msg.funding_satoshis * 1000 - msg.push_msat;
1011-
let commitment_tx_fee = Self::commit_tx_fee_msat(msg.feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT) / 1000;
1017+
let commitment_tx_fee = Self::commit_tx_fee_msat(msg.feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT, opt_anchors) / 1000;
10121018
if funders_amount_msat / 1000 < commitment_tx_fee {
10131019
return Err(ChannelError::Close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", funders_amount_msat / 1000, commitment_tx_fee)));
10141020
}
@@ -1128,7 +1134,7 @@ impl<Signer: Sign> Channel<Signer> {
11281134
pubkeys: counterparty_pubkeys,
11291135
}),
11301136
funding_outpoint: None,
1131-
opt_anchors: None
1137+
opt_anchors: if opt_anchors { Some(()) } else { None },
11321138
},
11331139
funding_transaction: None,
11341140

@@ -1320,7 +1326,7 @@ impl<Signer: Sign> Channel<Signer> {
13201326
broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, value_to_remote_msat as u64);
13211327
}
13221328

1323-
let total_fee_sat = Channel::<Signer>::commit_tx_fee_sat(feerate_per_kw, included_non_dust_htlcs.len());
1329+
let total_fee_sat = Channel::<Signer>::commit_tx_fee_sat(feerate_per_kw, included_non_dust_htlcs.len(), self.channel_transaction_parameters.opt_anchors.is_some());
13241330
let (value_to_self, value_to_remote) = if self.is_outbound() {
13251331
(value_to_self_msat / 1000 - total_fee_sat as i64, value_to_remote_msat / 1000)
13261332
} else {
@@ -2158,17 +2164,17 @@ impl<Signer: Sign> Channel<Signer> {
21582164

21592165
// Get the fee cost in MSATS of a commitment tx with a given number of HTLC outputs.
21602166
// Note that num_htlcs should not include dust HTLCs.
2161-
fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize) -> u64 {
2167+
fn commit_tx_fee_msat(feerate_per_kw: u32, num_htlcs: usize, opt_anchors: bool) -> u64 {
21622168
// Note that we need to divide before multiplying to round properly,
21632169
// since the lowest denomination of bitcoin on-chain is the satoshi.
2164-
(COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate_per_kw as u64 / 1000 * 1000
2170+
(commitment_tx_base_weight(opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate_per_kw as u64 / 1000 * 1000
21652171
}
21662172

21672173
// Get the fee cost in SATS of a commitment tx with a given number of HTLC outputs.
21682174
// Note that num_htlcs should not include dust HTLCs.
21692175
#[inline]
2170-
fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize) -> u64 {
2171-
feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
2176+
fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, opt_anchors: bool) -> u64 {
2177+
feerate_per_kw as u64 * (commitment_tx_base_weight(opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
21722178
}
21732179

21742180
// Get the commitment tx fee for the local's (i.e. our) next commitment transaction based on the
@@ -2235,12 +2241,12 @@ impl<Signer: Sign> Channel<Signer> {
22352241
}
22362242

22372243
let num_htlcs = included_htlcs + addl_htlcs;
2238-
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs);
2244+
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs, self.opt_anchors());
22392245
#[cfg(any(test, feature = "fuzztarget"))]
22402246
{
22412247
let mut fee = res;
22422248
if fee_spike_buffer_htlc.is_some() {
2243-
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1);
2249+
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1, self.opt_anchors());
22442250
}
22452251
let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len()
22462252
+ self.holding_cell_htlc_updates.len();
@@ -2313,12 +2319,12 @@ impl<Signer: Sign> Channel<Signer> {
23132319
}
23142320

23152321
let num_htlcs = included_htlcs + addl_htlcs;
2316-
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs);
2322+
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs, self.opt_anchors());
23172323
#[cfg(any(test, feature = "fuzztarget"))]
23182324
{
23192325
let mut fee = res;
23202326
if fee_spike_buffer_htlc.is_some() {
2321-
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1);
2327+
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1, self.opt_anchors());
23222328
}
23232329
let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
23242330
let commitment_tx_info = CommitmentTxInfoCached {
@@ -3151,7 +3157,7 @@ impl<Signer: Sign> Channel<Signer> {
31513157
let outbound_stats = self.get_outbound_pending_htlc_stats(Some(feerate_per_kw));
31523158
let keys = if let Ok(keys) = self.build_holder_transaction_keys(self.cur_holder_commitment_transaction_number) { keys } else { return None; };
31533159
let commitment_stats = self.build_commitment_transaction(self.cur_holder_commitment_transaction_number, &keys, true, true, logger);
3154-
let buffer_fee_msat = Channel::<Signer>::commit_tx_fee_sat(feerate_per_kw, commitment_stats.num_nondust_htlcs + outbound_stats.on_holder_tx_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize) * 1000;
3160+
let buffer_fee_msat = Channel::<Signer>::commit_tx_fee_sat(feerate_per_kw, commitment_stats.num_nondust_htlcs + outbound_stats.on_holder_tx_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.opt_anchors()) * 1000;
31553161
let holder_balance_msat = commitment_stats.local_balance_msat - outbound_stats.holding_cell_msat;
31563162
if holder_balance_msat < buffer_fee_msat + self.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
31573163
//TODO: auto-close after a number of failures?
@@ -4944,7 +4950,7 @@ impl<Signer: Sign> Channel<Signer> {
49444950
&& info.next_holder_htlc_id == self.next_holder_htlc_id
49454951
&& info.next_counterparty_htlc_id == self.next_counterparty_htlc_id
49464952
&& info.feerate == self.feerate_per_kw {
4947-
let actual_fee = Self::commit_tx_fee_msat(self.feerate_per_kw, commitment_stats.num_nondust_htlcs);
4953+
let actual_fee = Self::commit_tx_fee_msat(self.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.opt_anchors());
49484954
assert_eq!(actual_fee, info.fee);
49494955
}
49504956
}
@@ -5993,13 +5999,13 @@ mod tests {
59935999
// the dust limit check.
59946000
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
59956001
let local_commit_tx_fee = node_a_chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
5996-
let local_commit_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 0);
6002+
let local_commit_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 0, node_a_chan.opt_anchors());
59976003
assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
59986004

59996005
// Finally, make sure that when Node A calculates the remote's commitment transaction fees, all
60006006
// of the HTLCs are seen to be above the dust limit.
60016007
node_a_chan.channel_transaction_parameters.is_outbound_from_holder = false;
6002-
let remote_commit_fee_3_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 3);
6008+
let remote_commit_fee_3_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 3, node_a_chan.opt_anchors());
60036009
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
60046010
let remote_commit_tx_fee = node_a_chan.next_remote_commit_tx_fee_msat(htlc_candidate, None);
60056011
assert_eq!(remote_commit_tx_fee, remote_commit_fee_3_htlcs);
@@ -6021,8 +6027,8 @@ mod tests {
60216027
let config = UserConfig::default();
60226028
let mut chan = Channel::<EnforcingSigner>::new_outbound(&&fee_est, &&keys_provider, node_id, &InitFeatures::known(), 10000000, 100000, 42, &config, 0).unwrap();
60236029

6024-
let commitment_tx_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.feerate_per_kw, 0);
6025-
let commitment_tx_fee_1_htlc = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.feerate_per_kw, 1);
6030+
let commitment_tx_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.feerate_per_kw, 0, chan.opt_anchors());
6031+
let commitment_tx_fee_1_htlc = Channel::<EnforcingSigner>::commit_tx_fee_msat(chan.feerate_per_kw, 1, chan.opt_anchors());
60266032

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

lightning/src/ln/functional_test_utils.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,17 @@ macro_rules! get_feerate {
440440
}
441441
}
442442

443+
#[cfg(test)]
444+
macro_rules! get_opt_anchors {
445+
($node: expr, $channel_id: expr) => {
446+
{
447+
let mut lock;
448+
let chan = get_channel_ref!($node, lock, $channel_id);
449+
chan.opt_anchors()
450+
}
451+
}
452+
}
453+
443454
/// Returns a channel monitor given a channel id, making some naive assumptions
444455
#[macro_export]
445456
macro_rules! get_monitor {

0 commit comments

Comments
 (0)