Skip to content

replace duplicate code with common function #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions src/protocol/libp2p/kademlia/routing_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
//! Kademlia routing table implementation.

use crate::{
protocol::libp2p::kademlia::{
bucket::{KBucket, KBucketEntry},
types::{ConnectionType, Distance, KademliaPeer, Key, U256},
protocol::{
ensure_address_with_peer,
libp2p::kademlia::{
bucket::{KBucket, KBucketEntry},
types::{ConnectionType, Distance, KademliaPeer, Key, U256},
},
},
transport::{
manager::address::{scores, AddressRecord},
Expand All @@ -33,8 +36,7 @@ use crate::{
PeerId,
};

use multiaddr::{Multiaddr, Protocol};
use multihash::Multihash;
use multiaddr::Multiaddr;

/// Number of k-buckets.
const NUM_BUCKETS: usize = 256;
Expand Down Expand Up @@ -187,18 +189,7 @@ impl RoutingTable {
"add known peer"
);

// TODO: https://github.com/paritytech/litep2p/issues/337 this has to be moved elsewhere at some point
let addresses: Vec<Multiaddr> = addresses
.into_iter()
.filter_map(|address| {
let last = address.iter().last();
if std::matches!(last, Some(Protocol::P2p(_))) {
Some(address)
} else {
Some(address.with(Protocol::P2p(Multihash::from_bytes(&peer.to_bytes()).ok()?)))
}
})
.collect();
let addresses: Vec<Multiaddr> = ensure_address_with_peer(addresses.into_iter(), peer);

if addresses.is_empty() {
tracing::debug!(
Expand Down
21 changes: 20 additions & 1 deletion src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use crate::{
PeerId,
};

use multiaddr::Multiaddr;
use multiaddr::{Multiaddr, Protocol};
use multihash::Multihash;

use std::fmt::Debug;

Expand Down Expand Up @@ -141,3 +142,21 @@ pub trait UserProtocol: Send {
/// Start the the user protocol event loop.
async fn run(self: Box<Self>, service: TransportService) -> crate::Result<()>;
}

pub fn ensure_address_with_peer(
addresses: impl Iterator<Item = Multiaddr>,
peer_id: PeerId,
) -> Vec<Multiaddr> {
addresses
.filter_map(|address| {
let last = address.iter().last();
if std::matches!(last, Some(Protocol::P2p(_))) {
Some(address)
} else {
Some(address.with(Protocol::P2p(
Multihash::from_bytes(&peer_id.to_bytes()).ok()?,
)))
}
})
.collect()
}
18 changes: 7 additions & 11 deletions src/protocol/transport_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ use crate::{
};

use futures::{future::BoxFuture, stream::FuturesUnordered, Stream, StreamExt};
use multiaddr::{Multiaddr, Protocol};
use multihash::Multihash;
use multiaddr::Multiaddr;
use tokio::sync::mpsc::{channel, Receiver, Sender};

use std::{
Expand All @@ -44,6 +43,8 @@ use std::{
time::{Duration, Instant},
};

use super::ensure_address_with_peer;

/// Logging target for the file.
const LOG_TARGET: &str = "litep2p::transport-service";

Expand Down Expand Up @@ -467,15 +468,10 @@ impl TransportService {
///
/// The list is filtered for duplicates and unsupported transports.
pub fn add_known_address(&mut self, peer: &PeerId, addresses: impl Iterator<Item = Multiaddr>) {
let addresses: HashSet<Multiaddr> = addresses
.filter_map(|address| {
if !std::matches!(address.iter().last(), Some(Protocol::P2p(_))) {
Some(address.with(Protocol::P2p(Multihash::from_bytes(&peer.to_bytes()).ok()?)))
} else {
Some(address)
}
})
.collect();
let mut addresses = ensure_address_with_peer(addresses.into_iter(), *peer);

addresses.sort();
addresses.dedup();

self.transport_handle.add_known_address(peer, addresses.into_iter());
}
Expand Down
1 change: 1 addition & 0 deletions src/transport/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,7 @@ mod tests {
rx: tokio::sync::mpsc::Receiver<TransportEvent>,
}

#[allow(dead_code)]
impl MockTransport {
fn new(rx: tokio::sync::mpsc::Receiver<TransportEvent>) -> Self {
Self { rx }
Expand Down