Skip to content

Commit 1ae830f

Browse files
f avoid loop
1 parent dbaba7b commit 1ae830f

1 file changed

Lines changed: 27 additions & 31 deletions

File tree

lightning/src/chain/channelmonitor.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,6 +2069,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
20692069
htlc_outputs,
20702070
commitment_number,
20712071
their_per_commitment_point,
2072+
None,
20722073
)
20732074
}
20742075

@@ -3546,16 +3547,16 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35463547
}
35473548

35483549
self.provide_latest_counterparty_commitment_tx(commitment_tx.trust().txid(), Vec::new(), commitment_tx.commitment_number(),
3549-
commitment_tx.per_commitment_point());
3550+
commitment_tx.per_commitment_point(), Some(commitment_tx.clone()));
35503551
// Soon, we will only populate this field
3551-
self.funding.cur_counterparty_commitment_tx = Some(commitment_tx.clone());
35523552
self.initial_counterparty_commitment_tx = Some(commitment_tx);
35533553
}
35543554

35553555
#[rustfmt::skip]
35563556
fn provide_latest_counterparty_commitment_tx(
35573557
&mut self, txid: Txid, htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>,
35583558
commitment_number: u64, their_per_commitment_point: PublicKey,
3559+
commitment_tx: Option<CommitmentTransaction>,
35593560
) {
35603561
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
35613562
// so that a remote monitor doesn't learn anything unless there is a malicious close.
@@ -3570,6 +3571,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35703571
self.funding.counterparty_claimable_outpoints.insert(txid, htlc_outputs);
35713572
self.current_counterparty_commitment_number = commitment_number;
35723573

3574+
if let Some(commitment_tx) = commitment_tx {
3575+
self.funding.prev_counterparty_commitment_tx = self.funding.cur_counterparty_commitment_tx.take();
3576+
self.funding.cur_counterparty_commitment_tx = Some(commitment_tx);
3577+
}
3578+
35733579
//TODO: Merge this into the other per-counterparty-transaction output storage stuff
35743580
match self.their_cur_per_commitment_points {
35753581
Some(old_points) => {
@@ -3619,21 +3625,19 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36193625
nondust_htlcs.chain(dust_htlcs).collect::<Vec<_>>()
36203626
};
36213627

3622-
let current_funding_commitment_tx = commitment_txs.first().unwrap();
3623-
self.provide_latest_counterparty_commitment_tx(
3624-
current_funding_commitment_tx.trust().txid(),
3625-
htlcs_for_commitment(current_funding_commitment_tx),
3626-
current_funding_commitment_tx.commitment_number(),
3627-
current_funding_commitment_tx.per_commitment_point(),
3628-
);
3628+
let current_funding_commitment_tx = commitment_txs.first().unwrap().clone();
36293629
// Note: CommitmentSecret and new counterparty commitments may be batched
36303630
// in a single ChannelMonitorUpdate (e.g. when revoke_and_ack frees the
36313631
// holding cell). This is safe because get_pending_justice_txs checks both
36323632
// cur and prev for available secrets, so the revoked commitment remains
36333633
// accessible in prev after rotation.
3634-
self.funding.prev_counterparty_commitment_tx =
3635-
self.funding.cur_counterparty_commitment_tx.take();
3636-
self.funding.cur_counterparty_commitment_tx = Some(current_funding_commitment_tx.clone());
3634+
self.provide_latest_counterparty_commitment_tx(
3635+
current_funding_commitment_tx.trust().txid(),
3636+
htlcs_for_commitment(&current_funding_commitment_tx),
3637+
current_funding_commitment_tx.commitment_number(),
3638+
current_funding_commitment_tx.per_commitment_point(),
3639+
Some(current_funding_commitment_tx),
3640+
);
36373641

36383642
for (pending_funding, commitment_tx) in
36393643
self.pending_funding.iter_mut().zip(commitment_txs.iter().skip(1))
@@ -4286,10 +4290,19 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
42864290
// Soon we will drop the `LatestCounterpartyCommitmentTXInfo` variant in favor of `LatestCounterpartyCommitment`.
42874291
// For now we just add the code to handle the new updates.
42884292
// Next step: in channel, switch channel monitor updates to use the `LatestCounterpartyCommitment` variant.
4289-
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo { commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, .. } => {
4293+
ChannelMonitorUpdateStep::LatestCounterpartyCommitmentTXInfo {
4294+
commitment_txid, htlc_outputs, commitment_number, their_per_commitment_point, ..
4295+
} => {
42904296
log_trace!(logger, "Updating ChannelMonitor with latest counterparty commitment transaction info");
42914297
if self.pending_funding.is_empty() {
4292-
self.provide_latest_counterparty_commitment_tx(*commitment_txid, htlc_outputs.clone(), *commitment_number, *their_per_commitment_point)
4298+
// If this update was persisted by a pre-2023-06 LDK version, the feerate
4299+
// and balance fields will be `None` and we can't rebuild the full
4300+
// `CommitmentTransaction` for justice-tx tracking; fall back to txid/htlc-only.
4301+
let commitment_tx = self.counterparty_commitment_txs_from_update_step(update).pop();
4302+
self.provide_latest_counterparty_commitment_tx(
4303+
*commitment_txid, htlc_outputs.clone(), *commitment_number,
4304+
*their_per_commitment_point, commitment_tx,
4305+
)
42934306
} else {
42944307
log_error!(logger, "Received unexpected non-splice counterparty commitment monitor update");
42954308
ret = Err(());
@@ -4374,23 +4387,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
43744387
}
43754388
}
43764389

4377-
// Populate cur/prev for the LatestCounterpartyCommitmentTXInfo path, which
4378-
// doesn't go through update_counterparty_commitment_data.
4379-
for commitment_tx in self.counterparty_commitment_txs_from_update(updates) {
4380-
let txid = commitment_tx.trust().built_transaction().txid;
4381-
let funding = core::iter::once(&mut self.funding)
4382-
.chain(self.pending_funding.iter_mut())
4383-
.find(|f| f.current_counterparty_commitment_txid == Some(txid));
4384-
if let Some(funding) = funding {
4385-
if funding.cur_counterparty_commitment_tx.as_ref()
4386-
.map(|c| c.trust().built_transaction().txid) != Some(txid)
4387-
{
4388-
funding.prev_counterparty_commitment_tx = funding.cur_counterparty_commitment_tx.take();
4389-
funding.cur_counterparty_commitment_tx = Some(commitment_tx);
4390-
}
4391-
}
4392-
}
4393-
43944390
self.latest_update_id = updates.update_id;
43954391

43964392
// If a counterparty commitment update was applied while the funding output has already

0 commit comments

Comments
 (0)