Skip to content

Commit ef76dda

Browse files
committed
Add flag to disable broadcasting when it's dangerous due to information loss
1 parent f3d5b94 commit ef76dda

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

lightning/src/chain/channelmonitor.rs

+41-4
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ pub(crate) struct ChannelMonitorImpl<Signer: Sign> {
723723
best_block: BestBlock,
724724

725725
secp_ctx: Secp256k1<secp256k1::All>, //TODO: dedup this a bit...
726+
727+
// Used to track that the channel was updated with ChannelForceClosed {should_broadcast: false}
728+
// implying that it's unsafe to broadcast the latest holder commitment transaction.
729+
allow_automated_broadcast: bool,
726730
}
727731

728732
/// Transaction outputs to watch for on-chain spends.
@@ -1059,6 +1063,8 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
10591063
best_block,
10601064

10611065
secp_ctx,
1066+
1067+
allow_automated_broadcast: true,
10621068
}),
10631069
}
10641070
}
@@ -1110,15 +1116,26 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
11101116
payment_hash, payment_preimage, broadcaster, fee_estimator, logger)
11111117
}
11121118

1113-
pub(crate) fn broadcast_latest_holder_commitment_txn<B: Deref, L: Deref>(
1119+
pub(crate) fn maybe_broadcast_latest_holder_commitment_txn<B: Deref, L: Deref>(
1120+
&self,
1121+
broadcaster: &B,
1122+
logger: &L,
1123+
) where
1124+
B::Target: BroadcasterInterface,
1125+
L::Target: Logger,
1126+
{
1127+
self.inner.lock().unwrap().maybe_broadcast_latest_holder_commitment_txn(broadcaster, logger)
1128+
}
1129+
1130+
pub(crate) fn force_broadcast_latest_holder_commitment_txn_unsafe<B: Deref, L: Deref>(
11141131
&self,
11151132
broadcaster: &B,
11161133
logger: &L,
11171134
) where
11181135
B::Target: BroadcasterInterface,
11191136
L::Target: Logger,
11201137
{
1121-
self.inner.lock().unwrap().broadcast_latest_holder_commitment_txn(broadcaster, logger)
1138+
self.inner.lock().unwrap().force_broadcast_latest_holder_commitment_txn_unsafe(broadcaster, logger)
11221139
}
11231140

11241141
/// Updates a ChannelMonitor on the basis of some new information provided by the Channel
@@ -1923,7 +1940,22 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
19231940
}
19241941
}
19251942

1926-
pub(crate) fn broadcast_latest_holder_commitment_txn<B: Deref, L: Deref>(&mut self, broadcaster: &B, logger: &L)
1943+
pub(crate) fn maybe_broadcast_latest_holder_commitment_txn<B: Deref, L: Deref>(&mut self, broadcaster: &B, logger: &L)
1944+
where B::Target: BroadcasterInterface,
1945+
L::Target: Logger,
1946+
{
1947+
if self.allow_automated_broadcast{
1948+
for tx in self.get_latest_holder_commitment_txn(logger).iter() {
1949+
log_info!(logger, "Broadcasting local {}", log_tx!(tx));
1950+
broadcaster.broadcast_transaction(tx);
1951+
}
1952+
self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(self.funding_info.0));
1953+
} else {
1954+
log_error!(logger, "You have a toxic holder commitment transaction avaible in channel monitor, read comment in ChannelMonitor::get_latest_holder_commitment_txn to be informed of manual action to take");
1955+
}
1956+
}
1957+
1958+
pub(crate) fn force_broadcast_latest_holder_commitment_txn_unsafe<B: Deref, L: Deref>(&mut self, broadcaster: &B, logger: &L)
19271959
where B::Target: BroadcasterInterface,
19281960
L::Target: Logger,
19291961
{
@@ -1934,6 +1966,8 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
19341966
self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(self.funding_info.0));
19351967
}
19361968

1969+
1970+
19371971
pub fn update_monitor<B: Deref, F: Deref, L: Deref>(&mut self, updates: &ChannelMonitorUpdate, broadcaster: &B, fee_estimator: &F, logger: &L) -> Result<(), ()>
19381972
where B::Target: BroadcasterInterface,
19391973
F::Target: FeeEstimator,
@@ -1989,8 +2023,9 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
19892023
log_trace!(logger, "Updating ChannelMonitor: channel force closed, should broadcast: {}", should_broadcast);
19902024
self.lockdown_from_offchain = true;
19912025
if *should_broadcast {
1992-
self.broadcast_latest_holder_commitment_txn(broadcaster, logger);
2026+
self.maybe_broadcast_latest_holder_commitment_txn(broadcaster, logger);
19932027
} else if !self.holder_tx_signed {
2028+
self.allow_automated_broadcast = false;
19942029
log_error!(logger, "You have a toxic holder commitment transaction avaible in channel monitor, read comment in ChannelMonitor::get_latest_holder_commitment_txn to be informed of manual action to take");
19952030
} else {
19962031
// If we generated a MonitorEvent::CommitmentTxConfirmed, the ChannelManager
@@ -3397,6 +3432,8 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
33973432
best_block,
33983433

33993434
secp_ctx,
3435+
3436+
allow_automated_broadcast: true,
34003437
}),
34013438
}))
34023439
}

lightning/src/ln/channelmanager.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5267,7 +5267,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
52675267
/// Channel object.
52685268
fn handle_init_event_channel_failures(&self, mut failed_channels: Vec<ShutdownResult>) {
52695269
for mut failure in failed_channels.drain(..) {
5270-
// Either a commitment transactions has been confirmed on-chain or
5270+
// Either a commitment transaction has been confirmed on-chain or
52715271
// Channel::block_disconnected detected that the funding transaction has been
52725272
// reorganized out of the main chain.
52735273
// We cannot broadcast our latest local state via monitor update (as
@@ -6805,7 +6805,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
68056805
log_bytes!(channel.channel_id()), monitor.get_latest_update_id(), channel.get_latest_monitor_update_id());
68066806
let (_, mut new_failed_htlcs) = channel.force_shutdown(true);
68076807
failed_htlcs.append(&mut new_failed_htlcs);
6808-
monitor.broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);
6808+
monitor.maybe_broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);
68096809
channel_closures.push(events::Event::ChannelClosed {
68106810
channel_id: channel.channel_id(),
68116811
user_channel_id: channel.get_user_id(),
@@ -6831,7 +6831,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
68316831
for (ref funding_txo, ref mut monitor) in args.channel_monitors.iter_mut() {
68326832
if !funding_txo_set.contains(funding_txo) {
68336833
log_info!(args.logger, "Broadcasting latest holder commitment transaction for closed channel {}", log_bytes!(funding_txo.to_channel_id()));
6834-
monitor.broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);
6834+
monitor.maybe_broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);
68356835
}
68366836
}
68376837

0 commit comments

Comments
 (0)