Repository: mintlayer/mintlayer-core
Pull request #630 (2023-01-11 15:46:55)

Use libtest-mimic for peerdb tests
The diff is somewhat big because I unified the generic parameters in all `p2p-backend-test-suite` functions.


Patch:
diff --git a/p2p/src/peer_manager/peerdb/mod.rs b/p2p/src/peer_manager/peerdb/mod.rs
index 056434cfc..aef11b2a4 100644
--- a/p2p/src/peer_manager/peerdb/mod.rs
+++ b/p2p/src/peer_manager/peerdb/mod.rs
@@ -264,7 +264,8 @@ impl<T: NetworkingService> PeerDb<T> {
 
         false
     }
-}
 
-#[cfg(test)]
-mod tests;
+    pub fn peer_address(&self, id: &T::PeerId) -> Option<&T::Address> {
+        self.peers.get(id).map(|c| &c.address)
+    }
+}


Test patch:
diff --git a/p2p/backend-test-suite/src/ban.rs b/p2p/backend-test-suite/src/ban.rs
index dc9d6f0af..90ef4e61f 100644
--- a/p2p/backend-test-suite/src/ban.rs
+++ b/p2p/backend-test-suite/src/ban.rs
@@ -17,7 +17,6 @@ use std::{fmt::Debug, sync::Arc};
 
 use tokio::sync::mpsc;
 
-use p2p::testing_utils::{connect_services, TestTransportMaker};
 use p2p::{
     config::P2pConfig,
     error::{P2pError, PublishError},
@@ -25,6 +24,7 @@ use p2p::{
     message::{Announcement, HeaderListResponse, Request, Response},
     net::{types::SyncingEvent, ConnectivityService, NetworkingService, SyncingMessagingService},
     sync::BlockSyncManager,
+    testing_utils::{connect_services, TestTransportMaker},
 };
 use p2p_test_utils::TestBlockInfo;
 
@@ -33,9 +33,9 @@ tests![invalid_pubsub_block, invalid_sync_block,];
 // Start two network services, spawn a `SyncMessageHandler` for the first service, publish an
 // invalid block from the first service and verify that the `SyncManager` of the first service
 // receives a `AdjustPeerScore` event which bans the peer of the second service.
-async fn invalid_pubsub_block<A, S>()
+async fn invalid_pubsub_block<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + Debug + 'static,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
@@ -47,8 +47,8 @@ where
     let handle = p2p_test_utils::start_chainstate(Arc::clone(&chain_config)).await;
 
     let (mut conn1, sync1) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&chain_config),
         Default::default(),
     )
@@ -65,8 +65,8 @@ where
     );
 
     let (mut conn2, mut sync2) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&chain_config),
         Arc::clone(&p2p_config),
     )
@@ -126,9 +126,9 @@ where
 }
 
 // Start two networking services and give an invalid block, verify that `PeerManager` is informed.
-async fn invalid_sync_block<A, S>()
+async fn invalid_sync_block<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + Debug + 'static,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
@@ -139,8 +139,8 @@ where
     let handle = p2p_test_utils::start_chainstate(Arc::clone(&chain_config)).await;
 
     let (mut conn1, sync1) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&chain_config),
         Default::default(),
     )
@@ -148,8 +148,8 @@ where
     .unwrap();
 
     let (mut conn2, _sync2) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&chain_config),
         Default::default(),
     )
diff --git a/p2p/backend-test-suite/src/block_announcement.rs b/p2p/backend-test-suite/src/block_announcement.rs
index ccb781818..31fae6129 100644
--- a/p2p/backend-test-suite/src/block_announcement.rs
+++ b/p2p/backend-test-suite/src/block_announcement.rs
@@ -32,7 +32,6 @@ use common::{
 };
 use serialization::Encode;
 
-use p2p::testing_utils::{connect_services, TestTransportMaker};
 use p2p::{
     config::{NodeType, P2pConfig},
     error::{P2pError, PublishError},
@@ -41,6 +40,7 @@ use p2p::{
         mock::constants::ANNOUNCEMENT_MAX_SIZE, types::SyncingEvent, ConnectivityService,
         NetworkingService, SyncingMessagingService,
     },
+    testing_utils::{connect_services, TestTransportMaker},
 };
 
 tests![
@@ -49,25 +49,25 @@ tests![
     block_announcement_too_big_message,
 ];
 
-async fn block_announcement<A, S>()
+async fn block_announcement<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + Debug,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
     S::ConnectivityHandle: ConnectivityService<S>,
 {
     let config = Arc::new(common::chain::config::create_mainnet());
     let (mut conn1, mut sync1) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&config),
         Default::default(),
     )
     .await
     .unwrap();
     let (mut conn2, mut sync2) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&config),
         Default::default(),
     )
@@ -130,9 +130,9 @@ where
     assert_eq!(block.timestamp(), BlockTimestamp::from_int_seconds(1338u64));
 }
 
-async fn block_announcement_no_subscription<A, S>()
+async fn block_announcement_no_subscription<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + Debug,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
     S::ConnectivityHandle: ConnectivityService<S>,
@@ -147,16 +147,16 @@ where
         node_type: NodeType::Inactive.into(),
     });
     let (mut conn1, mut sync1) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&chain_config),
         Arc::clone(&p2p_config),
     )
     .await
     .unwrap();
     let (mut conn2, _sync2) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         chain_config,
         p2p_config,
     )
@@ -186,17 +186,17 @@ where
     }
 }
 
-async fn block_announcement_too_big_message<A, S>()
+async fn block_announcement_too_big_message<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + Debug,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
     S::ConnectivityHandle: ConnectivityService<S>,
 {
     let config = Arc::new(common::chain::config::create_mainnet());
     let (mut conn1, mut sync1) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&config),
         Default::default(),
     )
@@ -204,8 +204,8 @@ where
     .unwrap();
 
     let (mut conn2, _sync2) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&config),
         Default::default(),
     )
diff --git a/p2p/backend-test-suite/src/connect.rs b/p2p/backend-test-suite/src/connect.rs
index 19e05d526..bdc9cbe9e 100644
--- a/p2p/backend-test-suite/src/connect.rs
+++ b/p2p/backend-test-suite/src/connect.rs
@@ -30,17 +30,17 @@ tests![
     connect_accept,
 ];
 
-async fn connect<A, S>()
+async fn connect<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + Debug + 'static,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
 {
     let config = Arc::new(common::chain::config::create_mainnet());
     S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         config,
         Default::default(),
     )
@@ -51,17 +51,17 @@ where
 // Check that connecting twice to the same address isn't possible.
 // TODO: Investigate why this test fails on Windows.
 #[cfg(not(target_os = "windows"))]
-async fn connect_address_in_use<A, S>()
+async fn connect_address_in_use<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + Debug + 'static,
     S::ConnectivityHandle: ConnectivityService<S> + Debug,
     S::SyncingMessagingHandle: SyncingMessagingService<S> + Debug,
 {
     let config = Arc::new(common::chain::config::create_mainnet());
     let (connectivity, _sync) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&config),
         Default::default(),
     )
@@ -69,7 +69,7 @@ where
     .unwrap();
 
     let addresses = connectivity.local_addresses().await.unwrap();
-    let res = S::start(A::make_transport(), addresses, config, Default::default())
+    let res = S::start(T::make_transport(), addresses, config, Default::default())
         .await
         .expect_err("address is not in use");
     assert!(matches!(
@@ -82,25 +82,25 @@ where
 
 // Try to connect two nodes by having `service1` listen for network events and having `service2`
 // trying to connect to `service1`.
-async fn connect_accept<A, S>()
+async fn connect_accept<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + std::fmt::Debug + 'static,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
 {
     let config = Arc::new(common::chain::config::create_mainnet());
     let (mut service1, _) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&config),
         Default::default(),
     )
     .await
     .unwrap();
     let (mut service2, _) = S::start(
-        A::make_transport(),
-        vec![A::make_address()],
+        T::make_transport(),
+        vec![T::make_address()],
         Arc::clone(&config),
         Default::default(),
     )
diff --git a/p2p/backend-test-suite/src/lib.rs b/p2p/backend-test-suite/src/lib.rs
index 5dbb24ddc..4a16e7ff1 100644
--- a/p2p/backend-test-suite/src/lib.rs
+++ b/p2p/backend-test-suite/src/lib.rs
@@ -22,40 +22,48 @@ mod utils;
 mod ban;
 mod block_announcement;
 mod connect;
+mod peer_manager_peerdb;
 mod sync;
 
 use std::fmt::Debug;
 
 use libtest_mimic::{Arguments, Trial};
 
-use p2p::net::{ConnectivityService, NetworkingService, SyncingMessagingService};
-use p2p::testing_utils::TestTransportMaker;
+use p2p::{
+    net::{
+        mock::types::MockPeerId, ConnectivityService, NetworkingService, SyncingMessagingService,
+    },
+    testing_utils::{RandomAddressMaker, TestTransportMaker},
+};
 
 /// Runs all tests.
-pub fn run<A, S>()
+pub fn run<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
-    S: NetworkingService + Debug + 'static,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    S: NetworkingService<PeerId = MockPeerId> + Debug + 'static,
     S::ConnectivityHandle: ConnectivityService<S> + Debug,
     S::SyncingMessagingHandle: SyncingMessagingService<S> + Debug,
+    A: RandomAddressMaker<Address = S::Address>,
 {
     logging::init_logging::<&str>(None);
     let args = Arguments::from_args();
-    libtest_mimic::run(&args, tests::<A, S>()).exit();
+    libtest_mimic::run(&args, tests::<T, S, A>()).exit();
 }
 
 /// Collects all backend agnostic tests.
-fn tests<A, S>() -> Vec<Trial>
+fn tests<T, S, A>() -> Vec<Trial>
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
-    S: NetworkingService + Debug + 'static,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    S: NetworkingService<PeerId = MockPeerId> + Debug + 'static,
     S::ConnectivityHandle: ConnectivityService<S> + Debug,
     S::SyncingMessagingHandle: SyncingMessagingService<S> + Debug,
+    A: RandomAddressMaker<Address = S::Address>,
 {
     std::iter::empty()
-        .chain(connect::tests::<A, S>())
-        .chain(block_announcement::tests::<A, S>())
-        .chain(sync::tests::<A, S>())
-        .chain(ban::tests::<A, S>())
+        .chain(connect::tests::<T, S, A>())
+        .chain(block_announcement::tests::<T, S, A>())
+        .chain(sync::tests::<T, S, A>())
+        .chain(ban::tests::<T, S, A>())
+        .chain(peer_manager_peerdb::tests::<T, S, A>())
         .collect()
 }
diff --git a/p2p/src/peer_manager/peerdb/tests.rs b/p2p/backend-test-suite/src/peer_manager_peerdb.rs
similarity index 54%
rename from p2p/src/peer_manager/peerdb/tests.rs
rename to p2p/backend-test-suite/src/peer_manager_peerdb.rs
index ac0e6a751..2ae0d46e4 100644
--- a/p2p/src/peer_manager/peerdb/tests.rs
+++ b/p2p/backend-test-suite/src/peer_manager_peerdb.rs
@@ -13,83 +13,43 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+// Test functions need to be marked async because of the `tests!` macro.
+#![allow(clippy::unused_async)]
+
 use std::{sync::Arc, time::Duration};
 
-use crate::{
+use p2p::{
     config::P2pConfig,
     net::{
-        mock::{
-            transport::{MockChannelTransport, NoiseTcpTransport, TcpTransportSocket},
-            types::MockPeerId,
-            MockService,
-        },
+        mock::types::MockPeerId,
         types::{PeerInfo, PubSubTopic, Role},
         AsBannableAddress, NetworkingService,
     },
     peer_manager::peerdb::PeerDb,
-    testing_utils::{RandomAddressMaker, TestChannelAddressMaker, TestTcpAddressMaker},
+    testing_utils::RandomAddressMaker,
 };
 
-fn make_peer_info<S, A>() -> (S::PeerId, S::Address, PeerInfo<S>)
-where
-    S: NetworkingService<PeerId = MockPeerId>,
-    A: RandomAddressMaker<Address = S::Address>,
-{
-    let peer_id = MockPeerId::new();
-
-    (
-        peer_id,
-        A::new(),
-        PeerInfo::<S> {
-            peer_id,
-            magic_bytes: [1, 2, 3, 4],
-            version: common::primitives::semver::SemVer::new(0, 1, 0),
-            agent: None,
-            subscriptions: [PubSubTopic::Blocks, PubSubTopic::Transactions].into_iter().collect(),
-        },
-    )
-}
-
-fn add_active_peer<S, A>(peerdb: &mut PeerDb<S>) -> S::PeerId
-where
-    S: NetworkingService<PeerId = MockPeerId>,
-    A: RandomAddressMaker<Address = S::Address>,
-{
-    let (peer_id, address, info) = make_peer_info::<S, A>();
-    peerdb.peer_connected(address, Role::Inbound, info);
-    peer_id
-}
+tests![
+    adjust_peer_score_normal_threshold,
+    adjust_peer_score_higher_threshold,
+    adjust_peer_score_lower_threshold,
+    unban_peer,
+];
 
-fn adjust_peer_score_normal_threshold<S, A>()
+async fn adjust_peer_score_normal_threshold<T, S, A>()
 where
     S: NetworkingService<PeerId = MockPeerId>,
     A: RandomAddressMaker<Address = S::Address>,
 {
     let mut peerdb = PeerDb::<S>::new(Arc::new(P2pConfig::default())).unwrap();
 
-    let peer_id = add_active_peer::<S, A>(&mut peerdb);
-    let address = peerdb.peers.get(&peer_id).unwrap().address.as_bannable();
+    let peer_id = add_active_peer::<T, S, A>(&mut peerdb);
+    let address = peerdb.peer_address(&peer_id).unwrap().as_bannable();
     assert!(peerdb.adjust_peer_score(&peer_id, 100));
     assert!(peerdb.is_address_banned(&address));
 }
 
-#[test]
-fn adjust_peer_score_normal_threshold_mock_tcp() {
-    adjust_peer_score_normal_threshold::<MockService<TcpTransportSocket>, TestTcpAddressMaker>();
-}
-
-#[test]
-fn adjust_peer_score_normal_threshold_mock_channels() {
-    adjust_peer_score_normal_threshold::<MockService<MockChannelTransport>, TestChannelAddressMaker>(
-    );
-}
-
-#[test]
-fn adjust_peer_score_normal_threshold_mock_noise() {
-    adjust_peer_score_normal_threshold::<MockService<NoiseTcpTransport>, TestTcpAddressMaker>();
-}
-
-fn adjust_peer_score_higher_threshold<S, A>()
+async fn adjust_peer_score_higher_threshold<T, S, A>()
 where
     S: NetworkingService<PeerId = MockPeerId>,
     A: RandomAddressMaker<Address = S::Address>,
@@ -104,30 +64,14 @@ where
     };
     let mut peerdb = PeerDb::<S>::new(Arc::new(config)).unwrap();
 
-    let peer_id = add_active_peer::<S, A>(&mut peerdb);
+    let peer_id = add_active_peer::<T, S, A>(&mut peerdb);
     assert!(!peerdb.adjust_peer_score(&peer_id, 100));
 
-    let address = peerdb.peers.get(&peer_id).unwrap().address.as_bannable();
+    let address = peerdb.peer_address(&peer_id).unwrap().as_bannable();
     assert!(!peerdb.is_address_banned(&address));
 }
 
-#[test]
-fn adjust_peer_score_higher_threshold_mock_tcp() {
-    adjust_peer_score_higher_threshold::<MockService<TcpTransportSocket>, TestTcpAddressMaker>();
-}
-
-#[test]
-fn adjust_peer_score_higher_threshold_mock_channels() {
-    adjust_peer_score_higher_threshold::<MockService<MockChannelTransport>, TestChannelAddressMaker>(
-    );
-}
-
-#[test]
-fn adjust_peer_score_higher_threshold_mock_noise() {
-    adjust_peer_score_higher_threshold::<MockService<NoiseTcpTransport>, TestTcpAddressMaker>();
-}
-
-fn adjust_peer_score_lower_threshold<S, A>()
+async fn adjust_peer_score_lower_threshold<T, S, A>()
 where
     S: NetworkingService<PeerId = MockPeerId>,
     A: RandomAddressMaker<Address = S::Address>,
@@ -142,29 +86,13 @@ where
     };
     let mut peerdb = PeerDb::<S>::new(Arc::new(config)).unwrap();
 
-    let peer_id = add_active_peer::<S, A>(&mut peerdb);
-    let address = peerdb.peers.get(&peer_id).unwrap().address.as_bannable();
+    let peer_id = add_active_peer::<T, S, A>(&mut peerdb);
+    let address = peerdb.peer_address(&peer_id).unwrap().as_bannable();
     assert!(peerdb.adjust_peer_score(&peer_id, 30));
     assert!(peerdb.is_address_banned(&address));
 }
 
-#[test]
-fn adjust_peer_score_lower_threshold_mock_tcp() {
-    adjust_peer_score_lower_threshold::<MockService<TcpTransportSocket>, TestTcpAddressMaker>();
-}
-
-#[test]
-fn adjust_peer_score_lower_threshold_mock_channels() {
-    adjust_peer_score_lower_threshold::<MockService<MockChannelTransport>, TestChannelAddressMaker>(
-    );
-}
-
-#[test]
-fn adjust_peer_score_lower_threshold_mock_noise() {
-    adjust_peer_score_lower_threshold::<MockService<NoiseTcpTransport>, TestTcpAddressMaker>();
-}
-
-async fn unban_peer<S, A>()
+async fn unban_peer<T, S, A>()
 where
     S: NetworkingService<PeerId = MockPeerId>,
     A: RandomAddressMaker<Address = S::Address>,
@@ -179,10 +107,10 @@ where
     }))
     .unwrap();
 
-    let peer_id = add_active_peer::<S, A>(&mut peerdb);
-    let address = peerdb.peers.get(&peer_id).unwrap().address.as_bannable();
-
-    peerdb.ban_peer(&peer_id);
+    let peer_id = add_active_peer::<T, S, A>(&mut peerdb);
+    let address = peerdb.peer_address(&peer_id).unwrap().as_bannable();
+    assert!(peerdb.adjust_peer_score(&peer_id, 100));
+    assert!(peerdb.is_address_banned(&address));
 
     tokio::time::sleep(Duration::from_secs(1)).await;
     assert!(peerdb.is_address_banned(&address));
@@ -191,17 +119,32 @@ where
     assert!(!peerdb.is_address_banned(&address));
 }
 
-#[tokio::test]
-async fn unban_peer_mock_tcp() {
-    unban_peer::<MockService<TcpTransportSocket>, TestTcpAddressMaker>().await;
-}
+fn make_peer_info<T, S, A>() -> (S::PeerId, S::Address, PeerInfo<S>)
+where
+    S: NetworkingService<PeerId = MockPeerId>,
+    A: RandomAddressMaker<Address = S::Address>,
+{
+    let peer_id = MockPeerId::new();
 
-#[tokio::test]
-async fn unban_peer_mock_channels() {
-    unban_peer::<MockService<MockChannelTransport>, TestChannelAddressMaker>().await;
+    (
+        peer_id,
+        A::new(),
+        PeerInfo::<S> {
+            peer_id,
+            magic_bytes: [1, 2, 3, 4],
+            version: common::primitives::semver::SemVer::new(0, 1, 0),
+            agent: None,
+            subscriptions: [PubSubTopic::Blocks, PubSubTopic::Transactions].into_iter().collect(),
+        },
+    )
 }
 
-#[tokio::test]
-async fn unban_peer_mock_noise() {
-    unban_peer::<MockService<NoiseTcpTransport>, TestTcpAddressMaker>().await;
+fn add_active_peer<T, S, A>(peerdb: &mut PeerDb<S>) -> S::PeerId
+where
+    S: NetworkingService<PeerId = MockPeerId>,
+    A: RandomAddressMaker<Address = S::Address>,
+{
+    let (peer_id, address, info) = make_peer_info::<T, S, A>();
+    peerdb.peer_connected(address, Role::Inbound, info);
+    peer_id
 }
diff --git a/p2p/backend-test-suite/src/sync.rs b/p2p/backend-test-suite/src/sync.rs
index 617183b2b..f2a6147ad 100644
--- a/p2p/backend-test-suite/src/sync.rs
+++ b/p2p/backend-test-suite/src/sync.rs
@@ -53,9 +53,9 @@ tests![
     connect_disconnect_resyncing,
 ];
 
-async fn local_and_remote_in_sync<A, S>()
+async fn local_and_remote_in_sync<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + Debug + 'static,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
@@ -66,15 +66,15 @@ where
     let mgr2_handle = handle2.clone();
 
     let (mut mgr1, mut conn1, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle1,
         P2pConfig::default(),
     )
     .await;
     let (mut mgr2, mut conn2, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle2,
         P2pConfig::default(),
     )
@@ -102,9 +102,9 @@ where
 //
 // this the remote node is synced first and as it's ahead of local node,
 // no blocks are downloaded whereas local node downloads the 7 new blocks from remote
-async fn remote_ahead_by_7_blocks<A, S>()
+async fn remote_ahead_by_7_blocks<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + 'static + Debug,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
@@ -115,15 +115,15 @@ where
     let mgr2_handle = handle2.clone();
 
     let (mut mgr1, mut conn1, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle1,
         P2pConfig::default(),
     )
     .await;
     let (mut mgr2, mut conn2, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle2,
         P2pConfig::default(),
     )
@@ -203,15 +203,15 @@ where
 }
 
 // local and remote nodes are in the same chain but local is ahead of remote by 12 blocks
-async fn local_ahead_by_12_blocks<A, T>()
+async fn local_ahead_by_12_blocks<T, S, A>()
 where
-    A: TestTransportMaker<Transport = T::Transport, Address = T::Address>,
-    T: NetworkingService + 'static + Debug,
-    T::ConnectivityHandle: ConnectivityService<T>,
-    T::SyncingMessagingHandle: SyncingMessagingService<T>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    S: NetworkingService + 'static + Debug,
+    S::ConnectivityHandle: ConnectivityService<S>,
+    S::SyncingMessagingHandle: SyncingMessagingService<S>,
 {
-    let addr1 = A::make_address();
-    let addr2 = A::make_address();
+    let addr1 = T::make_address();
+    let addr2 = T::make_address();
 
     let config = Arc::new(common::chain::config::create_unit_test_config());
     let (handle1, handle2) = init_chainstate_2(Arc::clone(&config), 8).await;
@@ -219,9 +219,9 @@ where
     let mgr2_handle = handle2.clone();
 
     let (mut mgr1, mut conn1, _, _) =
-        make_sync_manager::<T>(A::make_transport(), addr1, handle1, P2pConfig::default()).await;
+        make_sync_manager::<S>(T::make_transport(), addr1, handle1, P2pConfig::default()).await;
     let (mut mgr2, mut conn2, _, _) =
-        make_sync_manager::<T>(A::make_transport(), addr2, handle2, P2pConfig::default()).await;
+        make_sync_manager::<S>(T::make_transport(), addr2, handle2, P2pConfig::default()).await;
 
     // add 12 more blocks on top of the best block (which is also known by mgr2)
     assert!(same_tip(&mgr1_handle, &mgr2_handle).await);
@@ -229,7 +229,7 @@ where
     assert!(!same_tip(&mgr1_handle, &mgr2_handle).await);
 
     // add peer to the hashmap of known peers and send getheaders request to them
-    let (_address, peer_info, peer_info2) = connect_services::<T>(&mut conn1, &mut conn2).await;
+    let (_address, peer_info, peer_info2) = connect_services::<S>(&mut conn1, &mut conn2).await;
     assert_eq!(mgr1.register_peer(peer_info2.peer_id).await, Ok(()));
     assert_eq!(mgr2.register_peer(peer_info.peer_id).await, Ok(()));
 
@@ -322,15 +322,15 @@ where
 
 // local and remote nodes are in the same chain but local is ahead of remote by 14 blocks
 // verify that remote nodes does a reorg
-async fn remote_local_diff_chains_local_higher<A, S>()
+async fn remote_local_diff_chains_local_higher<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + Debug + 'static,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
 {
-    let addr1 = A::make_address();
-    let addr2 = A::make_address();
+    let addr1 = T::make_address();
+    let addr2 = T::make_address();
 
     let config = Arc::new(common::chain::config::create_unit_test_config());
     let (handle1, handle2) = init_chainstate_2(Arc::clone(&config), 8).await;
@@ -338,9 +338,9 @@ where
     let mgr2_handle = handle2.clone();
 
     let (mut mgr1, mut conn1, _, _) =
-        make_sync_manager::<S>(A::make_transport(), addr1, handle1, P2pConfig::default()).await;
+        make_sync_manager::<S>(T::make_transport(), addr1, handle1, P2pConfig::default()).await;
     let (mut mgr2, mut conn2, _, _) =
-        make_sync_manager::<S>(A::make_transport(), addr2, handle2, P2pConfig::default()).await;
+        make_sync_manager::<S>(T::make_transport(), addr2, handle2, P2pConfig::default()).await;
 
     // add 14 more blocks to local chain and 7 more blocks to remote chain
     assert!(same_tip(&mgr1_handle, &mgr2_handle).await);
@@ -466,15 +466,15 @@ where
 
 // local and remote nodes are in different chains and remote has longer chain
 // verify that local node does a reorg
-async fn remote_local_diff_chains_remote_higher<A, S>()
+async fn remote_local_diff_chains_remote_higher<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + 'static + Debug,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
 {
-    let addr1 = A::make_address();
-    let addr2 = A::make_address();
+    let addr1 = T::make_address();
+    let addr2 = T::make_address();
 
     let config = Arc::new(common::chain::config::create_unit_test_config());
     let (handle1, handle2) = init_chainstate_2(Arc::clone(&config), 8).await;
@@ -482,9 +482,9 @@ where
     let mgr2_handle = handle2.clone();
 
     let (mut mgr1, mut conn1, _, _) =
-        make_sync_manager::<S>(A::make_transport(), addr1, handle1, P2pConfig::default()).await;
+        make_sync_manager::<S>(T::make_transport(), addr1, handle1, P2pConfig::default()).await;
     let (mut mgr2, mut conn2, _, _) =
-        make_sync_manager::<S>(A::make_transport(), addr2, handle2, P2pConfig::default()).await;
+        make_sync_manager::<S>(T::make_transport(), addr2, handle2, P2pConfig::default()).await;
 
     // add 5 more blocks to local chain and 12 more blocks to remote chain
     assert!(same_tip(&mgr1_handle, &mgr2_handle).await);
@@ -608,9 +608,9 @@ where
     assert!(!mgr1_handle.call(|c| c.is_initial_block_download()).await.unwrap().unwrap());
 }
 
-async fn two_remote_nodes_different_chains<A, S>()
+async fn two_remote_nodes_different_chains<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + 'static + Debug,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
@@ -622,22 +622,22 @@ where
     let mgr3_handle = handle3.clone();
 
     let (mut mgr1, mut conn1, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle1,
         P2pConfig::default(),
     )
     .await;
     let (mut mgr2, mut conn2, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle2,
         P2pConfig::default(),
     )
     .await;
     let (mut mgr3, mut conn3, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle3,
         P2pConfig::default(),
     )
@@ -729,9 +729,9 @@ where
     assert!(!mgr1_handle.call(|c| c.is_initial_block_download()).await.unwrap().unwrap());
 }
 
-async fn two_remote_nodes_same_chains<A, S>()
+async fn two_remote_nodes_same_chains<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + 'static + Debug,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
@@ -743,22 +743,22 @@ where
     let mgr3_handle = handle3.clone();
 
     let (mut mgr1, mut conn1, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle1.clone(),
         P2pConfig::default(),
     )
     .await;
     let (mut mgr2, mut conn2, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle2,
         P2pConfig::default(),
     )
     .await;
     let (mut mgr3, mut conn3, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle3,
         P2pConfig::default(),
     )
@@ -868,9 +868,9 @@ where
     assert!(!mgr1_handle.call(|c| c.is_initial_block_download()).await.unwrap().unwrap());
 }
 
-async fn two_remote_nodes_same_chains_new_blocks<A, S>()
+async fn two_remote_nodes_same_chains_new_blocks<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + 'static + Debug,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
@@ -879,22 +879,22 @@ where
     let (mgr1_handle, mgr2_handle, mgr3_handle) = init_chainstate_3(Arc::clone(&config), 8).await;
 
     let (mut mgr1, mut conn1, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         mgr1_handle.clone(),
         P2pConfig::default(),
     )
     .await;
     let (mut mgr2, mut conn2, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         mgr2_handle.clone(),
         P2pConfig::default(),
     )
     .await;
     let (mut mgr3, mut conn3, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         mgr3_handle.clone(),
         P2pConfig::default(),
     )
@@ -1017,9 +1017,9 @@ where
 // connect two nodes, they are in sync so no blocks are downloaded
 // then disconnect them, add more blocks to remote chains and reconnect the nodes
 // verify that local node downloads the blocks and after that they are in sync
-async fn connect_disconnect_resyncing<A, S>()
+async fn connect_disconnect_resyncing<T, S, A>()
 where
-    A: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+    T: TestTransportMaker<Transport = S::Transport, Address = S::Address>,
     S: NetworkingService + 'static + Debug,
     S::ConnectivityHandle: ConnectivityService<S>,
     S::SyncingMessagingHandle: SyncingMessagingService<S>,
@@ -1030,15 +1030,15 @@ where
     let mgr2_handle = handle2.clone();
 
     let (mut mgr1, mut conn1, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle1,
         P2pConfig::default(),
     )
     .await;
     let (mut mgr2, mut conn2, _, _) = make_sync_manager::<S>(
-        A::make_transport(),
-        A::make_address(),
+        T::make_transport(),
+        T::make_address(),
         handle2,
         P2pConfig::default(),
     )
diff --git a/p2p/backend-test-suite/src/utils.rs b/p2p/backend-test-suite/src/utils.rs
index c167ea496..f1bd43d80 100644
--- a/p2p/backend-test-suite/src/utils.rs
+++ b/p2p/backend-test-suite/src/utils.rs
@@ -15,12 +15,13 @@
 
 macro_rules! tests {
     ($($(#[$meta:meta])* $name:ident,)+) => {
-        pub fn tests<A, S>() -> impl Iterator<Item = libtest_mimic::Trial>
+        pub fn tests<T, S, A>() -> impl Iterator<Item = libtest_mimic::Trial>
         where
-            A: p2p::testing_utils::TestTransportMaker<Transport = S::Transport, Address = S::Address>,
-            S: p2p::net::NetworkingService + std::fmt::Debug + 'static,
+            T: p2p::testing_utils::TestTransportMaker<Transport = S::Transport, Address = S::Address>,
+            S: p2p::net::NetworkingService<PeerId = p2p::net::mock::types::MockPeerId> + std::fmt::Debug + 'static,
             S::ConnectivityHandle: p2p::net::ConnectivityService<S> + std::fmt::Debug,
             S::SyncingMessagingHandle: p2p::net::SyncingMessagingService<S> + std::fmt::Debug,
+            A: p2p::testing_utils::RandomAddressMaker<Address = S::Address>,
         {
             [
                 $($(#[$meta])*
@@ -32,7 +33,7 @@ macro_rules! tests {
                         .build()
                         .unwrap()
                         .block_on(async {
-                            $name::<A, S>().await;
+                            $name::<T, S, A>().await;
                         });
                     Ok(())
                 }
diff --git a/p2p/tests/backend_mock_channels.rs b/p2p/tests/backend_mock_channels.rs
index 3acb43c49..73c4279c8 100644
--- a/p2p/tests/backend_mock_channels.rs
+++ b/p2p/tests/backend_mock_channels.rs
@@ -13,9 +13,15 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-use p2p::net::mock::{transport::MockChannelTransport, MockService};
-use p2p::testing_utils::TestTransportChannel;
+use p2p::{
+    net::mock::{transport::MockChannelTransport, MockService},
+    testing_utils::{TestChannelAddressMaker, TestTransportChannel},
+};
 
 fn main() {
-    p2p_backend_test_suite::run::<TestTransportChannel, MockService<MockChannelTransport>>();
+    p2p_backend_test_suite::run::<
+        TestTransportChannel,
+        MockService<MockChannelTransport>,
+        TestChannelAddressMaker,
+    >();
 }
diff --git a/p2p/tests/backend_mock_noise.rs b/p2p/tests/backend_mock_noise.rs
index 81aabfa95..7b32aa8f0 100644
--- a/p2p/tests/backend_mock_noise.rs
+++ b/p2p/tests/backend_mock_noise.rs
@@ -13,15 +13,18 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-use p2p::net::mock::{
-    transport::{NoiseEncryptionAdapter, TcpTransportSocket, WrappedTransportSocket},
-    MockService,
+use p2p::{
+    net::mock::{
+        transport::{NoiseEncryptionAdapter, TcpTransportSocket, WrappedTransportSocket},
+        MockService,
+    },
+    testing_utils::{TestTcpAddressMaker, TestTransportNoise},
 };
-use p2p::testing_utils::TestTransportNoise;
 
 fn main() {
     p2p_backend_test_suite::run::<
         TestTransportNoise,
         MockService<WrappedTransportSocket<NoiseEncryptionAdapter, TcpTransportSocket>>,
+        TestTcpAddressMaker,
     >();
 }
diff --git a/p2p/tests/backend_mock_tcp.rs b/p2p/tests/backend_mock_tcp.rs
index 8e597bc59..d65c15804 100644
--- a/p2p/tests/backend_mock_tcp.rs
+++ b/p2p/tests/backend_mock_tcp.rs
@@ -13,9 +13,15 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-use p2p::net::mock::{transport::TcpTransportSocket, MockService};
-use p2p::testing_utils::TestTransportTcp;
+use p2p::{
+    net::mock::{transport::TcpTransportSocket, MockService},
+    testing_utils::{TestTcpAddressMaker, TestTransportTcp},
+};
 
 fn main() {
-    p2p_backend_test_suite::run::<TestTransportTcp, MockService<TcpTransportSocket>>();
+    p2p_backend_test_suite::run::<
+        TestTransportTcp,
+        MockService<TcpTransportSocket>,
+        TestTcpAddressMaker,
+    >();
 }