Skip to content

Commit 877a5fc

Browse files
authored
Merge pull request #1688 from valentinewallace/2022-08-flip-om-feature-bit
Onion messages: flip feature bit 🎉
2 parents ad24b8c + 5d9dddd commit 877a5fc

File tree

8 files changed

+103
-23
lines changed

8 files changed

+103
-23
lines changed

lightning-net-tokio/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ mod tests {
582582
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
583583
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: QueryChannelRange) -> Result<(), LightningError> { Ok(()) }
584584
fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: QueryShortChannelIds) -> Result<(), LightningError> { Ok(()) }
585+
fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::known() }
585586
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures { InitFeatures::known() }
586587
}
587588
impl ChannelMessageHandler for MsgHandler {

lightning/src/ln/channelmanager.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6122,7 +6122,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
61226122
}
61236123

61246124
fn provided_node_features(&self) -> NodeFeatures {
6125-
NodeFeatures::known()
6125+
NodeFeatures::known_channel_features()
61266126
}
61276127

61286128
fn provided_init_features(&self, _their_init_features: &PublicKey) -> InitFeatures {

lightning/src/ln/features.rs

+44-17
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@
3939
//! (see [BOLT-4](https://github.com/lightning/bolts/blob/master/04-onion-routing.md) for more information).
4040
//! - `BasicMPP` - requires/supports that a node can receive basic multi-part payments
4141
//! (see [BOLT-4](https://github.com/lightning/bolts/blob/master/04-onion-routing.md#basic-multi-part-payments) for more information).
42+
//! - `Wumbo` - requires/supports that a node create large channels. Called `option_support_large_channel` in the spec.
43+
//! (see [BOLT-2](https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#the-open_channel-message) for more information).
4244
//! - `ShutdownAnySegwit` - requires/supports that future segwit versions are allowed in `shutdown`
4345
//! (see [BOLT-2](https://github.com/lightning/bolts/blob/master/02-peer-protocol.md) for more information).
46+
//! - `OnionMessages` - requires/supports forwarding onion messages
47+
//! (see [BOLT-7](https://github.com/lightning/bolts/pull/759/files) for more information).
48+
//! TODO: update link
4449
//! - `ChannelType` - node supports the channel_type field in open/accept
4550
//! (see [BOLT-2](https://github.com/lightning/bolts/blob/master/02-peer-protocol.md) for more information).
4651
//! - `SCIDPrivacy` - supply channel aliases for routing
@@ -164,7 +169,8 @@ mod sealed {
164169
],
165170
optional_features: [
166171
// Note that if new "non-channel-related" flags are added here they should be
167-
// explicitly cleared in InitFeatures::known_channel_features.
172+
// explicitly cleared in InitFeatures::known_channel_features and
173+
// NodeFeatures::known_channel_features.
168174
// Byte 0
169175
DataLossProtect | InitialRoutingSync | UpfrontShutdownScript | GossipQueries,
170176
// Byte 1
@@ -174,7 +180,7 @@ mod sealed {
174180
// Byte 3
175181
ShutdownAnySegwit,
176182
// Byte 4
177-
,
183+
OnionMessages,
178184
// Byte 5
179185
ChannelType | SCIDPrivacy,
180186
// Byte 6
@@ -208,7 +214,7 @@ mod sealed {
208214
// Byte 3
209215
ShutdownAnySegwit,
210216
// Byte 4
211-
,
217+
OnionMessages,
212218
// Byte 5
213219
ChannelType | SCIDPrivacy,
214220
// Byte 6
@@ -435,8 +441,6 @@ mod sealed {
435441
define_feature!(27, ShutdownAnySegwit, [InitContext, NodeContext],
436442
"Feature flags for `opt_shutdown_anysegwit`.", set_shutdown_any_segwit_optional,
437443
set_shutdown_any_segwit_required, supports_shutdown_anysegwit, requires_shutdown_anysegwit);
438-
// We do not yet advertise the onion messages feature bit, but we need to detect when peers
439-
// support it.
440444
define_feature!(39, OnionMessages, [InitContext, NodeContext],
441445
"Feature flags for `option_onion_messages`.", set_onion_messages_optional,
442446
set_onion_messages_required, supports_onion_messages, requires_onion_messages);
@@ -470,6 +474,17 @@ pub struct Features<T: sealed::Context> {
470474
mark: PhantomData<T>,
471475
}
472476

477+
impl <T: sealed::Context> Features<T> {
478+
pub(crate) fn or(mut self, o: Self) -> Self {
479+
let total_feature_len = cmp::max(self.flags.len(), o.flags.len());
480+
self.flags.resize(total_feature_len, 0u8);
481+
for (byte, o_byte) in self.flags.iter_mut().zip(o.flags.iter()) {
482+
*byte |= *o_byte;
483+
}
484+
self
485+
}
486+
}
487+
473488
impl<T: sealed::Context> Clone for Features<T> {
474489
fn clone(&self) -> Self {
475490
Self {
@@ -532,16 +547,6 @@ impl InitFeatures {
532547
Ok(())
533548
}
534549

535-
/// or's another InitFeatures into this one.
536-
pub(crate) fn or(mut self, o: InitFeatures) -> InitFeatures {
537-
let total_feature_len = cmp::max(self.flags.len(), o.flags.len());
538-
self.flags.resize(total_feature_len, 0u8);
539-
for (byte, o_byte) in self.flags.iter_mut().zip(o.flags.iter()) {
540-
*byte |= *o_byte;
541-
}
542-
self
543-
}
544-
545550
/// Converts `InitFeatures` to `Features<C>`. Only known `InitFeatures` relevant to context `C`
546551
/// are included in the result.
547552
pub(crate) fn to_context<C: sealed::Context>(&self) -> Features<C> {
@@ -554,6 +559,16 @@ impl InitFeatures {
554559
Self::known()
555560
.clear_initial_routing_sync()
556561
.clear_gossip_queries()
562+
.clear_onion_messages()
563+
}
564+
}
565+
566+
impl NodeFeatures {
567+
/// Returns the set of known node features that are related to channels.
568+
pub fn known_channel_features() -> NodeFeatures {
569+
Self::known()
570+
.clear_gossip_queries()
571+
.clear_onion_messages()
557572
}
558573
}
559574

@@ -787,6 +802,13 @@ impl<T: sealed::InitialRoutingSync> Features<T> {
787802
}
788803
}
789804

805+
impl<T: sealed::OnionMessages> Features<T> {
806+
pub(crate) fn clear_onion_messages(mut self) -> Self {
807+
<T as sealed::OnionMessages>::clear_bits(&mut self.flags);
808+
self
809+
}
810+
}
811+
790812
impl<T: sealed::ShutdownAnySegwit> Features<T> {
791813
#[cfg(test)]
792814
pub(crate) fn clear_shutdown_anysegwit(mut self) -> Self {
@@ -913,6 +935,11 @@ mod tests {
913935
assert!(!InitFeatures::known().requires_wumbo());
914936
assert!(!NodeFeatures::known().requires_wumbo());
915937

938+
assert!(InitFeatures::known().supports_onion_messages());
939+
assert!(NodeFeatures::known().supports_onion_messages());
940+
assert!(!InitFeatures::known().requires_onion_messages());
941+
assert!(!NodeFeatures::known().requires_onion_messages());
942+
916943
assert!(InitFeatures::known().supports_zero_conf());
917944
assert!(!InitFeatures::known().requires_zero_conf());
918945
assert!(NodeFeatures::known().supports_zero_conf());
@@ -957,15 +984,15 @@ mod tests {
957984
// - var_onion_optin (req) | static_remote_key (req) | payment_secret(req)
958985
// - basic_mpp | wumbo
959986
// - opt_shutdown_anysegwit
960-
// -
987+
// - onion_messages
961988
// - option_channel_type | option_scid_alias
962989
// - option_zeroconf
963990
assert_eq!(node_features.flags.len(), 7);
964991
assert_eq!(node_features.flags[0], 0b00000010);
965992
assert_eq!(node_features.flags[1], 0b01010001);
966993
assert_eq!(node_features.flags[2], 0b00001010);
967994
assert_eq!(node_features.flags[3], 0b00001000);
968-
assert_eq!(node_features.flags[4], 0b00000000);
995+
assert_eq!(node_features.flags[4], 0b10000000);
969996
assert_eq!(node_features.flags[5], 0b10100000);
970997
assert_eq!(node_features.flags[6], 0b00001000);
971998
}

lightning/src/ln/msgs.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
900900
// Handler information:
901901
/// Gets the node feature flags which this handler itself supports. All available handlers are
902902
/// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
903-
/// which are broadcasted in our node_announcement message.
903+
/// which are broadcasted in our [`NodeAnnouncement`] message.
904904
fn provided_node_features(&self) -> NodeFeatures;
905905

906906
/// Gets the init feature flags which should be sent to the given peer. All available handlers
@@ -958,6 +958,10 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
958958
fn handle_query_short_channel_ids(&self, their_node_id: &PublicKey, msg: QueryShortChannelIds) -> Result<(), LightningError>;
959959

960960
// Handler information:
961+
/// Gets the node feature flags which this handler itself supports. All available handlers are
962+
/// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
963+
/// which are broadcasted in our [`NodeAnnouncement`] message.
964+
fn provided_node_features(&self) -> NodeFeatures;
961965
/// Gets the init feature flags which should be sent to the given peer. All available handlers
962966
/// are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
963967
/// which are sent in our [`Init`] message.
@@ -976,6 +980,19 @@ pub trait OnionMessageHandler : OnionMessageProvider {
976980
/// Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
977981
/// drop and refuse to forward onion messages to this peer.
978982
fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
983+
984+
// Handler information:
985+
/// Gets the node feature flags which this handler itself supports. All available handlers are
986+
/// queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
987+
/// which are broadcasted in our [`NodeAnnouncement`] message.
988+
fn provided_node_features(&self) -> NodeFeatures;
989+
990+
/// Gets the init feature flags which should be sent to the given peer. All available handlers
991+
/// are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
992+
/// which are sent in our [`Init`] message.
993+
///
994+
/// Note that this method is called before [`Self::peer_connected`].
995+
fn provided_init_features(&self, their_node_id: &PublicKey) -> InitFeatures;
979996
}
980997

981998
mod fuzzy_internal_msgs {

lightning/src/ln/peer_handler.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl RoutingMessageHandler for IgnoringMessageHandler {
7777
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
7878
fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::QueryChannelRange) -> Result<(), LightningError> { Ok(()) }
7979
fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: msgs::QueryShortChannelIds) -> Result<(), LightningError> { Ok(()) }
80+
fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
8081
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
8182
InitFeatures::empty()
8283
}
@@ -88,6 +89,10 @@ impl OnionMessageHandler for IgnoringMessageHandler {
8889
fn handle_onion_message(&self, _their_node_id: &PublicKey, _msg: &msgs::OnionMessage) {}
8990
fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init) {}
9091
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
92+
fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
93+
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
94+
InitFeatures::empty()
95+
}
9196
}
9297
impl Deref for IgnoringMessageHandler {
9398
type Target = IgnoringMessageHandler;
@@ -1061,7 +1066,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
10611066
peer.their_node_id = Some(their_node_id);
10621067
insert_node_id!();
10631068
let features = self.message_handler.chan_handler.provided_init_features(&their_node_id)
1064-
.or(self.message_handler.route_handler.provided_init_features(&their_node_id));
1069+
.or(self.message_handler.route_handler.provided_init_features(&their_node_id))
1070+
.or(self.message_handler.onion_message_handler.provided_init_features(&their_node_id));
10651071
let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
10661072
self.enqueue_message(peer, &resp);
10671073
peer.awaiting_pong_timer_tick_intervals = 0;
@@ -1074,7 +1080,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
10741080
peer.their_node_id = Some(their_node_id);
10751081
insert_node_id!();
10761082
let features = self.message_handler.chan_handler.provided_init_features(&their_node_id)
1077-
.or(self.message_handler.route_handler.provided_init_features(&their_node_id));
1083+
.or(self.message_handler.route_handler.provided_init_features(&their_node_id))
1084+
.or(self.message_handler.onion_message_handler.provided_init_features(&their_node_id));
10781085
let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone()) };
10791086
self.enqueue_message(peer, &resp);
10801087
peer.awaiting_pong_timer_tick_intervals = 0;
@@ -1969,8 +1976,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
19691976
// addresses be sorted for future compatibility.
19701977
addresses.sort_by_key(|addr| addr.get_id());
19711978

1979+
let features = self.message_handler.chan_handler.provided_node_features()
1980+
.or(self.message_handler.route_handler.provided_node_features())
1981+
.or(self.message_handler.onion_message_handler.provided_node_features());
19721982
let announcement = msgs::UnsignedNodeAnnouncement {
1973-
features: self.message_handler.chan_handler.provided_node_features(),
1983+
features,
19741984
timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel) as u32,
19751985
node_id: PublicKey::from_secret_key(&self.secp_ctx, &self.our_node_secret),
19761986
rgb, alias, addresses,

lightning/src/onion_message/messenger.rs

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1717

1818
use chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager, Recipient, Sign};
19+
use ln::features::{InitFeatures, NodeFeatures};
1920
use ln::msgs::{self, OnionMessageHandler};
2021
use ln::onion_utils;
2122
use super::blinded_route::{BlindedRoute, ForwardTlvs, ReceiveTlvs};
@@ -345,6 +346,18 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessageHandler for OnionMessenger<Si
345346
let mut pending_msgs = self.pending_messages.lock().unwrap();
346347
pending_msgs.remove(their_node_id);
347348
}
349+
350+
fn provided_node_features(&self) -> NodeFeatures {
351+
let mut features = NodeFeatures::empty();
352+
features.set_onion_messages_optional();
353+
features
354+
}
355+
356+
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
357+
let mut features = InitFeatures::empty();
358+
features.set_onion_messages_optional();
359+
features
360+
}
348361
}
349362

350363
impl<Signer: Sign, K: Deref, L: Deref> OnionMessageProvider for OnionMessenger<Signer, K, L>

lightning/src/routing/gossip.rs

+6
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,12 @@ where C::Target: chain::Access, L::Target: Logger
571571
})
572572
}
573573

574+
fn provided_node_features(&self) -> NodeFeatures {
575+
let mut features = NodeFeatures::empty();
576+
features.set_gossip_queries_optional();
577+
features
578+
}
579+
574580
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
575581
let mut features = InitFeatures::empty();
576582
features.set_gossip_queries_optional();

lightning/src/util/test_utils.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
358358
self.received_msg(wire::Message::Error(msg.clone()));
359359
}
360360
fn provided_node_features(&self) -> NodeFeatures {
361-
NodeFeatures::empty()
361+
NodeFeatures::known_channel_features()
362362
}
363363
fn provided_init_features(&self, _their_init_features: &PublicKey) -> InitFeatures {
364364
InitFeatures::known_channel_features()
@@ -511,6 +511,12 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
511511
Ok(())
512512
}
513513

514+
fn provided_node_features(&self) -> NodeFeatures {
515+
let mut features = NodeFeatures::empty();
516+
features.set_gossip_queries_optional();
517+
features
518+
}
519+
514520
fn provided_init_features(&self, _their_init_features: &PublicKey) -> InitFeatures {
515521
let mut features = InitFeatures::empty();
516522
features.set_gossip_queries_optional();

0 commit comments

Comments
 (0)