Skip to content

Commit

Permalink
Testing AutoNAT V2
Browse files Browse the repository at this point in the history
  • Loading branch information
Eligioo committed Sep 27, 2024
1 parent e956e4d commit a9d3465
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 43 deletions.
15 changes: 12 additions & 3 deletions network-libp2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub struct Behaviour {
pub discovery: discovery::Behaviour,
pub dht: kad::Behaviour<MemoryStore>,
pub gossipsub: gossipsub::Behaviour,
pub autonat: autonat::Behaviour,
pub autonat_server: autonat::v2::server::Behaviour,
pub autonat_client: autonat::v2::client::Behaviour,
pub ping: ping::Behaviour,
pub request_response: request_response::Behaviour<MessageCodec>,
}
Expand Down Expand Up @@ -101,7 +102,14 @@ impl Behaviour {
if config.autonat_allow_non_global_ips {
autonat_config.only_global_ips = false;
}
let autonat = autonat::Behaviour::new(peer_id, autonat_config);

// Autonat server behaviour
let autonat_server = autonat::v2::server::Behaviour::new(rand::rngs::OsRng);
// Autonat client behaviour
let autonat_client = autonat::v2::client::Behaviour::new(
rand::rngs::OsRng,
autonat::v2::client::Config::default(),
);

// Connection limits behaviour
let limits = connection_limits::ConnectionLimits::default()
Expand All @@ -119,7 +127,8 @@ impl Behaviour {
ping,
pool,
request_response,
autonat,
autonat_client,
autonat_server,
connection_limits,
}
}
Expand Down
46 changes: 23 additions & 23 deletions network-libp2p/src/connection_pool/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,29 +652,29 @@ impl Behaviour {

// Ignore connection if another connection to this peer already exists.
// TODO Do we still want to subject it to the IP limit checks?
if other_established > 0 {
debug!(
%peer_id,
connections = other_established,
"Already have connections established to this peer",
);
// We have more than one connection to the same peer. Deterministically
// choose which connection to close: close the connection only if the
// other peer ID is less than our own peer ID value.
// Note: We don't track all of the connection IDs and if the latest
// connection ID we get is from a peer ID with a lower value, we
// close it. If not, we optimistically expect that the other peer
// does it.
if *peer_id <= self.own_peer_id {
// Notify the handler that the connection must be closed
self.actions.push_back(ToSwarm::CloseConnection {
peer_id: *peer_id,
connection: CloseConnection::One(*connection_id),
});
self.waker.wake();
}
return;
}
// if other_established > 0 {
// debug!(
// %peer_id,
// connections = other_established,
// "Already have connections established to this peer",
// );
// // We have more than one connection to the same peer. Deterministically
// // choose which connection to close: close the connection only if the
// // other peer ID is less than our own peer ID value.
// // Note: We don't track all of the connection IDs and if the latest
// // connection ID we get is from a peer ID with a lower value, we
// // close it. If not, we optimistically expect that the other peer
// // does it.
// if *peer_id <= self.own_peer_id {
// // Notify the handler that the connection must be closed
// self.actions.push_back(ToSwarm::CloseConnection {
// peer_id: *peer_id,
// connection: CloseConnection::One(*connection_id),
// });
// self.waker.wake();
// }
// return;
// }

// Get IP from multiaddress if it exists.
let ip_info = self.get_ip_info_from_multiaddr(address);
Expand Down
35 changes: 34 additions & 1 deletion network-libp2p/src/discovery/handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::{HashSet, VecDeque},
pin::Pin,
sync::Arc,
task::{Context, Poll, Waker},
Expand All @@ -13,10 +14,11 @@ use libp2p::{
swarm::{
handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
ProtocolSupport,
},
ConnectionHandler, ConnectionHandlerEvent, Stream, SubstreamProtocol,
},
Multiaddr, PeerId,
Multiaddr, PeerId, StreamProtocol,
};
use nimiq_hash::Blake2bHash;
use nimiq_network_interface::peer_info::Services;
Expand Down Expand Up @@ -162,6 +164,8 @@ pub struct Handler {

/// Waker used when opening a substream.
waker: Option<Waker>,

events: VecDeque<ConnectionHandlerEvent<DiscoveryProtocol, (), HandlerOutEvent>>,
}

impl Handler {
Expand Down Expand Up @@ -189,6 +193,7 @@ impl Handler {
inbound: None,
outbound: None,
waker: None,
events: VecDeque::new(),
}
}

Expand Down Expand Up @@ -274,6 +279,15 @@ impl ConnectionHandler for Handler {
}
self.inbound = Some(protocol);
self.check_initialized();

let mut hs = HashSet::new();
hs.insert(StreamProtocol::new("/libp2p/autonat/2/dial-request"));
hs.insert(StreamProtocol::new("/libp2p/autonat/2/dial-back"));

self.events
.push_back(ConnectionHandlerEvent::ReportRemoteProtocols(
ProtocolSupport::Added(hs),
));
}
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
protocol, ..
Expand All @@ -286,10 +300,25 @@ impl ConnectionHandler for Handler {
}
self.outbound = Some(protocol);
self.check_initialized();

let mut hs = HashSet::new();
hs.insert(StreamProtocol::new("/libp2p/autonat/2/dial-request"));
hs.insert(StreamProtocol::new("/libp2p/autonat/2/dial-back"));

self.events
.push_back(ConnectionHandlerEvent::ReportRemoteProtocols(
ProtocolSupport::Added(hs),
));
}
ConnectionEvent::DialUpgradeError(DialUpgradeError { error, .. }) => {
error!(%error, "inject_dial_upgrade_error");
}
ConnectionEvent::RemoteProtocolsChange(change) => {
info!(?change, "RemoteProtocolsChange");
}
ConnectionEvent::LocalProtocolsChange(change) => {
info!(?change, "LocalProtocolsChange");
}
_ => {}
}
}
Expand All @@ -304,6 +333,10 @@ impl ConnectionHandler for Handler {
&mut self,
cx: &mut Context,
) -> Poll<ConnectionHandlerEvent<Self::OutboundProtocol, (), HandlerOutEvent>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(event);
}

loop {
// Check if we hit the state transition timeout
if let Some(ref mut state_timeout) = self.state_timeout {
Expand Down
38 changes: 22 additions & 16 deletions network-libp2p/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use instant::Instant;
#[cfg(all(target_family = "wasm", not(feature = "tokio-websocket")))]
use libp2p::websocket_websys;
use libp2p::{
autonat::{self, OutboundFailure},
autonat::OutboundFailure,
core::{
self,
muxing::StreamMuxerBox,
Expand Down Expand Up @@ -376,21 +376,27 @@ fn handle_event(

SwarmEvent::Behaviour(event) => {
match event {
behaviour::BehaviourEvent::Autonat(event) => match event {
autonat::Event::InboundProbe(event) => {
log::trace!(?event, "Autonat inbound probe");
}
autonat::Event::OutboundProbe(event) => {
log::trace!(?event, "Autonat outbound probe");
}
autonat::Event::StatusChanged { old, new } => {
log::debug!(?old, ?new, "Autonat status changed");
if new == autonat::NatStatus::Private {
log::warn!("Couldn't detect a public reachable address. Validator network operations won't be possible");
log::warn!("You may need to find a relay to enable validator network operations");
}
}
},
// behaviour::BehaviourEvent::Autonat(event) => match event {
// autonat::Event::InboundProbe(event) => {
// log::trace!(?event, "Autonat inbound probe");
// }
// autonat::Event::OutboundProbe(event) => {
// log::trace!(?event, "Autonat outbound probe");
// }
// autonat::Event::StatusChanged { old, new } => {
// log::debug!(?old, ?new, "Autonat status changed");
// if new == autonat::NatStatus::Private {
// log::warn!("Couldn't detect a public reachable address. Validator network operations won't be possible");
// log::warn!("You may need to find a relay to enable validator network operations");
// }
// }
// },
behaviour::BehaviourEvent::AutonatClient(event) => {
info!(?event, "Autonat client");
}
behaviour::BehaviourEvent::AutonatServer(event) => {
info!(?event, "Autonat server");
}
behaviour::BehaviourEvent::ConnectionLimits(_) => {}
behaviour::BehaviourEvent::Dht(event) => {
match event {
Expand Down

0 comments on commit a9d3465

Please sign in to comment.