Skip to content

Commit e34a2bc

Browse files
committed
Add a new InitFeatures constructor to capture the types of flags
When `ChannelMessageHandler` implementations wish to return an `InitFeatures` which contain all the known flags that are relevant to channel handling, but not gossip handling, they currently need to do so by manually constructing an InitFeatures with all known flags and then clearing the ones they dont want. Instead of spreading this logic out across the codebase, this consolidates such construction to one place in features.rs.
1 parent 06cb48a commit e34a2bc

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

lightning/src/ln/channelmanager.rs

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

61286128
fn provided_init_features(&self, _their_init_features: &PublicKey) -> InitFeatures {
6129-
InitFeatures::known()
6129+
InitFeatures::known_channel_features()
61306130
}
61316131
}
61326132

lightning/src/ln/features.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ mod sealed {
163163
,
164164
],
165165
optional_features: [
166+
// Note that if new "non-channel-related" flags are added here they should be
167+
// explicitly cleared in InitFeatures::known_channel_features.
166168
// Byte 0
167169
DataLossProtect | InitialRoutingSync | UpfrontShutdownScript | GossipQueries,
168170
// Byte 1
@@ -545,6 +547,14 @@ impl InitFeatures {
545547
pub(crate) fn to_context<C: sealed::Context>(&self) -> Features<C> {
546548
self.to_context_internal()
547549
}
550+
551+
/// Returns the set of known init features that are related to channels. At least some of
552+
/// these features are likely required for peers to talk to us.
553+
pub fn known_channel_features() -> InitFeatures {
554+
let mut features = Self::known().clear_gossip_queries();
555+
features.clear_initial_routing_sync();
556+
features
557+
}
548558
}
549559

550560
impl InvoiceFeatures {
@@ -763,18 +773,14 @@ impl<T: sealed::UpfrontShutdownScript> Features<T> {
763773

764774

765775
impl<T: sealed::GossipQueries> Features<T> {
766-
#[cfg(test)]
767776
pub(crate) fn clear_gossip_queries(mut self) -> Self {
768777
<T as sealed::GossipQueries>::clear_bits(&mut self.flags);
769778
self
770779
}
771780
}
772781

773782
impl<T: sealed::InitialRoutingSync> Features<T> {
774-
// We are no longer setting initial_routing_sync now that gossip_queries
775-
// is enabled. This feature is ignored by a peer when gossip_queries has
776-
// been negotiated.
777-
#[cfg(test)]
783+
// Note that initial_routing_sync is ignored if gossip_queries is set.
778784
pub(crate) fn clear_initial_routing_sync(&mut self) {
779785
<T as sealed::InitialRoutingSync>::clear_bits(&mut self.flags)
780786
}

lightning/src/ln/peer_handler.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ impl ChannelMessageHandler for ErroringMessageHandler {
206206
fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &msgs::Init) {}
207207
fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
208208
fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
209-
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures { InitFeatures::known() }
209+
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
210+
// Use our known channel feature set as peers may otherwise not be willing to talk to us at
211+
// all.
212+
InitFeatures::known_channel_features()
213+
}
210214
}
211215
impl Deref for ErroringMessageHandler {
212216
type Target = ErroringMessageHandler;

lightning/src/util/test_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
361361
NodeFeatures::empty()
362362
}
363363
fn provided_init_features(&self, _their_init_features: &PublicKey) -> InitFeatures {
364-
InitFeatures::known()
364+
InitFeatures::known_channel_features()
365365
}
366366
}
367367

0 commit comments

Comments
 (0)