From a25ab7e4448cc176c63127519af04661e9c2743c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 25 Jan 2023 22:51:51 +1100 Subject: [PATCH] refactor(gossipsub): use dummy handler instead of calling `new_handler` (#3384) The gossipsub tests are calling lifecycle functions of the `NetworkBehaviour` that aren't meant to be called outside of `Swarm`. This already surfaced as a problem in https://github.com/libp2p/rust-libp2p/pull/3327 and it is coming up again in https://github.com/libp2p/rust-libp2p/pull/3254 where `new_handler` gets deprecated. Try to mitigate that by constructing a dummy handler instead. Functionally, there is no difference as in both cases, the given handler has never seen a connection. --- protocols/gossipsub/src/behaviour/tests.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/protocols/gossipsub/src/behaviour/tests.rs b/protocols/gossipsub/src/behaviour/tests.rs index f4fd7800..175ccad4 100644 --- a/protocols/gossipsub/src/behaviour/tests.rs +++ b/protocols/gossipsub/src/behaviour/tests.rs @@ -33,6 +33,7 @@ use async_std::net::Ipv4Addr; use byteorder::{BigEndian, ByteOrder}; use libp2p_core::{ConnectedPoint, Endpoint}; use rand::Rng; +use std::borrow::Cow; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; use std::thread::sleep; @@ -265,15 +266,21 @@ where role_override: Endpoint::Dialer, }; // this is not relevant // peer_connections.connections should never be empty. + let mut active_connections = peer_connections.connections.len(); for connection_id in peer_connections.connections.clone() { - let handler = gs.new_handler(); active_connections = active_connections.checked_sub(1).unwrap(); + + let dummy_handler = GossipsubHandler::new( + ProtocolConfig::new(Cow::from(""), None, 0, ValidationMode::None, false), + Duration::ZERO, + ); + gs.on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed { peer_id: *peer_id, connection_id, endpoint: &fake_endpoint, - handler, + handler: dummy_handler, remaining_established: active_connections, })); }