Skip to content
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

chore(libp2p): expose Behaviour::connected and Behaviour::addresses #4101

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ impl Behaviour {
self.confidence
}

// Connected peers with the observed address of each connection.
// If the endpoint of a connection is relayed or not global (in case of Config::only_global_ips),
// the observed address is `None`.
pub fn connected(&self) -> HashMap<PeerId, HashMap<ConnectionId, Option<Multiaddr>>> {
self.connected.clone()
}

/// Add a peer to the list over servers that may be used for probes.
/// These peers are used for dial-request even if they are currently not connection, in which case a connection will be
/// establish before sending the dial-request.
Expand Down
5 changes: 5 additions & 0 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ impl Behaviour {
}
}

/// For each peer we're connected to, the observed address to send back to it.
pub fn connected(&self) -> HashMap<PeerId, HashMap<ConnectionId, Multiaddr>> {
self.connected.clone()
}

/// Initiates an active push of the local peer information to the given peers.
pub fn push<I>(&mut self, peers: I)
where
Expand Down
22 changes: 21 additions & 1 deletion protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,17 @@ where
{
Self::with_codec(TCodec::default(), protocols, cfg)
}

/// The currently connected peers, their pending outbound and inbound responses and their known,
/// reachable addresses, if any.
pub fn connected(&self) -> HashMap<PeerId, SmallVec<[Connection; 2]>> {
self.connected.clone()
}

/// Externally managed addresses via `add_address` and `remove_address`.
pub fn addresses(&self) -> HashMap<PeerId, SmallVec<[Multiaddr; 6]>> {
self.addresses.clone()
}
}

impl<TCodec> Behaviour<TCodec>
Expand Down Expand Up @@ -922,7 +933,8 @@ where
const EMPTY_QUEUE_SHRINK_THRESHOLD: usize = 100;

/// Internal information tracked for an established connection.
struct Connection {
#[derive(Clone)]
pub struct Connection {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This had to be pub for:

/// The currently connected peers, their pending outbound and inbound responses and their known,
/// reachable addresses, if any.
pub fn connected(&self) -> HashMap<PeerId, SmallVec<[Connection; 2]>> {
self.connected.clone()
}

id: ConnectionId,
address: Option<Multiaddr>,
/// Pending outbound responses where corresponding inbound requests have
Expand All @@ -943,4 +955,12 @@ impl Connection {
pending_inbound_responses: Default::default(),
}
}

pub fn id(&self) -> ConnectionId {
self.id
}

pub fn address(&self) -> Option<Multiaddr> {
self.address.clone()
}
}