Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ChannelContext::get_commitment_stats #3682

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl HolderCommitment {
let delayed_payment_key = &tx_keys.broadcaster_delayed_payment_key;
let per_commitment_point = &tx_keys.per_commitment_point;

let mut nondust_htlcs = self.tx.htlcs().iter().zip(self.tx.counterparty_htlc_sigs.iter());
let mut nondust_htlcs = self.tx.nondust_htlcs().iter().zip(self.tx.counterparty_htlc_sigs.iter());
let mut sources = self.nondust_htlc_sources.iter();

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

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

impl HolderCommitment {
fn has_htlcs(&self) -> bool {
self.tx.htlcs().len() > 0 || self.dust_htlcs.len() > 0
self.tx.nondust_htlcs().len() > 0 || self.dust_htlcs.len() > 0
}

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

fn htlcs_with_sources(&self) -> impl Iterator<Item = (&HTLCOutputInCommitment, Option<&HTLCSource>)> {
let mut sources = self.nondust_htlc_sources.iter();
let nondust_htlcs = self.tx.htlcs().iter().map(move |htlc| {
let nondust_htlcs = self.tx.nondust_htlcs().iter().map(move |htlc| {
let mut source = None;
if htlc.offered && htlc.transaction_output_index.is_some() {
source = sources.next();
Expand Down Expand Up @@ -3093,8 +3093,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
// If we have non-dust HTLCs in htlc_outputs, ensure they match the HTLCs in the
// `holder_commitment_tx`. In the future, we'll no longer provide the redundant data
// and just pass in source data via `nondust_htlc_sources`.
debug_assert_eq!(htlc_outputs.iter().filter(|(_, s, _)| s.is_some()).count(), holder_commitment_tx.trust().htlcs().len());
for (a, b) in htlc_outputs.iter().filter(|(_, s, _)| s.is_some()).map(|(h, _, _)| h).zip(holder_commitment_tx.trust().htlcs().iter()) {
debug_assert_eq!(htlc_outputs.iter().filter(|(_, s, _)| s.is_some()).count(), holder_commitment_tx.trust().nondust_htlcs().len());
for (a, b) in htlc_outputs.iter().filter(|(_, s, _)| s.is_some()).map(|(h, _, _)| h).zip(holder_commitment_tx.trust().nondust_htlcs().iter()) {
debug_assert_eq!(a, b);
}
debug_assert_eq!(htlc_outputs.iter().filter(|(_, s, _)| s.is_some()).count(), holder_commitment_tx.counterparty_htlc_sigs.len());
Expand All @@ -3104,7 +3104,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {

// Backfill the non-dust HTLC sources.
debug_assert!(nondust_htlc_sources.is_empty());
nondust_htlc_sources.reserve_exact(holder_commitment_tx.htlcs().len());
nondust_htlc_sources.reserve_exact(holder_commitment_tx.nondust_htlcs().len());
let dust_htlcs = htlc_outputs.into_iter().filter_map(|(htlc, _, source)| {
// Filter our non-dust HTLCs, while at the same time pushing their sources into
// `nondust_htlc_sources`.
Expand All @@ -3124,18 +3124,18 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
// `nondust_htlc_sources` and the `holder_commitment_tx`
{
let mut prev = -1;
for htlc in holder_commitment_tx.trust().htlcs().iter() {
for htlc in holder_commitment_tx.trust().nondust_htlcs().iter() {
assert!(htlc.transaction_output_index.unwrap() as i32 > prev);
prev = htlc.transaction_output_index.unwrap() as i32;
}
}

debug_assert!(htlc_outputs.iter().all(|(htlc, _, _)| htlc.transaction_output_index.is_none()));
debug_assert!(htlc_outputs.iter().all(|(_, sig_opt, _)| sig_opt.is_none()));
debug_assert_eq!(holder_commitment_tx.trust().htlcs().len(), holder_commitment_tx.counterparty_htlc_sigs.len());
debug_assert_eq!(holder_commitment_tx.trust().nondust_htlcs().len(), holder_commitment_tx.counterparty_htlc_sigs.len());

let mut sources = nondust_htlc_sources.iter();
for htlc in holder_commitment_tx.trust().htlcs().iter() {
for htlc in holder_commitment_tx.trust().nondust_htlcs().iter() {
if htlc.offered {
let source = sources.next().expect("Non-dust HTLC sources didn't match commitment tx");
assert!(source.possibly_matches_output(htlc));
Expand Down Expand Up @@ -3526,7 +3526,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
let counterparty_node_id = self.counterparty_node_id;
let commitment_txid = commitment_tx.compute_txid();
debug_assert_eq!(self.funding.current_holder_commitment.tx.trust().txid(), commitment_txid);
let pending_htlcs = self.funding.current_holder_commitment.tx.trust().htlcs().to_vec();
let pending_htlcs = self.funding.current_holder_commitment.tx.trust().nondust_htlcs().to_vec();
let channel_value_satoshis = self.funding.channel_parameters.channel_value_satoshis;
let commitment_tx_fee_satoshis = channel_value_satoshis -
commitment_tx.output.iter().fold(0u64, |sum, output| sum + output.value.to_sat());
Expand Down Expand Up @@ -3973,7 +3973,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
fn get_broadcasted_holder_claims(
&self, holder_tx: &HolderCommitmentTransaction, conf_height: u32,
) -> (Vec<PackageTemplate>, Option<(ScriptBuf, PublicKey, RevocationKey)>) {
let mut claim_requests = Vec::with_capacity(holder_tx.htlcs().len());
let mut claim_requests = Vec::with_capacity(holder_tx.nondust_htlcs().len());

let tx = holder_tx.trust();
let keys = tx.keys();
Expand All @@ -3985,7 +3985,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
));

let txid = tx.txid();
for htlc in holder_tx.htlcs() {
for htlc in holder_tx.nondust_htlcs() {
if let Some(transaction_output_index) = htlc.transaction_output_index {
let (htlc_output, counterparty_spendable_height) = if htlc.offered {
let htlc_output = HolderHTLCOutput::build_offered(
Expand Down Expand Up @@ -4020,9 +4020,9 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {

// Returns holder HTLC outputs to watch and react to in case of spending.
fn get_broadcasted_holder_watch_outputs(&self, holder_tx: &HolderCommitmentTransaction) -> Vec<(u32, TxOut)> {
let mut watch_outputs = Vec::with_capacity(holder_tx.htlcs().len());
let mut watch_outputs = Vec::with_capacity(holder_tx.nondust_htlcs().len());
let tx = holder_tx.trust();
for htlc in holder_tx.htlcs() {
for htlc in holder_tx.nondust_htlcs() {
if let Some(transaction_output_index) = htlc.transaction_output_index {
watch_outputs.push((
transaction_output_index,
Expand Down Expand Up @@ -4115,7 +4115,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
let txid = self.funding.current_holder_commitment.tx.trust().txid();
log_trace!(logger, "Canceling claims for previously broadcast holder commitment {}", txid);
let mut outpoint = BitcoinOutPoint { txid, vout: 0 };
for htlc in self.funding.current_holder_commitment.tx.htlcs() {
for htlc in self.funding.current_holder_commitment.tx.nondust_htlcs() {
if let Some(vout) = htlc.transaction_output_index {
outpoint.vout = vout;
self.onchain_tx_handler.abandon_claim(&outpoint);
Expand All @@ -4129,7 +4129,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
if txid != *confirmed_commitment_txid {
log_trace!(logger, "Canceling claims for previously broadcast holder commitment {}", txid);
let mut outpoint = BitcoinOutPoint { txid, vout: 0 };
for htlc in prev_holder_commitment.tx.htlcs() {
for htlc in prev_holder_commitment.tx.nondust_htlcs() {
if let Some(vout) = htlc.transaction_output_index {
outpoint.vout = vout;
self.onchain_tx_handler.abandon_claim(&outpoint);
Expand All @@ -4155,7 +4155,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
if self.channel_type_features().supports_anchors_zero_fee_htlc_tx() {
return holder_transactions;
}
for htlc in self.funding.current_holder_commitment.tx.htlcs() {
for htlc in self.funding.current_holder_commitment.tx.nondust_htlcs() {
if let Some(vout) = htlc.transaction_output_index {
let preimage = if !htlc.offered {
if let Some((preimage, _)) = self.payment_preimages.get(&htlc.payment_hash) { Some(preimage.clone()) } else {
Expand Down
4 changes: 2 additions & 2 deletions lightning/src/chain/onchaintx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
if trusted_tx.txid() != outp.txid {
return None;
}
let (htlc_idx, htlc) = trusted_tx.htlcs().iter().enumerate()
let (htlc_idx, htlc) = trusted_tx.nondust_htlcs().iter().enumerate()
.find(|(_, htlc)| htlc.transaction_output_index.unwrap() == outp.vout)
.unwrap();
let counterparty_htlc_sig = holder_commitment.counterparty_htlc_sigs[htlc_idx];
Expand Down Expand Up @@ -1248,7 +1248,7 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
if outp.txid != trusted_tx.txid() {
return None;
}
trusted_tx.htlcs().iter().enumerate()
trusted_tx.nondust_htlcs().iter().enumerate()
.find(|(_, htlc)| if let Some(output_index) = htlc.transaction_output_index {
output_index == outp.vout
} else {
Expand Down
34 changes: 17 additions & 17 deletions lightning/src/ln/chan_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ pub struct CommitmentTransaction {
feerate_per_kw: u32,
// The set of non-dust HTLCs included in the commitment. They must be sorted in increasing
// output index order.
htlcs: Vec<HTLCOutputInCommitment>,
nondust_htlcs: Vec<HTLCOutputInCommitment>,
// Note that on upgrades, some features of existing outputs may be missed.
channel_type_features: ChannelTypeFeatures,
// A cache of the parties' pubkeys required to construct the transaction, see doc for trust()
Expand All @@ -1446,7 +1446,7 @@ impl PartialEq for CommitmentTransaction {
self.to_broadcaster_value_sat == o.to_broadcaster_value_sat &&
self.to_countersignatory_value_sat == o.to_countersignatory_value_sat &&
self.feerate_per_kw == o.feerate_per_kw &&
self.htlcs == o.htlcs &&
self.nondust_htlcs == o.nondust_htlcs &&
self.channel_type_features == o.channel_type_features &&
self.keys == o.keys;
if eq {
Expand All @@ -1468,7 +1468,7 @@ impl Writeable for CommitmentTransaction {
(6, self.feerate_per_kw, required),
(8, self.keys, required),
(10, self.built, required),
(12, self.htlcs, required_vec),
(12, self.nondust_htlcs, required_vec),
(14, legacy_deserialization_prevention_marker, option),
(15, self.channel_type_features, required),
});
Expand All @@ -1486,7 +1486,7 @@ impl Readable for CommitmentTransaction {
(6, feerate_per_kw, required),
(8, keys, required),
(10, built, required),
(12, htlcs, required_vec),
(12, nondust_htlcs, required_vec),
(14, _legacy_deserialization_prevention_marker, (option, explicit_type: ())),
(15, channel_type_features, option),
});
Expand All @@ -1503,7 +1503,7 @@ impl Readable for CommitmentTransaction {
feerate_per_kw: feerate_per_kw.0.unwrap(),
keys: keys.0.unwrap(),
built: built.0.unwrap(),
htlcs,
nondust_htlcs,
channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
})
}
Expand All @@ -1526,7 +1526,7 @@ impl CommitmentTransaction {
let keys = TxCreationKeys::from_channel_static_keys(per_commitment_point, channel_parameters.broadcaster_pubkeys(), channel_parameters.countersignatory_pubkeys(), secp_ctx);

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

let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(commitment_number, channel_parameters);
let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
Expand All @@ -1537,7 +1537,7 @@ impl CommitmentTransaction {
to_countersignatory_value_sat,
to_broadcaster_delay: Some(channel_parameters.contest_delay()),
feerate_per_kw,
htlcs,
nondust_htlcs,
channel_type_features: channel_parameters.channel_type_features().clone(),
keys,
built: BuiltCommitmentTransaction {
Expand All @@ -1558,7 +1558,7 @@ impl CommitmentTransaction {
fn internal_rebuild_transaction(&self, keys: &TxCreationKeys, channel_parameters: &DirectedChannelTransactionParameters) -> BuiltCommitmentTransaction {
let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(self.commitment_number, channel_parameters);

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

let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
Expand Down Expand Up @@ -1653,7 +1653,7 @@ impl CommitmentTransaction {
}
}

let mut htlcs = Vec::with_capacity(htlcs_with_aux.len());
let mut nondust_htlcs = Vec::with_capacity(htlcs_with_aux.len());
for (htlc, _) in htlcs_with_aux {
let script = get_htlc_redeemscript(htlc, channel_type, keys);
let txout = TxOut {
Expand Down Expand Up @@ -1683,11 +1683,11 @@ impl CommitmentTransaction {
for (idx, out) in txouts.drain(..).enumerate() {
if let Some(htlc) = out.1 {
htlc.transaction_output_index = Some(idx as u32);
htlcs.push(htlc.clone());
nondust_htlcs.push(htlc.clone());
}
outputs.push(out.0);
}
(outputs, htlcs)
(outputs, nondust_htlcs)
}

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

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

for this_htlc in inner.htlcs.iter() {
for this_htlc in inner.nondust_htlcs.iter() {
assert!(this_htlc.transaction_output_index.is_some());
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);

Expand All @@ -1852,7 +1852,7 @@ impl<'a> TrustedCommitmentTransaction<'a> {
preimage: &Option<PaymentPreimage>,
) -> Transaction {
let keys = &self.inner.keys;
let this_htlc = &self.inner.htlcs[htlc_index];
let this_htlc = &self.inner.nondust_htlcs[htlc_index];
assert!(this_htlc.transaction_output_index.is_some());
// if we don't have preimage for an HTLC-Success, we can't generate an HTLC transaction.
if !this_htlc.offered && preimage.is_none() { unreachable!(); }
Expand All @@ -1874,7 +1874,7 @@ impl<'a> TrustedCommitmentTransaction<'a> {
) -> Witness {
let keys = &self.inner.keys;
let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(
&self.inner.htlcs[htlc_index], &self.channel_type_features, &keys.broadcaster_htlc_key,
&self.inner.nondust_htlcs[htlc_index], &self.channel_type_features, &keys.broadcaster_htlc_key,
&keys.countersignatory_htlc_key, &keys.revocation_key
);
build_htlc_input_witness(
Expand Down
Loading
Loading