Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/codec/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ mod tests {
let bytes = [3u8; 64];
let mut bytes = BytesMut::from(&bytes[..]);

let decoded = codec.decode(&mut bytes);
let _decoded = codec.decode(&mut bytes);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/protocol/libp2p/kademlia/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ mod tests {
.collect::<Vec<_>>();

let target = Key::from(PeerId::random());
let mut iter = bucket.closest_iter(&target);
let iter = bucket.closest_iter(&target);
let mut prev = None;

for node in iter {
Expand Down Expand Up @@ -173,7 +173,7 @@ mod tests {
.collect::<Vec<_>>();

let target = Key::from(PeerId::random());
let mut iter = bucket.closest_iter(&target);
let iter = bucket.closest_iter(&target);
let mut prev = None;
let mut num_peers = 0usize;

Expand Down
34 changes: 14 additions & 20 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::{
libp2p::kademlia::{
bucket::{KBucket, KBucketEntry},
types::{ConnectionType, Distance, KademliaPeer, Key, U256},
},
sort_address,
},
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 @@ -188,17 +190,7 @@ impl RoutingTable {
);

// TODO: https://github.com/paritytech/litep2p/issues/337 this has to be moved elsewhere at some point
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably remove the TODO here? Or do you think there are more places where this may happen? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I forgot to remove that. It's all covered now, no more duplicates

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> = sort_address(addresses.into_iter(), peer);

if addresses.is_empty() {
tracing::debug!(
Expand Down Expand Up @@ -304,23 +296,25 @@ impl Iterator for ClosestBucketsIter {
self.state = ClosestBucketsIterState::ZoomIn(i);
Some(i)
}
ClosestBucketsIterState::ZoomIn(i) =>
ClosestBucketsIterState::ZoomIn(i) => {
if let Some(i) = self.next_in(i) {
self.state = ClosestBucketsIterState::ZoomIn(i);
Some(i)
} else {
let i = BucketIndex(0);
self.state = ClosestBucketsIterState::ZoomOut(i);
Some(i)
},
ClosestBucketsIterState::ZoomOut(i) =>
}
}
ClosestBucketsIterState::ZoomOut(i) => {
if let Some(i) = self.next_out(i) {
self.state = ClosestBucketsIterState::ZoomOut(i);
Some(i)
} else {
self.state = ClosestBucketsIterState::Done;
None
},
}
}
ClosestBucketsIterState::Done => None,
}
}
Expand Down
18 changes: 17 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,18 @@ pub trait UserProtocol: Send {
/// Start the the user protocol event loop.
async fn run(self: Box<Self>, service: TransportService) -> crate::Result<()>;
}

pub fn sort_address(addresses: impl Iterator<Item = Multiaddr>, peer_id: PeerId) -> Vec<Multiaddr> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would name this method ensure_address_with_peer or similar, since it is not really sorting the input 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a better name. Would use that instead

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()
}
15 changes: 4 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::sort_address;

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

Expand Down Expand Up @@ -467,15 +468,7 @@ 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 addresses = sort_address(addresses.into_iter(), *peer);

self.transport_handle.add_known_address(peer, addresses.into_iter());
}
Expand Down
6 changes: 4 additions & 2 deletions src/transport/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,11 @@ impl TransportManager {
#[cfg(feature = "websocket")]
Some(Protocol::Ws(_)) | Some(Protocol::Wss(_)) => SupportedTransport::WebSocket,
Some(Protocol::P2p(_)) => SupportedTransport::Tcp,
_ =>
_ => {
return Err(Error::TransportNotSupported(
address_record.address().clone(),
)),
))
}
},
#[cfg(feature = "quic")]
Protocol::Udp(_) => match protocol_stack
Expand Down Expand Up @@ -1358,6 +1359,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
2 changes: 1 addition & 1 deletion tests/conformance/rust/kademlia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use libp2p::{
identify, identity,
kad::{
self, store::RecordStore, AddProviderOk, GetProvidersOk, InboundRequest,
KademliaEvent as Libp2pKademliaEvent, QueryResult, RecordKey as Libp2pRecordKey,
KademliaEvent as Libp2pKademliaEvent, QueryResult,
},
swarm::{keep_alive, AddressScore, NetworkBehaviour, SwarmBuilder, SwarmEvent},
PeerId, Swarm,
Expand Down