Skip to content

Commit 390a284

Browse files
committed
Provide features in CustomMessageHandler
CustomMessageHandler implementations may need to advertise support for features. Add methods to CustomMessageHandler to provide these and combine them with features from other message handlers.
1 parent 69ca854 commit 390a284

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

lightning-custom-message/src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
//! # use bitcoin::secp256k1::PublicKey;
2121
//! # use lightning::io;
2222
//! # use lightning::ln::msgs::{DecodeError, LightningError};
23+
//! # use lightning::ln::features::{InitFeatures, NodeFeatures};
2324
//! use lightning::ln::peer_handler::CustomMessageHandler;
2425
//! use lightning::ln::wire::{CustomMessageReader, self};
2526
//! use lightning::util::ser::Writeable;
@@ -66,6 +67,12 @@
6667
//! # fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> {
6768
//! # unimplemented!()
6869
//! # }
70+
//! # fn provided_node_features(&self) -> NodeFeatures {
71+
//! # unimplemented!()
72+
//! # }
73+
//! # fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
74+
//! # unimplemented!()
75+
//! # }
6976
//! }
7077
//!
7178
//! #[derive(Debug)]
@@ -106,6 +113,12 @@
106113
//! # fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> {
107114
//! # unimplemented!()
108115
//! # }
116+
//! # fn provided_node_features(&self) -> NodeFeatures {
117+
//! # unimplemented!()
118+
//! # }
119+
//! # fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
120+
//! # unimplemented!()
121+
//! # }
109122
//! }
110123
//!
111124
//! #[derive(Debug)]
@@ -146,6 +159,12 @@
146159
//! # fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> {
147160
//! # unimplemented!()
148161
//! # }
162+
//! # fn provided_node_features(&self) -> NodeFeatures {
163+
//! # unimplemented!()
164+
//! # }
165+
//! # fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
166+
//! # unimplemented!()
167+
//! # }
149168
//! }
150169
//!
151170
//! # fn main() {
@@ -268,6 +287,22 @@ macro_rules! composite_custom_message_handler {
268287
)*
269288
.collect()
270289
}
290+
291+
fn provided_node_features(&self) -> $crate::lightning::ln::features::NodeFeatures {
292+
$crate::lightning::ln::features::NodeFeatures::empty()
293+
$(
294+
| self.$field.provided_node_features()
295+
)*
296+
}
297+
298+
fn provided_init_features(
299+
&self, their_node_id: &$crate::bitcoin::secp256k1::PublicKey
300+
) -> $crate::lightning::ln::features::InitFeatures {
301+
$crate::lightning::ln::features::InitFeatures::empty()
302+
$(
303+
| self.$field.provided_init_features(their_node_id)
304+
)*
305+
}
271306
}
272307

273308
impl $crate::lightning::ln::wire::CustomMessageReader for $handler {

lightning/src/ln/peer_handler.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ pub trait CustomMessageHandler: wire::CustomMessageReader {
6464
/// in the process. Each message is paired with the node id of the intended recipient. If no
6565
/// connection to the node exists, then the message is simply not sent.
6666
fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)>;
67+
68+
/// Gets the node feature flags which this handler itself supports. All available handlers are
69+
/// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
70+
/// which are broadcasted in our [`NodeAnnouncement`] message.
71+
///
72+
/// [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
73+
fn provided_node_features(&self) -> NodeFeatures;
74+
75+
/// Gets the init feature flags which should be sent to the given peer. All available handlers
76+
/// are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
77+
/// which are sent in our [`Init`] message.
78+
///
79+
/// [`Init`]: crate::ln::msgs::Init
80+
fn provided_init_features(&self, their_node_id: &PublicKey) -> InitFeatures;
6781
}
6882

6983
/// A dummy struct which implements `RoutingMessageHandler` without storing any routing information
@@ -149,6 +163,12 @@ impl CustomMessageHandler for IgnoringMessageHandler {
149163
}
150164

151165
fn get_and_clear_pending_msg(&self) -> Vec<(PublicKey, Self::CustomMessage)> { Vec::new() }
166+
167+
fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
168+
169+
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
170+
InitFeatures::empty()
171+
}
152172
}
153173

154174
/// A dummy struct which implements `ChannelMessageHandler` without having any channels.
@@ -1192,7 +1212,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
11921212
insert_node_id!();
11931213
let features = self.message_handler.chan_handler.provided_init_features(&their_node_id)
11941214
| self.message_handler.route_handler.provided_init_features(&their_node_id)
1195-
| self.message_handler.onion_message_handler.provided_init_features(&their_node_id);
1215+
| self.message_handler.onion_message_handler.provided_init_features(&their_node_id)
1216+
| self.custom_message_handler.provided_init_features(&their_node_id);
11961217
let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
11971218
self.enqueue_message(peer, &resp);
11981219
peer.awaiting_pong_timer_tick_intervals = 0;
@@ -1206,7 +1227,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
12061227
insert_node_id!();
12071228
let features = self.message_handler.chan_handler.provided_init_features(&their_node_id)
12081229
| self.message_handler.route_handler.provided_init_features(&their_node_id)
1209-
| self.message_handler.onion_message_handler.provided_init_features(&their_node_id);
1230+
| self.message_handler.onion_message_handler.provided_init_features(&their_node_id)
1231+
| self.custom_message_handler.provided_init_features(&their_node_id);
12101232
let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
12111233
self.enqueue_message(peer, &resp);
12121234
peer.awaiting_pong_timer_tick_intervals = 0;
@@ -2148,7 +2170,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
21482170

21492171
let features = self.message_handler.chan_handler.provided_node_features()
21502172
| self.message_handler.route_handler.provided_node_features()
2151-
| self.message_handler.onion_message_handler.provided_node_features();
2173+
| self.message_handler.onion_message_handler.provided_node_features()
2174+
| self.custom_message_handler.provided_node_features();
21522175
let announcement = msgs::UnsignedNodeAnnouncement {
21532176
features,
21542177
timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel),

0 commit comments

Comments
 (0)