From 2c77c9b021bdbcf57a2e40097e73722f1770e5e0 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Mon, 20 Sep 2021 19:51:46 -0400 Subject: [PATCH] -f address lastest Matt's comments --- lightning/src/ln/channelmanager.rs | 24 +++++++++++++++++++++++- lightning/src/util/events.rs | 14 ++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 1e6a865e2..6ab97c242 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -242,7 +242,7 @@ type ShutdownResult = (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource struct MsgHandleErrInternal { err: msgs::LightningError, - chan_id: Option<[u8; 32]>, + chan_id: Option<[u8; 32]>, // If Some a channel of ours has been closed shutdown_finish: Option<(ShutdownResult, Option)>, } impl MsgHandleErrInternal { @@ -1372,6 +1372,12 @@ impl ChannelMana msg: channel_update }); } + if let Ok(mut pending_events_lock) = self.pending_events.lock() { + pending_events_lock.push(events::Event::ChannelClosed { + channel_id: *channel_id, + reason: ClosureReason::HolderForceClosed + }); + } } break Ok(()); }, @@ -4093,6 +4099,13 @@ impl ChannelMana }); } + if let Ok(mut pending_events_lock) = self.pending_events.lock() { + pending_events_lock.push(events::Event::ChannelClosed { + channel_id: *channel_id, + reason: ClosureReason::HolderForceClosed + }); + } + log_info!(self.logger, "Broadcasting {}", log_tx!(tx)); self.tx_broadcaster.broadcast_transaction(&tx); false @@ -5316,6 +5329,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> let mut funding_txo_set = HashSet::with_capacity(cmp::min(channel_count as usize, 128)); let mut by_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128)); let mut short_to_id = HashMap::with_capacity(cmp::min(channel_count as usize, 128)); + let mut channel_closures = Vec::new(); for _ in 0..channel_count { let mut channel: Channel = Channel::read(reader, &args.keys_manager)?; let funding_txo = channel.get_funding_txo().ok_or(DecodeError::InvalidValue)?; @@ -5346,6 +5360,10 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> let (_, mut new_failed_htlcs) = channel.force_shutdown(true); failed_htlcs.append(&mut new_failed_htlcs); monitor.broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger); + channel_closures.push(events::Event::ChannelClosed { + channel_id: channel.channel_id(), + reason: ClosureReason::OutdatedChanMan + }); } else { if let Some(short_channel_id) = channel.get_short_channel_id() { short_to_id.insert(short_channel_id, channel.channel_id()); @@ -5453,6 +5471,10 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> let mut secp_ctx = Secp256k1::new(); secp_ctx.seeded_randomize(&args.keys_manager.get_secure_random_bytes()); + if !channel_closures.is_empty() { + pending_events_read.append(&mut channel_closures); + } + let channel_manager = ChannelManager { genesis_hash, fee_estimator: args.fee_estimator, diff --git a/lightning/src/util/events.rs b/lightning/src/util/events.rs index 5a91758d8..691d30a84 100644 --- a/lightning/src/util/events.rs +++ b/lightning/src/util/events.rs @@ -73,12 +73,15 @@ pub enum PaymentPurpose { /// The reason which the channel was closed. See individual variants more details. pub enum ClosureReason { /// Closure generated from receiving a peer error message. + /// + /// Our counterparty may have broadcasted their latest commitment state, and we have + /// as well CounterpartyForceClosed { /// The error which the peer sent us. /// /// The string should be sanitized before it is used (e.g emitted to logs - /// or printed to stdout). Otherwise, a well crafted error message may trigger - /// a security in the terminal emulator or the logging subsystem. + /// or printed to stdout). Otherwise, a well crafted error message may exploit + /// a security vulnerability in the terminal emulator or the logging subsystem. peer_msg: String, }, /// Closure generated from [`ChannelManager::force_close_channel`], called by the user. @@ -96,9 +99,11 @@ pub enum ClosureReason { err: String, }, /// The `PeerManager` informed us that we've disconnected from the peer and that it is - /// unlikely we'll be able to connect to the peer, most likely because we have incompatible - /// features and the peer, or we, require features which we, or the peer, do not support. + /// unlikely we'll be able to connect to the peer. DisconnectedPeer, + /// Closure generated from [`ChannelManager::read`] if the ChannelMonitor is newer than + /// the ChannelManager deserialized. + OutdatedChanMan } impl_writeable_tlv_based_enum_upgradable!(ClosureReason, @@ -108,6 +113,7 @@ impl_writeable_tlv_based_enum_upgradable!(ClosureReason, (4, CooperativeClosure) => {}, (8, ProcessingError) => { (1, err, required) }, (10, DisconnectedPeer) => {}, + (12, OutdatedChanMan) => {}, ); /// An Event which you should probably take some action in response to. -- 2.39.5