From: Matt Corallo Date: Mon, 12 Sep 2022 18:54:05 +0000 (+0000) Subject: Add a note that `peer_disconnected` impls must be idempotent X-Git-Tag: v0.0.112~41^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=91db4ba2f8097d29af4363bfd6f5445be1420096;p=rust-lightning Add a note that `peer_disconnected` impls must be idempotent It appears our code is already correct here, but its also nice to add a quick safety check in `channel.rs` which ensures we will remain idempotent. --- diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index b37550b0d..c8209a9ea 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -3550,6 +3550,12 @@ impl Channel { return; } + if self.channel_state & (ChannelState::PeerDisconnected as u32) == (ChannelState::PeerDisconnected as u32) { + // While the below code should be idempotent, it's simpler to just return early, as + // redundant disconnect events can fire, though they should be rare. + return; + } + if self.announcement_sigs_state == AnnouncementSigsState::MessageSent || self.announcement_sigs_state == AnnouncementSigsState::Committed { self.announcement_sigs_state = AnnouncementSigsState::NotSent; } diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 747107c08..98831137b 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -883,6 +883,9 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider { /// is believed to be possible in the future (eg they're sending us messages we don't /// understand or indicate they require unknown feature bits), no_connection_possible is set /// and any outstanding channels should be failed. + /// + /// Note that in some rare cases this may be called without a corresponding + /// [`Self::peer_connected`]. fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool); /// Handle a peer reconnecting, possibly generating channel_reestablish message(s). @@ -979,6 +982,9 @@ pub trait OnionMessageHandler : OnionMessageProvider { fn peer_connected(&self, their_node_id: &PublicKey, init: &Init); /// Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to /// drop and refuse to forward onion messages to this peer. + /// + /// Note that in some rare cases this may be called without a corresponding + /// [`Self::peer_connected`]. fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool); // Handler information: