Skip to content

Commit 5af299e

Browse files
committed
Add gossip_queries feature flag
Support for the gossip_queries feature flag (bits 6/7) is added to the Features struct. This feature is available in the Init and Node contexts. The gossip_queries feature is not fully implemented so this feature is disabled when sent to peers in the Init message.
1 parent a008464 commit 5af299e

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

lightning/src/ln/features.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ mod sealed {
104104
],
105105
optional_features: [
106106
// Byte 0
107-
DataLossProtect | InitialRoutingSync | UpfrontShutdownScript,
107+
DataLossProtect | InitialRoutingSync | UpfrontShutdownScript | GossipQueries,
108108
// Byte 1
109109
VariableLengthOnion | PaymentSecret,
110110
// Byte 2
@@ -122,7 +122,7 @@ mod sealed {
122122
],
123123
optional_features: [
124124
// Byte 0
125-
DataLossProtect | UpfrontShutdownScript,
125+
DataLossProtect | UpfrontShutdownScript | GossipQueries,
126126
// Byte 1
127127
VariableLengthOnion | PaymentSecret,
128128
// Byte 2
@@ -243,6 +243,8 @@ mod sealed {
243243
"Feature flags for `initial_routing_sync`.");
244244
define_feature!(5, UpfrontShutdownScript, [InitContext, NodeContext],
245245
"Feature flags for `option_upfront_shutdown_script`.");
246+
define_feature!(7, GossipQueries, [InitContext, NodeContext],
247+
"Feature flags for `gossip_queries`.");
246248
define_feature!(9, VariableLengthOnion, [InitContext, NodeContext],
247249
"Feature flags for `var_onion_optin`.");
248250
define_feature!(13, StaticRemoteKey, [InitContext, NodeContext],
@@ -473,6 +475,21 @@ impl<T: sealed::UpfrontShutdownScript> Features<T> {
473475
}
474476
}
475477

478+
479+
impl<T: sealed::GossipQueries> Features<T> {
480+
#[cfg(test)]
481+
pub(crate) fn requires_gossip_queries(&self) -> bool {
482+
<T as sealed::GossipQueries>::requires_feature(&self.flags)
483+
}
484+
pub(crate) fn supports_gossip_queries(&self) -> bool {
485+
<T as sealed::GossipQueries>::supports_feature(&self.flags)
486+
}
487+
pub(crate) fn clear_gossip_queries(mut self) -> Self {
488+
<T as sealed::GossipQueries>::clear_bits(&mut self.flags);
489+
self
490+
}
491+
}
492+
476493
impl<T: sealed::VariableLengthOnion> Features<T> {
477494
#[cfg(test)]
478495
pub(crate) fn requires_variable_length_onion(&self) -> bool {
@@ -568,6 +585,11 @@ mod tests {
568585
assert!(!InitFeatures::known().requires_upfront_shutdown_script());
569586
assert!(!NodeFeatures::known().requires_upfront_shutdown_script());
570587

588+
assert!(InitFeatures::known().supports_gossip_queries());
589+
assert!(NodeFeatures::known().supports_gossip_queries());
590+
assert!(!InitFeatures::known().requires_gossip_queries());
591+
assert!(!NodeFeatures::known().requires_gossip_queries());
592+
571593
assert!(InitFeatures::known().supports_data_loss_protect());
572594
assert!(NodeFeatures::known().supports_data_loss_protect());
573595
assert!(!InitFeatures::known().requires_data_loss_protect());
@@ -620,9 +642,10 @@ mod tests {
620642

621643
#[test]
622644
fn convert_to_context_with_relevant_flags() {
623-
let init_features = InitFeatures::known().clear_upfront_shutdown_script();
645+
let init_features = InitFeatures::known().clear_upfront_shutdown_script().clear_gossip_queries();
624646
assert!(init_features.initial_routing_sync());
625647
assert!(!init_features.supports_upfront_shutdown_script());
648+
assert!(!init_features.supports_gossip_queries());
626649

627650
let node_features: NodeFeatures = init_features.to_context();
628651
{
@@ -639,8 +662,10 @@ mod tests {
639662
// Check that cleared flags are kept blank when converting back:
640663
// - initial_routing_sync was not applicable to NodeContext
641664
// - upfront_shutdown_script was cleared before converting
665+
// - gossip_queries was cleared before converting
642666
let features: InitFeatures = node_features.to_context_internal();
643667
assert!(!features.initial_routing_sync());
644668
assert!(!features.supports_upfront_shutdown_script());
669+
assert!(!init_features.supports_gossip_queries());
645670
}
646671
}

lightning/src/ln/peer_handler.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
584584

585585
peer.their_node_id = Some(their_node_id);
586586
insert_node_id!();
587-
let mut features = InitFeatures::known();
587+
let mut features = InitFeatures::known().clear_gossip_queries();
588588
if !self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
589589
features.clear_initial_routing_sync();
590590
}
@@ -694,10 +694,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
694694
}
695695

696696
log_info!(
697-
self.logger, "Received peer Init message: data_loss_protect: {}, initial_routing_sync: {}, upfront_shutdown_script: {}, static_remote_key: {}, unknown flags (local and global): {}",
697+
self.logger, "Received peer Init message: data_loss_protect: {}, initial_routing_sync: {}, upfront_shutdown_script: {}, gossip_queries: {}, static_remote_key: {}, unknown flags (local and global): {}",
698698
if msg.features.supports_data_loss_protect() { "supported" } else { "not supported"},
699699
if msg.features.initial_routing_sync() { "requested" } else { "not requested" },
700700
if msg.features.supports_upfront_shutdown_script() { "supported" } else { "not supported"},
701+
if msg.features.supports_gossip_queries() { "supported" } else { "not supported" },
701702
if msg.features.supports_static_remote_key() { "supported" } else { "not supported"},
702703
if msg.features.supports_unknown_bits() { "present" } else { "none" }
703704
);
@@ -712,7 +713,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
712713
}
713714

714715
if !peer.outbound {
715-
let mut features = InitFeatures::known();
716+
let mut features = InitFeatures::known().clear_gossip_queries();
716717
if !self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
717718
features.clear_initial_routing_sync();
718719
}

lightning/src/ln/wire.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,24 +415,25 @@ mod tests {
415415
fn read_lnd_init_msg() {
416416
// Taken from lnd v0.9.0-beta.
417417
let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 162, 161];
418-
check_init_msg(buffer);
418+
check_init_msg(buffer, false);
419419
}
420420

421421
#[test]
422422
fn read_clightning_init_msg() {
423423
// Taken from c-lightning v0.8.0.
424424
let buffer = vec![0, 16, 0, 2, 34, 0, 0, 3, 2, 170, 162, 1, 32, 6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15];
425-
check_init_msg(buffer);
425+
check_init_msg(buffer, true);
426426
}
427427

428-
fn check_init_msg(buffer: Vec<u8>) {
428+
fn check_init_msg(buffer: Vec<u8>, expect_unknown: bool) {
429429
let mut reader = ::std::io::Cursor::new(buffer);
430430
let decoded_msg = read(&mut reader).unwrap();
431431
match decoded_msg {
432432
Message::Init(msgs::Init { features }) => {
433433
assert!(features.supports_variable_length_onion());
434434
assert!(features.supports_upfront_shutdown_script());
435-
assert!(features.supports_unknown_bits());
435+
assert!(features.supports_gossip_queries());
436+
assert_eq!(expect_unknown, features.supports_unknown_bits());
436437
assert!(!features.requires_unknown_bits());
437438
assert!(!features.initial_routing_sync());
438439
},
@@ -450,7 +451,7 @@ mod tests {
450451
Message::NodeAnnouncement(msgs::NodeAnnouncement { contents: msgs::UnsignedNodeAnnouncement { features, ..}, ..}) => {
451452
assert!(features.supports_variable_length_onion());
452453
assert!(features.supports_upfront_shutdown_script());
453-
assert!(features.supports_unknown_bits());
454+
assert!(features.supports_gossip_queries());
454455
assert!(!features.requires_unknown_bits());
455456
},
456457
_ => panic!("Expected node announcement, found message type: {}", decoded_msg.type_id())

0 commit comments

Comments
 (0)