refactor(swarm): remove deprecated inject calls (#3264)

Finishes work first started with https://github.com/libp2p/rust-libp2p/issues/2832
This commit is contained in:
João Oliveira
2023-01-12 11:21:02 +00:00
committed by GitHub
parent 3cc824796d
commit 4c65c7d7c7
23 changed files with 1177 additions and 1376 deletions

View File

@ -101,8 +101,10 @@ pub mod derive_prelude {
}
pub use behaviour::{
CloseConnection, ExternalAddresses, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction,
NotifyHandler, PollParameters,
AddressChange, CloseConnection, ConnectionClosed, DialFailure, ExpiredExternalAddr,
ExpiredListenAddr, ExternalAddresses, FromSwarm, ListenAddresses, ListenFailure,
ListenerClosed, ListenerError, NetworkBehaviour, NetworkBehaviourAction, NewExternalAddr,
NewListenAddr, NotifyHandler, PollParameters,
};
pub use connection::pool::{ConnectionCounters, ConnectionLimits};
pub use connection::{
@ -462,8 +464,10 @@ where
/// Depending on the underlying transport, one listener may have multiple listening addresses.
pub fn listen_on(&mut self, addr: Multiaddr) -> Result<ListenerId, TransportError<io::Error>> {
let id = self.transport.listen_on(addr)?;
#[allow(deprecated)]
self.behaviour.inject_new_listener(id);
self.behaviour
.on_swarm_event(FromSwarm::NewListener(behaviour::NewListener {
listener_id: id,
}));
Ok(id)
}
@ -487,9 +491,9 @@ where
/// # use libp2p_swarm::dummy;
/// #
/// let mut swarm = Swarm::without_executor(
/// DummyTransport::new().boxed(),
/// dummy::Behaviour,
/// PeerId::random(),
/// DummyTransport::new().boxed(),
/// dummy::Behaviour,
/// PeerId::random(),
/// );
///
/// // Dial a known peer.
@ -524,8 +528,12 @@ where
if !should_dial {
let e = DialError::DialPeerConditionFalse(condition);
#[allow(deprecated)]
self.behaviour.inject_dial_failure(peer_id, handler, &e);
self.behaviour
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
peer_id,
handler,
error: &e,
}));
return Err(e);
}
@ -534,9 +542,13 @@ where
// Check if peer is banned.
if self.banned_peers.contains(&peer_id) {
let error = DialError::Banned;
#[allow(deprecated)]
self.behaviour
.inject_dial_failure(Some(peer_id), handler, &error);
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
peer_id: Some(peer_id),
handler,
error: &error,
}));
return Err(error);
}
}
@ -558,8 +570,12 @@ where
if addresses.is_empty() {
let error = DialError::NoAddresses;
#[allow(deprecated)]
self.behaviour.inject_dial_failure(peer_id, handler, &error);
self.behaviour
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
peer_id,
handler,
error: &error,
}));
return Err(error);
};
@ -599,8 +615,13 @@ where
Ok(_connection_id) => Ok(()),
Err((connection_limit, handler)) => {
let error = DialError::ConnectionLimit(connection_limit);
#[allow(deprecated)]
self.behaviour.inject_dial_failure(peer_id, handler, &error);
self.behaviour
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
peer_id,
handler,
error: &error,
}));
Err(error)
}
}
@ -641,15 +662,17 @@ where
let result = self.external_addrs.add(a.clone(), s);
let expired = match &result {
AddAddressResult::Inserted { expired } => {
#[allow(deprecated)]
self.behaviour.inject_new_external_addr(&a);
self.behaviour
.on_swarm_event(FromSwarm::NewExternalAddr(NewExternalAddr { addr: &a }));
expired
}
AddAddressResult::Updated { expired } => expired,
};
for a in expired {
#[allow(deprecated)]
self.behaviour.inject_expired_external_addr(&a.addr);
self.behaviour
.on_swarm_event(FromSwarm::ExpiredExternalAddr(ExpiredExternalAddr {
addr: &a.addr,
}));
}
result
}
@ -662,8 +685,8 @@ where
/// otherwise.
pub fn remove_external_address(&mut self, addr: &Multiaddr) -> bool {
if self.external_addrs.remove(addr) {
#[allow(deprecated)]
self.behaviour.inject_expired_external_addr(addr);
self.behaviour
.on_swarm_event(FromSwarm::ExpiredExternalAddr(ExpiredExternalAddr { addr }));
true
} else {
false
@ -769,15 +792,23 @@ where
);
let failed_addresses = concurrent_dial_errors
.as_ref()
.map(|es| es.iter().map(|(a, _)| a).cloned().collect());
#[allow(deprecated)]
self.behaviour.inject_connection_established(
&peer_id,
&id,
&endpoint,
failed_addresses.as_ref(),
non_banned_established,
);
.map(|es| {
es.iter()
.map(|(a, _)| a)
.cloned()
.collect::<Vec<Multiaddr>>()
})
.unwrap_or_default();
self.behaviour
.on_swarm_event(FromSwarm::ConnectionEstablished(
behaviour::ConnectionEstablished {
peer_id,
connection_id: id,
endpoint: &endpoint,
failed_addresses: &failed_addresses,
other_established: non_banned_established,
},
));
return Some(SwarmEvent::ConnectionEstablished {
peer_id,
num_established,
@ -795,8 +826,12 @@ where
} => {
let error = error.into();
#[allow(deprecated)]
self.behaviour.inject_dial_failure(peer, handler, &error);
self.behaviour
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
peer_id: peer,
handler,
error: &error,
}));
if let Some(peer) = peer {
log::debug!("Connection attempt to {:?} failed with {:?}.", peer, error,);
@ -817,9 +852,12 @@ where
handler,
} => {
log::debug!("Incoming connection failed: {:?}", error);
#[allow(deprecated)]
self.behaviour
.inject_listen_failure(&local_addr, &send_back_addr, handler);
.on_swarm_event(FromSwarm::ListenFailure(ListenFailure {
local_addr: &local_addr,
send_back_addr: &send_back_addr,
handler,
}));
return Some(SwarmEvent::IncomingConnectionError {
local_addr,
send_back_addr,
@ -858,14 +896,14 @@ where
.into_iter()
.filter(|conn_id| !self.banned_peer_connections.contains(conn_id))
.count();
#[allow(deprecated)]
self.behaviour.inject_connection_closed(
&peer_id,
&id,
&endpoint,
handler,
remaining_non_banned,
);
self.behaviour
.on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id,
connection_id: id,
endpoint: &endpoint,
handler,
remaining_established: remaining_non_banned,
}));
}
return Some(SwarmEvent::ConnectionClosed {
peer_id,
@ -878,8 +916,8 @@ where
if self.banned_peer_connections.contains(&id) {
log::debug!("Ignoring event from banned peer: {} {:?}.", peer_id, id);
} else {
#[allow(deprecated)]
self.behaviour.inject_event(peer_id, id, event);
self.behaviour
.on_connection_handler_event(peer_id, id, event);
}
}
PoolEvent::AddressChange {
@ -889,13 +927,13 @@ where
old_endpoint,
} => {
if !self.banned_peer_connections.contains(&id) {
#[allow(deprecated)]
self.behaviour.inject_address_change(
&peer_id,
&id,
&old_endpoint,
&new_endpoint,
);
self.behaviour
.on_swarm_event(FromSwarm::AddressChange(AddressChange {
peer_id,
connection_id: id,
old: &old_endpoint,
new: &new_endpoint,
}));
}
}
}
@ -933,9 +971,12 @@ where
});
}
Err((connection_limit, handler)) => {
#[allow(deprecated)]
self.behaviour
.inject_listen_failure(&local_addr, &send_back_addr, handler);
.on_swarm_event(FromSwarm::ListenFailure(ListenFailure {
local_addr: &local_addr,
send_back_addr: &send_back_addr,
handler,
}));
log::warn!("Incoming connection rejected: {:?}", connection_limit);
}
};
@ -949,9 +990,11 @@ where
if !addrs.contains(&listen_addr) {
addrs.push(listen_addr.clone())
}
#[allow(deprecated)]
self.behaviour
.inject_new_listen_addr(listener_id, &listen_addr);
.on_swarm_event(FromSwarm::NewListenAddr(NewListenAddr {
listener_id,
addr: &listen_addr,
}));
return Some(SwarmEvent::NewListenAddr {
listener_id,
address: listen_addr,
@ -969,9 +1012,11 @@ where
if let Some(addrs) = self.listened_addrs.get_mut(&listener_id) {
addrs.retain(|a| a != &listen_addr);
}
#[allow(deprecated)]
self.behaviour
.inject_expired_listen_addr(listener_id, &listen_addr);
.on_swarm_event(FromSwarm::ExpiredListenAddr(ExpiredListenAddr {
listener_id,
addr: &listen_addr,
}));
return Some(SwarmEvent::ExpiredListenAddr {
listener_id,
address: listen_addr,
@ -984,17 +1029,15 @@ where
log::debug!("Listener {:?}; Closed by {:?}.", listener_id, reason);
let addrs = self.listened_addrs.remove(&listener_id).unwrap_or_default();
for addr in addrs.iter() {
#[allow(deprecated)]
self.behaviour.inject_expired_listen_addr(listener_id, addr);
self.behaviour.on_swarm_event(FromSwarm::ExpiredListenAddr(
ExpiredListenAddr { listener_id, addr },
));
}
#[allow(deprecated)]
self.behaviour.inject_listener_closed(
listener_id,
match &reason {
Ok(()) => Ok(()),
Err(err) => Err(err),
},
);
self.behaviour
.on_swarm_event(FromSwarm::ListenerClosed(ListenerClosed {
listener_id,
reason: reason.as_ref().copied(),
}));
return Some(SwarmEvent::ListenerClosed {
listener_id,
addresses: addrs.to_vec(),
@ -1002,8 +1045,11 @@ where
});
}
TransportEvent::ListenerError { listener_id, error } => {
#[allow(deprecated)]
self.behaviour.inject_listener_error(listener_id, &error);
self.behaviour
.on_swarm_event(FromSwarm::ListenerError(ListenerError {
listener_id,
err: &error,
}));
return Some(SwarmEvent::ListenerError { listener_id, error });
}
}
@ -1797,12 +1843,12 @@ mod tests {
/// Establishes multiple connections between two peers,
/// after which one peer bans the other.
///
/// The test expects both behaviours to be notified via pairs of
/// [`NetworkBehaviour::inject_connection_established`] / [`NetworkBehaviour::inject_connection_closed`]
/// calls while unbanned.
/// The test expects both behaviours to be notified via calls to [`NetworkBehaviour::on_swarm_event`]
/// with pairs of [`FromSwarm::ConnectionEstablished`] / [`FromSwarm::ConnectionClosed`]
/// while unbanned.
///
/// While the ban is in effect, further dials occur. For these connections no
/// [`NetworkBehaviour::inject_connection_established`], [`NetworkBehaviour::inject_connection_closed`]
/// [`FromSwarm::ConnectionEstablished`], [`FromSwarm::ConnectionClosed`]
/// calls should be registered.
#[test]
fn test_connect_disconnect_ban() {
@ -1920,8 +1966,8 @@ mod tests {
/// Establishes multiple connections between two peers,
/// after which one peer disconnects the other using [`Swarm::disconnect_peer_id`].
///
/// The test expects both behaviours to be notified via pairs of
/// [`NetworkBehaviour::inject_connection_established`] / [`NetworkBehaviour::inject_connection_closed`] calls.
/// The test expects both behaviours to be notified via calls to [`NetworkBehaviour::on_swarm_event`]
/// with pairs of [`FromSwarm::ConnectionEstablished`] / [`FromSwarm::ConnectionClosed`]
#[test]
fn test_swarm_disconnect() {
// Since the test does not try to open any substreams, we can
@ -1986,8 +2032,8 @@ mod tests {
/// after which one peer disconnects the other
/// using [`NetworkBehaviourAction::CloseConnection`] returned by a [`NetworkBehaviour`].
///
/// The test expects both behaviours to be notified via pairs of
/// [`NetworkBehaviour::inject_connection_established`] / [`NetworkBehaviour::inject_connection_closed`] calls.
/// The test expects both behaviours to be notified via calls to [`NetworkBehaviour::on_swarm_event`]
/// with pairs of [`FromSwarm::ConnectionEstablished`] / [`FromSwarm::ConnectionClosed`]
#[test]
fn test_behaviour_disconnect_all() {
// Since the test does not try to open any substreams, we can
@ -2054,8 +2100,8 @@ mod tests {
/// after which one peer closes a single connection
/// using [`NetworkBehaviourAction::CloseConnection`] returned by a [`NetworkBehaviour`].
///
/// The test expects both behaviours to be notified via pairs of
/// [`NetworkBehaviour::inject_connection_established`] / [`NetworkBehaviour::inject_connection_closed`] calls.
/// The test expects both behaviours to be notified via calls to [`NetworkBehaviour::on_swarm_event`]
/// with pairs of [`FromSwarm::ConnectionEstablished`] / [`FromSwarm::ConnectionClosed`]
#[test]
fn test_behaviour_disconnect_one() {
// Since the test does not try to open any substreams, we can