Skip to content

Commit 43a44b7

Browse files
committed
Move checking of specific require peer feature bits to handlers
As we remove the concept of a global "known/supported" feature set in LDK, we should also remove the concept of a global "required" feature set. This does so by moving the checks for specific required features into handlers. Specifically, it allows the handler `peer_connected` method to return an `Err` if the peer should be disconnected. Only one such required feature bit is currently set - `static_remote_key`, which is required in `ChannelManager`.
1 parent 1c06906 commit 43a44b7

File tree

8 files changed

+54
-23
lines changed

8 files changed

+54
-23
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ mod tests {
577577
fn handle_channel_update(&self, _msg: &ChannelUpdate) -> Result<bool, LightningError> { Ok(false) }
578578
fn get_next_channel_announcement(&self, _starting_point: u64) -> Option<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> { None }
579579
fn get_next_node_announcement(&self, _starting_point: Option<&PublicKey>) -> Option<NodeAnnouncement> { None }
580-
fn peer_connected(&self, _their_node_id: &PublicKey, _init_msg: &Init) { }
580+
fn peer_connected(&self, _their_node_id: &PublicKey, _init_msg: &Init) -> Result<(), ()> { Ok(()) }
581581
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: ReplyChannelRange) -> Result<(), LightningError> { Ok(()) }
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(()) }
@@ -608,10 +608,11 @@ mod tests {
608608
self.pubkey_disconnected.clone().try_send(()).unwrap();
609609
}
610610
}
611-
fn peer_connected(&self, their_node_id: &PublicKey, _msg: &Init) {
611+
fn peer_connected(&self, their_node_id: &PublicKey, _init_msg: &Init) -> Result<(), ()> {
612612
if *their_node_id == self.expected_pubkey {
613613
self.pubkey_connected.clone().try_send(()).unwrap();
614614
}
615+
Ok(())
615616
}
616617
fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, _msg: &ChannelReestablish) {}
617618
fn handle_error(&self, _their_node_id: &PublicKey, _msg: &ErrorMessage) {}

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6034,7 +6034,12 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
60346034
}
60356035
}
60366036

6037-
fn peer_connected(&self, counterparty_node_id: &PublicKey, init_msg: &msgs::Init) {
6037+
fn peer_connected(&self, counterparty_node_id: &PublicKey, init_msg: &msgs::Init) -> Result<(), ()> {
6038+
if !init_msg.features.supports_static_remote_key() {
6039+
log_debug!(self.logger, "Peer {} does not support static remote key, disconnecting with no_connection_possible", log_pubkey!(counterparty_node_id));
6040+
return Err(());
6041+
}
6042+
60386043
log_debug!(self.logger, "Generating channel_reestablish events for {}", log_pubkey!(counterparty_node_id));
60396044

60406045
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
@@ -6085,6 +6090,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
60856090
retain
60866091
});
60876092
//TODO: Also re-broadcast announcement_signatures
6093+
Ok(())
60886094
}
60896095

60906096
fn handle_error(&self, counterparty_node_id: &PublicKey, msg: &msgs::ErrorMessage) {

lightning/src/ln/msgs.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,11 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
889889
fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool);
890890

891891
/// Handle a peer reconnecting, possibly generating channel_reestablish message(s).
892-
fn peer_connected(&self, their_node_id: &PublicKey, msg: &Init);
892+
///
893+
/// May return an `Err(ErrorMessage)` if the features the peer supports are not sufficient to
894+
/// communicate with us. Implementors should be somewhat conservative about doing so, however,
895+
/// as other message handlers may still wish to communicate with this peer.
896+
fn peer_connected(&self, their_node_id: &PublicKey, msg: &Init) -> Result<(), ()>;
893897
/// Handle an incoming channel_reestablish message from the given peer.
894898
fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish);
895899

@@ -943,7 +947,11 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
943947
/// Called when a connection is established with a peer. This can be used to
944948
/// perform routing table synchronization using a strategy defined by the
945949
/// implementor.
946-
fn peer_connected(&self, their_node_id: &PublicKey, init: &Init);
950+
///
951+
/// May return an `Err(ErrorMessage)` if the features the peer supports are not sufficient to
952+
/// communicate with us. Implementors should be somewhat conservative about doing so, however,
953+
/// as other message handlers may still wish to communicate with this peer.
954+
fn peer_connected(&self, their_node_id: &PublicKey, init: &Init) -> Result<(), ()>;
947955
/// Handles the reply of a query we initiated to learn about channels
948956
/// for a given range of blocks. We can expect to receive one or more
949957
/// replies to a single query.
@@ -979,7 +987,11 @@ pub trait OnionMessageHandler : OnionMessageProvider {
979987
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage);
980988
/// Called when a connection is established with a peer. Can be used to track which peers
981989
/// advertise onion message support and are online.
982-
fn peer_connected(&self, their_node_id: &PublicKey, init: &Init);
990+
///
991+
/// May return an `Err(ErrorMessage)` if the features the peer supports are not sufficient to
992+
/// communicate with us. Implementors should be somewhat conservative about doing so, however,
993+
/// as other message handlers may still wish to communicate with this peer.
994+
fn peer_connected(&self, their_node_id: &PublicKey, init: &Init) -> Result<(), ()>;
983995
/// Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
984996
/// drop and refuse to forward onion messages to this peer.
985997
///

lightning/src/ln/onion_route_tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,10 +887,12 @@ fn test_do_not_default_to_onion_payload_tlv_format_when_unsupported() {
887887
let chanmon_cfgs = create_chanmon_cfgs(4);
888888
let mut node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
889889

890-
// Set `node[1]` config to `InitFeatures::empty()` which return `false` for
891-
// `supports_variable_length_onion()`
890+
// Set `node[1]` config to `InitFeatures::empty()` + `static_remote_key` which implies
891+
// `!supports_variable_length_onion()` but still supprts the required static-remote-key
892+
// feature.
892893
let mut node_1_cfg = &mut node_cfgs[1];
893894
node_1_cfg.features = InitFeatures::empty();
895+
node_1_cfg.features.set_static_remote_key_required();
894896

895897
let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
896898
let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);

lightning/src/ln/peer_handler.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl RoutingMessageHandler for IgnoringMessageHandler {
7272
fn get_next_channel_announcement(&self, _starting_point: u64) ->
7373
Option<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> { None }
7474
fn get_next_node_announcement(&self, _starting_point: Option<&PublicKey>) -> Option<msgs::NodeAnnouncement> { None }
75-
fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init) {}
75+
fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init) -> Result<(), ()> { Ok(()) }
7676
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyChannelRange) -> Result<(), LightningError> { Ok(()) }
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(()) }
@@ -87,7 +87,7 @@ impl OnionMessageProvider for IgnoringMessageHandler {
8787
}
8888
impl OnionMessageHandler for IgnoringMessageHandler {
8989
fn handle_onion_message(&self, _their_node_id: &PublicKey, _msg: &msgs::OnionMessage) {}
90-
fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init) {}
90+
fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init) -> Result<(), ()> { Ok(()) }
9191
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
9292
fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
9393
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
@@ -208,7 +208,7 @@ impl ChannelMessageHandler for ErroringMessageHandler {
208208
// msgs::ChannelUpdate does not contain the channel_id field, so we just drop them.
209209
fn handle_channel_update(&self, _their_node_id: &PublicKey, _msg: &msgs::ChannelUpdate) {}
210210
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
211-
fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &msgs::Init) {}
211+
fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init) -> Result<(), ()> { Ok(()) }
212212
fn handle_error(&self, _their_node_id: &PublicKey, _msg: &msgs::ErrorMessage) {}
213213
fn provided_node_features(&self) -> NodeFeatures { NodeFeatures::empty() }
214214
fn provided_init_features(&self, _their_node_id: &PublicKey) -> InitFeatures {
@@ -1210,14 +1210,18 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
12101210
peer_lock.sync_status = InitSyncTracker::ChannelsSyncing(0);
12111211
}
12121212

1213-
if !msg.features.supports_static_remote_key() {
1214-
log_debug!(self.logger, "Peer {} does not support static remote key, disconnecting with no_connection_possible", log_pubkey!(their_node_id));
1213+
if let Err(()) = self.message_handler.route_handler.peer_connected(&their_node_id, &msg) {
1214+
log_debug!(self.logger, "Route Handler decided we couldn't communicate with peer {}", log_pubkey!(their_node_id));
1215+
return Err(PeerHandleError{ no_connection_possible: true }.into());
1216+
}
1217+
if let Err(()) = self.message_handler.chan_handler.peer_connected(&their_node_id, &msg) {
1218+
log_debug!(self.logger, "Channel Handler decided we couldn't communicate with peer {}", log_pubkey!(their_node_id));
1219+
return Err(PeerHandleError{ no_connection_possible: true }.into());
1220+
}
1221+
if let Err(()) = self.message_handler.onion_message_handler.peer_connected(&their_node_id, &msg) {
1222+
log_debug!(self.logger, "Onion Message Handler decided we couldn't communicate with peer {}", log_pubkey!(their_node_id));
12151223
return Err(PeerHandleError{ no_connection_possible: true }.into());
12161224
}
1217-
1218-
self.message_handler.route_handler.peer_connected(&their_node_id, &msg);
1219-
self.message_handler.chan_handler.peer_connected(&their_node_id, &msg);
1220-
self.message_handler.onion_message_handler.peer_connected(&their_node_id, &msg);
12211225

12221226
peer_lock.their_features = Some(msg.features);
12231227
return Ok(None);

lightning/src/onion_message/messenger.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,12 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessageHandler for OnionMessenger<Si
335335
};
336336
}
337337

338-
fn peer_connected(&self, their_node_id: &PublicKey, init: &msgs::Init) {
338+
fn peer_connected(&self, their_node_id: &PublicKey, init: &msgs::Init) -> Result<(), ()> {
339339
if init.features.supports_onion_messages() {
340340
let mut peers = self.pending_messages.lock().unwrap();
341341
peers.insert(their_node_id.clone(), VecDeque::new());
342342
}
343+
Ok(())
343344
}
344345

345346
fn peer_disconnected(&self, their_node_id: &PublicKey, _no_connection_possible: bool) {

lightning/src/routing/gossip.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,12 @@ where C::Target: chain::Access, L::Target: Logger
368368
/// to request gossip messages for each channel. The sync is considered complete
369369
/// when the final reply_scids_end message is received, though we are not
370370
/// tracking this directly.
371-
fn peer_connected(&self, their_node_id: &PublicKey, init_msg: &Init) {
371+
fn peer_connected(&self, their_node_id: &PublicKey, init_msg: &Init) -> Result<(), ()> {
372372
// We will only perform a sync with peers that support gossip_queries.
373373
if !init_msg.features.supports_gossip_queries() {
374-
return ();
374+
// Don't disconnect peers for not supporting gossip queries. We may wish to have
375+
// channels with peers even without being able to exchange gossip.
376+
return Ok(());
375377
}
376378

377379
// The lightning network's gossip sync system is completely broken in numerous ways.
@@ -445,6 +447,7 @@ where C::Target: chain::Access, L::Target: Logger
445447
timestamp_range: u32::max_value(),
446448
},
447449
});
450+
Ok(())
448451
}
449452

450453
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: ReplyChannelRange) -> Result<(), LightningError> {

lightning/src/util/test_utils.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,10 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
350350
self.received_msg(wire::Message::ChannelReestablish(msg.clone()));
351351
}
352352
fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {}
353-
fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &msgs::Init) {
353+
fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &msgs::Init) -> Result<(), ()> {
354354
// Don't bother with `received_msg` for Init as its auto-generated and we don't want to
355355
// bother re-generating the expected Init message in all tests.
356+
Ok(())
356357
}
357358
fn handle_error(&self, _their_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
358359
self.received_msg(wire::Message::Error(msg.clone()));
@@ -465,9 +466,9 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
465466
None
466467
}
467468

468-
fn peer_connected(&self, their_node_id: &PublicKey, init_msg: &msgs::Init) {
469+
fn peer_connected(&self, their_node_id: &PublicKey, init_msg: &msgs::Init) -> Result<(), ()> {
469470
if !init_msg.features.supports_gossip_queries() {
470-
return ();
471+
return Ok(());
471472
}
472473

473474
#[allow(unused_mut, unused_assignments)]
@@ -491,6 +492,7 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
491492
timestamp_range: u32::max_value(),
492493
},
493494
});
495+
Ok(())
494496
}
495497

496498
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyChannelRange) -> Result<(), msgs::LightningError> {

0 commit comments

Comments
 (0)