Skip to content

Commit af58ca9

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.
1 parent d71e302 commit af58ca9

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

lightning/src/ln/channel.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,7 @@ 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
887885
local_balance_msat: u64, // local balance before fees *not* considering dust limits
888886
remote_balance_msat: u64, // remote balance before fees *not* considering dust limits
@@ -3528,8 +3526,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
35283526
}
35293527
}
35303528

3531-
if msg.htlc_signatures.len() != commitment_stats.num_nondust_htlcs {
3532-
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)));
3529+
if msg.htlc_signatures.len() != commitment_stats.tx.htlcs().len() {
3530+
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())));
35333531
}
35343532

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

@@ -3801,8 +3799,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
38013799
to_countersignatory_value_sat = 0;
38023800
}
38033801

3804-
let num_nondust_htlcs = included_non_dust_htlcs.len();
3805-
38063802
let channel_parameters =
38073803
if local { funding.channel_transaction_parameters.as_holder_broadcastable() }
38083804
else { funding.channel_transaction_parameters.as_counterparty_broadcastable() };
@@ -3822,9 +3818,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
38223818

38233819
CommitmentStats {
38243820
tx,
3825-
feerate_per_kw,
38263821
total_fee_sat,
3827-
num_nondust_htlcs,
38283822
htlcs_included,
38293823
local_balance_msat: value_to_self_msat,
38303824
remote_balance_msat: value_to_remote_msat,
@@ -6330,7 +6324,7 @@ impl<SP: Deref> FundedChannel<SP> where
63306324
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
63316325
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
63326326
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);
6333-
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;
6327+
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;
63346328
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
63356329
if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
63366330
//TODO: auto-close after a number of failures?
@@ -8729,7 +8723,7 @@ impl<SP: Deref> FundedChannel<SP> where
87298723
&& info.next_holder_htlc_id == self.context.next_holder_htlc_id
87308724
&& info.next_counterparty_htlc_id == self.context.next_counterparty_htlc_id
87318725
&& info.feerate == self.context.feerate_per_kw {
8732-
let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.context.get_channel_type()) * 1000;
8726+
let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, counterparty_commitment_tx.htlcs().len(), self.context.get_channel_type()) * 1000;
87338727
assert_eq!(actual_fee, info.fee);
87348728
}
87358729
}
@@ -8773,7 +8767,7 @@ impl<SP: Deref> FundedChannel<SP> where
87738767
debug_assert_eq!(htlc_signatures.len(), commitment_stats.tx.htlcs().len());
87748768
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(commitment_stats.tx.htlcs()) {
87758769
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
8776-
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)),
8770+
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)),
87778771
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
87788772
log_bytes!(counterparty_keys.broadcaster_htlc_key.to_public_key().serialize()),
87798773
log_bytes!(htlc_sig.serialize_compact()[..]), &self.context.channel_id());

0 commit comments

Comments
 (0)