Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 4c67aee

Browse files
authored
client/authority-discovery: Compare PeerIds and not Multihashes (#6414)
In order to tell whether an address is the local nodes address the authority discovery module previously compared the Multihash within the `p2p` Multiaddr protocol. rust-libp2p recently switched to a new PeerId representation (see [1]). Multihashes of the same PeerId in the new and the old format don't equal. Instead of comparing the Multihashes, this patch ensures the module compares the PeerIds [1] libp2p/rust-libp2p#555
1 parent 26aec42 commit 4c67aee

File tree

1 file changed

+19
-4
lines changed
  • client/authority-discovery/src

1 file changed

+19
-4
lines changed

client/authority-discovery/src/lib.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ use sc_network::{
7272
ExHashT,
7373
Multiaddr,
7474
NetworkStateInfo,
75+
PeerId,
7576
};
7677
use sp_authority_discovery::{AuthorityDiscoveryApi, AuthorityId, AuthoritySignature, AuthorityPair};
7778
use sp_core::crypto::{key_types, Pair};
@@ -430,7 +431,7 @@ where
430431
.get(&remote_key)
431432
.ok_or(Error::MatchingHashedAuthorityIdWithAuthorityId)?;
432433

433-
let local_peer_id = multiaddr::Protocol::P2p(self.network.local_peer_id().into());
434+
let local_peer_id = self.network.local_peer_id();
434435

435436
let remote_addresses: Vec<Multiaddr> = values.into_iter()
436437
.map(|(_k, v)| {
@@ -459,9 +460,23 @@ where
459460
.into_iter()
460461
.flatten()
461462
// Ignore own addresses.
462-
.filter(|addr| !addr.iter().any(|protocol|
463-
protocol == local_peer_id
464-
))
463+
.filter(|addr| !addr.iter().any(|protocol| {
464+
// Parse to PeerId first as Multihashes of old and new PeerId
465+
// representation don't equal.
466+
//
467+
// See https://github.com/libp2p/rust-libp2p/issues/555 for
468+
// details.
469+
if let multiaddr::Protocol::P2p(hash) = protocol {
470+
let peer_id = match PeerId::from_multihash(hash) {
471+
Ok(peer_id) => peer_id,
472+
Err(_) => return true, // Discard address.
473+
};
474+
475+
return peer_id == local_peer_id;
476+
}
477+
478+
false // Multiaddr does not contain a PeerId.
479+
}))
465480
.collect();
466481

467482
if !remote_addresses.is_empty() {

0 commit comments

Comments
 (0)