From 54546ccec2067657402535e205305297db7b997f Mon Sep 17 00:00:00 2001 From: Aaro Altonen Date: Fri, 30 Aug 2024 12:58:40 +0300 Subject: [PATCH] transport: Implement `TransportService::local_peer_id()` Previously `local_peer_id` was declared `pub(crate)` which made it inaccessible to protocols implemented with `UserProtocol` Implement getter for local peer ID, allowing all protocols having access to `TransportService` to query the local peer ID --- src/protocol/libp2p/kademlia/mod.rs | 8 ++++---- src/protocol/transport_service.rs | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/protocol/libp2p/kademlia/mod.rs b/src/protocol/libp2p/kademlia/mod.rs index b59a1fcf..9d94af83 100644 --- a/src/protocol/libp2p/kademlia/mod.rs +++ b/src/protocol/libp2p/kademlia/mod.rs @@ -167,8 +167,8 @@ pub(crate) struct Kademlia { impl Kademlia { /// Create new [`Kademlia`]. pub(crate) fn new(mut service: TransportService, config: Config) -> Self { - let local_peer_id = service.local_peer_id; - let local_key = Key::from(service.local_peer_id); + let local_peer_id = service.local_peer_id(); + let local_key = Key::from(service.local_peer_id()); let mut routing_table = RoutingTable::new(local_key.clone()); for (peer, addresses) in config.known_peers { @@ -357,7 +357,7 @@ impl Kademlia { /// the mode was set to manual. async fn update_routing_table(&mut self, peers: &[KademliaPeer]) { let peers: Vec<_> = - peers.iter().filter(|peer| peer.peer != self.service.local_peer_id).collect(); + peers.iter().filter(|peer| peer.peer != self.service.local_peer_id()).collect(); // inform user about the routing table update, regardless of what the routing table update // mode is @@ -889,7 +889,7 @@ impl Kademlia { // Put the record to the specified peers. let peers = peers.into_iter().filter_map(|peer| { - if peer == self.service.local_peer_id { + if peer == self.service.local_peer_id() { return None; } diff --git a/src/protocol/transport_service.rs b/src/protocol/transport_service.rs index 63d5a117..f8b6b282 100644 --- a/src/protocol/transport_service.rs +++ b/src/protocol/transport_service.rs @@ -102,7 +102,7 @@ impl ConnectionContext { #[derive(Debug)] pub struct TransportService { /// Local peer ID. - pub(crate) local_peer_id: PeerId, + local_peer_id: PeerId, /// Protocol. protocol: ProtocolName, @@ -362,6 +362,11 @@ impl TransportService { connection.primary.force_close() } + + /// Get local peer ID. + pub fn local_peer_id(&self) -> PeerId { + self.local_peer_id + } } impl Stream for TransportService {