@@ -7,98 +7,43 @@ use std::{
77
88use aleph_primitives:: KEY_TYPE ;
99use async_trait:: async_trait;
10- use codec:: { Decode , Encode } ;
1110use futures:: {
1211 channel:: { mpsc, oneshot} ,
1312 StreamExt ,
1413} ;
1514use parking_lot:: Mutex ;
16- use rand:: random;
1715use sp_keystore:: { testing:: KeyStore , CryptoStore } ;
1816use tokio:: time:: timeout;
1917
2018use crate :: {
2119 crypto:: { AuthorityPen , AuthorityVerifier } ,
2220 network:: {
2321 manager:: VersionedAuthentication , AddressedData , ConnectionCommand , Event , EventStream ,
24- Multiaddress , Network , NetworkIdentity , NetworkSender , NetworkServiceIO , PeerId , Protocol ,
22+ Multiaddress , Network , NetworkIdentity , NetworkSender , NetworkServiceIO , Protocol ,
2523 } ,
24+ testing:: mocks:: validator_network:: { random_identity, MockMultiaddress } ,
25+ validator_network:: mock:: MockPublicKey ,
2626 AuthorityId , NodeIndex ,
2727} ;
2828
29- #[ derive( PartialEq , Eq , Copy , Clone , Debug , Hash , Encode , Decode ) ]
30- pub struct MockPeerId ( u32 ) ;
31-
32- impl MockPeerId {
33- pub fn random ( ) -> Self {
34- MockPeerId ( random ( ) )
35- }
36- }
37- impl fmt:: Display for MockPeerId {
38- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
39- write ! ( f, "{}" , self . 0 )
40- }
41- }
42-
43- impl PeerId for MockPeerId { }
44-
45- #[ derive( PartialEq , Eq , Clone , Debug , Hash , Encode , Decode ) ]
46- pub struct MockMultiaddress {
47- peer_id : Option < MockPeerId > ,
48- address : u32 ,
49- }
50-
51- impl MockMultiaddress {
52- pub fn random_with_id ( peer_id : MockPeerId ) -> Self {
53- MockMultiaddress {
54- peer_id : Some ( peer_id) ,
55- address : random ( ) ,
56- }
57- }
58- }
59-
60- impl Multiaddress for MockMultiaddress {
61- type PeerId = MockPeerId ;
62-
63- fn get_peer_id ( & self ) -> Option < Self :: PeerId > {
64- self . peer_id
65- }
66-
67- fn add_matching_peer_id ( mut self , peer_id : Self :: PeerId ) -> Option < Self > {
68- match self . peer_id {
69- Some ( old_peer_id) => match old_peer_id == peer_id {
70- true => Some ( self ) ,
71- false => None ,
72- } ,
73- None => {
74- self . peer_id = Some ( peer_id) ;
75- Some ( self )
76- }
77- }
78- }
79- }
80-
8129pub struct MockNetworkIdentity {
8230 addresses : Vec < MockMultiaddress > ,
83- peer_id : MockPeerId ,
31+ peer_id : MockPublicKey ,
8432}
8533
8634impl MockNetworkIdentity {
8735 pub fn new ( ) -> Self {
88- let peer_id = MockPeerId :: random ( ) ;
89- let addresses = ( 0 ..3 )
90- . map ( |_| MockMultiaddress :: random_with_id ( peer_id) )
91- . collect ( ) ;
36+ let ( addresses, peer_id) = random_identity ( ) ;
9237 MockNetworkIdentity { addresses, peer_id }
9338 }
9439}
9540
9641impl NetworkIdentity for MockNetworkIdentity {
97- type PeerId = MockPeerId ;
42+ type PeerId = MockPublicKey ;
9843 type Multiaddress = MockMultiaddress ;
9944
10045 fn identity ( & self ) -> ( Vec < Self :: Multiaddress > , Self :: PeerId ) {
101- ( self . addresses . clone ( ) , self . peer_id )
46+ ( self . addresses . clone ( ) , self . peer_id . clone ( ) )
10247 }
10348}
10449
@@ -133,7 +78,7 @@ impl<T> Channel<T> {
13378 self . 1 . lock ( ) . await . by_ref ( ) . take ( n) . collect :: < Vec < _ > > ( ) ,
13479 )
13580 . await
136- . unwrap_or ( Vec :: new ( ) )
81+ . unwrap_or_default ( )
13782 }
13883
13984 pub async fn try_next ( & self ) -> Option < T > {
@@ -152,7 +97,7 @@ impl<T> Default for Channel<T> {
15297 }
15398}
15499
155- pub type MockEvent = Event < MockMultiaddress , MockPeerId > ;
100+ pub type MockEvent = Event < MockMultiaddress , MockPublicKey > ;
156101
157102pub type MockData = Vec < u8 > ;
158103
@@ -196,15 +141,15 @@ impl<M: Multiaddress + 'static> MockIO<M> {
196141pub struct MockEventStream ( mpsc:: UnboundedReceiver < MockEvent > ) ;
197142
198143#[ async_trait]
199- impl EventStream < MockMultiaddress , MockPeerId > for MockEventStream {
144+ impl EventStream < MockMultiaddress , MockPublicKey > for MockEventStream {
200145 async fn next_event ( & mut self ) -> Option < MockEvent > {
201146 self . 0 . next ( ) . await
202147 }
203148}
204149
205150pub struct MockNetworkSender {
206- sender : mpsc:: UnboundedSender < ( Vec < u8 > , MockPeerId , Protocol ) > ,
207- peer_id : MockPeerId ,
151+ sender : mpsc:: UnboundedSender < ( Vec < u8 > , MockPublicKey , Protocol ) > ,
152+ peer_id : MockPublicKey ,
208153 protocol : Protocol ,
209154 error : Result < ( ) , MockSenderError > ,
210155}
@@ -219,7 +164,7 @@ impl NetworkSender for MockNetworkSender {
219164 ) -> Result < ( ) , MockSenderError > {
220165 self . error ?;
221166 self . sender
222- . unbounded_send ( ( data. into ( ) , self . peer_id , self . protocol ) )
167+ . unbounded_send ( ( data. into ( ) , self . peer_id . clone ( ) , self . protocol ) )
223168 . unwrap ( ) ;
224169 Ok ( ( ) )
225170 }
@@ -228,8 +173,8 @@ impl NetworkSender for MockNetworkSender {
228173#[ derive( Clone ) ]
229174pub struct MockNetwork {
230175 pub add_reserved : Channel < ( HashSet < MockMultiaddress > , Protocol ) > ,
231- pub remove_reserved : Channel < ( HashSet < MockPeerId > , Protocol ) > ,
232- pub send_message : Channel < ( Vec < u8 > , MockPeerId , Protocol ) > ,
176+ pub remove_reserved : Channel < ( HashSet < MockPublicKey > , Protocol ) > ,
177+ pub send_message : Channel < ( Vec < u8 > , MockPublicKey , Protocol ) > ,
233178 pub event_sinks : Arc < Mutex < Vec < mpsc:: UnboundedSender < MockEvent > > > > ,
234179 event_stream_taken_oneshot : Arc < Mutex < Option < oneshot:: Sender < ( ) > > > > ,
235180 pub create_sender_errors : Arc < Mutex < VecDeque < MockSenderError > > > ,
@@ -256,7 +201,7 @@ impl std::error::Error for MockSenderError {}
256201impl Network for MockNetwork {
257202 type SenderError = MockSenderError ;
258203 type NetworkSender = MockNetworkSender ;
259- type PeerId = MockPeerId ;
204+ type PeerId = MockPublicKey ;
260205 type Multiaddress = MockMultiaddress ;
261206 type EventStream = MockEventStream ;
262207
0 commit comments