Skip to content

Commit 44f712d

Browse files
committed
Add peer_[dis]connected to CustomMessageHandler trait
There's not much reason not to have these methods on the `CustomMessageHandler` trait, and they can be quite useful, so we add them here.
1 parent 6cafba9 commit 44f712d

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lightning-custom-message/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,28 @@ macro_rules! composite_custom_message_handler {
288288
.collect()
289289
}
290290

291+
fn peer_connected(
292+
&self, their_node_id: &$crate::bitcoin::secp256k1::PublicKey,
293+
init_msg: &$crate::lightning::ln::msgs::Init, inbound: bool
294+
) -> Result<(), ()> {
295+
let should_disconnect = false;
296+
$(
297+
should_disconnect |= self.$field.peer_connected(their_node_id, init_msg, inbound).is_err();
298+
)*
299+
if should_disconnect {
300+
$(
301+
self.$field.peer_disconnected(their_node_id).is_err();
302+
)*
303+
Err(())
304+
} else { Ok(()) }
305+
}
306+
307+
fn peer_disconnected(&self, their_node_id: &$crate::bitcoin::secp256k1::PublicKey) {
308+
$(
309+
self.$field.peer_disconnected(their_node_id)
310+
)*
311+
}
312+
291313
fn provided_node_features(&self) -> $crate::lightning::ln::features::NodeFeatures {
292314
$crate::lightning::ln::features::NodeFeatures::empty()
293315
$(

lightning/src/ln/peer_handler.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ pub trait CustomMessageHandler: wire::CustomMessageReader {
6868
/// connection to the node exists, then the message is simply not sent.
6969
fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)>;
7070

71+
/// Indicates a connection to the peer failed/an existing connection was lost.
72+
fn peer_disconnected(&self, their_node_id: &PublicKey);
73+
74+
/// Handle a peer reconnecting, possibly generating `channel_reestablish` message(s).
75+
///
76+
/// May return an `Err(())` if the features the peer supports are not sufficient to communicate
77+
/// with us. Implementors should be somewhat conservative about doing so, however, as other
78+
/// message handlers may still wish to communicate with this peer.
79+
fn peer_connected(&self, their_node_id: &PublicKey, msg: &msgs::Init, inbound: bool) -> Result<(), ()>;
80+
7181
/// Gets the node feature flags which this handler itself supports. All available handlers are
7282
/// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
7383
/// which are broadcasted in our [`NodeAnnouncement`] message.
@@ -170,6 +180,10 @@ impl CustomMessageHandler for IgnoringMessageHandler {
170180

171181
fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { Vec::new() }
172182

183+
fn peer_disconnected(&self, _: &PublicKey) {}
184+
185+
fn peer_connected(&self, _: &PublicKey, _: &msgs::Init, _: bool) -> Result<(), ()> { Ok(()) }
186+
173187
fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
174188

175189
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
@@ -1549,6 +1563,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15491563
log_debug!(self.logger, "Onion Message Handler decided we couldn't communicate with peer {}", log_pubkey!(their_node_id));
15501564
return Err(PeerHandleError { }.into());
15511565
}
1566+
if let Err(()) = self.message_handler.custom_message_handler.peer_connected(&their_node_id, &msg, peer_lock.inbound_connection) {
1567+
log_debug!(self.logger, "Custom Message Handler decided we couldn't communicate with peer {}", log_pubkey!(their_node_id));
1568+
return Err(PeerHandleError { }.into());
1569+
}
15521570

15531571
peer_lock.their_features = Some(msg.features);
15541572
return Ok(None);
@@ -2237,6 +2255,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
22372255
log_trace!(self.logger, "Disconnecting peer with id {} due to {}", node_id, reason);
22382256
self.message_handler.chan_handler.peer_disconnected(&node_id);
22392257
self.message_handler.onion_message_handler.peer_disconnected(&node_id);
2258+
self.message_handler.custom_message_handler.peer_disconnected(&node_id);
22402259
}
22412260
descriptor.disconnect_socket();
22422261
}
@@ -2259,6 +2278,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
22592278
if !peer.handshake_complete() { return; }
22602279
self.message_handler.chan_handler.peer_disconnected(&node_id);
22612280
self.message_handler.onion_message_handler.peer_disconnected(&node_id);
2281+
self.message_handler.custom_message_handler.peer_disconnected(&node_id);
22622282
}
22632283
}
22642284
};
@@ -2553,6 +2573,9 @@ mod tests {
25532573

25542574
fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { Vec::new() }
25552575

2576+
fn peer_disconnected(&self, _: &PublicKey) {}
2577+
fn peer_connected(&self, _: &PublicKey, _: &msgs::Init, _: bool) -> Result<(), ()> { Ok(()) }
2578+
25562579
fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
25572580

25582581
fn provided_init_features(&self, _: &PublicKey) -> InitFeatures {

0 commit comments

Comments
 (0)