-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autonat and Kademlia settings updates. #2242
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4e76b72
networking: Add AutonatWrapper.
shamil-gadelshin 91b4cdc
networking: Make autonat mandatory.
shamil-gadelshin d0d084a
networking: Fix kademlia settings.
shamil-gadelshin 9e5f337
networking: Add small tweaks to autonat boot delay.
shamil-gadelshin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod autonat_wrapper; | ||
pub(crate) mod connected_peers; | ||
pub mod peer_info; | ||
pub mod request_response; | ||
|
150 changes: 150 additions & 0 deletions
150
crates/subspace-networking/src/protocols/autonat_wrapper.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
use crate::utils::is_global_address_or_dns; | ||
use libp2p::autonat::{Behaviour as Autonat, Config as AutonatConfig, Event as AutonatEvent}; | ||
use libp2p::core::Endpoint; | ||
use libp2p::multiaddr::Protocol; | ||
use libp2p::swarm::{ | ||
ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler, THandlerInEvent, | ||
THandlerOutEvent, ToSwarm, | ||
}; | ||
use libp2p::{Multiaddr, PeerId}; | ||
use std::collections::HashSet; | ||
use std::task::{Context, Poll}; | ||
use tracing::debug; | ||
|
||
pub(crate) struct Config { | ||
pub(crate) inner_config: AutonatConfig, | ||
pub(crate) local_peer_id: PeerId, | ||
} | ||
|
||
pub(crate) struct Behaviour { | ||
inner: Autonat, | ||
config: Config, | ||
listen_addresses: HashSet<Multiaddr>, | ||
} | ||
|
||
impl Behaviour { | ||
pub(crate) fn new(config: Config) -> Self { | ||
Self { | ||
inner: Autonat::new(config.local_peer_id, config.inner_config.clone()), | ||
config, | ||
listen_addresses: Default::default(), | ||
} | ||
} | ||
|
||
fn private_ips_enabled(&self) -> bool { | ||
!self.config.inner_config.only_global_ips | ||
} | ||
|
||
fn address_corresponds_to_listening_addresses(&self, addr: &Multiaddr) -> bool { | ||
let candidate_protocol = addr | ||
.iter() | ||
.find_map(|protocol| match protocol { | ||
udp @ Protocol::Udp(_) => Some(udp), | ||
tcp @ Protocol::Tcp(_) => Some(tcp), | ||
_ => None, | ||
}) | ||
.expect("Either TCP or UDP protocol should be enabled."); | ||
|
||
let address_result = self | ||
.listen_addresses | ||
.iter() | ||
.any(|addr| addr.iter().any(|protocol| protocol == candidate_protocol)); | ||
|
||
debug!( | ||
%address_result, | ||
?addr, | ||
listen_addresses=?self.listen_addresses, | ||
"Address candidate corresponds to listening addresses." | ||
); | ||
|
||
address_result | ||
} | ||
|
||
pub(crate) fn public_address(&self) -> Option<&Multiaddr> { | ||
self.inner.public_address() | ||
} | ||
|
||
pub(crate) fn confidence(&self) -> usize { | ||
self.inner.confidence() | ||
} | ||
} | ||
|
||
impl NetworkBehaviour for Behaviour { | ||
type ConnectionHandler = <Autonat as NetworkBehaviour>::ConnectionHandler; | ||
type ToSwarm = AutonatEvent; | ||
|
||
fn handle_established_inbound_connection( | ||
&mut self, | ||
connection_id: ConnectionId, | ||
peer: PeerId, | ||
local_addr: &Multiaddr, | ||
remote_addr: &Multiaddr, | ||
) -> Result<THandler<Self>, ConnectionDenied> { | ||
self.inner.handle_established_inbound_connection( | ||
connection_id, | ||
peer, | ||
local_addr, | ||
remote_addr, | ||
) | ||
} | ||
|
||
fn handle_established_outbound_connection( | ||
&mut self, | ||
connection_id: ConnectionId, | ||
peer: PeerId, | ||
addr: &Multiaddr, | ||
role_override: Endpoint, | ||
) -> Result<THandler<Self>, ConnectionDenied> { | ||
self.inner | ||
.handle_established_outbound_connection(connection_id, peer, addr, role_override) | ||
} | ||
|
||
fn on_swarm_event(&mut self, event: FromSwarm) { | ||
match event { | ||
new_listen_addr_event @ FromSwarm::NewListenAddr(_) => { | ||
if let FromSwarm::NewListenAddr(addr) = new_listen_addr_event { | ||
//TODO: handle listener address change | ||
self.listen_addresses.insert(addr.addr.clone()); | ||
|
||
if self.private_ips_enabled() || is_global_address_or_dns(addr.addr) { | ||
self.inner.on_swarm_event(new_listen_addr_event); | ||
} else { | ||
debug!(addr=?addr.addr, "Skipped listening address in AutonatWrapper."); | ||
} | ||
} | ||
} | ||
nazar-pc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new_external_addr_event @ FromSwarm::NewExternalAddrCandidate(_) => { | ||
if let FromSwarm::NewExternalAddrCandidate(addr) = new_external_addr_event { | ||
if self.address_corresponds_to_listening_addresses(addr.addr) { | ||
self.inner.on_swarm_event(new_external_addr_event); | ||
} else { | ||
debug!( | ||
addr=?addr.addr, | ||
"Skipped external address candidate in AutonatWrapper." | ||
); | ||
} | ||
} | ||
} | ||
event => { | ||
self.inner.on_swarm_event(event); | ||
} | ||
} | ||
} | ||
|
||
fn on_connection_handler_event( | ||
&mut self, | ||
peer_id: PeerId, | ||
connection_id: ConnectionId, | ||
event: THandlerOutEvent<Self>, | ||
) { | ||
self.inner | ||
.on_connection_handler_event(peer_id, connection_id, event) | ||
} | ||
|
||
fn poll( | ||
&mut self, | ||
cx: &mut Context<'_>, | ||
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> { | ||
self.inner.poll(cx) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
Duration::MAX
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is why: async-rs/futures-timer#68