swarm/src/behaviour: Merge inject_* paired methods (#2445)

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Divma
2022-02-09 10:08:28 -05:00
committed by GitHub
parent 5a95a46cd3
commit dc8433e3fc
30 changed files with 583 additions and 481 deletions

View File

@ -662,12 +662,17 @@ where
u32::try_from(other_established_connection_ids.len() + 1).unwrap(),
)
.expect("n + 1 is always non-zero; qed");
let non_banned_established = other_established_connection_ids
.into_iter()
.filter(|conn_id| !this.banned_peer_connections.contains(&conn_id))
.count();
log::debug!(
"Connection established: {:?} {:?}; Total (peer): {}.",
"Connection established: {:?} {:?}; Total (peer): {}. Total non-banned (peer): {}",
connection.peer_id(),
connection.endpoint(),
num_established
num_established,
non_banned_established + 1,
);
let endpoint = connection.endpoint().clone();
let failed_addresses = concurrent_dial_errors
@ -678,16 +683,8 @@ where
&connection.id(),
&endpoint,
failed_addresses.as_ref(),
non_banned_established,
);
// The peer is not banned, but there could be previous banned connections
// if the peer was just unbanned. Check if this is the first non-banned
// connection.
let first_non_banned = other_established_connection_ids
.into_iter()
.all(|conn_id| this.banned_peer_connections.contains(&conn_id));
if first_non_banned {
this.behaviour.inject_connected(&peer_id);
}
return Poll::Ready(SwarmEvent::ConnectionEstablished {
peer_id,
num_established,
@ -723,22 +720,17 @@ where
u32::try_from(remaining_established_connection_ids.len()).unwrap();
let conn_was_reported = !this.banned_peer_connections.remove(&id);
if conn_was_reported {
let remaining_non_banned = remaining_established_connection_ids
.into_iter()
.filter(|conn_id| !this.banned_peer_connections.contains(&conn_id))
.count();
this.behaviour.inject_connection_closed(
&peer_id,
&id,
&endpoint,
handler.into_protocols_handler(),
remaining_non_banned,
);
// This connection was reported as open to the behaviour. Check if this is
// the last non-banned connection for the peer.
let last_non_banned = remaining_established_connection_ids
.into_iter()
.all(|conn_id| this.banned_peer_connections.contains(&conn_id));
if last_non_banned {
this.behaviour.inject_disconnected(&peer_id)
}
}
return Poll::Ready(SwarmEvent::ConnectionClosed {
peer_id,
@ -1534,9 +1526,10 @@ mod tests {
[swarm1, swarm2]
.iter()
.all(|s| s.behaviour.inject_connection_closed.len() == num_connections)
&& [swarm1, swarm2]
.iter()
.all(|s| s.behaviour.inject_disconnected.len() == 1)
&& [swarm1, swarm2].iter().all(|s| {
let (.., last_remaining) = s.behaviour.inject_connection_closed.last().unwrap();
*last_remaining == 0
})
}
/// Establishes multiple connections between two peers,
@ -1862,12 +1855,16 @@ mod tests {
}
State::Disconnecting => {
for s in &[&swarm1, &swarm2] {
assert_eq!(s.behaviour.inject_disconnected.len(), 0);
assert!(s
.behaviour
.inject_connection_closed
.iter()
.all(|(.., remaining_conns)| *remaining_conns > 0));
assert_eq!(
s.behaviour.inject_connection_established.len(),
num_connections
);
assert_eq!(s.behaviour.inject_connected.len(), 1);
s.behaviour.assert_connected(num_connections, 1);
}
if [&swarm1, &swarm2]
.iter()