Skip to content
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

network-libp2p: Add fixes and improvements to discovery protocol #2461

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ impl ClientInner {
identity_keypair.public(),
provided_services,
None,
);
)
.map_err(|e| Error::Network(nimiq_network_libp2p::NetworkError::PeerContactError(e)))?;
peer_contact.set_current_time();

let seeds: Vec<Multiaddr> = config
Expand Down
1 change: 1 addition & 0 deletions network-libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async-trait = "0.1"
base64 = "0.22"
bytes = "1.6"
futures = { workspace = true }
futures-timer = "3.0"
hex = "0.4"
instant = { version = "0.1", features = ["wasm-bindgen"] }
ip_network = "0.4"
Expand Down
3 changes: 2 additions & 1 deletion network-libp2p/src/connection_pool/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ impl Behaviour {

let num_seeds = 1;
let contacts = self.contacts.read();
let own_addresses: HashSet<&Multiaddr> = contacts.get_own_contact().addresses().collect();
let own_contact = contacts.get_own_contact();
let own_addresses: HashSet<&Multiaddr> = own_contact.addresses().collect();
self.seeds
.iter()
.filter(|address| !own_addresses.contains(address) && self.addresses.can_dial(*address))
Expand Down
38 changes: 11 additions & 27 deletions network-libp2p/src/discovery/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use libp2p::{
identity::Keypair,
swarm::{
behaviour::{ConnectionClosed, ConnectionEstablished},
CloseConnection, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour,
NotifyHandler, ToSwarm,
CloseConnection, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, ToSwarm,
},
Multiaddr, PeerId,
};
Expand All @@ -22,7 +21,7 @@ use nimiq_time::{interval, Interval};
use parking_lot::RwLock;

use super::{
handler::{Handler, HandlerInEvent, HandlerOutEvent},
handler::{Handler, HandlerOutEvent},
peer_contacts::{PeerContact, PeerContactBook},
};

Expand Down Expand Up @@ -87,7 +86,7 @@ pub enum Event {
Update,
}

type DiscoveryToSwarm = ToSwarm<Event, HandlerInEvent>;
type DiscoveryToSwarm = ToSwarm<Event, ()>;

/// Network behaviour for peer exchange.
///
Expand Down Expand Up @@ -143,7 +142,7 @@ impl Behaviour {
}
}

/// Adds our own addresses into our own contact within the peer contact book
/// Adds addresses into our own contact within the peer contact book
pub fn add_own_addresses(&self, addresses: Vec<Multiaddr>) {
self.peer_contact_book
.write()
Expand All @@ -170,28 +169,30 @@ impl NetworkBehaviour for Behaviour {
_connection_id: ConnectionId,
peer: PeerId,
_local_addr: &Multiaddr,
_remote_addr: &Multiaddr,
remote_addr: &Multiaddr,
) -> Result<Handler, ConnectionDenied> {
Ok(Handler::new(
peer,
self.config.clone(),
self.keypair.clone(),
self.peer_contact_book(),
remote_addr.clone(),
))
}

fn handle_established_outbound_connection(
&mut self,
_connection_id: ConnectionId,
peer: PeerId,
_addr: &Multiaddr,
addr: &Multiaddr,
_role_override: Endpoint,
) -> Result<Handler, ConnectionDenied> {
Ok(Handler::new(
peer,
self.config.clone(),
self.keypair.clone(),
self.peer_contact_book(),
addr.clone(),
))
}

Expand Down Expand Up @@ -254,28 +255,13 @@ impl NetworkBehaviour for Behaviour {
failed_addresses,
other_established,
}) => {
let peer_address = endpoint.get_remote_address().clone();

// Signal to the handler the address that got us a connection
self.events.push_back(ToSwarm::NotifyHandler {
peer_id,
handler: NotifyHandler::One(connection_id),
event: HandlerInEvent::ConnectionAddress(peer_address.clone()),
});

if other_established == 0 {
trace!(%peer_id, ?connection_id, ?endpoint, "Behaviour::inject_connection_established:");

// This is the first connection to this peer
self.connected_peers.insert(peer_id);

// Report the observed addresses of the endpoint if a peer successfully connected to us
if endpoint.is_listener() {
self.events.push_back(ToSwarm::NotifyHandler {
peer_id,
handler: NotifyHandler::One(connection_id),
event: HandlerInEvent::ObservedAddress(peer_address),
});
// Peer failed to connect with some of our own addresses, remove them from our own addresses
if !failed_addresses.is_empty() {
debug!(
Expand Down Expand Up @@ -318,11 +304,9 @@ impl NetworkBehaviour for Behaviour {
}));
}
}
HandlerOutEvent::ObservedAddresses { observed_addresses } => {
for address in observed_addresses {
self.events
.push_back(ToSwarm::NewExternalAddrCandidate(address));
}
HandlerOutEvent::ObservedAddress { observed_address } => {
self.events
.push_back(ToSwarm::NewExternalAddrCandidate(observed_address));
}
HandlerOutEvent::Update => self.events.push_back(ToSwarm::GenerateEvent(Event::Update)),
HandlerOutEvent::Error(_) => self.events.push_back(ToSwarm::CloseConnection {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly this is where the StateTransitionTimeout would end up. I may have missed it, but I think some logging would be nice here.

Expand Down
Loading
Loading