Skip to content

Commit ef0fcab

Browse files
authored
Merge pull request #3682 from tankyleo/commitment-stats-less-duplication
Add `ChannelContext::get_commitment_stats`
2 parents 46cb5ff + 671c4b0 commit ef0fcab

File tree

6 files changed

+367
-268
lines changed

6 files changed

+367
-268
lines changed

lightning/src/chain/channelmonitor.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl HolderCommitment {
317317
let delayed_payment_key = &tx_keys.broadcaster_delayed_payment_key;
318318
let per_commitment_point = &tx_keys.per_commitment_point;
319319

320-
let mut nondust_htlcs = self.tx.htlcs().iter().zip(self.tx.counterparty_htlc_sigs.iter());
320+
let mut nondust_htlcs = self.tx.nondust_htlcs().iter().zip(self.tx.counterparty_htlc_sigs.iter());
321321
let mut sources = self.nondust_htlc_sources.iter();
322322

323323
// Use an iterator to write `htlc_outputs` to avoid allocations.
@@ -937,7 +937,7 @@ impl TryFrom<(HolderCommitmentTransaction, HolderSignedTx)> for HolderCommitment
937937
// HTLC sources, separately. All offered, non-dust HTLCs must have a source available.
938938

939939
let mut missing_nondust_source = false;
940-
let mut nondust_htlc_sources = Vec::with_capacity(holder_commitment_tx.htlcs().len());
940+
let mut nondust_htlc_sources = Vec::with_capacity(holder_commitment_tx.nondust_htlcs().len());
941941
let dust_htlcs = holder_signed_tx.htlc_outputs.into_iter().filter_map(|(htlc, _, source)| {
942942
// Filter our non-dust HTLCs, while at the same time pushing their sources into
943943
// `nondust_htlc_sources`.
@@ -967,16 +967,16 @@ impl TryFrom<(HolderCommitmentTransaction, HolderSignedTx)> for HolderCommitment
967967

968968
impl HolderCommitment {
969969
fn has_htlcs(&self) -> bool {
970-
self.tx.htlcs().len() > 0 || self.dust_htlcs.len() > 0
970+
self.tx.nondust_htlcs().len() > 0 || self.dust_htlcs.len() > 0
971971
}
972972

973973
fn htlcs(&self) -> impl Iterator<Item = &HTLCOutputInCommitment> {
974-
self.tx.htlcs().iter().chain(self.dust_htlcs.iter().map(|(htlc, _)| htlc))
974+
self.tx.nondust_htlcs().iter().chain(self.dust_htlcs.iter().map(|(htlc, _)| htlc))
975975
}
976976

977977
fn htlcs_with_sources(&self) -> impl Iterator<Item = (&HTLCOutputInCommitment, Option<&HTLCSource>)> {
978978
let mut sources = self.nondust_htlc_sources.iter();
979-
let nondust_htlcs = self.tx.htlcs().iter().map(move |htlc| {
979+
let nondust_htlcs = self.tx.nondust_htlcs().iter().map(move |htlc| {
980980
let mut source = None;
981981
if htlc.offered && htlc.transaction_output_index.is_some() {
982982
source = sources.next();
@@ -3098,8 +3098,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
30983098
// If we have non-dust HTLCs in htlc_outputs, ensure they match the HTLCs in the
30993099
// `holder_commitment_tx`. In the future, we'll no longer provide the redundant data
31003100
// and just pass in source data via `nondust_htlc_sources`.
3101-
debug_assert_eq!(htlc_outputs.iter().filter(|(_, s, _)| s.is_some()).count(), holder_commitment_tx.trust().htlcs().len());
3102-
for (a, b) in htlc_outputs.iter().filter(|(_, s, _)| s.is_some()).map(|(h, _, _)| h).zip(holder_commitment_tx.trust().htlcs().iter()) {
3101+
debug_assert_eq!(htlc_outputs.iter().filter(|(_, s, _)| s.is_some()).count(), holder_commitment_tx.trust().nondust_htlcs().len());
3102+
for (a, b) in htlc_outputs.iter().filter(|(_, s, _)| s.is_some()).map(|(h, _, _)| h).zip(holder_commitment_tx.trust().nondust_htlcs().iter()) {
31033103
debug_assert_eq!(a, b);
31043104
}
31053105
debug_assert_eq!(htlc_outputs.iter().filter(|(_, s, _)| s.is_some()).count(), holder_commitment_tx.counterparty_htlc_sigs.len());
@@ -3109,7 +3109,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
31093109

31103110
// Backfill the non-dust HTLC sources.
31113111
debug_assert!(nondust_htlc_sources.is_empty());
3112-
nondust_htlc_sources.reserve_exact(holder_commitment_tx.htlcs().len());
3112+
nondust_htlc_sources.reserve_exact(holder_commitment_tx.nondust_htlcs().len());
31133113
let dust_htlcs = htlc_outputs.into_iter().filter_map(|(htlc, _, source)| {
31143114
// Filter our non-dust HTLCs, while at the same time pushing their sources into
31153115
// `nondust_htlc_sources`.
@@ -3129,18 +3129,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
31293129
// `nondust_htlc_sources` and the `holder_commitment_tx`
31303130
{
31313131
let mut prev = -1;
3132-
for htlc in holder_commitment_tx.trust().htlcs().iter() {
3132+
for htlc in holder_commitment_tx.trust().nondust_htlcs().iter() {
31333133
assert!(htlc.transaction_output_index.unwrap() as i32 > prev);
31343134
prev = htlc.transaction_output_index.unwrap() as i32;
31353135
}
31363136
}
31373137

31383138
debug_assert!(htlc_outputs.iter().all(|(htlc, _, _)| htlc.transaction_output_index.is_none()));
31393139
debug_assert!(htlc_outputs.iter().all(|(_, sig_opt, _)| sig_opt.is_none()));
3140-
debug_assert_eq!(holder_commitment_tx.trust().htlcs().len(), holder_commitment_tx.counterparty_htlc_sigs.len());
3140+
debug_assert_eq!(holder_commitment_tx.trust().nondust_htlcs().len(), holder_commitment_tx.counterparty_htlc_sigs.len());
31413141

31423142
let mut sources = nondust_htlc_sources.iter();
3143-
for htlc in holder_commitment_tx.trust().htlcs().iter() {
3143+
for htlc in holder_commitment_tx.trust().nondust_htlcs().iter() {
31443144
if htlc.offered {
31453145
let source = sources.next().expect("Non-dust HTLC sources didn't match commitment tx");
31463146
assert!(source.possibly_matches_output(htlc));
@@ -3955,9 +3955,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
39553955
&self, holder_tx: &HolderCommitmentTransaction,
39563956
) -> Vec<HTLCDescriptor> {
39573957
let tx = holder_tx.trust();
3958-
let mut htlcs = Vec::with_capacity(holder_tx.htlcs().len());
3959-
debug_assert_eq!(holder_tx.htlcs().len(), holder_tx.counterparty_htlc_sigs.len());
3960-
for (htlc, counterparty_sig) in holder_tx.htlcs().iter().zip(holder_tx.counterparty_htlc_sigs.iter()) {
3958+
let mut htlcs = Vec::with_capacity(holder_tx.nondust_htlcs().len());
3959+
debug_assert_eq!(holder_tx.nondust_htlcs().len(), holder_tx.counterparty_htlc_sigs.len());
3960+
for (htlc, counterparty_sig) in holder_tx.nondust_htlcs().iter().zip(holder_tx.counterparty_htlc_sigs.iter()) {
39613961
assert!(htlc.transaction_output_index.is_some(), "Expected transaction output index for non-dust HTLC");
39623962

39633963
let preimage = if htlc.offered {
@@ -4026,9 +4026,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
40264026

40274027
// Returns holder HTLC outputs to watch and react to in case of spending.
40284028
fn get_broadcasted_holder_watch_outputs(&self, holder_tx: &HolderCommitmentTransaction) -> Vec<(u32, TxOut)> {
4029-
let mut watch_outputs = Vec::with_capacity(holder_tx.htlcs().len());
4029+
let mut watch_outputs = Vec::with_capacity(holder_tx.nondust_htlcs().len());
40304030
let tx = holder_tx.trust();
4031-
for htlc in holder_tx.htlcs() {
4031+
for htlc in holder_tx.nondust_htlcs() {
40324032
if let Some(transaction_output_index) = htlc.transaction_output_index {
40334033
watch_outputs.push((
40344034
transaction_output_index,
@@ -4121,7 +4121,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
41214121
let txid = self.funding.current_holder_commitment.tx.trust().txid();
41224122
log_trace!(logger, "Canceling claims for previously broadcast holder commitment {}", txid);
41234123
let mut outpoint = BitcoinOutPoint { txid, vout: 0 };
4124-
for htlc in self.funding.current_holder_commitment.tx.htlcs() {
4124+
for htlc in self.funding.current_holder_commitment.tx.nondust_htlcs() {
41254125
if let Some(vout) = htlc.transaction_output_index {
41264126
outpoint.vout = vout;
41274127
self.onchain_tx_handler.abandon_claim(&outpoint);
@@ -4135,7 +4135,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
41354135
if txid != *confirmed_commitment_txid {
41364136
log_trace!(logger, "Canceling claims for previously broadcast holder commitment {}", txid);
41374137
let mut outpoint = BitcoinOutPoint { txid, vout: 0 };
4138-
for htlc in prev_holder_commitment.tx.htlcs() {
4138+
for htlc in prev_holder_commitment.tx.nondust_htlcs() {
41394139
if let Some(vout) = htlc.transaction_output_index {
41404140
outpoint.vout = vout;
41414141
self.onchain_tx_handler.abandon_claim(&outpoint);

lightning/src/chain/onchaintx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
688688
OnchainClaim::Event(ClaimEvent::BumpCommitment {
689689
package_target_feerate_sat_per_1000_weight,
690690
commitment_tx: tx,
691-
pending_nondust_htlcs: holder_commitment.htlcs().to_vec(),
691+
pending_nondust_htlcs: holder_commitment.nondust_htlcs().to_vec(),
692692
commitment_tx_fee_satoshis: fee_sat,
693693
anchor_output_idx: idx,
694694
channel_parameters: channel_parameters.clone(),
@@ -1339,7 +1339,7 @@ mod tests {
13391339
let holder_commit = tx_handler.current_holder_commitment_tx();
13401340
let holder_commit_txid = holder_commit.trust().txid();
13411341
let mut requests = Vec::new();
1342-
for (htlc, counterparty_sig) in holder_commit.htlcs().iter().zip(holder_commit.counterparty_htlc_sigs.iter()) {
1342+
for (htlc, counterparty_sig) in holder_commit.nondust_htlcs().iter().zip(holder_commit.counterparty_htlc_sigs.iter()) {
13431343
requests.push(PackageTemplate::build_package(
13441344
holder_commit_txid,
13451345
htlc.transaction_output_index.unwrap(),

lightning/src/chain/package.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ impl HolderHTLCOutput {
472472
}
473473

474474
let (htlc, counterparty_sig) =
475-
trusted_tx.htlcs().iter().zip(holder_commitment.counterparty_htlc_sigs.iter())
475+
trusted_tx.nondust_htlcs().iter().zip(holder_commitment.counterparty_htlc_sigs.iter())
476476
.find(|(htlc, _)| htlc.transaction_output_index.unwrap() == outp.vout)
477477
.unwrap();
478478

lightning/src/ln/chan_utils.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ pub struct CommitmentTransaction {
14301430
feerate_per_kw: u32,
14311431
// The set of non-dust HTLCs included in the commitment. They must be sorted in increasing
14321432
// output index order.
1433-
htlcs: Vec<HTLCOutputInCommitment>,
1433+
nondust_htlcs: Vec<HTLCOutputInCommitment>,
14341434
// Note that on upgrades, some features of existing outputs may be missed.
14351435
channel_type_features: ChannelTypeFeatures,
14361436
// A cache of the parties' pubkeys required to construct the transaction, see doc for trust()
@@ -1446,7 +1446,7 @@ impl PartialEq for CommitmentTransaction {
14461446
self.to_broadcaster_value_sat == o.to_broadcaster_value_sat &&
14471447
self.to_countersignatory_value_sat == o.to_countersignatory_value_sat &&
14481448
self.feerate_per_kw == o.feerate_per_kw &&
1449-
self.htlcs == o.htlcs &&
1449+
self.nondust_htlcs == o.nondust_htlcs &&
14501450
self.channel_type_features == o.channel_type_features &&
14511451
self.keys == o.keys;
14521452
if eq {
@@ -1468,7 +1468,7 @@ impl Writeable for CommitmentTransaction {
14681468
(6, self.feerate_per_kw, required),
14691469
(8, self.keys, required),
14701470
(10, self.built, required),
1471-
(12, self.htlcs, required_vec),
1471+
(12, self.nondust_htlcs, required_vec),
14721472
(14, legacy_deserialization_prevention_marker, option),
14731473
(15, self.channel_type_features, required),
14741474
});
@@ -1486,7 +1486,7 @@ impl Readable for CommitmentTransaction {
14861486
(6, feerate_per_kw, required),
14871487
(8, keys, required),
14881488
(10, built, required),
1489-
(12, htlcs, required_vec),
1489+
(12, nondust_htlcs, required_vec),
14901490
(14, _legacy_deserialization_prevention_marker, (option, explicit_type: ())),
14911491
(15, channel_type_features, option),
14921492
});
@@ -1503,7 +1503,7 @@ impl Readable for CommitmentTransaction {
15031503
feerate_per_kw: feerate_per_kw.0.unwrap(),
15041504
keys: keys.0.unwrap(),
15051505
built: built.0.unwrap(),
1506-
htlcs,
1506+
nondust_htlcs,
15071507
channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
15081508
})
15091509
}
@@ -1526,7 +1526,7 @@ impl CommitmentTransaction {
15261526
let keys = TxCreationKeys::from_channel_static_keys(per_commitment_point, channel_parameters.broadcaster_pubkeys(), channel_parameters.countersignatory_pubkeys(), secp_ctx);
15271527

15281528
// Sort outputs and populate output indices while keeping track of the auxiliary data
1529-
let (outputs, htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters);
1529+
let (outputs, nondust_htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters);
15301530

15311531
let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(commitment_number, channel_parameters);
15321532
let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
@@ -1537,7 +1537,7 @@ impl CommitmentTransaction {
15371537
to_countersignatory_value_sat,
15381538
to_broadcaster_delay: Some(channel_parameters.contest_delay()),
15391539
feerate_per_kw,
1540-
htlcs,
1540+
nondust_htlcs,
15411541
channel_type_features: channel_parameters.channel_type_features().clone(),
15421542
keys,
15431543
built: BuiltCommitmentTransaction {
@@ -1558,7 +1558,7 @@ impl CommitmentTransaction {
15581558
fn internal_rebuild_transaction(&self, keys: &TxCreationKeys, channel_parameters: &DirectedChannelTransactionParameters) -> BuiltCommitmentTransaction {
15591559
let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(self.commitment_number, channel_parameters);
15601560

1561-
let mut htlcs_with_aux = self.htlcs.iter().map(|h| (h.clone(), ())).collect();
1561+
let mut htlcs_with_aux = self.nondust_htlcs.iter().map(|h| (h.clone(), ())).collect();
15621562
let (outputs, _) = Self::internal_build_outputs(keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &mut htlcs_with_aux, channel_parameters);
15631563

15641564
let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
@@ -1653,7 +1653,7 @@ impl CommitmentTransaction {
16531653
}
16541654
}
16551655

1656-
let mut htlcs = Vec::with_capacity(htlcs_with_aux.len());
1656+
let mut nondust_htlcs = Vec::with_capacity(htlcs_with_aux.len());
16571657
for (htlc, _) in htlcs_with_aux {
16581658
let script = get_htlc_redeemscript(htlc, channel_type, keys);
16591659
let txout = TxOut {
@@ -1683,11 +1683,11 @@ impl CommitmentTransaction {
16831683
for (idx, out) in txouts.drain(..).enumerate() {
16841684
if let Some(htlc) = out.1 {
16851685
htlc.transaction_output_index = Some(idx as u32);
1686-
htlcs.push(htlc.clone());
1686+
nondust_htlcs.push(htlc.clone());
16871687
}
16881688
outputs.push(out.0);
16891689
}
1690-
(outputs, htlcs)
1690+
(outputs, nondust_htlcs)
16911691
}
16921692

16931693
fn internal_build_inputs(commitment_number: u64, channel_parameters: &DirectedChannelTransactionParameters) -> (u64, Vec<TxIn>) {
@@ -1746,8 +1746,8 @@ impl CommitmentTransaction {
17461746
///
17471747
/// This is not exported to bindings users as we cannot currently convert Vec references to/from C, though we should
17481748
/// expose a less effecient version which creates a Vec of references in the future.
1749-
pub fn htlcs(&self) -> &Vec<HTLCOutputInCommitment> {
1750-
&self.htlcs
1749+
pub fn nondust_htlcs(&self) -> &Vec<HTLCOutputInCommitment> {
1750+
&self.nondust_htlcs
17511751
}
17521752

17531753
/// Trust our pre-built transaction and derived transaction creation public keys.
@@ -1831,10 +1831,10 @@ impl<'a> TrustedCommitmentTransaction<'a> {
18311831
let inner = self.inner;
18321832
let keys = &inner.keys;
18331833
let txid = inner.built.txid;
1834-
let mut ret = Vec::with_capacity(inner.htlcs.len());
1834+
let mut ret = Vec::with_capacity(inner.nondust_htlcs.len());
18351835
let holder_htlc_key = derive_private_key(secp_ctx, &inner.keys.per_commitment_point, htlc_base_key);
18361836

1837-
for this_htlc in inner.htlcs.iter() {
1837+
for this_htlc in inner.nondust_htlcs.iter() {
18381838
assert!(this_htlc.transaction_output_index.is_some());
18391839
let htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, &self.channel_type_features, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
18401840

0 commit comments

Comments
 (0)