Skip to content

Commit dbe7434

Browse files
fixup: addressing comments:
- set funding_seen_onchain to true on upgrade if missing - allow broadcast_latest_holder_commitment_txn to override auto-broadcast logic - do not reset funding_seen_onchain when block disconnected - fix unnecesary call to generate_claimable_outpoints_and_watch_outputs - fix indentation with spaces
1 parent dd46295 commit dbe7434

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,11 +2256,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
22562256
/// may be to contact the other node operator out-of-band to coordinate other options available
22572257
/// to you.
22582258
///
2259-
/// Note: For channels where the funding transaction is being manually managed (see
2260-
/// [`crate::ln::channelmanager::ChannelManager::funding_transaction_generated_manual_broadcast`]), this call will be
2261-
/// ignored until the funding transaction has been observed on-chain.
2262-
/// This prevents unconfirmable commitment transactions from being broadcast before funding is
2263-
/// visible.
2259+
/// Note: For channels using manual funding broadcast (see
2260+
/// [`crate::ln::channelmanager::ChannelManager::funding_transaction_generated_manual_broadcast`]),
2261+
/// automatic broadcasts are suppressed until the funding transaction has been observed on-chain.
2262+
/// Calling this method overrides that suppression and queues the latest holder commitment
2263+
/// transaction for broadcast even if the funding has not yet been seen on-chain. This is unsafe
2264+
/// and may result in unconfirmable transactions.
22642265
#[rustfmt::skip]
22652266
pub fn broadcast_latest_holder_commitment_txn<B: Deref, F: Deref, L: Deref>(
22662267
&self, broadcaster: &B, fee_estimator: &F, logger: &L
@@ -2273,7 +2274,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
22732274
let mut inner = self.inner.lock().unwrap();
22742275
let fee_estimator = LowerBoundedFeeEstimator::new(&**fee_estimator);
22752276
let logger = WithChannelMonitor::from_impl(logger, &*inner, None);
2276-
inner.queue_latest_holder_commitment_txn_for_broadcast(broadcaster, &fee_estimator, &logger);
2277+
inner.queue_latest_holder_commitment_txn_for_broadcast(broadcaster, &fee_estimator, &logger, false);
22772278
}
22782279

22792280
/// Unsafe test-only version of `broadcast_latest_holder_commitment_txn` used by our test framework
@@ -3918,15 +3919,25 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
39183919
}
39193920

39203921
#[rustfmt::skip]
3922+
/// Note: For channels where the funding transaction is being manually managed (see
3923+
/// [`crate::ln::channelmanager::ChannelManager::funding_transaction_generated_manual_broadcast`]),
3924+
/// this method returns without queuing any transactions until the funding transaction has been
3925+
/// observed on-chain, unless `require_funding_seen` is `false`. This prevents attempting to
3926+
/// broadcast unconfirmable holder commitment transactions before the funding is visible.
3927+
/// See also
3928+
/// [`crate::chain::channelmonitor::ChannelMonitor::broadcast_latest_holder_commitment_txn`].
39213929
pub(crate) fn queue_latest_holder_commitment_txn_for_broadcast<B: Deref, F: Deref, L: Deref>(
3922-
&mut self, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &WithChannelMonitor<L>
3930+
&mut self, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &WithChannelMonitor<L>,
3931+
require_funding_seen: bool,
39233932
)
39243933
where
39253934
B::Target: BroadcasterInterface,
39263935
F::Target: FeeEstimator,
39273936
L::Target: Logger,
39283937
{
3929-
if self.is_manual_broadcast && !self.funding_seen_onchain {
3938+
// In manual-broadcast mode, if `require_funding_seen` is true and we have not yet observed
3939+
// the funding transaction on-chain, do not queue any transactions.
3940+
if require_funding_seen && self.is_manual_broadcast && !self.funding_seen_onchain {
39303941
log_info!(logger, "Not broadcasting holder commitment for manual-broadcast channel before funding appears on-chain");
39313942
return;
39323943
}
@@ -4248,7 +4259,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
42484259
log_trace!(logger, "Avoiding commitment broadcast, already detected confirmed spend onchain");
42494260
continue;
42504261
}
4251-
self.queue_latest_holder_commitment_txn_for_broadcast(broadcaster, &bounded_fee_estimator, logger);
4262+
self.queue_latest_holder_commitment_txn_for_broadcast(broadcaster, &bounded_fee_estimator, logger, true);
42524263
} else if !self.holder_tx_signed {
42534264
log_error!(logger, "WARNING: You have a potentially-unsafe holder commitment transaction available to broadcast");
42544265
log_error!(logger, " in channel monitor for channel {}!", &self.channel_id());
@@ -5414,11 +5425,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
54145425

54155426
if should_broadcast_commitment {
54165427
// Avoid broadcasting in manual-broadcast mode until funding is seen on-chain.
5417-
if !self.is_manual_broadcast || self.funding_seen_onchain {
5418-
let (mut claimables, mut outputs) =
5419-
self.generate_claimable_outpoints_and_watch_outputs(None);
5420-
claimable_outpoints.append(&mut claimables);
5421-
watch_outputs.append(&mut outputs);
5428+
if !self.is_manual_broadcast || self.funding_seen_onchain {
5429+
let (mut claimables, mut outputs) =
5430+
self.generate_claimable_outpoints_and_watch_outputs(None);
5431+
claimable_outpoints.append(&mut claimables);
5432+
watch_outputs.append(&mut outputs);
54225433
}
54235434
}
54245435

@@ -5456,9 +5467,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
54565467
let should_broadcast = self.should_broadcast_holder_commitment_txn(logger);
54575468
if let Some(payment_hash) = should_broadcast {
54585469
let reason = ClosureReason::HTLCsTimedOut { payment_hash: Some(payment_hash) };
5459-
if self.is_manual_broadcast && !self.funding_seen_onchain {
5460-
let _ = self.generate_claimable_outpoints_and_watch_outputs(Some(reason));
5461-
} else {
5470+
if !self.is_manual_broadcast || self.funding_seen_onchain {
54625471
let (mut new_outpoints, mut new_outputs) =
54635472
self.generate_claimable_outpoints_and_watch_outputs(Some(reason));
54645473
claimable_outpoints.append(&mut new_outpoints);
@@ -5690,7 +5699,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
56905699
// Only attempt to broadcast the new commitment after the `block_disconnected` call above so that
56915700
// it doesn't get removed from the set of pending claims.
56925701
if should_broadcast_commitment {
5693-
self.queue_latest_holder_commitment_txn_for_broadcast(&broadcaster, &bounded_fee_estimator, logger);
5702+
self.queue_latest_holder_commitment_txn_for_broadcast(&broadcaster, &bounded_fee_estimator, logger, true);
56945703
}
56955704

56965705
self.best_block = fork_point;
@@ -5725,12 +5734,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
57255734
}
57265735

57275736
debug_assert!(!self.onchain_events_awaiting_threshold_conf.iter().any(|ref entry| entry.txid == *txid));
5728-
if *txid == self.funding.funding_txid() ||
5729-
self.pending_funding.iter().any(|f| f.funding_txid() == *txid)
5730-
{
5731-
log_trace!(logger, "transaction_unconfirmed removed observed funding. resetting funding_seen_onchain");
5732-
self.funding_seen_onchain = false;
5733-
}
57345737

57355738
// TODO: Replace with `take_if` once our MSRV is >= 1.80.
57365739
let mut should_broadcast_commitment = false;
@@ -5757,7 +5760,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
57575760
// Only attempt to broadcast the new commitment after the `transaction_unconfirmed` call above so
57585761
// that it doesn't get removed from the set of pending claims.
57595762
if should_broadcast_commitment {
5760-
self.queue_latest_holder_commitment_txn_for_broadcast(&broadcaster, fee_estimator, logger);
5763+
self.queue_latest_holder_commitment_txn_for_broadcast(&broadcaster, fee_estimator, logger, true);
57615764
}
57625765
}
57635766

@@ -6556,7 +6559,8 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
65566559
},
65576560
pending_funding: pending_funding.unwrap_or(vec![]),
65586561
is_manual_broadcast: is_manual_broadcast.unwrap_or(false),
6559-
funding_seen_onchain: funding_seen_onchain.unwrap_or(false),
6562+
// Assume "seen" when absent to prevent gating holder broadcasts after upgrade.
6563+
funding_seen_onchain: funding_seen_onchain.unwrap_or(true),
65606564

65616565
latest_update_id,
65626566
commitment_transaction_number_obscure_factor,

0 commit comments

Comments
 (0)