Skip to main content

soil_network/sync/service/
mock.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use futures::channel::oneshot;
8
9use soil_client::import::{BlockImportError, BlockImportStatus, RuntimeOrigin};
10use soil_network::common::role::ObservedRole;
11use soil_network::types::{multiaddr::Multiaddr, PeerId};
12use soil_network::{
13	config::MultiaddrWithPeerId,
14	request_responses::{IfDisconnected, RequestFailure},
15	types::ProtocolName,
16	NetworkPeers, NetworkRequest, NetworkSyncForkRequest, ReputationChange,
17};
18use subsoil::runtime::traits::{Block as BlockT, NumberFor};
19
20use std::collections::HashSet;
21
22mockall::mock! {
23	pub ChainSyncInterface<B: BlockT> {
24		pub fn justification_sync_link_request_justification(&self, hash: &B::Hash, number: NumberFor<B>);
25		pub fn justification_sync_link_clear_justification_requests(&self);
26	}
27
28	impl<B: BlockT + 'static> NetworkSyncForkRequest<B::Hash, NumberFor<B>>
29		for ChainSyncInterface<B>
30	{
31		fn set_sync_fork_request(&self, peers: Vec<PeerId>, hash: B::Hash, number: NumberFor<B>);
32	}
33
34	impl<B: BlockT> soil_client::import::Link<B> for ChainSyncInterface<B> {
35		fn blocks_processed(
36			&self,
37			imported: usize,
38			count: usize,
39			results: Vec<(Result<BlockImportStatus<NumberFor<B>>, BlockImportError>, B::Hash)>,
40		);
41		fn justification_imported(
42			&self,
43			who: RuntimeOrigin,
44			hash: &B::Hash,
45			number: NumberFor<B>,
46			import_result: soil_client::import::JustificationImportResult,
47		);
48		fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>);
49	}
50}
51
52impl<B: BlockT> soil_client::import::JustificationSyncLink<B> for MockChainSyncInterface<B> {
53	fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>) {
54		self.justification_sync_link_request_justification(hash, number);
55	}
56
57	fn clear_justification_requests(&self) {
58		self.justification_sync_link_clear_justification_requests();
59	}
60}
61
62mockall::mock! {
63	pub NetworkServiceHandle {}
64}
65
66// Mocked `Network` for `ChainSync`-related tests
67mockall::mock! {
68	pub Network {}
69
70	#[async_trait::async_trait]
71	impl NetworkPeers for Network {
72		fn set_authorized_peers(&self, peers: HashSet<PeerId>);
73		fn set_authorized_only(&self, reserved_only: bool);
74		fn add_known_address(&self, peer_id: PeerId, addr: Multiaddr);
75		fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange);
76		fn peer_reputation(&self, peer_id: &PeerId) -> i32;
77		fn disconnect_peer(&self, peer_id: PeerId, protocol: ProtocolName);
78		fn accept_unreserved_peers(&self);
79		fn deny_unreserved_peers(&self);
80		fn add_reserved_peer(&self, peer: MultiaddrWithPeerId) -> Result<(), String>;
81		fn remove_reserved_peer(&self, peer_id: PeerId);
82		fn set_reserved_peers(
83			&self,
84			protocol: ProtocolName,
85			peers: HashSet<Multiaddr>,
86		) -> Result<(), String>;
87		fn add_peers_to_reserved_set(
88			&self,
89			protocol: ProtocolName,
90			peers: HashSet<Multiaddr>,
91		) -> Result<(), String>;
92		fn remove_peers_from_reserved_set(
93			&self,
94			protocol: ProtocolName,
95			peers: Vec<PeerId>
96		) -> Result<(), String>;
97		fn sync_num_connected(&self) -> usize;
98		fn peer_role(&self, peer_id: PeerId, handshake: Vec<u8>) -> Option<ObservedRole>;
99		async fn reserved_peers(&self) -> Result<Vec<soil_network::types::PeerId>, ()>;
100	}
101
102	#[async_trait::async_trait]
103	impl NetworkRequest for Network {
104		async fn request(
105			&self,
106			target: PeerId,
107			protocol: ProtocolName,
108			request: Vec<u8>,
109			fallback_request: Option<(Vec<u8>, ProtocolName)>,
110			connect: IfDisconnected,
111		) -> Result<(Vec<u8>, ProtocolName), RequestFailure>;
112		fn start_request(
113			&self,
114			target: PeerId,
115			protocol: ProtocolName,
116			request: Vec<u8>,
117			fallback_request: Option<(Vec<u8>, ProtocolName)>,
118			tx: oneshot::Sender<Result<(Vec<u8>, ProtocolName), RequestFailure>>,
119			connect: IfDisconnected,
120		);
121	}
122}