From 0f72628f126e8caab51b87573cfada26d3cb88cb Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Sat, 18 Nov 2023 12:42:27 +0100 Subject: [PATCH] Filter out unsupported addresses during the discovery process (#1359) * Filter out unsupported addresses during the discovery process * PR link --- light-base/src/network_service.rs | 20 ++++++++++++++++++-- light-base/src/platform/address_parse.rs | 11 ++++++++++- wasm-node/CHANGELOG.md | 4 ++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/light-base/src/network_service.rs b/light-base/src/network_service.rs index dc4b5d3e8f..3a2b400477 100644 --- a/light-base/src/network_service.rs +++ b/light-base/src/network_service.rs @@ -1770,7 +1770,24 @@ async fn background_task(mut task: BackgroundTask) { let mut valid_addrs = Vec::with_capacity(addrs.len()); for addr in addrs { match Multiaddr::try_from(addr) { - Ok(a) => valid_addrs.push(a), + Ok(a) => { + if platform::address_parse::multiaddr_to_address(&a) + .ok() + .map_or(false, |addr| { + task.platform.supports_connection_type((&addr).into()) + }) + { + valid_addrs.push(a) + } else { + log::debug!( + target: "network", + "Discovery({}) => UnsupportedAddress(peer_id={}, addr={})", + &task.network[chain_id].log_name, + peer_id, + &a + ); + } + } Err(err) => { log::debug!( target: "network", @@ -1779,7 +1796,6 @@ async fn background_task(mut task: BackgroundTask) { peer_id, hex::encode(&err.addr) ); - continue; } } } diff --git a/light-base/src/platform/address_parse.rs b/light-base/src/platform/address_parse.rs index 2a80c105f8..037713306b 100644 --- a/light-base/src/platform/address_parse.rs +++ b/light-base/src/platform/address_parse.rs @@ -17,7 +17,7 @@ use smoldot::libp2p::{multiaddr::ProtocolRef, multihash, Multiaddr}; -use super::{Address, IpAddr, MultiStreamAddress}; +use super::{Address, ConnectionType, IpAddr, MultiStreamAddress}; use core::str; pub enum AddressOrMultiStreamAddress<'a> { @@ -25,6 +25,15 @@ pub enum AddressOrMultiStreamAddress<'a> { MultiStreamAddress(MultiStreamAddress), } +impl<'a> From<&'a AddressOrMultiStreamAddress<'a>> for ConnectionType { + fn from(address: &'a AddressOrMultiStreamAddress<'a>) -> ConnectionType { + match address { + AddressOrMultiStreamAddress::Address(a) => ConnectionType::from(a), + AddressOrMultiStreamAddress::MultiStreamAddress(a) => ConnectionType::from(a), + } + } +} + /// Parses a [`Multiaddr`] into an [`Address`] or [`MultiStreamAddress`]. pub fn multiaddr_to_address(multiaddr: &Multiaddr) -> Result { let mut iter = multiaddr.iter().fuse(); diff --git a/wasm-node/CHANGELOG.md b/wasm-node/CHANGELOG.md index 8373e2e9d4..56e4ffeda8 100644 --- a/wasm-node/CHANGELOG.md +++ b/wasm-node/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Changed + +- Addresses that are not supported by the host platform are now ignored during the discovery process. For example, TCP/IP connections are ignored while in a browser. This avoids populating the address book with peers that we know we can't connect to anyway. ([#1359](https://github.com/smol-dot/smoldot/pull/1359)) + ## 2.0.10 - 2023-11-17 ### Fixed