Skip to content

Commit 415973e

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 687e587 commit 415973e

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.
@@ -1247,7 +1267,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
12471267
insert_node_id!();
12481268
let features = self.message_handler.chan_handler.provided_init_features(&their_node_id)
12491269
| self.message_handler.route_handler.provided_init_features(&their_node_id)
1250-
| self.message_handler.onion_message_handler.provided_init_features(&their_node_id);
1270+
| self.message_handler.onion_message_handler.provided_init_features(&their_node_id)
1271+
| self.message_handler.custom_message_handler.provided_init_features(&their_node_id);
12511272
let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
12521273
self.enqueue_message(peer, &resp);
12531274
peer.awaiting_pong_timer_tick_intervals = 0;
@@ -1261,7 +1282,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
12611282
insert_node_id!();
12621283
let features = self.message_handler.chan_handler.provided_init_features(&their_node_id)
12631284
| self.message_handler.route_handler.provided_init_features(&their_node_id)
1264-
| self.message_handler.onion_message_handler.provided_init_features(&their_node_id);
1285+
| self.message_handler.onion_message_handler.provided_init_features(&their_node_id)
1286+
| self.message_handler.custom_message_handler.provided_init_features(&their_node_id);
12651287
let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
12661288
self.enqueue_message(peer, &resp);
12671289
peer.awaiting_pong_timer_tick_intervals = 0;
@@ -2203,7 +2225,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
22032225

22042226
let features = self.message_handler.chan_handler.provided_node_features()
22052227
| self.message_handler.route_handler.provided_node_features()
2206-
| self.message_handler.onion_message_handler.provided_node_features();
2228+
| self.message_handler.onion_message_handler.provided_node_features()
2229+
| self.message_handler.custom_message_handler.provided_node_features();
22072230
let announcement = msgs::UnsignedNodeAnnouncement {
22082231
features,
22092232
timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel),

0 commit comments

Comments
 (0)