Skip to content

Commit 99c83bc

Browse files
committed
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.
1 parent 15a5966 commit 99c83bc

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lightning/src/ln/channel.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3550,6 +3550,12 @@ impl<Signer: Sign> Channel<Signer> {
35503550
return;
35513551
}
35523552

3553+
if self.channel_state & (ChannelState::PeerDisconnected as u32) == (ChannelState::PeerDisconnected as u32) {
3554+
// While the below code should be idempotent, its simpler to just return early, as
3555+
// redundant disconnect events can fire, though they should be rare.
3556+
return;
3557+
}
3558+
35533559
if self.announcement_sigs_state == AnnouncementSigsState::MessageSent || self.announcement_sigs_state == AnnouncementSigsState::Committed {
35543560
self.announcement_sigs_state = AnnouncementSigsState::NotSent;
35553561
}

lightning/src/ln/msgs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,9 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
883883
/// is believed to be possible in the future (eg they're sending us messages we don't
884884
/// understand or indicate they require unknown feature bits), no_connection_possible is set
885885
/// and any outstanding channels should be failed.
886+
///
887+
/// Note that in some rare cases this may be called without a corresponding
888+
/// [`Self::peer_connected`].
886889
fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
887890

888891
/// Handle a peer reconnecting, possibly generating channel_reestablish message(s).
@@ -979,6 +982,9 @@ pub trait OnionMessageHandler : OnionMessageProvider {
979982
fn peer_connected(&self, their_node_id: &PublicKey, init: &Init);
980983
/// Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
981984
/// drop and refuse to forward onion messages to this peer.
985+
///
986+
/// Note that in some rare cases this may be called without a corresponding
987+
/// [`Self::peer_connected`].
982988
fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
983989

984990
// Handler information:

0 commit comments

Comments
 (0)