Skip to content

Commit 70cfeff

Browse files
authored
Avoid self-lookups in Authority Discovery (paritytech#6317)
* Ensure authority discovery avoids self-lookups. Thereby additionally guard the `NetworkService` against adding the local peer to the PSM or registering a "known address" for the local peer. * Clarify comments. * See if returning errors is ok.
1 parent 8aeda51 commit 70cfeff

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

client/authority-discovery/src/lib.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,26 @@ where
294294
.authorities(&id)
295295
.map_err(Error::CallingRuntime)?;
296296

297+
let local_keys = match &self.role {
298+
Role::Authority(key_store) => {
299+
key_store.read()
300+
.sr25519_public_keys(key_types::AUTHORITY_DISCOVERY)
301+
.into_iter()
302+
.collect::<HashSet<_>>()
303+
},
304+
Role::Sentry => HashSet::new(),
305+
};
306+
297307
for authority_id in authorities.iter() {
298-
if let Some(metrics) = &self.metrics {
299-
metrics.request.inc();
300-
}
308+
// Make sure we don't look up our own keys.
309+
if !local_keys.contains(authority_id.as_ref()) {
310+
if let Some(metrics) = &self.metrics {
311+
metrics.request.inc();
312+
}
301313

302-
self.network
303-
.get_value(&hash_authority_id(authority_id.as_ref()));
314+
self.network
315+
.get_value(&hash_authority_id(authority_id.as_ref()));
316+
}
304317
}
305318

306319
Ok(())

client/network/src/service.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,15 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
672672

673673
/// Adds a `PeerId` and its address as reserved. The string should encode the address
674674
/// and peer ID of the remote node.
675+
///
676+
/// Returns an `Err` if the given string is not a valid multiaddress
677+
/// or contains an invalid peer ID (which includes the local peer ID).
675678
pub fn add_reserved_peer(&self, peer: String) -> Result<(), String> {
676679
let (peer_id, addr) = parse_str_addr(&peer).map_err(|e| format!("{:?}", e))?;
680+
// Make sure the local peer ID is never added to the PSM.
681+
if peer_id == self.local_peer_id {
682+
return Err("Local peer ID cannot be added as a reserved peer.".to_string())
683+
}
677684
self.peerset.add_reserved_peer(peer_id.clone());
678685
let _ = self
679686
.to_worker
@@ -694,12 +701,26 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
694701
}
695702

696703
/// Modify a peerset priority group.
704+
///
705+
/// Returns an `Err` if one of the given addresses contains an invalid
706+
/// peer ID (which includes the local peer ID).
697707
pub fn set_priority_group(&self, group_id: String, peers: HashSet<Multiaddr>) -> Result<(), String> {
698-
let peers = peers.into_iter().map(|p| {
699-
parse_addr(p).map_err(|e| format!("{:?}", e))
700-
}).collect::<Result<Vec<(PeerId, Multiaddr)>, String>>()?;
708+
let peers = peers.into_iter()
709+
.map(|p| match parse_addr(p) {
710+
Err(e) => Err(format!("{:?}", e)),
711+
Ok((peer, addr)) =>
712+
// Make sure the local peer ID is never added to the PSM
713+
// or added as a "known address", even if given.
714+
if peer == self.local_peer_id {
715+
Err("Local peer ID in priority group.".to_string())
716+
} else {
717+
Ok((peer, addr))
718+
}
719+
})
720+
.collect::<Result<Vec<(PeerId, Multiaddr)>, String>>()?;
701721

702722
let peer_ids = peers.iter().map(|(peer_id, _addr)| peer_id.clone()).collect();
723+
703724
self.peerset.set_priority_group(group_id, peer_ids);
704725

705726
for (peer_id, addr) in peers.into_iter() {

0 commit comments

Comments
 (0)