Skip to content

Commit

Permalink
core/src/connection: Add ConnectedPoint::is_relayed (#2392)
Browse files Browse the repository at this point in the history
Add convenience function `is_relayed` to `ConnectedPoint`, determining
whether a connection is relayed based on whether it contains a
`Protocol::P2pCircuit`.
  • Loading branch information
mxinden committed Dec 19, 2021
1 parent b6c82a4 commit 176636f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

- Add support for ECDSA identities (see [PR 2352]).

- Add `ConnectedPoint::is_relayed` (see [PR 2392]).

[PR 2339]: https://github.com/libp2p/rust-libp2p/pull/2339
[PR 2350]: https://github.com/libp2p/rust-libp2p/pull/2350
[PR 2352]: https://github.com/libp2p/rust-libp2p/pull/2352
[PR 2392]: https://github.com/libp2p/rust-libp2p/pull/2392

# 0.30.1 [2021-11-16]

Expand Down
13 changes: 12 additions & 1 deletion core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ pub use pool::{ConnectionCounters, ConnectionLimits};
pub use pool::{EstablishedConnection, EstablishedConnectionIter, PendingConnection};
pub use substream::{Close, Substream, SubstreamEndpoint};

use crate::multiaddr::{Multiaddr, Protocol};
use crate::muxing::StreamMuxer;
use crate::{Multiaddr, PeerId};
use crate::PeerId;
use std::hash::Hash;
use std::{error::Error, fmt, pin::Pin, task::Context, task::Poll};
use substream::{Muxing, SubstreamEvent};
Expand Down Expand Up @@ -175,6 +176,16 @@ impl ConnectedPoint {
}
}

/// Returns true if the connection is relayed.
pub fn is_relayed(&self) -> bool {
match self {
ConnectedPoint::Dialer { address } => address,
ConnectedPoint::Listener { local_addr, .. } => local_addr,
}
.iter()
.any(|p| p == Protocol::P2pCircuit)
}

/// Returns the address of the remote stored in this struct.
///
/// For `Dialer`, this returns `address`. For `Listener`, this returns `send_back_addr`.
Expand Down

0 comments on commit 176636f

Please sign in to comment.