Skip to content

Commit

Permalink
Filter out unsupported addresses during the discovery process (#1359)
Browse files Browse the repository at this point in the history
* Filter out unsupported addresses during the discovery process

* PR link
  • Loading branch information
tomaka authored Nov 18, 2023
1 parent 6e53dcf commit 0f72628
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
20 changes: 18 additions & 2 deletions light-base/src/network_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1770,7 +1770,24 @@ async fn background_task<TPlat: PlatformRef>(mut task: BackgroundTask<TPlat>) {
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",
Expand All @@ -1779,7 +1796,6 @@ async fn background_task<TPlat: PlatformRef>(mut task: BackgroundTask<TPlat>) {
peer_id,
hex::encode(&err.addr)
);
continue;
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion light-base/src/platform/address_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@

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> {
Address(Address<'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<AddressOrMultiStreamAddress, Error> {
let mut iter = multiaddr.iter().fuse();
Expand Down
4 changes: 4 additions & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f72628

Please sign in to comment.