Skip to content

Commit

Permalink
Fix addresses_to_publish_respects_existing_p2p_protocol test in sc-…
Browse files Browse the repository at this point in the history
…authority-discovery (paritytech#3895)

Fixes paritytech#3887.
  • Loading branch information
dmitry-markin authored Mar 29, 2024
1 parent 5638d1a commit 0d93248
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
22 changes: 16 additions & 6 deletions substrate/client/authority-discovery/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,23 @@ where
}

fn addresses_to_publish(&self) -> impl Iterator<Item = Multiaddr> {
let local_peer_id = self.network.local_peer_id();
let publish_non_global_ips = self.publish_non_global_ips;
let addresses = self
.public_addresses
.clone()
.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();
}

Expand All @@ -375,15 +384,16 @@ where
})
.collect::<Vec<_>>();

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.
Expand Down
14 changes: 9 additions & 5 deletions substrate/client/authority-discovery/src/worker/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Multiaddr>()
.unwrap()
.with(multiaddr::Protocol::P2p(peer_id.into()));
let network: Arc<TestNetwork> = 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()
});

Expand Down

0 comments on commit 0d93248

Please sign in to comment.