mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-31 17:01:58 +00:00
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:
@@ -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},
|
||||
@@ -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 {
|
||||
@@ -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(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)),
|
||||
@@ -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
|
||||
|
@@ -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},
|
||||
@@ -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> {
|
||||
@@ -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))
|
||||
@@ -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) {
|
||||
|
Reference in New Issue
Block a user