Skip to content

Commit cdf46ea

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 6317aa5 commit cdf46ea

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
@@ -879,9 +879,7 @@ struct HTLCStats {
879879
/// An enum gathering stats on commitment transaction, either local or remote.
880880
struct CommitmentStats<'a> {
881881
tx: CommitmentTransaction, // the transaction info
882-
feerate_per_kw: u32, // the feerate included to build the transaction
883882
total_fee_sat: u64, // the total fee included in the transaction
884-
num_nondust_htlcs: usize, // the number of HTLC outputs (dust HTLCs *non*-included)
885883
htlcs_included: Vec<(HTLCOutputInCommitment, Option<&'a HTLCSource>)>, // the list of HTLCs (dust HTLCs *included*) which were not ignored when building the transaction
886884
local_balance_msat: u64, // local balance before fees *not* considering dust limits
887885
remote_balance_msat: u64, // remote balance before fees *not* considering dust limits
@@ -3644,8 +3642,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36443642
value_to_b = 0;
36453643
}
36463644

3647-
let num_nondust_htlcs = included_non_dust_htlcs.len();
3648-
36493645
let channel_parameters =
36503646
if local { funding.channel_transaction_parameters.as_holder_broadcastable() }
36513647
else { funding.channel_transaction_parameters.as_counterparty_broadcastable() };
@@ -3685,9 +3681,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36853681

36863682
CommitmentStats {
36873683
tx,
3688-
feerate_per_kw,
36893684
total_fee_sat,
3690-
num_nondust_htlcs,
36913685
htlcs_included: htlcs_in_tx,
36923686
local_balance_msat: value_to_self_msat,
36933687
remote_balance_msat: value_to_remote_msat,
@@ -5527,8 +5521,8 @@ impl<SP: Deref> FundedChannel<SP> where
55275521
}
55285522
}
55295523

5530-
if msg.htlc_signatures.len() != commitment_stats.num_nondust_htlcs {
5531-
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)));
5524+
if msg.htlc_signatures.len() != commitment_stats.tx.htlcs().len() {
5525+
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())));
55325526
}
55335527

55345528
// Up to LDK 0.0.115, HTLC information was required to be duplicated in the
@@ -5551,7 +5545,7 @@ impl<SP: Deref> FundedChannel<SP> where
55515545
let holder_keys = commitment_stats.tx.trust().keys();
55525546
for (idx, (htlc, mut source_opt)) in htlcs_cloned.drain(..).enumerate() {
55535547
if let Some(_) = htlc.transaction_output_index {
5554-
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
5548+
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.tx.feerate_per_kw(),
55555549
self.funding.get_counterparty_selected_contest_delay().unwrap(), &htlc, &self.context.channel_type,
55565550
&holder_keys.broadcaster_delayed_payment_key, &holder_keys.revocation_key);
55575551

@@ -6244,7 +6238,7 @@ impl<SP: Deref> FundedChannel<SP> where
62446238
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
62456239
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
62466240
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);
6247-
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;
6241+
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;
62486242
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
62496243
if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
62506244
//TODO: auto-close after a number of failures?
@@ -8506,7 +8500,7 @@ impl<SP: Deref> FundedChannel<SP> where
85068500
&& info.next_holder_htlc_id == self.context.next_holder_htlc_id
85078501
&& info.next_counterparty_htlc_id == self.context.next_counterparty_htlc_id
85088502
&& info.feerate == self.context.feerate_per_kw {
8509-
let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.context.get_channel_type()) * 1000;
8503+
let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, counterparty_commitment_tx.htlcs().len(), self.context.get_channel_type()) * 1000;
85108504
assert_eq!(actual_fee, info.fee);
85118505
}
85128506
}
@@ -8549,7 +8543,7 @@ impl<SP: Deref> FundedChannel<SP> where
85498543
let counterparty_keys = commitment_stats.tx.trust().keys();
85508544
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(commitment_stats.tx.htlcs()) {
85518545
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
8552-
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)),
8546+
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)),
85538547
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
85548548
log_bytes!(counterparty_keys.broadcaster_htlc_key.to_public_key().serialize()),
85558549
log_bytes!(htlc_sig.serialize_compact()[..]), &self.context.channel_id());

0 commit comments

Comments
 (0)