Skip to content

Commit

Permalink
f Avoid back-to-back locking
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Feb 1, 2023
1 parent b7462fd commit 65a1cfa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
19 changes: 14 additions & 5 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,20 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
self.inner.lock().unwrap().broadcast_latest_holder_commitment_txn(broadcaster, logger);
}

pub(crate) fn broadcast_latest_holder_commitment_txn_if_unspent<B: Deref, L: Deref>(
&self,
broadcaster: &B,
logger: &L,
) where
B::Target: BroadcasterInterface,
L::Target: Logger,
{
let mut locked_inner = self.inner.lock().unwrap();
if !locked_inner.detected_funding_spend() {
locked_inner.broadcast_latest_holder_commitment_txn(broadcaster, logger);
}
}

/// Updates a ChannelMonitor on the basis of some new information provided by the Channel
/// itself.
///
Expand Down Expand Up @@ -1236,11 +1250,6 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
self.inner.lock().unwrap().get_funding_txo().clone()
}

/// Gets whether we've seen a confirmed transaction spending the funding output.
pub(crate) fn detected_funding_spend(&self) -> bool {
self.inner.lock().unwrap().detected_funding_spend()
}

/// Gets a list of txids, with their output scripts (in the order they appear in the
/// transaction), which we must learn about spends of via block_connected().
pub fn get_outputs_to_watch(&self) -> Vec<(Txid, Vec<(u32, Script)>)> {
Expand Down
10 changes: 5 additions & 5 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7208,12 +7208,12 @@ where
}

for (funding_txo, monitor) in args.channel_monitors.iter_mut() {
// There's no need to broadcast our commitment transaction if we've seen one
// confirmed (even with 1 confirmation) as it'll be rejected as
// duplicate/conflicting.
if !funding_txo_set.contains(funding_txo) && !monitor.detected_funding_spend() {
if !funding_txo_set.contains(funding_txo) {
log_info!(args.logger, "Broadcasting latest holder commitment transaction for closed channel {}", log_bytes!(funding_txo.to_channel_id()));
monitor.broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);
// There's no need to broadcast our commitment transaction if
// we've seen one confirmed (even with 1 confirmation) as it'll
// be rejected as duplicate/conflicting.
monitor.broadcast_latest_holder_commitment_txn_if_unspent(&args.tx_broadcaster, &args.logger);
}
}

Expand Down

0 comments on commit 65a1cfa

Please sign in to comment.