refactor(swarm)!: deprecate PollParameters where possible (#3153)

This patch deprecates 3 out of 4 functions on `PollParameters`:

- `local_peer_id`
- `listened_addresses`
- `external_addresses`

The addresses can be obtained by inspecting the `FromSwarm` event. To make this easier, we introduce two utility structs in `libp2p-swarm`:

- `ExternalAddresses`
- `ListenAddresses`

A node's `PeerId` is always known to the caller, thus we can require them to pass it in.

Related: #3124.
This commit is contained in:
Thomas Eizinger
2022-12-14 11:50:08 +11:00
committed by GitHub
parent 5fe0dc44bd
commit be3ec6c62b
28 changed files with 282 additions and 112 deletions

View File

@ -32,8 +32,8 @@ use libp2p_core::multiaddr::Protocol;
use libp2p_core::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::{
ConnectionHandlerUpgrErr, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters,
ConnectionHandlerUpgrErr, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction,
NotifyHandler, PollParameters,
};
use std::collections::{hash_map, HashMap, HashSet, VecDeque};
use std::num::NonZeroU32;
@ -201,6 +201,8 @@ pub struct Relay {
/// Queue of actions to return when polled.
queued_actions: VecDeque<Action>,
external_addresses: ExternalAddresses,
}
impl Relay {
@ -211,6 +213,7 @@ impl Relay {
reservations: Default::default(),
circuits: Default::default(),
queued_actions: Default::default(),
external_addresses: Default::default(),
}
}
@ -263,6 +266,8 @@ impl NetworkBehaviour for Relay {
}
fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
self.external_addresses.on_swarn_event(&event);
match event {
FromSwarm::ConnectionClosed(connection_closed) => {
self.on_connection_closed(connection_closed)
@ -639,10 +644,10 @@ impl NetworkBehaviour for Relay {
fn poll(
&mut self,
_cx: &mut Context<'_>,
poll_parameters: &mut impl PollParameters,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
if let Some(action) = self.queued_actions.pop_front() {
return Poll::Ready(action.build(poll_parameters));
return Poll::Ready(action.build(self.local_peer_id, &self.external_addresses));
}
Poll::Pending
@ -760,7 +765,8 @@ impl From<NetworkBehaviourAction<Event, handler::Prototype>> for Action {
impl Action {
fn build(
self,
poll_parameters: &mut impl PollParameters,
local_peer_id: PeerId,
external_addresses: &ExternalAddresses,
) -> NetworkBehaviourAction<Event, handler::Prototype> {
match self {
Action::Done(action) => action,
@ -773,15 +779,13 @@ impl Action {
peer_id,
event: Either::Left(handler::In::AcceptReservationReq {
inbound_reservation_req,
addrs: poll_parameters
.external_addresses()
.map(|a| a.addr)
addrs: external_addresses
.iter()
.cloned()
// Add local peer ID in case it isn't present yet.
.filter_map(|a| match a.iter().last()? {
Protocol::P2p(_) => Some(a),
_ => Some(
a.with(Protocol::P2p(*poll_parameters.local_peer_id().as_ref())),
),
_ => Some(a.with(Protocol::P2p(local_peer_id.into()))),
})
.collect(),
}),