sc_network_sync/service/
mock.rs1use 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
78mockall::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}