From 21da1ef0980afd34fa6bdb9dec463503bc5107f9 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Mon, 3 Jun 2024 17:08:41 +0300 Subject: [PATCH] Update `sc-authority-discovery` --- substrate/client/authority-discovery/src/tests.rs | 2 +- substrate/client/authority-discovery/src/worker.rs | 13 +++++-------- .../authority-discovery/src/worker/addr_cache.rs | 10 +++++----- .../client/authority-discovery/src/worker/tests.rs | 4 ++-- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/substrate/client/authority-discovery/src/tests.rs b/substrate/client/authority-discovery/src/tests.rs index 301bad59a4e0..acfd0e61de01 100644 --- a/substrate/client/authority-discovery/src/tests.rs +++ b/substrate/client/authority-discovery/src/tests.rs @@ -53,7 +53,7 @@ fn get_addresses_and_authority_id() { let remote_addr = "/ip6/2001:db8:0:0:0:0:0:2/tcp/30333" .parse::() .unwrap() - .with(Protocol::P2p(remote_peer_id)); + .with(Protocol::P2p(remote_peer_id.into())); let test_api = Arc::new(TestApi { authorities: vec![] }); diff --git a/substrate/client/authority-discovery/src/worker.rs b/substrate/client/authority-discovery/src/worker.rs index 2573c9dcf2ad..0676e2df5f49 100644 --- a/substrate/client/authority-discovery/src/worker.rs +++ b/substrate/client/authority-discovery/src/worker.rs @@ -45,10 +45,7 @@ use sc_network::{ event::DhtEvent, multiaddr, KademliaKey, Multiaddr, NetworkDHTProvider, NetworkSigner, NetworkStateInfo, }; -use sc_network_types::{ - multihash::{Code, Multihash}, - PeerId, -}; +use sc_network_types::{multihash::Code, PeerId}; use sp_api::{ApiError, ProvideRuntimeApi}; use sp_authority_discovery::{ AuthorityDiscoveryApi, AuthorityId, AuthorityPair, AuthoritySignature, @@ -247,7 +244,7 @@ where .into_iter() .map(|mut address| { if let Some(multiaddr::Protocol::P2p(peer_id)) = address.iter().last() { - if peer_id != local_peer_id { + if peer_id != *local_peer_id.as_ref() { error!( target: LOG_TARGET, "Discarding invalid local peer ID in public address {address}.", @@ -353,7 +350,7 @@ where .chain(self.network.external_addresses().into_iter().filter_map(|mut address| { // Make sure the reported external address does not contain `/p2p/...` protocol. if let Some(multiaddr::Protocol::P2p(peer_id)) = address.iter().last() { - if peer_id != local_peer_id { + if peer_id != *local_peer_id.as_ref() { error!( target: LOG_TARGET, "Network returned external address '{address}' with peer id \ @@ -394,7 +391,7 @@ where // The address must include the local peer id. addresses .into_iter() - .map(move |a| a.with(multiaddr::Protocol::P2p(local_peer_id))) + .map(move |a| a.with(multiaddr::Protocol::P2p(*local_peer_id.as_ref()))) } /// Publish own public addresses. @@ -619,7 +616,7 @@ where .map_err(Error::ParsingMultiaddress)?; let get_peer_id = |a: &Multiaddr| match a.iter().last() { - Some(multiaddr::Protocol::P2p(peer_id)) => Some(peer_id), + Some(multiaddr::Protocol::P2p(key)) => PeerId::from_multihash(key).ok(), _ => None, }; diff --git a/substrate/client/authority-discovery/src/worker/addr_cache.rs b/substrate/client/authority-discovery/src/worker/addr_cache.rs index 6f8fa2440e61..13bb990bf8b9 100644 --- a/substrate/client/authority-discovery/src/worker/addr_cache.rs +++ b/substrate/client/authority-discovery/src/worker/addr_cache.rs @@ -160,8 +160,8 @@ impl AddrCache { fn peer_id_from_multiaddr(addr: &Multiaddr) -> Option { addr.iter().last().and_then(|protocol| { - if let Protocol::P2p(peer_id) = protocol { - Some(peer_id) + if let Protocol::P2p(multihash) = protocol { + PeerId::from_multihash(multihash).ok() } else { None } @@ -177,7 +177,7 @@ mod tests { use super::*; use quickcheck::{Arbitrary, Gen, QuickCheck, TestResult}; - use sc_network_types::multihash::Multihash; + use sc_network_types::multihash::{Code, Multihash}; use sp_authority_discovery::{AuthorityId, AuthorityPair}; use sp_core::crypto::Pair; @@ -204,7 +204,7 @@ mod tests { let multiaddr = "/ip6/2001:db8:0:0:0:0:0:2/tcp/30333" .parse::() .unwrap() - .with(Protocol::P2p(peer_id)); + .with(Protocol::P2p(peer_id.into())); TestMultiaddr(multiaddr) } @@ -363,7 +363,7 @@ mod tests { let mut addr_cache = AddrCache::new(); let peer_id = PeerId::random(); - let addr = Multiaddr::empty().with(Protocol::P2p(peer_id)); + let addr = Multiaddr::empty().with(Protocol::P2p(peer_id.into())); let authority_id0 = AuthorityPair::generate().0.public(); let authority_id1 = AuthorityPair::generate().0.public(); diff --git a/substrate/client/authority-discovery/src/worker/tests.rs b/substrate/client/authority-discovery/src/worker/tests.rs index a6bda9642c0a..70107c89a851 100644 --- a/substrate/client/authority-discovery/src/worker/tests.rs +++ b/substrate/client/authority-discovery/src/worker/tests.rs @@ -549,7 +549,7 @@ impl DhtValueFoundTester { let address: Multiaddr = format!("/ip6/2001:db8:0:0:0:0:0:{:x}/tcp/30333", idx).parse().unwrap(); - address.with(multiaddr::Protocol::P2p(peer_id)) + address.with(multiaddr::Protocol::P2p(peer_id.into())) } fn process_value_found( @@ -776,7 +776,7 @@ fn lookup_throttling() { let peer_id = PeerId::random(); let address: Multiaddr = "/ip6/2001:db8:0:0:0:0:0:1/tcp/30333".parse().unwrap(); - address.with(multiaddr::Protocol::P2p(peer_id)) + address.with(multiaddr::Protocol::P2p(peer_id.into())) }; let remote_key_store = MemoryKeystore::new(); let remote_public_keys: Vec = (0..20)