diff --git a/crates/papyrus_network/src/network_manager/mod.rs b/crates/papyrus_network/src/network_manager/mod.rs index 37cde03895..ffd64ce0ea 100644 --- a/crates/papyrus_network/src/network_manager/mod.rs +++ b/crates/papyrus_network/src/network_manager/mod.rs @@ -78,8 +78,8 @@ impl GenericNetworkManager { Some((topic_hash, message)) = self.messages_to_broadcast_receivers.next() => { self.broadcast_message(message, topic_hash); } - Some(Some(peer_id)) = self.reported_peer_receivers.next() => self.swarm.report_peer(peer_id), - Some(peer_id) = self.reported_peers_receiver.next() => self.swarm.report_peer(peer_id), + Some(Some(peer_id)) = self.reported_peer_receivers.next() => self.swarm.report_peer_as_malicious(peer_id), + Some(peer_id) = self.reported_peers_receiver.next() => self.swarm.report_peer_as_malicious(peer_id), Some(broadcasted_message_manager) = self.continue_propagation_receiver.next() => { self.swarm.continue_propagation(broadcasted_message_manager); } @@ -721,6 +721,7 @@ pub struct ClientResponsesManager> { } impl> ClientResponsesManager { + /// Use this function to report peer as malicious pub fn report_peer(self) { warn!("Reporting peer"); if let Err(e) = self.report_sender.send(()) { diff --git a/crates/papyrus_network/src/network_manager/swarm_trait.rs b/crates/papyrus_network/src/network_manager/swarm_trait.rs index d0ef0cd81b..bcafa027fb 100644 --- a/crates/papyrus_network/src/network_manager/swarm_trait.rs +++ b/crates/papyrus_network/src/network_manager/swarm_trait.rs @@ -50,7 +50,7 @@ pub trait SwarmTrait: Stream + Unpin { fn broadcast_message(&mut self, message: Bytes, topic_hash: TopicHash); - fn report_peer(&mut self, peer_id: PeerId); + fn report_peer_as_malicious(&mut self, peer_id: PeerId); fn add_new_supported_inbound_protocol(&mut self, protocol_name: StreamProtocol); @@ -125,8 +125,9 @@ impl SwarmTrait for Swarm { } } - fn report_peer(&mut self, peer_id: PeerId) { - let _ = self.behaviour_mut().peer_manager.report_peer(peer_id, ReputationModifier::Bad {}); + fn report_peer_as_malicious(&mut self, peer_id: PeerId) { + let _ = + self.behaviour_mut().peer_manager.report_peer(peer_id, ReputationModifier::Malicious); } fn add_new_supported_inbound_protocol(&mut self, protocol: StreamProtocol) { diff --git a/crates/papyrus_network/src/network_manager/test.rs b/crates/papyrus_network/src/network_manager/test.rs index 1ea404122e..cc42d54f90 100644 --- a/crates/papyrus_network/src/network_manager/test.rs +++ b/crates/papyrus_network/src/network_manager/test.rs @@ -185,7 +185,7 @@ impl SwarmTrait for MockSwarm { } } - fn report_peer(&mut self, peer_id: PeerId) { + fn report_peer_as_malicious(&mut self, peer_id: PeerId) { for sender in &self.reported_peer_senders { sender.unbounded_send(peer_id).unwrap(); } diff --git a/crates/papyrus_network/src/peer_manager/behaviour_impl.rs b/crates/papyrus_network/src/peer_manager/behaviour_impl.rs index 82fa9317bc..fec23b9fc1 100644 --- a/crates/papyrus_network/src/peer_manager/behaviour_impl.rs +++ b/crates/papyrus_network/src/peer_manager/behaviour_impl.rs @@ -105,7 +105,7 @@ where ); return; } - let res = self.report_peer(peer_id, super::ReputationModifier::Bad); + let res = self.report_peer(peer_id, super::ReputationModifier::Unstable); if res.is_err() { error!("Dial failure of an unknown peer. peer id: {}", peer_id) } diff --git a/crates/papyrus_network/src/peer_manager/mod.rs b/crates/papyrus_network/src/peer_manager/mod.rs index efa4bebb92..c4e5733f3b 100644 --- a/crates/papyrus_network/src/peer_manager/mod.rs +++ b/crates/papyrus_network/src/peer_manager/mod.rs @@ -21,9 +21,10 @@ pub(crate) mod peer; mod test; #[cfg_attr(test, derive(Debug, PartialEq))] +#[derive(Clone, Copy)] pub enum ReputationModifier { - // TODO: Implement this enum - Bad, + Malicious, + Unstable, } pub struct PeerManager { @@ -41,7 +42,8 @@ pub struct PeerManager { #[derive(Clone)] pub struct PeerManagerConfig { - blacklist_timeout: Duration, + malicious_timeout: Duration, + unstable_timeout: Duration, } #[derive(thiserror::Error, Debug)] @@ -58,7 +60,8 @@ impl Default for PeerManagerConfig { fn default() -> Self { Self { // 1 year. - blacklist_timeout: Duration::from_secs(3600 * 24 * 365), + malicious_timeout: Duration::from_secs(3600 * 24 * 365), + unstable_timeout: Duration::from_secs(1), } } } @@ -82,9 +85,8 @@ where } } - fn add_peer(&mut self, mut peer: P) { + fn add_peer(&mut self, peer: P) { info!("Peer Manager found new peer {:?}", peer.peer_id()); - peer.set_timeout_duration(self.config.blacklist_timeout); self.peers.insert(peer.peer_id(), peer); // The new peer is unblocked so we don't need to wait for unblocked peer. self.sleep_waiting_for_unblocked_peer = None; @@ -184,7 +186,10 @@ where self.pending_events .push(ToSwarm::GenerateEvent(ToOtherBehaviourEvent::PeerBlacklisted { peer_id })); if let Some(peer) = self.peers.get_mut(&peer_id) { - peer.update_reputation(reason); + peer.update_reputation(match reason { + ReputationModifier::Malicious => self.config.malicious_timeout, + ReputationModifier::Unstable => self.config.unstable_timeout, + }); Ok(()) } else { Err(PeerManagerError::NoSuchPeer(peer_id)) @@ -198,7 +203,10 @@ where ) -> Result<(), PeerManagerError> { if let Some(peer_id) = self.session_to_peer_map.get(&outbound_session_id) { if let Some(peer) = self.peers.get_mut(peer_id) { - peer.update_reputation(reason); + peer.update_reputation(match reason { + ReputationModifier::Malicious => self.config.malicious_timeout, + ReputationModifier::Unstable => self.config.unstable_timeout, + }); Ok(()) } else { Err(PeerManagerError::NoSuchPeer(*peer_id)) diff --git a/crates/papyrus_network/src/peer_manager/peer.rs b/crates/papyrus_network/src/peer_manager/peer.rs index e797a321d9..74d56f27a5 100644 --- a/crates/papyrus_network/src/peer_manager/peer.rs +++ b/crates/papyrus_network/src/peer_manager/peer.rs @@ -2,24 +2,17 @@ use std::time::{Duration, Instant}; use libp2p::swarm::ConnectionId; use libp2p::{Multiaddr, PeerId}; -#[cfg(test)] -use mockall::automock; -use tracing::{error, info}; - -use super::ReputationModifier; +use tracing::info; -#[cfg_attr(test, automock)] pub trait PeerTrait { fn new(peer_id: PeerId, multiaddr: Multiaddr) -> Self; - fn update_reputation(&mut self, reason: ReputationModifier); + fn update_reputation(&mut self, timeout_duration: Duration); fn peer_id(&self) -> PeerId; fn multiaddr(&self) -> Multiaddr; - fn set_timeout_duration(&mut self, duration: Duration); - fn is_blocked(&self) -> bool; /// Returns Instant::now if not blocked. @@ -36,36 +29,22 @@ pub trait PeerTrait { pub struct Peer { peer_id: PeerId, multiaddr: Multiaddr, - timed_out_until: Option, - timeout_duration: Option, + timed_out_until: Instant, connection_ids: Vec, } impl PeerTrait for Peer { fn new(peer_id: PeerId, multiaddr: Multiaddr) -> Self { - Self { - peer_id, - multiaddr, - timeout_duration: None, - timed_out_until: None, - connection_ids: Vec::new(), - } + Self { peer_id, multiaddr, timed_out_until: get_instant_now(), connection_ids: Vec::new() } } - fn update_reputation(&mut self, _reason: ReputationModifier) { - if let Some(timeout_duration) = self.timeout_duration { - info!( - "Peer {:?} misbehaved. Blacklisting it for {:.3} seconds.", - self.peer_id, - timeout_duration.as_secs_f64(), - ); - self.timed_out_until = Some(Instant::now() + timeout_duration); - } else { - error!( - "Peer {:?} misbehaved but its timeout duration wasn't set. Not doing anything.", - self.peer_id - ); - } + fn update_reputation(&mut self, timeout_duration: Duration) { + self.timed_out_until = get_instant_now() + timeout_duration; + info!( + "Peer {:?} misbehaved. Blacklisting it for {:.3} seconds.", + self.peer_id, + timeout_duration.as_secs_f64(), + ); } fn peer_id(&self) -> PeerId { @@ -76,20 +55,16 @@ impl PeerTrait for Peer { self.multiaddr.clone() } - fn set_timeout_duration(&mut self, duration: Duration) { - self.timeout_duration = Some(duration); - } - fn is_blocked(&self) -> bool { - if let Some(timed_out_until) = self.timed_out_until { - timed_out_until > Instant::now() - } else { - false - } + self.timed_out_until > get_instant_now() } fn blocked_until(&self) -> Instant { - self.timed_out_until.unwrap_or_else(Instant::now) + if self.timed_out_until > get_instant_now() { + self.timed_out_until + } else { + get_instant_now() + } } fn connection_ids(&self) -> &Vec { @@ -104,3 +79,14 @@ impl PeerTrait for Peer { self.connection_ids.retain(|&id| id != connection_id); } } + +#[cfg(not(test))] +fn get_instant_now() -> Instant { + Instant::now() +} + +// In tests we simulate time passing using tokio, so we need to use tokio's Instant instead of std. +#[cfg(test)] +fn get_instant_now() -> Instant { + tokio::time::Instant::now().into_std() +} diff --git a/crates/papyrus_network/src/peer_manager/test.rs b/crates/papyrus_network/src/peer_manager/test.rs index 5c2c96f469..7719784fb7 100644 --- a/crates/papyrus_network/src/peer_manager/test.rs +++ b/crates/papyrus_network/src/peer_manager/test.rs @@ -3,7 +3,7 @@ use core::{panic, time}; use std::pin::Pin; use std::task::{Context, Poll}; -use std::time::{Duration, Instant}; +use std::time::Duration; use assert_matches::assert_matches; use futures::future::poll_fn; @@ -11,7 +11,6 @@ use futures::{FutureExt, Stream, StreamExt}; use libp2p::swarm::behaviour::ConnectionEstablished; use libp2p::swarm::{ConnectionId, NetworkBehaviour, ToSwarm}; use libp2p::{Multiaddr, PeerId}; -use mockall::predicate::eq; use tokio::time::sleep; use void::Void; @@ -19,7 +18,7 @@ use super::behaviour_impl::ToOtherBehaviourEvent; use crate::discovery::identify_impl::IdentifyToOtherBehaviourEvent; use crate::mixed_behaviour; use crate::mixed_behaviour::BridgedBehaviour; -use crate::peer_manager::peer::{MockPeerTrait, Peer, PeerTrait}; +use crate::peer_manager::peer::{Peer, PeerTrait}; use crate::peer_manager::{PeerManager, PeerManagerConfig, ReputationModifier}; use crate::sqmr::OutboundSessionId; @@ -74,12 +73,12 @@ fn peer_assignment_round_robin() { } // check assignment events - for event in peer_manager.pending_events { - let ToSwarm::GenerateEvent(ToOtherBehaviourEvent::SessionAssigned { + while let Some(event) = peer_manager.next().now_or_never() { + let Some(ToSwarm::GenerateEvent(ToOtherBehaviourEvent::SessionAssigned { outbound_session_id, peer_id, connection_id, - }) = event + })) = event else { continue; }; @@ -123,7 +122,7 @@ fn peer_assignment_round_robin() { async fn peer_assignment_no_peers() { // Create a new peer manager let config = PeerManagerConfig::default(); - let mut peer_manager: PeerManager = PeerManager::new(config.clone()); + let mut peer_manager: PeerManager = PeerManager::new(config.clone()); // Create a session let outbound_session_id = OutboundSessionId { value: 1 }; @@ -134,9 +133,9 @@ async fn peer_assignment_no_peers() { // Now the peer manager finds a new peer and can assign the session. let connection_id = ConnectionId::new_unchecked(0); - let (mut peer, peer_id) = - create_mock_peer(config.blacklist_timeout, false, Some(connection_id)); - peer.expect_is_blocked().times(1).return_const(false); + let peer_id = PeerId::random(); + let mut peer = Peer::new(peer_id, Multiaddr::empty()); + peer.add_connection_id(connection_id); peer_manager.add_peer(peer); assert_matches!( peer_manager.next().await.unwrap(), @@ -157,21 +156,20 @@ async fn peer_assignment_no_unblocked_peers() { const BLOCKED_UNTIL: Duration = Duration::from_secs(5); const TIMEOUT: Duration = Duration::from_secs(1); // Create a new peer manager - let config = PeerManagerConfig::default(); - let mut peer_manager: PeerManager = PeerManager::new(config.clone()); + let config = PeerManagerConfig { malicious_timeout: TIMEOUT, unstable_timeout: TIMEOUT }; + let mut peer_manager: PeerManager = PeerManager::new(config.clone()); // Create a session let outbound_session_id = OutboundSessionId { value: 1 }; // Create a peer let connection_id = ConnectionId::new_unchecked(0); - let (mut peer, peer_id) = create_mock_peer(config.blacklist_timeout, true, Some(connection_id)); - peer.expect_is_blocked().times(1).return_const(true); - peer.expect_is_blocked().times(1).return_const(false); - peer.expect_blocked_until().times(1).returning(|| Instant::now() + BLOCKED_UNTIL); + let peer_id = PeerId::random(); + let mut peer = Peer::new(peer_id, Multiaddr::empty()); + peer.add_connection_id(connection_id); peer_manager.add_peer(peer); - peer_manager.report_peer(peer_id, ReputationModifier::Bad {}).unwrap(); + peer_manager.report_peer(peer_id, ReputationModifier::Unstable).unwrap(); // Consume the peer blacklisted event let event = tokio::time::timeout(TIMEOUT, peer_manager.next()).await.unwrap().unwrap(); @@ -210,17 +208,17 @@ async fn peer_assignment_no_unblocked_peers() { fn report_peer_calls_update_reputation_and_notifies_kad() { // Create a new peer manager let config = PeerManagerConfig::default(); - let mut peer_manager: PeerManager = PeerManager::new(config.clone()); + let mut peer_manager: PeerManager = PeerManager::new(config.clone()); - // Create a mock peer - let (peer, peer_id) = create_mock_peer(config.blacklist_timeout, true, None); + // Create a peer + let peer_id = PeerId::random(); + let peer = Peer::new(peer_id, Multiaddr::empty()); - // Add the mock peer to the peer manager peer_manager.add_peer(peer); // Call the report_peer function on the peer manager - peer_manager.report_peer(peer_id, ReputationModifier::Bad {}).unwrap(); - peer_manager.get_mut_peer(peer_id).unwrap().checkpoint(); + peer_manager.report_peer(peer_id, ReputationModifier::Unstable).unwrap(); + peer_manager.get_mut_peer(peer_id).unwrap(); // Validate that we have an event to notify Kademlia assert_eq!(peer_manager.pending_events.len(), 1); @@ -232,11 +230,10 @@ fn report_peer_calls_update_reputation_and_notifies_kad() { } #[tokio::test] -async fn peer_block_realeased_after_timeout() { +async fn peer_block_released_after_timeout() { const DURATION_IN_MILLIS: u64 = 50; let mut peer = Peer::new(PeerId::random(), Multiaddr::empty()); - peer.set_timeout_duration(Duration::from_millis(DURATION_IN_MILLIS)); - peer.update_reputation(ReputationModifier::Bad {}); + peer.update_reputation(Duration::from_millis(DURATION_IN_MILLIS)); assert!(peer.is_blocked()); sleep(time::Duration::from_millis(DURATION_IN_MILLIS)).await; assert!(!peer.is_blocked()); @@ -245,13 +242,12 @@ async fn peer_block_realeased_after_timeout() { #[test] fn report_peer_on_unknown_peer_id() { // Create a new peer manager - let mut peer_manager: PeerManager = - PeerManager::new(PeerManagerConfig::default()); + let mut peer_manager: PeerManager = PeerManager::new(PeerManagerConfig::default()); // report peer on an unknown peer_id let peer_id = PeerId::random(); peer_manager - .report_peer(peer_id, ReputationModifier::Bad {}) + .report_peer(peer_id, ReputationModifier::Unstable {}) .expect_err("report_peer on unknown peer_id should return an error"); } @@ -259,14 +255,14 @@ fn report_peer_on_unknown_peer_id() { fn report_session_calls_update_reputation() { // Create a new peer manager let config = PeerManagerConfig::default(); - let mut peer_manager: PeerManager = PeerManager::new(config.clone()); + let mut peer_manager: PeerManager = PeerManager::new(config.clone()); - // Create a mock peer - let (mut peer, peer_id) = - create_mock_peer(config.blacklist_timeout, true, Some(ConnectionId::new_unchecked(0))); - peer.expect_is_blocked().times(1).return_const(false); + // Create a peer + let peer_id = PeerId::random(); + let mut peer = Peer::new(peer_id, Multiaddr::empty()); + peer.add_connection_id(ConnectionId::new_unchecked(0)); - // Add the mock peer to the peer manager + // Add the peer to the peer manager peer_manager.add_peer(peer); // Create a session @@ -277,21 +273,20 @@ fn report_session_calls_update_reputation() { assert_eq!(res_peer_id, peer_id); // Call the report_peer function on the peer manager - peer_manager.report_session(outbound_session_id, ReputationModifier::Bad {}).unwrap(); - peer_manager.get_mut_peer(peer_id).unwrap().checkpoint(); + peer_manager.report_session(outbound_session_id, ReputationModifier::Unstable {}).unwrap(); + peer_manager.get_mut_peer(peer_id).unwrap(); } #[test] fn report_session_on_unknown_session_id() { // Create a new peer manager - let mut peer_manager: PeerManager = - PeerManager::new(PeerManagerConfig::default()); + let mut peer_manager: PeerManager = PeerManager::new(PeerManagerConfig::default()); // Create a session let outbound_session_id = OutboundSessionId { value: 1 }; peer_manager - .report_session(outbound_session_id, ReputationModifier::Bad {}) + .report_session(outbound_session_id, ReputationModifier::Malicious {}) .expect_err("report_session on unknown outbound_session_id should return an error"); } @@ -299,18 +294,17 @@ fn report_session_on_unknown_session_id() { async fn timed_out_peer_not_assignable_to_queries() { // Create a new peer manager let config = PeerManagerConfig::default(); - let mut peer_manager: PeerManager = PeerManager::new(config.clone()); + let mut peer_manager: PeerManager = PeerManager::new(config.clone()); - // Create a mock peer - let (mut peer, peer_id) = create_mock_peer(config.blacklist_timeout, true, None); - peer.expect_is_blocked().times(1).return_const(true); - peer.expect_blocked_until().times(1).returning(|| Instant::now() + Duration::from_secs(1)); + // Create a peer + let peer_id = PeerId::random(); + let peer = Peer::new(peer_id, Multiaddr::empty()); - // Add the mock peer to the peer manager + // Add the peer to the peer manager peer_manager.add_peer(peer); // Report the peer as bad - peer_manager.report_peer(peer_id, ReputationModifier::Bad {}).unwrap(); + peer_manager.report_peer(peer_id, ReputationModifier::Malicious {}).unwrap(); // Create a session let outbound_session_id = OutboundSessionId { value: 1 }; @@ -323,25 +317,25 @@ async fn timed_out_peer_not_assignable_to_queries() { fn wrap_around_in_peer_assignment() { // Create a new peer manager let config = PeerManagerConfig::default(); - let mut peer_manager: PeerManager = PeerManager::new(config.clone()); + let mut peer_manager: PeerManager = PeerManager::new(config.clone()); - // Create a mock peer - let (mut peer1, peer_id1) = - create_mock_peer(config.blacklist_timeout, true, Some(ConnectionId::new_unchecked(0))); - peer1.expect_is_blocked().times(..2).return_const(true); + // Create a peer + let peer_id1 = PeerId::random(); + let mut peer1 = Peer::new(peer_id1, Multiaddr::empty()); + peer1.add_connection_id(ConnectionId::new_unchecked(0)); - // Add the mock peer to the peer manager + // Add the peer to the peer manager peer_manager.add_peer(peer1); - // Report the peer as bad - peer_manager.report_peer(peer_id1, ReputationModifier::Bad {}).unwrap(); + // Report the peer as malicious + peer_manager.report_peer(peer_id1, ReputationModifier::Malicious {}).unwrap(); - // Create a mock peer - let (mut peer2, peer_id2) = - create_mock_peer(config.blacklist_timeout, false, Some(ConnectionId::new_unchecked(0))); - peer2.expect_is_blocked().times(2).return_const(false); + // Create a peer + let peer_id2 = PeerId::random(); + let mut peer2 = Peer::new(peer_id2, Multiaddr::empty()); + peer2.add_connection_id(ConnectionId::new_unchecked(0)); - // Add the mock peer to the peer manager + // Add the peer to the peer manager peer_manager.add_peer(peer2); // Create a session @@ -353,51 +347,25 @@ fn wrap_around_in_peer_assignment() { assert_matches!(peer_manager.assign_peer_to_session(outbound_session_id), Some(peer_id) if peer_id == peer_id2); } -fn create_mock_peer( - blacklist_timeout_duration: Duration, - call_update_reputaion: bool, - connection_id: Option, -) -> (MockPeerTrait, PeerId) { - let peer_id = PeerId::random(); - let mut peer = MockPeerTrait::default(); - let mut mockall_seq = mockall::Sequence::new(); - - peer.expect_peer_id().return_const(peer_id); - peer.expect_set_timeout_duration() - .times(1) - .with(eq(blacklist_timeout_duration)) - .return_const(()) - .in_sequence(&mut mockall_seq); - if call_update_reputaion { - peer.expect_update_reputation() - .times(1) - .with(eq(ReputationModifier::Bad {})) - .return_once(|_| ()) - .in_sequence(&mut mockall_seq); - } - peer.expect_connection_ids().return_const(connection_id.map(|x| vec![x]).unwrap_or_default()); - - (peer, peer_id) -} - #[test] fn block_and_allow_inbound_connection() { // Create a new peer manager let config = PeerManagerConfig::default(); - let mut peer_manager: PeerManager = PeerManager::new(config.clone()); + let mut peer_manager: PeerManager = PeerManager::new(config.clone()); - // Create a mock peer - blocked - let (mut peer1, peer_id1) = create_mock_peer(config.blacklist_timeout, false, None); - peer1.expect_is_blocked().times(..2).return_const(true); + // Create a peer - report as malicious + let peer_id1 = PeerId::random(); + let peer1 = Peer::new(peer_id1, Multiaddr::empty()); - // Create a mock peer - not blocked - let (mut peer2, peer_id2) = create_mock_peer(config.blacklist_timeout, false, None); - peer2.expect_is_blocked().times(..2).return_const(false); + // Create a peer - not blocked + let peer_id2 = PeerId::random(); + let peer2 = Peer::new(peer_id2, Multiaddr::empty()); - // Add the mock peers to the peer manager peer_manager.add_peer(peer1); peer_manager.add_peer(peer2); + peer_manager.report_peer(peer_id1, ReputationModifier::Malicious).unwrap(); + // call handle_established_inbound_connection with the blocked peer let res = peer_manager.handle_established_inbound_connection( libp2p::swarm::ConnectionId::new_unchecked(0), @@ -423,14 +391,13 @@ fn block_and_allow_inbound_connection() { fn assign_non_connected_peer_raises_dial_event() { // Create a new peer manager let config = PeerManagerConfig::default(); - let mut peer_manager: PeerManager = PeerManager::new(config.clone()); + let mut peer_manager: PeerManager = PeerManager::new(config.clone()); - // Create a mock peer - let (mut peer, _) = create_mock_peer(config.blacklist_timeout, false, None); - peer.expect_is_blocked().times(1).return_const(false); - peer.expect_multiaddr().return_const(Multiaddr::empty()); + // Create a peer + let peer_id = PeerId::random(); + let peer = Peer::new(peer_id, Multiaddr::empty()); - // Add the mock peer to the peer manager + // Add the peer to the peer manager peer_manager.add_peer(peer); // Create a session @@ -449,15 +416,13 @@ fn assign_non_connected_peer_raises_dial_event() { async fn flow_test_assign_non_connected_peer() { // Create a new peer manager let config = PeerManagerConfig::default(); - let mut peer_manager: PeerManager = PeerManager::new(config.clone()); + let mut peer_manager: PeerManager = PeerManager::new(config.clone()); - // Create a mock peer - let (mut peer, peer_id) = create_mock_peer(config.blacklist_timeout, false, None); - peer.expect_is_blocked().times(1).return_const(false); - peer.expect_multiaddr().return_const(Multiaddr::empty()); - peer.expect_add_connection_id().times(1).return_const(()); + // Create a peer + let peer_id = PeerId::random(); + let peer = Peer::new(peer_id, Multiaddr::empty()); - // Add the mock peer to the peer manager + // Add the peer to the peer manager peer_manager.add_peer(peer); // Create a session