Skip to content

Commit 4932709

Browse files
authored
A0-1586: At most one discovery message (#734)
* At most one discovery message * Clippy being helpful
1 parent 4a817b9 commit 4932709

File tree

2 files changed

+95
-99
lines changed

2 files changed

+95
-99
lines changed

finality-aleph/src/network/manager/discovery.rs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,20 @@ impl<M: Multiaddress> Discovery<M> {
6464
}
6565
}
6666

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.
6868
pub fn discover_authorities(
6969
&mut self,
7070
handler: &SessionHandler<M>,
71-
) -> Vec<DiscoveryCommand<M>> {
71+
) -> Option<DiscoveryCommand<M>> {
7272
let authentication = match handler.authentication() {
7373
Some(authentication) => authentication,
74-
None => return Vec::new(),
74+
None => return None,
7575
};
7676

7777
let missing_authorities = handler.missing_nodes();
7878
let node_count = handler.node_count();
7979
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))
8181
}
8282

8383
/// Checks the authentication using the handler and returns the addresses we should be
@@ -104,39 +104,37 @@ impl<M: Multiaddress> Discovery<M> {
104104
&mut self,
105105
authentication: Authentication<M>,
106106
handler: &mut SessionHandler<M>,
107-
) -> (Vec<M>, Vec<DiscoveryCommand<M>>) {
107+
) -> (Vec<M>, Option<DiscoveryCommand<M>>) {
108108
debug!(target: "aleph-network", "Handling broadcast with authentication {:?}.", authentication);
109109
let addresses = self.handle_authentication(authentication.clone(), handler);
110110
if addresses.is_empty() {
111-
return (Vec::new(), Vec::new());
111+
return (Vec::new(), None);
112112
}
113113
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);
119116
}
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)))
121120
}
122121

123122
/// 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.
126125
pub fn handle_message(
127126
&mut self,
128127
message: DiscoveryMessage<M>,
129128
handler: &mut SessionHandler<M>,
130-
) -> (Vec<M>, Vec<DiscoveryCommand<M>>) {
129+
) -> (Vec<M>, Option<DiscoveryCommand<M>>) {
131130
use DiscoveryMessage::*;
132131
match message {
133132
AuthenticationBroadcast(authentication) => {
134133
self.handle_broadcast(authentication, handler)
135134
}
136-
Authentication(authentication) => (
137-
self.handle_authentication(authentication, handler),
138-
Vec::new(),
139-
),
135+
Authentication(authentication) => {
136+
(self.handle_authentication(authentication, handler), None)
137+
}
140138
}
141139
}
142140
}
@@ -215,11 +213,9 @@ mod tests {
215213
for num_nodes in 2..NUM_NODES {
216214
let (mut discovery, mut handlers, _) = build_number(num_nodes).await;
217215
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);
221217
assert_eq!(
222-
message,
218+
message.expect("there is a discovery message"),
223219
(
224220
DiscoveryMessage::AuthenticationBroadcast(handler.authentication().unwrap()),
225221
DataCommand::Broadcast
@@ -231,41 +227,39 @@ mod tests {
231227
#[tokio::test]
232228
async fn non_validator_discover_authorities_returns_empty_vector() {
233229
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());
236232
}
237233

238234
#[tokio::test]
239235
async fn rebroadcasts_and_accepts_addresses() {
240236
let (mut discovery, mut handlers, _) = build().await;
241237
let authentication = handlers[1].authentication().unwrap();
242238
let handler = &mut handlers[0];
243-
let (addresses, commands) = discovery.handle_message(
239+
let (addresses, command) = discovery.handle_message(
244240
DiscoveryMessage::AuthenticationBroadcast(authentication.clone()),
245241
handler,
246242
);
247243
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((
250245
DiscoveryMessage::AuthenticationBroadcast(rebroadcast_authentication),
251246
DataCommand::Broadcast,
252-
) if rebroadcast_authentication == &authentication)));
247+
)) if rebroadcast_authentication == authentication));
253248
}
254249

255250
#[tokio::test]
256251
async fn non_validators_rebroadcasts() {
257252
let (mut discovery, handlers, mut non_validator) = build().await;
258253
let authentication = handlers[1].authentication().unwrap();
259-
let (addresses, commands) = discovery.handle_message(
254+
let (addresses, command) = discovery.handle_message(
260255
DiscoveryMessage::AuthenticationBroadcast(authentication.clone()),
261256
&mut non_validator,
262257
);
263258
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((
266260
DiscoveryMessage::AuthenticationBroadcast(rebroadcast_authentication),
267261
DataCommand::Broadcast,
268-
) if rebroadcast_authentication == &authentication)));
262+
)) if rebroadcast_authentication == authentication));
269263
}
270264

271265
#[tokio::test]
@@ -275,12 +269,12 @@ mod tests {
275269
let (_, signature) = handlers[2].authentication().unwrap();
276270
let authentication = (auth_data, signature);
277271
let handler = &mut handlers[0];
278-
let (addresses, commands) = discovery.handle_message(
272+
let (addresses, command) = discovery.handle_message(
279273
DiscoveryMessage::AuthenticationBroadcast(authentication),
280274
handler,
281275
);
282276
assert!(addresses.is_empty());
283-
assert!(commands.is_empty());
277+
assert!(command.is_none());
284278
}
285279

286280
#[tokio::test]
@@ -293,15 +287,15 @@ mod tests {
293287
handler,
294288
);
295289
sleep(Duration::from_millis(MS_COOLDOWN + 5));
296-
let (addresses, commands) = discovery.handle_message(
290+
let (addresses, command) = discovery.handle_message(
297291
DiscoveryMessage::AuthenticationBroadcast(authentication.clone()),
298292
handler,
299293
);
300294
assert_eq!(addresses, authentication.0.addresses());
301-
assert!(commands.iter().any(|command| matches!(command, (
295+
assert!(matches!(command, Some((
302296
DiscoveryMessage::AuthenticationBroadcast(rebroadcast_authentication),
303297
DataCommand::Broadcast,
304-
) if rebroadcast_authentication == &authentication)));
298+
)) if rebroadcast_authentication == authentication));
305299
}
306300

307301
#[tokio::test]
@@ -310,12 +304,12 @@ mod tests {
310304
let expected_address = handlers[1].authentication().unwrap().0.addresses()[0].encode();
311305
let authentication = handlers[1].authentication().unwrap();
312306
let handler = &mut handlers[0];
313-
let (addresses, commands) =
307+
let (addresses, command) =
314308
discovery.handle_message(DiscoveryMessage::Authentication(authentication), handler);
315309
assert_eq!(addresses.len(), 1);
316310
let address = addresses[0].encode();
317311
assert_eq!(address, expected_address);
318-
assert!(commands.is_empty());
312+
assert!(command.is_none());
319313
}
320314

321315
#[tokio::test]
@@ -325,11 +319,11 @@ mod tests {
325319
let (_, signature) = handlers[2].authentication().unwrap();
326320
let incorrect_authentication = (auth_data, signature);
327321
let handler = &mut handlers[0];
328-
let (addresses, commands) = discovery.handle_message(
322+
let (addresses, command) = discovery.handle_message(
329323
DiscoveryMessage::Authentication(incorrect_authentication),
330324
handler,
331325
);
332326
assert!(addresses.is_empty());
333-
assert!(commands.is_empty());
327+
assert!(command.is_none());
334328
}
335329
}

0 commit comments

Comments
 (0)