Skip to content

Commit

Permalink
refactor(swarm)!: deprecate PollParameters where possible (libp2p#3153
Browse files Browse the repository at this point in the history
)

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: libp2p#3124.
  • Loading branch information
thomaseizinger authored Dec 14, 2022
1 parent 369337e commit 76592e0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# 0.10.0 [unreleased]

- Require the node's local `PeerId` to be passed into the constructor of `libp2p_autonat::Behaviour`. See [PR 3153].

- Update to `libp2p-request-response` `v0.24.0`.

- Update to `libp2p-swarm` `v0.42.0`.

[PR 3153]: https://github.com/libp2p/rust-libp2p/pull/3153

# 0.9.0

- Update to `libp2p-core` `v0.38.0`.
Expand Down
16 changes: 13 additions & 3 deletions src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use libp2p_swarm::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr,
ExpiredListenAddr, FromSwarm,
},
ConnectionHandler, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction,
PollParameters,
ConnectionHandler, ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour,
NetworkBehaviourAction, PollParameters,
};
use std::{
collections::{HashMap, VecDeque},
Expand Down Expand Up @@ -212,6 +212,9 @@ pub struct Behaviour {
pending_out_events: VecDeque<<Self as NetworkBehaviour>::OutEvent>,

probe_id: ProbeId,

listen_addresses: ListenAddresses,
external_addresses: ExternalAddresses,
}

impl Behaviour {
Expand All @@ -236,6 +239,8 @@ impl Behaviour {
last_probe: None,
pending_out_events: VecDeque::new(),
probe_id: ProbeId(0),
listen_addresses: Default::default(),
external_addresses: Default::default(),
}
}

Expand Down Expand Up @@ -288,6 +293,8 @@ impl Behaviour {
ongoing_outbound: &mut self.ongoing_outbound,
last_probe: &mut self.last_probe,
schedule_probe: &mut self.schedule_probe,
listen_addresses: &self.listen_addresses,
external_addresses: &self.external_addresses,
}
}

Expand Down Expand Up @@ -457,7 +464,7 @@ impl NetworkBehaviour for Behaviour {
Poll::Pending => is_inner_pending = true,
}

match self.as_client().poll_auto_probe(params, cx) {
match self.as_client().poll_auto_probe(cx) {
Poll::Ready(event) => self
.pending_out_events
.push_back(Event::OutboundProbe(event)),
Expand All @@ -476,6 +483,9 @@ impl NetworkBehaviour for Behaviour {
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
self.listen_addresses.on_swarm_event(&event);
self.external_addresses.on_swarn_event(&event);

match event {
FromSwarm::ConnectionEstablished(connection_established) => {
self.inner
Expand Down
23 changes: 15 additions & 8 deletions src/behaviour/as_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use futures_timer::Delay;
use instant::Instant;
use libp2p_core::{connection::ConnectionId, Multiaddr, PeerId};
use libp2p_request_response::{self as request_response, OutboundFailure, RequestId};
use libp2p_swarm::{AddressScore, NetworkBehaviourAction, PollParameters};
use libp2p_swarm::{
AddressScore, ExternalAddresses, ListenAddresses, NetworkBehaviourAction, PollParameters,
};
use rand::{seq::SliceRandom, thread_rng};
use std::{
collections::{HashMap, VecDeque},
Expand Down Expand Up @@ -97,6 +99,9 @@ pub struct AsClient<'a> {

pub last_probe: &'a mut Option<Instant>,
pub schedule_probe: &'a mut Delay,

pub listen_addresses: &'a ListenAddresses,
pub external_addresses: &'a ExternalAddresses,
}

impl<'a> HandleInnerEvent for AsClient<'a> {
Expand Down Expand Up @@ -146,6 +151,8 @@ impl<'a> HandleInnerEvent for AsClient<'a> {

if let Ok(address) = response.result {
// Update observed address score if it is finite.
#[allow(deprecated)]
// TODO: Fix once we report `AddressScore` through `FromSwarm` event.
let score = params
.external_addresses()
.find_map(|r| (r.addr == address).then_some(r.score))
Expand Down Expand Up @@ -188,17 +195,17 @@ impl<'a> HandleInnerEvent for AsClient<'a> {
}

impl<'a> AsClient<'a> {
pub fn poll_auto_probe(
&mut self,
params: &mut impl PollParameters,
cx: &mut Context<'_>,
) -> Poll<OutboundProbeEvent> {
pub fn poll_auto_probe(&mut self, cx: &mut Context<'_>) -> Poll<OutboundProbeEvent> {
match self.schedule_probe.poll_unpin(cx) {
Poll::Ready(()) => {
self.schedule_probe.reset(self.config.retry_interval);

let mut addresses: Vec<_> = params.external_addresses().map(|r| r.addr).collect();
addresses.extend(params.listened_addresses());
let addresses = self
.external_addresses
.iter()
.chain(self.listen_addresses.iter())
.cloned()
.collect();

let probe_id = self.probe_id.next();
let event = match self.do_probe(probe_id, addresses) {
Expand Down

0 comments on commit 76592e0

Please sign in to comment.