Skip to content

Commit 371ca43

Browse files
committed
Remove redundant fields in CommitmentStats
The fields `feerate_per_kw` and `num_nondust_htlcs` of `CommitmentStats` can both be accessed directly from the `tx: CommitmentTransaction` field. We also take this opportunity to rename the balance fields to make it extra clear what they refer to.
1 parent 1df2b58 commit 371ca43

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -880,12 +880,10 @@ struct HTLCStats {
880880
/// An enum gathering stats on commitment transaction, either local or remote.
881881
struct CommitmentStats<'a> {
882882
tx: CommitmentTransaction, // the transaction info
883-
feerate_per_kw: u32, // the feerate included to build the transaction
884883
total_fee_sat: u64, // the total fee included in the transaction
885-
num_nondust_htlcs: usize, // the number of HTLC outputs (dust HTLCs *non*-included)
886884
htlcs_included: Vec<(HTLCOutputInCommitment, Option<&'a HTLCSource>)>, // the list of HTLCs (dust HTLCs *included*) which were not ignored when building the transaction
887-
local_balance_msat: u64, // local balance before fees *not* considering dust limits
888-
remote_balance_msat: u64, // remote balance before fees *not* considering dust limits
885+
local_balance_before_fee_msat: u64, // local balance before fees *not* considering dust limits
886+
remote_balance_before_fee_msat: u64, // remote balance before fees *not* considering dust limits
889887
outbound_htlc_preimages: Vec<PaymentPreimage>, // preimages for successful offered HTLCs since last commitment
890888
inbound_htlc_preimages: Vec<PaymentPreimage>, // preimages for successful received HTLCs since last commitment
891889
}
@@ -3504,7 +3502,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
35043502
if update_fee {
35053503
debug_assert!(!funding.is_outbound());
35063504
let counterparty_reserve_we_require_msat = funding.holder_selected_channel_reserve_satoshis * 1000;
3507-
if commitment_stats.remote_balance_msat < commitment_stats.total_fee_sat * 1000 + counterparty_reserve_we_require_msat {
3505+
if commitment_stats.remote_balance_before_fee_msat < commitment_stats.total_fee_sat * 1000 + counterparty_reserve_we_require_msat {
35083506
return Err(ChannelError::close("Funding remote cannot afford proposed new fee".to_owned()));
35093507
}
35103508
}
@@ -3526,8 +3524,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
35263524
}
35273525
}
35283526

3529-
if msg.htlc_signatures.len() != commitment_stats.num_nondust_htlcs {
3530-
return Err(ChannelError::close(format!("Got wrong number of HTLC signatures ({}) from remote. It must be {}", msg.htlc_signatures.len(), commitment_stats.num_nondust_htlcs)));
3527+
if msg.htlc_signatures.len() != commitment_stats.tx.htlcs().len() {
3528+
return Err(ChannelError::close(format!("Got wrong number of HTLC signatures ({}) from remote. It must be {}", msg.htlc_signatures.len(), commitment_stats.tx.htlcs().len())));
35313529
}
35323530

35333531
// Up to LDK 0.0.115, HTLC information was required to be duplicated in the
@@ -3550,7 +3548,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
35503548
let holder_keys = commitment_stats.tx.trust().keys();
35513549
for (idx, (htlc, mut source_opt)) in htlcs_cloned.drain(..).enumerate() {
35523550
if let Some(_) = htlc.transaction_output_index {
3553-
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
3551+
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.tx.feerate_per_kw(),
35543552
funding.get_counterparty_selected_contest_delay().unwrap(), &htlc, &self.channel_type,
35553553
&holder_keys.broadcaster_delayed_payment_key, &holder_keys.revocation_key);
35563554

@@ -3808,8 +3806,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
38083806
to_countersignatory_value_sat = 0;
38093807
}
38103808

3811-
let num_nondust_htlcs = included_non_dust_htlcs.len();
3812-
38133809
let channel_parameters =
38143810
if local { funding.channel_transaction_parameters.as_holder_broadcastable() }
38153811
else { funding.channel_transaction_parameters.as_counterparty_broadcastable() };
@@ -3829,12 +3825,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
38293825

38303826
CommitmentStats {
38313827
tx,
3832-
feerate_per_kw,
38333828
total_fee_sat,
3834-
num_nondust_htlcs,
38353829
htlcs_included,
3836-
local_balance_msat: value_to_self_msat,
3837-
remote_balance_msat: value_to_remote_msat,
3830+
local_balance_before_fee_msat: value_to_self_msat,
3831+
remote_balance_before_fee_msat: value_to_remote_msat,
38383832
inbound_htlc_preimages,
38393833
outbound_htlc_preimages,
38403834
}
@@ -6337,8 +6331,8 @@ impl<SP: Deref> FundedChannel<SP> where
63376331
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
63386332
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
63396333
let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.holder_commitment_point.transaction_number(), &self.holder_commitment_point.current_point(), true, true, logger);
6340-
let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_stats.num_nondust_htlcs + htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.context.get_channel_type()) * 1000;
6341-
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
6334+
let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_stats.tx.htlcs().len() + htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.context.get_channel_type()) * 1000;
6335+
let holder_balance_msat = commitment_stats.local_balance_before_fee_msat - htlc_stats.outbound_holding_cell_msat;
63426336
if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
63436337
//TODO: auto-close after a number of failures?
63446338
log_debug!(logger, "Cannot afford to send new feerate at {}", feerate_per_kw);
@@ -8736,7 +8730,7 @@ impl<SP: Deref> FundedChannel<SP> where
87368730
&& info.next_holder_htlc_id == self.context.next_holder_htlc_id
87378731
&& info.next_counterparty_htlc_id == self.context.next_counterparty_htlc_id
87388732
&& info.feerate == self.context.feerate_per_kw {
8739-
let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.context.get_channel_type()) * 1000;
8733+
let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, counterparty_commitment_tx.htlcs().len(), self.context.get_channel_type()) * 1000;
87408734
assert_eq!(actual_fee, info.fee);
87418735
}
87428736
}
@@ -8780,7 +8774,7 @@ impl<SP: Deref> FundedChannel<SP> where
87808774
debug_assert_eq!(htlc_signatures.len(), commitment_stats.tx.htlcs().len());
87818775
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(commitment_stats.tx.htlcs()) {
87828776
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
8783-
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.funding.get_holder_selected_contest_delay(), htlc, &self.context.channel_type, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
8777+
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.tx.feerate_per_kw(), self.funding.get_holder_selected_contest_delay(), htlc, &self.context.channel_type, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
87848778
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
87858779
log_bytes!(counterparty_keys.broadcaster_htlc_key.to_public_key().serialize()),
87868780
log_bytes!(htlc_sig.serialize_compact()[..]), &self.context.channel_id());

0 commit comments

Comments
 (0)