@@ -64,20 +64,20 @@ impl<M: Multiaddress> Discovery<M> {
64
64
}
65
65
}
66
66
67
- /// Returns messages that should be sent as part of authority discovery at this moment.
67
+ /// Returns a message that should be sent as part of authority discovery at this moment.
68
68
pub fn discover_authorities (
69
69
& mut self ,
70
70
handler : & SessionHandler < M > ,
71
- ) -> Vec < DiscoveryCommand < M > > {
71
+ ) -> Option < DiscoveryCommand < M > > {
72
72
let authentication = match handler. authentication ( ) {
73
73
Some ( authentication) => authentication,
74
- None => return Vec :: new ( ) ,
74
+ None => return None ,
75
75
} ;
76
76
77
77
let missing_authorities = handler. missing_nodes ( ) ;
78
78
let node_count = handler. node_count ( ) ;
79
79
info ! ( target: "aleph-network" , "{}/{} authorities known for session {}." , node_count. 0 -missing_authorities. len( ) , node_count. 0 , handler. session_id( ) . 0 ) ;
80
- vec ! [ authentication_broadcast( authentication) ]
80
+ Some ( authentication_broadcast ( authentication) )
81
81
}
82
82
83
83
/// Checks the authentication using the handler and returns the addresses we should be
@@ -104,39 +104,37 @@ impl<M: Multiaddress> Discovery<M> {
104
104
& mut self ,
105
105
authentication : Authentication < M > ,
106
106
handler : & mut SessionHandler < M > ,
107
- ) -> ( Vec < M > , Vec < DiscoveryCommand < M > > ) {
107
+ ) -> ( Vec < M > , Option < DiscoveryCommand < M > > ) {
108
108
debug ! ( target: "aleph-network" , "Handling broadcast with authentication {:?}." , authentication) ;
109
109
let addresses = self . handle_authentication ( authentication. clone ( ) , handler) ;
110
110
if addresses. is_empty ( ) {
111
- return ( Vec :: new ( ) , Vec :: new ( ) ) ;
111
+ return ( Vec :: new ( ) , None ) ;
112
112
}
113
113
let node_id = authentication. 0 . creator ( ) ;
114
- let mut messages = Vec :: new ( ) ;
115
- if self . should_rebroadcast ( & node_id) {
116
- trace ! ( target: "aleph-network" , "Rebroadcasting {:?}." , authentication) ;
117
- self . last_broadcast . insert ( node_id, Instant :: now ( ) ) ;
118
- messages. push ( authentication_broadcast ( authentication) ) ;
114
+ if !self . should_rebroadcast ( & node_id) {
115
+ return ( addresses, None ) ;
119
116
}
120
- ( addresses, messages)
117
+ trace ! ( target: "aleph-network" , "Rebroadcasting {:?}." , authentication) ;
118
+ self . last_broadcast . insert ( node_id, Instant :: now ( ) ) ;
119
+ ( addresses, Some ( authentication_broadcast ( authentication) ) )
121
120
}
122
121
123
122
/// Analyzes the provided message and returns all the new multiaddresses we should
124
- /// be connected to if we want to stay connected to the committee and any messages
125
- /// that we should send as a result of it.
123
+ /// be connected to if we want to stay connected to the committee and an optional
124
+ /// message that we should send as a result of it.
126
125
pub fn handle_message (
127
126
& mut self ,
128
127
message : DiscoveryMessage < M > ,
129
128
handler : & mut SessionHandler < M > ,
130
- ) -> ( Vec < M > , Vec < DiscoveryCommand < M > > ) {
129
+ ) -> ( Vec < M > , Option < DiscoveryCommand < M > > ) {
131
130
use DiscoveryMessage :: * ;
132
131
match message {
133
132
AuthenticationBroadcast ( authentication) => {
134
133
self . handle_broadcast ( authentication, handler)
135
134
}
136
- Authentication ( authentication) => (
137
- self . handle_authentication ( authentication, handler) ,
138
- Vec :: new ( ) ,
139
- ) ,
135
+ Authentication ( authentication) => {
136
+ ( self . handle_authentication ( authentication, handler) , None )
137
+ }
140
138
}
141
139
}
142
140
}
@@ -215,11 +213,9 @@ mod tests {
215
213
for num_nodes in 2 ..NUM_NODES {
216
214
let ( mut discovery, mut handlers, _) = build_number ( num_nodes) . await ;
217
215
let handler = & mut handlers[ 0 ] ;
218
- let mut messages = discovery. discover_authorities ( handler) ;
219
- assert_eq ! ( messages. len( ) , 1 ) ;
220
- let message = messages. pop ( ) . unwrap ( ) ;
216
+ let message = discovery. discover_authorities ( handler) ;
221
217
assert_eq ! (
222
- message,
218
+ message. expect ( "there is a discovery message" ) ,
223
219
(
224
220
DiscoveryMessage :: AuthenticationBroadcast ( handler. authentication( ) . unwrap( ) ) ,
225
221
DataCommand :: Broadcast
@@ -231,41 +227,39 @@ mod tests {
231
227
#[ tokio:: test]
232
228
async fn non_validator_discover_authorities_returns_empty_vector ( ) {
233
229
let ( mut discovery, _, non_validator) = build ( ) . await ;
234
- let messages = discovery. discover_authorities ( & non_validator) ;
235
- assert ! ( messages . is_empty ( ) ) ;
230
+ let message = discovery. discover_authorities ( & non_validator) ;
231
+ assert ! ( message . is_none ( ) ) ;
236
232
}
237
233
238
234
#[ tokio:: test]
239
235
async fn rebroadcasts_and_accepts_addresses ( ) {
240
236
let ( mut discovery, mut handlers, _) = build ( ) . await ;
241
237
let authentication = handlers[ 1 ] . authentication ( ) . unwrap ( ) ;
242
238
let handler = & mut handlers[ 0 ] ;
243
- let ( addresses, commands ) = discovery. handle_message (
239
+ let ( addresses, command ) = discovery. handle_message (
244
240
DiscoveryMessage :: AuthenticationBroadcast ( authentication. clone ( ) ) ,
245
241
handler,
246
242
) ;
247
243
assert_eq ! ( addresses, authentication. 0 . addresses( ) ) ;
248
- assert_eq ! ( commands. len( ) , 1 ) ;
249
- assert ! ( commands. iter( ) . any( |command| matches!( command, (
244
+ assert ! ( matches!( command, Some ( (
250
245
DiscoveryMessage :: AuthenticationBroadcast ( rebroadcast_authentication) ,
251
246
DataCommand :: Broadcast ,
252
- ) if rebroadcast_authentication == & authentication) ) ) ;
247
+ ) ) if rebroadcast_authentication == authentication) ) ;
253
248
}
254
249
255
250
#[ tokio:: test]
256
251
async fn non_validators_rebroadcasts ( ) {
257
252
let ( mut discovery, handlers, mut non_validator) = build ( ) . await ;
258
253
let authentication = handlers[ 1 ] . authentication ( ) . unwrap ( ) ;
259
- let ( addresses, commands ) = discovery. handle_message (
254
+ let ( addresses, command ) = discovery. handle_message (
260
255
DiscoveryMessage :: AuthenticationBroadcast ( authentication. clone ( ) ) ,
261
256
& mut non_validator,
262
257
) ;
263
258
assert_eq ! ( addresses, authentication. 0 . addresses( ) ) ;
264
- assert_eq ! ( commands. len( ) , 1 ) ;
265
- assert ! ( commands. iter( ) . any( |command| matches!( command, (
259
+ assert ! ( matches!( command, Some ( (
266
260
DiscoveryMessage :: AuthenticationBroadcast ( rebroadcast_authentication) ,
267
261
DataCommand :: Broadcast ,
268
- ) if rebroadcast_authentication == & authentication) ) ) ;
262
+ ) ) if rebroadcast_authentication == authentication) ) ;
269
263
}
270
264
271
265
#[ tokio:: test]
@@ -275,12 +269,12 @@ mod tests {
275
269
let ( _, signature) = handlers[ 2 ] . authentication ( ) . unwrap ( ) ;
276
270
let authentication = ( auth_data, signature) ;
277
271
let handler = & mut handlers[ 0 ] ;
278
- let ( addresses, commands ) = discovery. handle_message (
272
+ let ( addresses, command ) = discovery. handle_message (
279
273
DiscoveryMessage :: AuthenticationBroadcast ( authentication) ,
280
274
handler,
281
275
) ;
282
276
assert ! ( addresses. is_empty( ) ) ;
283
- assert ! ( commands . is_empty ( ) ) ;
277
+ assert ! ( command . is_none ( ) ) ;
284
278
}
285
279
286
280
#[ tokio:: test]
@@ -293,15 +287,15 @@ mod tests {
293
287
handler,
294
288
) ;
295
289
sleep ( Duration :: from_millis ( MS_COOLDOWN + 5 ) ) ;
296
- let ( addresses, commands ) = discovery. handle_message (
290
+ let ( addresses, command ) = discovery. handle_message (
297
291
DiscoveryMessage :: AuthenticationBroadcast ( authentication. clone ( ) ) ,
298
292
handler,
299
293
) ;
300
294
assert_eq ! ( addresses, authentication. 0 . addresses( ) ) ;
301
- assert ! ( commands . iter ( ) . any ( |command| matches!( command, (
295
+ assert ! ( matches!( command, Some ( (
302
296
DiscoveryMessage :: AuthenticationBroadcast ( rebroadcast_authentication) ,
303
297
DataCommand :: Broadcast ,
304
- ) if rebroadcast_authentication == & authentication) ) ) ;
298
+ ) ) if rebroadcast_authentication == authentication) ) ;
305
299
}
306
300
307
301
#[ tokio:: test]
@@ -310,12 +304,12 @@ mod tests {
310
304
let expected_address = handlers[ 1 ] . authentication ( ) . unwrap ( ) . 0 . addresses ( ) [ 0 ] . encode ( ) ;
311
305
let authentication = handlers[ 1 ] . authentication ( ) . unwrap ( ) ;
312
306
let handler = & mut handlers[ 0 ] ;
313
- let ( addresses, commands ) =
307
+ let ( addresses, command ) =
314
308
discovery. handle_message ( DiscoveryMessage :: Authentication ( authentication) , handler) ;
315
309
assert_eq ! ( addresses. len( ) , 1 ) ;
316
310
let address = addresses[ 0 ] . encode ( ) ;
317
311
assert_eq ! ( address, expected_address) ;
318
- assert ! ( commands . is_empty ( ) ) ;
312
+ assert ! ( command . is_none ( ) ) ;
319
313
}
320
314
321
315
#[ tokio:: test]
@@ -325,11 +319,11 @@ mod tests {
325
319
let ( _, signature) = handlers[ 2 ] . authentication ( ) . unwrap ( ) ;
326
320
let incorrect_authentication = ( auth_data, signature) ;
327
321
let handler = & mut handlers[ 0 ] ;
328
- let ( addresses, commands ) = discovery. handle_message (
322
+ let ( addresses, command ) = discovery. handle_message (
329
323
DiscoveryMessage :: Authentication ( incorrect_authentication) ,
330
324
handler,
331
325
) ;
332
326
assert ! ( addresses. is_empty( ) ) ;
333
- assert ! ( commands . is_empty ( ) ) ;
327
+ assert ! ( command . is_none ( ) ) ;
334
328
}
335
329
}
0 commit comments