Skip to content

Commit

Permalink
Merge pull request #1839 from subspace/autonat3
Browse files Browse the repository at this point in the history
Introduce Autonat protocol for DSN.
  • Loading branch information
nazar-pc authored Aug 18, 2023
2 parents d6fb246 + d8c7114 commit f918878
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/subspace-networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void = "1.0.2"
version = "0.52.1"
default-features = false
features = [
"autonat",
"dns",
"gossipsub",
"identify",
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-networking/examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ pub async fn configure_dsn(
kademlia_mode: Some(Mode::Client),
request_response_protocols: vec![PieceByIndexRequestHandler::create(|_, _| async { None })],
bootstrap_addresses,
enable_autonat: false,
..default_config
};
let (node, mut node_runner_1) = subspace_networking::create(config).unwrap();
Expand Down
9 changes: 9 additions & 0 deletions crates/subspace-networking/src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::reserved_peers::{
use crate::PeerInfoProvider;
use derive_more::From;
use libp2p::allow_block_list::{Behaviour as AllowBlockListBehaviour, BlockedPeers};
use libp2p::autonat::{Behaviour as Autonat, Config as AutonatConfig, Event as AutonatEvent};
use libp2p::connection_limits::{Behaviour as ConnectionLimitsBehaviour, ConnectionLimits};
use libp2p::gossipsub::{
Behaviour as Gossipsub, Config as GossipsubConfig, Event as GossipsubEvent, MessageAuthenticity,
Expand Down Expand Up @@ -57,6 +58,8 @@ pub(crate) struct BehaviorConfig<RecordStore> {
pub(crate) general_connected_peers_config: Option<ConnectedPeersConfig>,
/// The configuration for the [`ConnectedPeers`] protocol (special instance).
pub(crate) special_connected_peers_config: Option<ConnectedPeersConfig>,
/// Autonat configuration (optional).
pub(crate) autonat: Option<AutonatConfig>,
}

#[derive(Debug, Clone, Copy)]
Expand All @@ -81,6 +84,7 @@ pub(crate) struct Behavior<RecordStore> {
Toggle<ConnectedPeersBehaviour<GeneralConnectedPeersInstance>>,
pub(crate) special_connected_peers:
Toggle<ConnectedPeersBehaviour<SpecialConnectedPeersInstance>>,
pub(crate) autonat: Toggle<Autonat>,
}

impl<RecordStore> Behavior<RecordStore>
Expand Down Expand Up @@ -130,6 +134,10 @@ where
.special_connected_peers_config
.map(ConnectedPeersBehaviour::new)
.into(),
autonat: config
.autonat
.map(|autonat_config| Autonat::new(config.peer_id, autonat_config))
.into(),
}
}
}
Expand All @@ -147,4 +155,5 @@ pub(crate) enum Event {
PeerInfo(PeerInfoEvent),
GeneralConnectedPeers(ConnectedPeersEvent<GeneralConnectedPeersInstance>),
SpecialConnectedPeers(ConnectedPeersEvent<SpecialConnectedPeersInstance>),
Autonat(AutonatEvent),
}
12 changes: 11 additions & 1 deletion crates/subspace-networking/src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::utils::{strip_peer_id, ResizableSemaphore};
use crate::{PeerInfo, PeerInfoConfig};
use backoff::{ExponentialBackoff, SystemClock};
use futures::channel::mpsc;
use libp2p::autonat::Config as AutonatConfig;
use libp2p::connection_limits::ConnectionLimits;
use libp2p::gossipsub::{
Config as GossipsubConfig, ConfigBuilder as GossipsubConfigBuilder,
Expand Down Expand Up @@ -74,7 +75,7 @@ const SWARM_TARGET_CONNECTION_NUMBER: u32 = 30;
// "Good citizen" supports the network health.
const YAMUX_MAX_STREAMS: usize = 256;
const KADEMLIA_QUERY_TIMEOUT: Duration = Duration::from_secs(40);
const SWARM_MAX_ESTABLISHED_CONNECTIONS_PER_PEER: Option<u32> = Some(2);
const SWARM_MAX_ESTABLISHED_CONNECTIONS_PER_PEER: Option<u32> = Some(3);
// TODO: Consider moving this constant to configuration or removing `Toggle` wrapper when we find a
// use-case for gossipsub protocol.
const ENABLE_GOSSIP_PROTOCOL: bool = false;
Expand Down Expand Up @@ -247,6 +248,8 @@ pub struct Config<LocalRecordProvider> {
/// Known external addresses to the local peer. The addresses will be added on the swarm start
/// and enable peer to notify others about its reachable address.
pub external_addresses: Vec<Multiaddr>,
/// Enable autonat protocol. Helps detecting whether we're behind the firewall.
pub enable_autonat: bool,
}

impl<LocalRecordProvider> fmt::Debug for Config<LocalRecordProvider> {
Expand Down Expand Up @@ -361,6 +364,7 @@ where
bootstrap_addresses: Vec::new(),
kademlia_mode: Some(Mode::Server),
external_addresses: Vec::new(),
enable_autonat: true,
}
}
}
Expand Down Expand Up @@ -423,6 +427,7 @@ where
bootstrap_addresses,
kademlia_mode,
external_addresses,
enable_autonat,
} = config;
let local_peer_id = peer_id(&keypair);

Expand Down Expand Up @@ -482,6 +487,11 @@ where
..ConnectedPeersConfig::default()
}
}),
autonat: enable_autonat.then(|| AutonatConfig {
use_connected: true,
only_global_ips: !config.allow_non_global_addresses_in_dht,
..Default::default()
}),
});

let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id)
Expand Down
19 changes: 19 additions & 0 deletions crates/subspace-networking/src/node_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use event_listener_primitives::HandlerId;
use futures::channel::mpsc;
use futures::future::Fuse;
use futures::{FutureExt, StreamExt};
use libp2p::autonat::Event as AutonatEvent;
use libp2p::core::{address_translation, ConnectedPoint};
use libp2p::gossipsub::{Event as GossipsubEvent, TopicHash};
use libp2p::identify::Event as IdentifyEvent;
Expand Down Expand Up @@ -419,6 +420,9 @@ where
SwarmEvent::Behaviour(Event::SpecialConnectedPeers(event)) => {
self.handle_special_connected_peers_event(event).await;
}
SwarmEvent::Behaviour(Event::Autonat(event)) => {
self.handle_autonat_event(event).await;
}
SwarmEvent::NewListenAddr { address, .. } => {
let shared = match self.shared_weak.upgrade() {
Some(shared) => shared,
Expand Down Expand Up @@ -1106,6 +1110,21 @@ where
}
}

async fn handle_autonat_event(&mut self, event: AutonatEvent) {
trace!(?event, "Autonat event received.");
if let Some(autonat) = self.swarm.behaviour().autonat.as_ref() {
debug!(
public_address=?autonat.public_address(),
confidence=%autonat.confidence(),
"Current public address confidence."
);
}

if let AutonatEvent::StatusChanged { old, new } = event {
info!(?old, ?new, "Public address status changed.")
}
}

fn handle_command(&mut self, command: Command) {
match command {
Command::GetValue {
Expand Down

0 comments on commit f918878

Please sign in to comment.