diff --git a/substrate/client/authority-discovery/src/worker.rs b/substrate/client/authority-discovery/src/worker.rs index 4ad7db5f7da9..546f8cdbffdc 100644 --- a/substrate/client/authority-discovery/src/worker.rs +++ b/substrate/client/authority-discovery/src/worker.rs @@ -342,6 +342,7 @@ where } fn addresses_to_publish(&self) -> impl Iterator { + let local_peer_id = self.network.local_peer_id(); let publish_non_global_ips = self.publish_non_global_ips; let addresses = self .public_addresses @@ -349,7 +350,15 @@ where .into_iter() .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(_)) = address.iter().last() { + if let Some(multiaddr::Protocol::P2p(peer_id)) = address.iter().last() { + if peer_id != *local_peer_id.as_ref() { + error!( + target: LOG_TARGET, + "Network returned external address '{address}' with peer id \ + not matching the local peer id '{local_peer_id}'.", + ); + debug_assert!(false); + } address.pop(); } @@ -375,15 +384,16 @@ where }) .collect::>(); - let peer_id = self.network.local_peer_id(); debug!( target: LOG_TARGET, - "Authority DHT record peer_id='{peer_id}' addresses='{addresses:?}'", + "Authority DHT record peer_id='{local_peer_id}' addresses='{addresses:?}'", ); - // The address must include the peer id. - let peer_id: Multihash = peer_id.into(); - addresses.into_iter().map(move |a| a.with(multiaddr::Protocol::P2p(peer_id))) + // The address must include the local peer id. + let local_peer_id: Multihash = local_peer_id.into(); + addresses + .into_iter() + .map(move |a| a.with(multiaddr::Protocol::P2p(local_peer_id))) } /// Publish own public addresses. diff --git a/substrate/client/authority-discovery/src/worker/tests.rs b/substrate/client/authority-discovery/src/worker/tests.rs index c29120881940..6c684d88e502 100644 --- a/substrate/client/authority-discovery/src/worker/tests.rs +++ b/substrate/client/authority-discovery/src/worker/tests.rs @@ -716,12 +716,16 @@ fn addresses_to_publish_adds_p2p() { #[test] fn addresses_to_publish_respects_existing_p2p_protocol() { let (_dht_event_tx, dht_event_rx) = channel(1000); + let identity = Keypair::generate_ed25519(); + let peer_id = identity.public().to_peer_id(); + let external_address = "/ip6/2001:db8::/tcp/30333" + .parse::() + .unwrap() + .with(multiaddr::Protocol::P2p(peer_id.into())); let network: Arc = Arc::new(TestNetwork { - external_addresses: vec![ - "/ip6/2001:db8::/tcp/30333/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC" - .parse() - .unwrap(), - ], + peer_id, + identity, + external_addresses: vec![external_address], ..Default::default() });