Skip to content

Commit 372bae7

Browse files
committed
convert COMMITMENT_TX_BASE_WEIGHT to anchor-aware
1 parent 4b0030c commit 372bae7

File tree

4 files changed

+88
-52
lines changed

4 files changed

+88
-52
lines changed

lightning/src/ln/channel.rs

+26-20
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

@@ -2159,17 +2165,17 @@ impl<Signer: Sign> Channel<Signer> {
21592165

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

21682174
// Get the fee cost in SATS of a commitment tx with a given number of HTLC outputs.
21692175
// Note that num_htlcs should not include dust HTLCs.
21702176
#[inline]
21712177
fn commit_tx_fee_sat(feerate_per_kw: u32, num_htlcs: usize, opt_anchors: bool) -> u64 {
2172-
feerate_per_kw as u64 * (if opt_anchors { 1124 } else { COMMITMENT_TX_BASE_WEIGHT} + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
2178+
feerate_per_kw as u64 * (commitment_tx_base_weight(opt_anchors) + num_htlcs as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000
21732179
}
21742180

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

22382244
let num_htlcs = included_htlcs + addl_htlcs;
2239-
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs);
2245+
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs, self.opt_anchors());
22402246
#[cfg(any(test, feature = "fuzztarget"))]
22412247
{
22422248
let mut fee = res;
22432249
if fee_spike_buffer_htlc.is_some() {
2244-
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1);
2250+
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1, self.opt_anchors());
22452251
}
22462252
let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len()
22472253
+ self.holding_cell_htlc_updates.len();
@@ -2314,12 +2320,12 @@ impl<Signer: Sign> Channel<Signer> {
23142320
}
23152321

23162322
let num_htlcs = included_htlcs + addl_htlcs;
2317-
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs);
2323+
let res = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs, self.opt_anchors());
23182324
#[cfg(any(test, feature = "fuzztarget"))]
23192325
{
23202326
let mut fee = res;
23212327
if fee_spike_buffer_htlc.is_some() {
2322-
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1);
2328+
fee = Self::commit_tx_fee_msat(self.feerate_per_kw, num_htlcs - 1, self.opt_anchors());
23232329
}
23242330
let total_pending_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
23252331
let commitment_tx_info = CommitmentTxInfoCached {
@@ -4945,7 +4951,7 @@ impl<Signer: Sign> Channel<Signer> {
49454951
&& info.next_holder_htlc_id == self.next_holder_htlc_id
49464952
&& info.next_counterparty_htlc_id == self.next_counterparty_htlc_id
49474953
&& info.feerate == self.feerate_per_kw {
4948-
let actual_fee = Self::commit_tx_fee_msat(self.feerate_per_kw, commitment_stats.num_nondust_htlcs);
4954+
let actual_fee = Self::commit_tx_fee_msat(self.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.opt_anchors());
49494955
assert_eq!(actual_fee, info.fee);
49504956
}
49514957
}
@@ -5994,13 +6000,13 @@ mod tests {
59946000
// the dust limit check.
59956001
let htlc_candidate = HTLCCandidate::new(htlc_amount_msat, HTLCInitiator::LocalOffered);
59966002
let local_commit_tx_fee = node_a_chan.next_local_commit_tx_fee_msat(htlc_candidate, None);
5997-
let local_commit_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 0);
6003+
let local_commit_fee_0_htlcs = Channel::<EnforcingSigner>::commit_tx_fee_msat(node_a_chan.feerate_per_kw, 0, node_a_chan.opt_anchors());
59986004
assert_eq!(local_commit_tx_fee, local_commit_fee_0_htlcs);
59996005

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

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

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

lightning/src/ln/functional_test_utils.rs

+11
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)