Skip to content

Commit 00bf32d

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 5468eb9 commit 00bf32d

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

lightning/src/ln/channel.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,7 @@ struct HTLCStats {
878878
/// An enum gathering stats on commitment transaction, either local or remote.
879879
struct CommitmentStats<'a> {
880880
tx: CommitmentTransaction, // the transaction info
881-
feerate_per_kw: u32, // the feerate included to build the transaction
882881
total_fee_sat: u64, // the total fee included in the transaction
883-
num_nondust_htlcs: usize, // the number of HTLC outputs (dust HTLCs *non*-included)
884882
htlcs_included: Vec<(HTLCOutputInCommitment, Option<&'a HTLCSource>)>, // the list of HTLCs (dust HTLCs *included*) which were not ignored when building the transaction
885883
local_balance_msat: u64, // local balance before fees *not* considering dust limits
886884
remote_balance_msat: u64, // remote balance before fees *not* considering dust limits
@@ -3602,8 +3600,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36023600
value_to_b = 0;
36033601
}
36043602

3605-
let num_nondust_htlcs = included_non_dust_htlcs.len();
3606-
36073603
let channel_parameters =
36083604
if local { self.channel_transaction_parameters.as_holder_broadcastable() }
36093605
else { self.channel_transaction_parameters.as_counterparty_broadcastable() };
@@ -3643,9 +3639,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
36433639

36443640
CommitmentStats {
36453641
tx,
3646-
feerate_per_kw,
36473642
total_fee_sat,
3648-
num_nondust_htlcs,
36493643
htlcs_included: htlcs_in_tx,
36503644
local_balance_msat: value_to_self_msat,
36513645
remote_balance_msat: value_to_remote_msat,
@@ -5488,8 +5482,8 @@ impl<SP: Deref> FundedChannel<SP> where
54885482
}
54895483
}
54905484

5491-
if msg.htlc_signatures.len() != commitment_stats.num_nondust_htlcs {
5492-
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)));
5485+
if msg.htlc_signatures.len() != commitment_stats.tx.htlcs().len() {
5486+
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())));
54935487
}
54945488

54955489
// Up to LDK 0.0.115, HTLC information was required to be duplicated in the
@@ -5512,7 +5506,7 @@ impl<SP: Deref> FundedChannel<SP> where
55125506
let holder_keys = TxCreationKeys::from_channel_static_keys(&self.holder_commitment_point.current_point(), self.context.get_holder_pubkeys(), self.context.get_counterparty_pubkeys(), &self.context.secp_ctx);
55135507
for (idx, (htlc, mut source_opt)) in htlcs_cloned.drain(..).enumerate() {
55145508
if let Some(_) = htlc.transaction_output_index {
5515-
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
5509+
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.tx.feerate_per_kw(),
55165510
self.context.get_counterparty_selected_contest_delay().unwrap(), &htlc, &self.context.channel_type,
55175511
&holder_keys.broadcaster_delayed_payment_key, &holder_keys.revocation_key);
55185512

@@ -6205,7 +6199,7 @@ impl<SP: Deref> FundedChannel<SP> where
62056199
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
62066200
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
62076201
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);
6208-
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;
6202+
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;
62096203
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
62106204
if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
62116205
//TODO: auto-close after a number of failures?
@@ -8467,7 +8461,7 @@ impl<SP: Deref> FundedChannel<SP> where
84678461
&& info.next_holder_htlc_id == self.context.next_holder_htlc_id
84688462
&& info.next_counterparty_htlc_id == self.context.next_counterparty_htlc_id
84698463
&& info.feerate == self.context.feerate_per_kw {
8470-
let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, commitment_stats.num_nondust_htlcs, self.context.get_channel_type()) * 1000;
8464+
let actual_fee = commit_tx_fee_sat(self.context.feerate_per_kw, counterparty_commitment_tx.htlcs().len(), self.context.get_channel_type()) * 1000;
84718465
assert_eq!(actual_fee, info.fee);
84728466
}
84738467
}
@@ -8514,7 +8508,7 @@ impl<SP: Deref> FundedChannel<SP> where
85148508
let counterparty_keys = TxCreationKeys::from_channel_static_keys(&self.context.counterparty_cur_commitment_point.unwrap(), self.context.get_counterparty_pubkeys(), self.context.get_holder_pubkeys(), &self.context.secp_ctx);
85158509
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
85168510
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
8517-
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.context.get_holder_selected_contest_delay(), htlc, &self.context.channel_type, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
8511+
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.tx.feerate_per_kw(), self.context.get_holder_selected_contest_delay(), htlc, &self.context.channel_type, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
85188512
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
85198513
log_bytes!(counterparty_keys.broadcaster_htlc_key.to_public_key().serialize()),
85208514
log_bytes!(htlc_sig.serialize_compact()[..]), &self.context.channel_id());

0 commit comments

Comments
 (0)