Skip to content

Commit

Permalink
transport: Implement TransportService::local_peer_id() (#224)
Browse files Browse the repository at this point in the history
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

Co-authored-by: Dmitry Markin <dmitry@markin.tech>
  • Loading branch information
altonen and dmitry-markin authored Aug 30, 2024
1 parent 86f2a30 commit 1fbbadd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/protocol/libp2p/kademlia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -892,7 +892,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;
}

Expand Down
7 changes: 6 additions & 1 deletion src/protocol/transport_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1fbbadd

Please sign in to comment.