From: John Cantrell Date: Thu, 6 Jun 2024 17:59:29 +0000 (-0400) Subject: add peer_(dis)connected to custom message handler X-Git-Tag: v0.0.124-beta~95^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=602921cf376d5b1126d22977d0cd258707728738;p=rust-lightning add peer_(dis)connected to custom message handler --- diff --git a/lightning-custom-message/src/lib.rs b/lightning-custom-message/src/lib.rs index e2000bc58..a0948f23b 100644 --- a/lightning-custom-message/src/lib.rs +++ b/lightning-custom-message/src/lib.rs @@ -20,7 +20,7 @@ //! //! # use bitcoin::secp256k1::PublicKey; //! # use lightning::io; -//! # use lightning::ln::msgs::{DecodeError, LightningError}; +//! # use lightning::ln::msgs::{DecodeError, Init, LightningError}; //! # use lightning::ln::features::{InitFeatures, NodeFeatures}; //! use lightning::ln::peer_handler::CustomMessageHandler; //! use lightning::ln::wire::{CustomMessageReader, self}; @@ -68,6 +68,12 @@ //! # fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { //! # unimplemented!() //! # } +//! # fn peer_disconnected(&self, _their_node_id: &PublicKey) { +//! # unimplemented!() +//! # } +//! # fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &Init, _inbound: bool) -> Result<(), ()> { +//! # unimplemented!() +//! # } //! # fn provided_node_features(&self) -> NodeFeatures { //! # unimplemented!() //! # } @@ -114,6 +120,12 @@ //! # fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { //! # unimplemented!() //! # } +//! # fn peer_disconnected(&self, _their_node_id: &PublicKey) { +//! # unimplemented!() +//! # } +//! # fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &Init, _inbound: bool) -> Result<(), ()> { +//! # unimplemented!() +//! # } //! # fn provided_node_features(&self) -> NodeFeatures { //! # unimplemented!() //! # } @@ -160,6 +172,12 @@ //! # fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { //! # unimplemented!() //! # } +//! # fn peer_disconnected(&self, _their_node_id: &PublicKey) { +//! # unimplemented!() +//! # } +//! # fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &Init, _inbound: bool) -> Result<(), ()> { +//! # unimplemented!() +//! # } //! # fn provided_node_features(&self) -> NodeFeatures { //! # unimplemented!() //! # } @@ -287,6 +305,22 @@ macro_rules! composite_custom_message_handler { .collect() } + fn peer_disconnected(&self, their_node_id: &$crate::bitcoin::secp256k1::PublicKey) { + $( + self.$field.peer_disconnected(their_node_id); + )* + } + + fn peer_connected(&self, their_node_id: &$crate::bitcoin::secp256k1::PublicKey, msg: &$crate::lightning::ln::msgs::Init, inbound: bool) -> Result<(), ()> { + let mut result = Ok(()); + $( + if let Err(e) = self.$field.peer_connected(their_node_id, msg, inbound) { + result = Err(e); + } + )* + result + } + fn provided_node_features(&self) -> $crate::lightning::ln::features::NodeFeatures { $crate::lightning::ln::features::NodeFeatures::empty() $( diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index d19928fa1..448dd213d 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -23,7 +23,7 @@ use crate::events::{MessageSendEvent, MessageSendEventsProvider}; use crate::ln::types::ChannelId; use crate::ln::features::{InitFeatures, NodeFeatures}; use crate::ln::msgs; -use crate::ln::msgs::{ChannelMessageHandler, LightningError, SocketAddress, OnionMessageHandler, RoutingMessageHandler}; +use crate::ln::msgs::{ChannelMessageHandler, Init, LightningError, SocketAddress, OnionMessageHandler, RoutingMessageHandler}; use crate::util::ser::{VecWriter, Writeable, Writer}; use crate::ln::peer_channel_encryptor::{PeerChannelEncryptor, NextNoiseStep, MessageBuf, MSG_BUF_ALLOC_SIZE}; use crate::ln::wire; @@ -79,6 +79,16 @@ pub trait CustomMessageHandler: wire::CustomMessageReader { /// connection to the node exists, then the message is simply not sent. fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)>; + /// Indicates a peer disconnected. + fn peer_disconnected(&self, their_node_id: &PublicKey); + + /// Handle a peer connecting. + /// + /// May return an `Err(())` if the features the peer supports are not sufficient to communicate + /// with us. Implementors should be somewhat conservative about doing so, however, as other + /// message handlers may still wish to communicate with this peer. + fn peer_connected(&self, their_node_id: &PublicKey, msg: &Init, inbound: bool) -> Result<(), ()>; + /// Gets the node feature flags which this handler itself supports. All available handlers are /// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`] /// which are broadcasted in our [`NodeAnnouncement`] message. @@ -190,6 +200,10 @@ impl CustomMessageHandler for IgnoringMessageHandler { fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { Vec::new() } + fn peer_disconnected(&self, _their_node_id: &PublicKey) {} + + fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &Init, _inbound: bool) -> Result<(), ()> { Ok(()) } + fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() } fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures { @@ -1680,6 +1694,10 @@ impl Vec<(PublicKey, Self::CustomMessage)> { Vec::new() } + + fn peer_disconnected(&self, _their_node_id: &PublicKey) {} + + fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &Init, _inbound: bool) -> Result<(), ()> { Ok(()) } + fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() } fn provided_init_features(&self, _: &PublicKey) -> InitFeatures {