Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

Commit 2c54092

Browse files
bors[bot]koivunej
andauthored
Merge #454
454: Fix connecting to peers r=koivunej a=koivunej The correct way to connect to a peer is in my understanding to DialPeer and then proceed to give the address of that said peer from `NetworkBehaviour::addresses_of_peer`. While doing this, the previous workaround for missing events from banning a peer (the way we force a disconnect) was also necessary to fix. Co-authored-by: Joonas Koivunen <joonas.koivunen@gmail.com>
2 parents 23fc80b + 01e058e commit 2c54092

File tree

6 files changed

+277
-73
lines changed

6 files changed

+277
-73
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* feat: `sled` pinstore [#439], [#442], [#444]
88
* chore: update a lot of dependencies including libp2p, tokio, warp [#446]
99
* fix: rename spans (part of [#453])
10+
* fix: connect using DialPeer instead of DialAddress [#454]
1011

1112
[#429]: https://github.com/rs-ipfs/rust-ipfs/pull/429
1213
[#428]: https://github.com/rs-ipfs/rust-ipfs/pull/428
@@ -17,6 +18,7 @@
1718
[#444]: https://github.com/rs-ipfs/rust-ipfs/pull/444
1819
[#446]: https://github.com/rs-ipfs/rust-ipfs/pull/446
1920
[#453]: https://github.com/rs-ipfs/rust-ipfs/pull/453
21+
[#454]: https://github.com/rs-ipfs/rust-ipfs/pull/454
2022

2123
# 0.2.1
2224

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,8 @@ impl<Types: IpfsTypes> Ipfs<Types> {
656656
/// Accepts only multiaddresses with the PeerId to authenticate the connection.
657657
///
658658
/// Returns a future which will complete when the connection has been successfully made or
659-
/// failed for whatever reason.
659+
/// failed for whatever reason. It is possible for this method to return an error, while ending
660+
/// up being connected to the peer by the means of another connection.
660661
pub async fn connect(&self, target: MultiaddrWithPeerId) -> Result<(), Error> {
661662
async move {
662663
let (tx, rx) = oneshot_channel();
@@ -716,6 +717,8 @@ impl<Types: IpfsTypes> Ipfs<Types> {
716717
///
717718
/// At the moment the peer is disconnected by temporarily banning the peer and unbanning it
718719
/// right after. This should always disconnect all connections to the peer.
720+
///
721+
/// Note: this is rarely needed in pratice as connections will time out if left unused.
719722
pub async fn disconnect(&self, target: MultiaddrWithPeerId) -> Result<(), Error> {
720723
async move {
721724
let (tx, rx) = oneshot_channel();
@@ -1510,7 +1513,7 @@ impl<TRepoTypes: RepoTypes> Future for IpfsFuture<TRepoTypes> {
15101513
let _ = ret.send(peers);
15111514
}
15121515
IpfsEvent::FindPeer(peer_id, local_only, ret) => {
1513-
let swarm_addrs = self.swarm.swarm.addresses_of_peer(&peer_id);
1516+
let swarm_addrs = self.swarm.swarm.connections_to(&peer_id);
15141517
let locally_known_addrs = if !swarm_addrs.is_empty() {
15151518
swarm_addrs
15161519
} else {

src/p2p/addr.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ impl std::error::Error for MultiaddrWrapperError {}
3434
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3535
pub struct MultiaddrWithoutPeerId(Multiaddr);
3636

37+
impl fmt::Display for MultiaddrWithoutPeerId {
38+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
39+
fmt::Display::fmt(&self.0, fmt)
40+
}
41+
}
42+
3743
impl TryFrom<Multiaddr> for MultiaddrWithoutPeerId {
3844
type Error = MultiaddrWrapperError;
3945

@@ -77,6 +83,20 @@ impl FromStr for MultiaddrWithoutPeerId {
7783
}
7884
}
7985

86+
impl PartialEq<Multiaddr> for MultiaddrWithoutPeerId {
87+
fn eq(&self, other: &Multiaddr) -> bool {
88+
&self.0 == other
89+
}
90+
}
91+
92+
impl MultiaddrWithoutPeerId {
93+
/// Adds the peer_id information to this address without peer_id, turning it into
94+
/// [`MultiaddrWithPeerId`].
95+
pub fn with(self, peer_id: PeerId) -> MultiaddrWithPeerId {
96+
(self, peer_id).into()
97+
}
98+
}
99+
80100
/// A `Multiaddr` paired with a discrete `PeerId`. The `Multiaddr` can contain a
81101
/// `Protocol::P2p`, but it's not as easy to work with, and some functionalities
82102
/// don't support it being contained within the `Multiaddr`.
@@ -147,7 +167,7 @@ impl FromStr for MultiaddrWithPeerId {
147167

148168
impl fmt::Display for MultiaddrWithPeerId {
149169
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150-
write!(f, "{}/p2p/{}", self.multiaddr.as_ref(), self.peer_id)
170+
write!(f, "{}/p2p/{}", self.multiaddr, self.peer_id)
151171
}
152172
}
153173

0 commit comments

Comments
 (0)