Skip to content

Commit

Permalink
Expose provided InitFeatures in PeerManager::get_peer_node_ids
Browse files Browse the repository at this point in the history
Previously, we wouldn't offer any way to retrieve the features a
peer provided in their `Init` message.

Here, we allow to retrieve them via `get_peer_node_ids`, similar to the
`SocketAddress`es.
  • Loading branch information
tnull committed Feb 20, 2024
1 parent cd84757 commit 5556830
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
19 => {
let mut list = loss_detector.handler.get_peer_node_ids();
list.sort_by_key(|v| v.0);
if let Some((id, _)) = list.get(0) {
if let Some((id, _, _)) = list.get(0) {
loss_detector.handler.disconnect_by_node_id(*id);
}
},
Expand Down
15 changes: 9 additions & 6 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
}
}

/// Get a list of tuples mapping from node id to network addresses for peers which have
/// completed the initial handshake.
/// Get a list of tuples mapping from node id to network addresses and init features for peers
/// which have completed the initial handshake.
///
/// For outbound connections, the [`PublicKey`] will be the same as the `their_node_id` parameter
/// passed in to [`Self::new_outbound_connection`], however entries will only appear once the initial
Expand All @@ -968,14 +968,15 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
///
/// The returned `Option`s will only be `Some` if an address had been previously given via
/// [`Self::new_outbound_connection`] or [`Self::new_inbound_connection`].
pub fn get_peer_node_ids(&self) -> Vec<(PublicKey, Option<SocketAddress>)> {
pub fn get_peer_node_ids(&self) -> Vec<(PublicKey, Option<SocketAddress>, InitFeatures)> {
let peers = self.peers.read().unwrap();
peers.values().filter_map(|peer_mutex| {
let p = peer_mutex.lock().unwrap();
if !p.handshake_complete() {
return None;
}
Some((p.their_node_id.unwrap().0, p.their_socket_address.clone()))
Some((p.their_node_id.unwrap().0, p.their_socket_address.clone(),
p.their_features.clone().unwrap()))
}).collect()
}

Expand Down Expand Up @@ -2746,6 +2747,8 @@ mod tests {
};
let addr_a = SocketAddress::TcpIpV4{addr: [127, 0, 0, 1], port: 1000};
let id_b = peer_b.node_signer.get_node_id(Recipient::Node).unwrap();
let features_a = peer_a.init_features(&id_b);
let features_b = peer_b.init_features(&id_a);
let mut fd_b = FileDescriptor {
fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())),
disconnect: Arc::new(AtomicBool::new(false)),
Expand All @@ -2767,8 +2770,8 @@ mod tests {
let a_data = fd_a.outbound_data.lock().unwrap().split_off(0);
assert_eq!(peer_b.read_event(&mut fd_b, &a_data).unwrap(), false);

assert!(peer_a.get_peer_node_ids().contains(&(id_b, Some(addr_b))));
assert!(peer_b.get_peer_node_ids().contains(&(id_a, Some(addr_a))));
assert!(peer_a.get_peer_node_ids().contains(&(id_b, Some(addr_b), features_b)));
assert!(peer_b.get_peer_node_ids().contains(&(id_a, Some(addr_a), features_a)));

(fd_a.clone(), fd_b.clone())
}
Expand Down

0 comments on commit 5556830

Please sign in to comment.