Skip to content

Commit cc05008

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 6c62d46 commit cc05008

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
@@ -3504,8 +3502,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
35043502
}
35053503
}
35063504

3507-
if msg.htlc_signatures.len() != commitment_stats.num_nondust_htlcs {
3508-
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)));
3505+
if msg.htlc_signatures.len() != commitment_stats.tx.htlcs().len() {
3506+
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())));
35093507
}
35103508

35113509
// Up to LDK 0.0.115, HTLC information was required to be duplicated in the
@@ -3528,7 +3526,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
35283526
let holder_keys = commitment_stats.tx.trust().keys();
35293527
for (idx, (htlc, mut source_opt)) in htlcs_cloned.drain(..).enumerate() {
35303528
if let Some(_) = htlc.transaction_output_index {
3531-
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
3529+
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.tx.feerate_per_kw(),
35323530
funding.get_counterparty_selected_contest_delay().unwrap(), &htlc, &self.channel_type,
35333531
&holder_keys.broadcaster_delayed_payment_key, &holder_keys.revocation_key);
35343532

@@ -3773,8 +3771,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
37733771
value_to_b = 0;
37743772
}
37753773

3776-
let num_nondust_htlcs = included_non_dust_htlcs.len();
3777-
37783774
let channel_parameters =
37793775
if local { funding.channel_transaction_parameters.as_holder_broadcastable() }
37803776
else { funding.channel_transaction_parameters.as_counterparty_broadcastable() };
@@ -3814,9 +3810,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
38143810

38153811
CommitmentStats {
38163812
tx,
3817-
feerate_per_kw,
38183813
total_fee_sat,
3819-
num_nondust_htlcs,
38203814
htlcs_included: htlcs_in_tx,
38213815
local_balance_msat: value_to_self_msat,
38223816
remote_balance_msat: value_to_remote_msat,
@@ -6277,7 +6271,7 @@ impl<SP: Deref> FundedChannel<SP> where
62776271
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
62786272
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
62796273
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);
6280-
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;
6274+
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;
62816275
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
62826276
if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
62836277
//TODO: auto-close after a number of failures?
@@ -8543,7 +8537,7 @@ impl<SP: Deref> FundedChannel<SP> where
85438537
&& info.next_holder_htlc_id == self.context.next_holder_htlc_id
85448538
&& info.next_counterparty_htlc_id == self.context.next_counterparty_htlc_id
85458539
&& info.feerate == self.context.feerate_per_kw {
8546-
let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.context.get_channel_type()) * 1000;
8540+
let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, counterparty_commitment_tx.htlcs().len(), self.context.get_channel_type()) * 1000;
85478541
assert_eq!(actual_fee, info.fee);
85488542
}
85498543
}
@@ -8586,7 +8580,7 @@ impl<SP: Deref> FundedChannel<SP> where
85868580
let counterparty_keys = commitment_stats.tx.trust().keys();
85878581
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(commitment_stats.tx.htlcs()) {
85888582
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
8589-
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)),
8583+
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)),
85908584
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
85918585
log_bytes!(counterparty_keys.broadcaster_htlc_key.to_public_key().serialize()),
85928586
log_bytes!(htlc_sig.serialize_compact()[..]), &self.context.channel_id());

0 commit comments

Comments
 (0)