11use std:: {
22 collections:: HashMap ,
3- fmt:: Debug ,
43 marker:: PhantomData ,
54 time:: { Duration , Instant } ,
65} ;
@@ -9,39 +8,34 @@ use log::{debug, info, trace};
98
109use crate :: {
1110 network:: {
12- session:: {
13- compatibility:: PeerAuthentications , Authentication , LegacyAuthentication ,
14- SessionHandler ,
15- } ,
16- AddressingInformation , Data ,
11+ session:: { Authentication , SessionHandler } ,
12+ AddressingInformation ,
1713 } ,
1814 NodeIndex ,
1915} ;
2016
2117/// Handles creating and rebroadcasting discovery messages.
22- pub struct Discovery < M : Data , A : AddressingInformation + TryFrom < Vec < M > > + Into < Vec < M > > > {
18+ pub struct Discovery < A : AddressingInformation > {
2319 cooldown : Duration ,
2420 last_broadcast : HashMap < NodeIndex , Instant > ,
25- last_legacy_broadcast : HashMap < NodeIndex , Instant > ,
26- _phantom : PhantomData < ( M , A ) > ,
21+ _phantom : PhantomData < A > ,
2722}
2823
29- impl < M : Data + Debug , A : AddressingInformation + TryFrom < Vec < M > > + Into < Vec < M > > > Discovery < M , A > {
24+ impl < A : AddressingInformation > Discovery < A > {
3025 /// Create a new discovery handler with the given response/broadcast cooldown.
3126 pub fn new ( cooldown : Duration ) -> Self {
3227 Discovery {
3328 cooldown,
3429 last_broadcast : HashMap :: new ( ) ,
35- last_legacy_broadcast : HashMap :: new ( ) ,
3630 _phantom : PhantomData ,
3731 }
3832 }
3933
4034 /// Returns a message that should be sent as part of authority discovery at this moment.
4135 pub fn discover_authorities (
4236 & mut self ,
43- handler : & SessionHandler < M , A > ,
44- ) -> Option < PeerAuthentications < M , A > > {
37+ handler : & SessionHandler < A > ,
38+ ) -> Option < Authentication < A > > {
4539 let authentication = match handler. authentication ( ) {
4640 Some ( authentication) => authentication,
4741 None => return None ,
@@ -60,21 +54,14 @@ impl<M: Data + Debug, A: AddressingInformation + TryFrom<Vec<M>> + Into<Vec<M>>>
6054 }
6155 }
6256
63- fn should_legacy_rebroadcast ( & self , node_id : & NodeIndex ) -> bool {
64- match self . last_legacy_broadcast . get ( node_id) {
65- Some ( instant) => Instant :: now ( ) > * instant + self . cooldown ,
66- None => true ,
67- }
68- }
69-
7057 /// Processes the provided authentication and returns any new address we should
7158 /// be connected to if we want to stay connected to the committee and an optional
7259 /// message that we should send as a result of it.
7360 pub fn handle_authentication (
7461 & mut self ,
7562 authentication : Authentication < A > ,
76- handler : & mut SessionHandler < M , A > ,
77- ) -> ( Option < A > , Option < PeerAuthentications < M , A > > ) {
63+ handler : & mut SessionHandler < A > ,
64+ ) -> ( Option < A > , Option < Authentication < A > > ) {
7865 debug ! ( target: "aleph-network" , "Handling broadcast with authentication {:?}." , authentication) ;
7966 let address = match handler. handle_authentication ( authentication. clone ( ) ) {
8067 Some ( address) => Some ( address) ,
@@ -86,32 +73,7 @@ impl<M: Data + Debug, A: AddressingInformation + TryFrom<Vec<M>> + Into<Vec<M>>>
8673 }
8774 trace ! ( target: "aleph-network" , "Rebroadcasting {:?}." , authentication) ;
8875 self . last_broadcast . insert ( node_id, Instant :: now ( ) ) ;
89- ( address, Some ( PeerAuthentications :: NewOnly ( authentication) ) )
90- }
91-
92- /// Processes the legacy authentication and returns any new address we should
93- /// be connected to if we want to stay connected to the committee and an optional
94- /// message that we should send as a result of it.
95- pub fn handle_legacy_authentication (
96- & mut self ,
97- legacy_authentication : LegacyAuthentication < M > ,
98- handler : & mut SessionHandler < M , A > ,
99- ) -> ( Option < A > , Option < PeerAuthentications < M , A > > ) {
100- debug ! ( target: "aleph-network" , "Handling broadcast with legacy authentication {:?}." , legacy_authentication) ;
101- let address = match handler. handle_legacy_authentication ( legacy_authentication. clone ( ) ) {
102- Some ( address) => Some ( address) ,
103- None => return ( None , None ) ,
104- } ;
105- let node_id = legacy_authentication. 0 . creator ( ) ;
106- if !self . should_legacy_rebroadcast ( & node_id) {
107- return ( address, None ) ;
108- }
109- trace ! ( target: "aleph-network" , "Rebroadcasting {:?}." , legacy_authentication) ;
110- self . last_legacy_broadcast . insert ( node_id, Instant :: now ( ) ) ;
111- (
112- address,
113- Some ( PeerAuthentications :: LegacyOnly ( legacy_authentication) ) ,
114- )
76+ ( address, Some ( authentication) )
11577 }
11678}
11779
@@ -124,10 +86,7 @@ mod tests {
12486 network:: {
12587 clique:: mock:: { random_address, MockAddressingInformation } ,
12688 mock:: crypto_basics,
127- session:: {
128- authentication, compatibility:: PeerAuthentications , legacy_authentication,
129- SessionHandler ,
130- } ,
89+ session:: { authentication, Authentication , SessionHandler } ,
13190 } ,
13291 SessionId ,
13392 } ;
@@ -142,9 +101,9 @@ mod tests {
142101 async fn build_number (
143102 num_nodes : u8 ,
144103 ) -> (
145- Discovery < MockAddressingInformation , MockAddressingInformation > ,
146- Vec < SessionHandler < MockAddressingInformation , MockAddressingInformation > > ,
147- SessionHandler < MockAddressingInformation , MockAddressingInformation > ,
104+ Discovery < MockAddressingInformation > ,
105+ Vec < SessionHandler < MockAddressingInformation > > ,
106+ SessionHandler < MockAddressingInformation > ,
148107 ) {
149108 let crypto_basics = crypto_basics ( num_nodes. into ( ) ) . await ;
150109 let mut handlers = Vec :: new ( ) ;
@@ -174,9 +133,9 @@ mod tests {
174133 }
175134
176135 async fn build ( ) -> (
177- Discovery < MockAddressingInformation , MockAddressingInformation > ,
178- Vec < SessionHandler < MockAddressingInformation , MockAddressingInformation > > ,
179- SessionHandler < MockAddressingInformation , MockAddressingInformation > ,
136+ Discovery < MockAddressingInformation > ,
137+ Vec < SessionHandler < MockAddressingInformation > > ,
138+ SessionHandler < MockAddressingInformation > ,
180139 ) {
181140 build_number ( NUM_NODES ) . await
182141 }
@@ -211,18 +170,7 @@ mod tests {
211170 let ( address, command) = discovery. handle_authentication ( authentication. clone ( ) , handler) ;
212171 assert_eq ! ( address, Some ( authentication. 0 . address( ) ) ) ;
213172 assert ! ( matches!( command, Some (
214- PeerAuthentications :: NewOnly ( rebroadcast_authentication) ,
215- ) if rebroadcast_authentication == authentication) ) ;
216- }
217-
218- #[ tokio:: test]
219- async fn legacy_rebroadcasts_and_accepts_addresses ( ) {
220- let ( mut discovery, mut handlers, _) = build ( ) . await ;
221- let authentication = legacy_authentication ( & handlers[ 1 ] ) ;
222- let handler = & mut handlers[ 0 ] ;
223- let ( _, command) = discovery. handle_legacy_authentication ( authentication. clone ( ) , handler) ;
224- assert ! ( matches!( command, Some (
225- PeerAuthentications :: LegacyOnly ( rebroadcast_authentication) ,
173+ rebroadcast_authentication,
226174 ) if rebroadcast_authentication == authentication) ) ;
227175 }
228176
@@ -234,45 +182,22 @@ mod tests {
234182 discovery. handle_authentication ( authentication. clone ( ) , & mut non_validator) ;
235183 assert_eq ! ( address, Some ( authentication. 0 . address( ) ) ) ;
236184 assert ! ( matches!( command, Some (
237- PeerAuthentications :: NewOnly ( rebroadcast_authentication) ,
238- ) if rebroadcast_authentication == authentication) ) ;
239- }
240-
241- #[ tokio:: test]
242- async fn legacy_non_validator_rebroadcasts ( ) {
243- let ( mut discovery, handlers, mut non_validator) = build ( ) . await ;
244- let authentication = legacy_authentication ( & handlers[ 1 ] ) ;
245- let ( _, command) =
246- discovery. handle_legacy_authentication ( authentication. clone ( ) , & mut non_validator) ;
247- assert ! ( matches!( command, Some (
248- PeerAuthentications :: LegacyOnly ( rebroadcast_authentication) ,
185+ rebroadcast_authentication,
249186 ) if rebroadcast_authentication == authentication) ) ;
250187 }
251188
252189 #[ tokio:: test]
253190 async fn does_not_rebroadcast_wrong_authentications ( ) {
254191 let ( mut discovery, mut handlers, _) = build ( ) . await ;
255- let ( auth_data, _) = authentication ( & handlers[ 1 ] ) ;
256- let ( _, signature) = authentication ( & handlers[ 2 ] ) ;
257- let authentication = ( auth_data, signature) ;
192+ let Authentication ( auth_data, _) = authentication ( & handlers[ 1 ] ) ;
193+ let Authentication ( _, signature) = authentication ( & handlers[ 2 ] ) ;
194+ let authentication = Authentication ( auth_data, signature) ;
258195 let handler = & mut handlers[ 0 ] ;
259196 let ( address, command) = discovery. handle_authentication ( authentication, handler) ;
260197 assert ! ( address. is_none( ) ) ;
261198 assert ! ( command. is_none( ) ) ;
262199 }
263200
264- #[ tokio:: test]
265- async fn legacy_does_not_rebroadcast_wrong_authentications ( ) {
266- let ( mut discovery, mut handlers, _) = build ( ) . await ;
267- let ( auth_data, _) = legacy_authentication ( & handlers[ 1 ] ) ;
268- let ( _, signature) = legacy_authentication ( & handlers[ 2 ] ) ;
269- let authentication = ( auth_data, signature) ;
270- let handler = & mut handlers[ 0 ] ;
271- let ( address, command) = discovery. handle_legacy_authentication ( authentication, handler) ;
272- assert ! ( address. is_none( ) ) ;
273- assert ! ( command. is_none( ) ) ;
274- }
275-
276201 #[ tokio:: test]
277202 async fn rebroadcasts_after_cooldown ( ) {
278203 let ( mut discovery, mut handlers, _) = build ( ) . await ;
@@ -283,34 +208,7 @@ mod tests {
283208 let ( address, command) = discovery. handle_authentication ( authentication. clone ( ) , handler) ;
284209 assert_eq ! ( address, Some ( authentication. 0 . address( ) ) ) ;
285210 assert ! ( matches!( command, Some (
286- PeerAuthentications :: NewOnly ( rebroadcast_authentication) ,
287- ) if rebroadcast_authentication == authentication) ) ;
288- }
289-
290- #[ tokio:: test]
291- async fn legacy_rebroadcasts_after_cooldown ( ) {
292- let ( mut discovery, mut handlers, _) = build ( ) . await ;
293- let authentication = legacy_authentication ( & handlers[ 1 ] ) ;
294- let handler = & mut handlers[ 0 ] ;
295- discovery. handle_legacy_authentication ( authentication. clone ( ) , handler) ;
296- sleep ( Duration :: from_millis ( MS_COOLDOWN + 5 ) ) ;
297- let ( _, command) = discovery. handle_legacy_authentication ( authentication. clone ( ) , handler) ;
298- assert ! ( matches!( command, Some (
299- PeerAuthentications :: LegacyOnly ( rebroadcast_authentication) ,
211+ rebroadcast_authentication,
300212 ) if rebroadcast_authentication == authentication) ) ;
301213 }
302-
303- #[ tokio:: test]
304- async fn rebroadcasts_legacy_immediately ( ) {
305- let ( mut discovery, mut handlers, _) = build ( ) . await ;
306- let authentication = authentication ( & handlers[ 1 ] ) ;
307- let legacy_authentication = legacy_authentication ( & handlers[ 1 ] ) ;
308- let handler = & mut handlers[ 0 ] ;
309- discovery. handle_authentication ( authentication, handler) ;
310- let ( _, command) =
311- discovery. handle_legacy_authentication ( legacy_authentication. clone ( ) , handler) ;
312- assert ! ( matches!( command, Some (
313- PeerAuthentications :: LegacyOnly ( rebroadcast_authentication) ,
314- ) if rebroadcast_authentication == legacy_authentication) ) ;
315- }
316214}
0 commit comments