Skip to content

Commit

Permalink
[net/libp2p] Use raw Identify observed addresses to discover extern…
Browse files Browse the repository at this point in the history
…al addresses (#7338)

Instead of using libp2p-provided external address candidates,
susceptible to address translation issues, use litep2p-backend approach
based on confirming addresses observed by multiple peers as external.

Fixes #7207.

---------

Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
dmitry-markin and github-actions[bot] authored Jan 28, 2025
1 parent 29eb533 commit 758db43
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 46 deletions.
10 changes: 10 additions & 0 deletions prdoc/pr_7338.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: '[net/libp2p] Use raw `Identify` observed addresses to discover external addresses'
doc:
- audience: Node Dev
description: |-
Instead of using libp2p-provided external address candidates, susceptible to address translation issues, use litep2p-backend approach based on confirming addresses observed by multiple peers as external.

Fixes https://github.com/paritytech/polkadot-sdk/issues/7207.
crates:
- name: sc-network
bump: major
2 changes: 2 additions & 0 deletions substrate/client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ impl<B: BlockT> Behaviour<B> {
request_response_protocols: Vec<ProtocolConfig>,
peer_store_handle: Arc<dyn PeerStoreProvider>,
external_addresses: Arc<Mutex<HashSet<Multiaddr>>>,
public_addresses: Vec<Multiaddr>,
connection_limits: ConnectionLimits,
) -> Result<Self, request_responses::RegisterError> {
Ok(Self {
Expand All @@ -192,6 +193,7 @@ impl<B: BlockT> Behaviour<B> {
user_agent,
local_public_key,
external_addresses,
public_addresses,
),
discovery: disco_config.finish(),
request_responses: request_responses::RequestResponsesBehaviour::new(
Expand Down
3 changes: 3 additions & 0 deletions substrate/client/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ pub use service::{
};
pub use types::ProtocolName;

/// Log target for `sc-network`.
const LOG_TARGET: &str = "sub-libp2p";

/// The maximum allowed number of established connections per peer.
///
/// Typically, and by design of the network behaviours in this crate,
Expand Down
11 changes: 5 additions & 6 deletions substrate/client/network/src/litep2p/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use schnellru::{ByLength, LruMap};
use std::{
cmp,
collections::{HashMap, HashSet, VecDeque},
iter,
num::NonZeroUsize,
pin::Pin,
sync::Arc,
Expand All @@ -72,11 +73,9 @@ const GET_RECORD_REDUNDANCY_FACTOR: usize = 4;
/// The maximum number of tracked external addresses we allow.
const MAX_EXTERNAL_ADDRESSES: u32 = 32;

/// Minimum number of confirmations received before an address is verified.
///
/// Note: all addresses are confirmed by libp2p on the first encounter. This aims to make
/// addresses a bit more robust.
const MIN_ADDRESS_CONFIRMATIONS: usize = 2;
/// Number of times observed address is received from different peers before it is confirmed as
/// external.
const MIN_ADDRESS_CONFIRMATIONS: usize = 3;

/// Discovery events.
#[derive(Debug)]
Expand Down Expand Up @@ -509,7 +508,7 @@ impl Discovery {
.flatten()
.flatten();

self.address_confirmations.insert(address.clone(), Default::default());
self.address_confirmations.insert(address.clone(), iter::once(peer).collect());

return (false, oldest)
},
Expand Down
Loading

0 comments on commit 758db43

Please sign in to comment.