Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions node/network/bridge/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use parity_scale_codec::Encode;

use sc_network::Event as NetworkEvent;
use sc_network::{IfDisconnected, NetworkService, OutboundFailure, RequestFailure};
use sc_network::config::parse_addr;

use polkadot_node_network_protocol::{
peer_set::PeerSet,
Expand All @@ -35,7 +36,7 @@ use polkadot_node_network_protocol::{
use polkadot_primitives::v1::{Block, Hash};
use polkadot_subsystem::{SubsystemError, SubsystemResult};

use crate::validator_discovery::{peer_id_from_multiaddr, AuthorityDiscovery};
use crate::validator_discovery::AuthorityDiscovery;

use super::LOG_TARGET;

Expand Down Expand Up @@ -235,16 +236,28 @@ impl Network for Arc<NetworkService<Block, Hash>> {

let peer_id = match peer {
Recipient::Peer(peer_id) => Some(peer_id),
Recipient::Authority(authority) =>
authority_discovery
.get_addresses_by_authority_id(authority)
.await
.and_then(|addrs| {
addrs
.into_iter()
.find_map(|addr| peer_id_from_multiaddr(&addr))
}),
};
Recipient::Authority(authority) => {
let mut found_peer_id = None;
// Note: `get_addresses_by_authority_id` searched in a cache, and it thus expected
// to be very quick.
for addr in authority_discovery
.get_addresses_by_authority_id(authority).await
.into_iter().flat_map(|list| list.into_iter())
{
let (peer_id, addr) = match parse_addr(addr) {
Ok(v) => v,
Err(_) => continue,
};
NetworkService::add_known_address(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions:

  1. Why do we only need this when we want to connect to authorities?
  2. Don't we keep a DHT to resolve PeerIds to addresses? I mean if we have them cached locally, it would be stupid to not use them, but it should still work if we did not - right?

&*self,
peer_id.clone(),
addr,
);
found_peer_id = Some(peer_id);
}
found_peer_id
}
};

let peer_id = match peer_id {
None => {
Expand Down
12 changes: 2 additions & 10 deletions node/network/bridge/src/validator_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use futures::channel::mpsc;

use sc_network::multiaddr::{Multiaddr, Protocol};
use sc_network::{config::parse_addr, multiaddr::Multiaddr};
use sc_authority_discovery::Service as AuthorityDiscoveryService;
use polkadot_node_network_protocol::PeerId;
use polkadot_primitives::v1::{AuthorityDiscoveryId, Block, Hash};
Expand Down Expand Up @@ -131,14 +131,6 @@ fn on_revoke(map: &mut HashMap<AuthorityDiscoveryId, u64>, id: AuthorityDiscover
None
}

pub(crate) fn peer_id_from_multiaddr(addr: &Multiaddr) -> Option<PeerId> {
addr.iter().last().and_then(|protocol| if let Protocol::P2p(multihash) = protocol {
PeerId::from_multihash(multihash).ok()
} else {
None
})
}


pub(super) struct Service<N, AD> {
state: PerPeerSet<StatePerPeerSet>,
Expand Down Expand Up @@ -197,7 +189,7 @@ impl<N: Network, AD: AuthorityDiscovery> Service<N, AD> {

// If not ask the authority discovery
if let Some(addresses) = authority_discovery_service.get_addresses_by_authority_id(id.clone()).await {
for peer_id in addresses.iter().filter_map(peer_id_from_multiaddr) {
for (peer_id, _) in addresses.into_iter().filter_map(|a| parse_addr(a).ok()) {
if let Some(ids) = state.connected_peers.get_mut(&peer_id) {
ids.insert(id.clone());
result.insert(id.clone(), peer_id);
Expand Down
6 changes: 6 additions & 0 deletions node/network/protocol/src/request_response/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ pub enum Recipient {
///
/// The network implementation will make use of that sender for informing the requesting subsystem
/// about responses/errors.
///
/// When using `Recipient::Peer`, keep in mind that no address (as in IP address and port) might
/// be known for that specific peer. You are encouraged to use `Peer` for peers that you are
/// expected to be already connected to.
/// When using `Recipient::Authority`, the addresses can be found thanks to the authority
/// discovery system.
#[derive(Debug)]
pub struct OutgoingRequest<Req> {
/// Intendent recipient of this request.
Expand Down