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.
This commit is contained in:
Thomas Eizinger 2023-01-25 22:51:51 +11:00 committed by GitHub
parent 62c0532de6
commit a25ab7e444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,
}));
}