sc_network_sync/service/
mock.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use futures::channel::oneshot;
20
21use sc_consensus::{BlockImportError, BlockImportStatus};
22use sc_network::{
23	config::MultiaddrWithPeerId,
24	request_responses::{IfDisconnected, RequestFailure},
25	types::ProtocolName,
26	NetworkPeers, NetworkRequest, NetworkSyncForkRequest, ReputationChange,
27};
28use sc_network_common::role::ObservedRole;
29use sc_network_types::{multiaddr::Multiaddr, PeerId};
30use sp_runtime::traits::{Block as BlockT, NumberFor};
31
32use std::collections::HashSet;
33
34mockall::mock! {
35	pub ChainSyncInterface<B: BlockT> {
36		pub fn justification_sync_link_request_justification(&self, hash: &B::Hash, number: NumberFor<B>);
37		pub fn justification_sync_link_clear_justification_requests(&self);
38	}
39
40	impl<B: BlockT + 'static> NetworkSyncForkRequest<B::Hash, NumberFor<B>>
41		for ChainSyncInterface<B>
42	{
43		fn set_sync_fork_request(&self, peers: Vec<PeerId>, hash: B::Hash, number: NumberFor<B>);
44	}
45
46	impl<B: BlockT> sc_consensus::Link<B> for ChainSyncInterface<B> {
47		fn blocks_processed(
48			&self,
49			imported: usize,
50			count: usize,
51			results: Vec<(Result<BlockImportStatus<NumberFor<B>>, BlockImportError>, B::Hash)>,
52		);
53		fn justification_imported(
54			&self,
55			who: PeerId,
56			hash: &B::Hash,
57			number: NumberFor<B>,
58			success: bool,
59		);
60		fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>);
61	}
62}
63
64impl<B: BlockT> sc_consensus::JustificationSyncLink<B> for MockChainSyncInterface<B> {
65	fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>) {
66		self.justification_sync_link_request_justification(hash, number);
67	}
68
69	fn clear_justification_requests(&self) {
70		self.justification_sync_link_clear_justification_requests();
71	}
72}
73
74mockall::mock! {
75	pub NetworkServiceHandle {}
76}
77
78// Mocked `Network` for `ChainSync`-related tests
79mockall::mock! {
80	pub Network {}
81
82	#[async_trait::async_trait]
83	impl NetworkPeers for Network {
84		fn set_authorized_peers(&self, peers: HashSet<PeerId>);
85		fn set_authorized_only(&self, reserved_only: bool);
86		fn add_known_address(&self, peer_id: PeerId, addr: Multiaddr);
87		fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange);
88		fn peer_reputation(&self, peer_id: &PeerId) -> i32;
89		fn disconnect_peer(&self, peer_id: PeerId, protocol: ProtocolName);
90		fn accept_unreserved_peers(&self);
91		fn deny_unreserved_peers(&self);
92		fn add_reserved_peer(&self, peer: MultiaddrWithPeerId) -> Result<(), String>;
93		fn remove_reserved_peer(&self, peer_id: PeerId);
94		fn set_reserved_peers(
95			&self,
96			protocol: ProtocolName,
97			peers: HashSet<Multiaddr>,
98		) -> Result<(), String>;
99		fn add_peers_to_reserved_set(
100			&self,
101			protocol: ProtocolName,
102			peers: HashSet<Multiaddr>,
103		) -> Result<(), String>;
104		fn remove_peers_from_reserved_set(
105			&self,
106			protocol: ProtocolName,
107			peers: Vec<PeerId>
108		) -> Result<(), String>;
109		fn sync_num_connected(&self) -> usize;
110		fn peer_role(&self, peer_id: PeerId, handshake: Vec<u8>) -> Option<ObservedRole>;
111		async fn reserved_peers(&self) -> Result<Vec<sc_network_types::PeerId>, ()>;
112	}
113
114	#[async_trait::async_trait]
115	impl NetworkRequest for Network {
116		async fn request(
117			&self,
118			target: PeerId,
119			protocol: ProtocolName,
120			request: Vec<u8>,
121			fallback_request: Option<(Vec<u8>, ProtocolName)>,
122			connect: IfDisconnected,
123		) -> Result<(Vec<u8>, ProtocolName), RequestFailure>;
124		fn start_request(
125			&self,
126			target: PeerId,
127			protocol: ProtocolName,
128			request: Vec<u8>,
129			fallback_request: Option<(Vec<u8>, ProtocolName)>,
130			tx: oneshot::Sender<Result<(Vec<u8>, ProtocolName), RequestFailure>>,
131			connect: IfDisconnected,
132		);
133	}
134}