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

Introduce identify and rendezvous network protocols #304

Merged
merged 20 commits into from
Mar 23, 2023
Merged

Conversation

mycognosist
Copy link
Contributor

@mycognosist mycognosist commented Mar 17, 2023

Addresses #303

  • Move NetworkBehaviour into a separate module (behaviour.rs)
  • Add the identify network behaviour / protocol
    • Manually add an external address when reported via remote peer
    • Handle identify::Event::Received event
    • Handle identify::Event::Sent event
    • Handle identify::Event::Error event
  • Add the rendezvous network behaviour / protocol
    • Handle rendezvous::client::Event::Registered event
    • Handle rendezvous::client::Event::Discovered event
    • Handle rendezvous::client::Event::Failed event
    • Handle rendezvous::server::Event::PeerRegistered event
    • Handle rendezvous::server::Event::DiscoverServed event
  • Add configuration options for rendezvous
  • Expose rendezvous configuration options via CLI
  • Replace String with Multiaddr for --remote-node-addresses and --rendezvous-address (results in validation by parser)
  • Replace String with PeerId for --rendezvous-peer-id (results in validation by parser)
  • Add glyph to list of authors
  • Update CLI README with latest usage instructions

📋 Checklist

  • Add tests that cover your changes
  • Add this PR to the Unreleased section in CHANGELOG.md
  • Link this PR to any issues it closes
  • New files contain a SPDX license header

@mycognosist mycognosist marked this pull request as draft March 17, 2023 12:15
@codecov
Copy link

codecov bot commented Mar 17, 2023

Codecov Report

Patch coverage: 23.40% and project coverage change: -1.48 ⚠️

Comparison is base (3ebe1eb) 92.42% compared to head (63aa43f) 90.94%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #304      +/-   ##
==========================================
- Coverage   92.42%   90.94%   -1.48%     
==========================================
  Files          51       52       +1     
  Lines        3987     4065      +78     
==========================================
+ Hits         3685     3697      +12     
- Misses        302      368      +66     
Impacted Files Coverage Δ
aquadoggo/src/network/config.rs 100.00% <ø> (ø)
aquadoggo/src/network/service.rs 23.07% <6.34%> (-20.30%) ⬇️
aquadoggo/src/network/behaviour.rs 58.06% <58.06%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@mycognosist mycognosist marked this pull request as ready for review March 20, 2023 12:57
@@ -13,6 +14,9 @@ use crate::network::identity::Identity;
/// Key pair file name.
const KEY_PAIR_FILE_NAME: &str = "private-key";

/// The namespace used by the `identify` network behaviour.
pub const NODE_NAMESPACE: &str = "aquadoggo";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We might want to make this user-definable in the future. The namespace value is used when registering a rendezvous client with a rendezvous server and querying for other registered peers.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, ideally that parameter would be reused by other discovery mechanisms, like mDNS (probably not a "namespace" there but something similar?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, sharing a common identifier would be best. mDNS uses a 'service name' which is currently not user-configurable in the Rust implementation (see libp2p/rust-libp2p#2898 for more detail).

@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

mod behaviour;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moving the network behaviour into a module makes the service less busy.

let remote: Multiaddr = addr.parse()?;
swarm.dial(remote)?;
// Dial each peer identified by the multi-address provided via `--remote-node-addresses` if given
for addr in network_config.remote_peers.clone() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ensure we dial all remote nodes and not just the first one in the list.

swarm
.behaviour_mut()
.rendezvous_client
.as_mut()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looks messy, unfortunately. We have to call .as_mut() because the rendezvous_client behaviour is behind a Toggle type.

@@ -140,13 +110,34 @@ pub async fn network_service(
} => {
debug!("ConnectionClosed: {peer_id} {endpoint:?} {num_established} {cause:?}")
}
SwarmEvent::ConnectionEstablished { peer_id, .. }
if network_config.rendezvous_client
&& network_config.rendezvous_peer_id.unwrap() == peer_id =>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should always be safe to unwrap the rendezvous_peer_id here because the CLI will ensure it's provided if rendezvous_client was set to true.

Copy link
Member

Choose a reason for hiding this comment

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

Give it a comment in the code 👍🏻

address.clone()
};

swarm.dial(address_with_p2p).unwrap();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dial every peer we learn about from the rendezvous server.

@@ -25,7 +27,7 @@ struct Cli {

/// URLs of remote nodes to replicate with.
#[arg(short, long)]
remote_node_addresses: Vec<String>,
remote_node_addresses: Vec<Multiaddr>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

By using Multiaddr and PeerId (instead of String) we get validation of the input automatically :) No need to try and .parse() later on.

@mycognosist
Copy link
Contributor Author

mycognosist commented Mar 20, 2023

@adzialocha @sandreae

This is now ready for review. I've left some comments to explain bits and pieces.

I'm interested to hear what you think of the rendezvous-related API (especially as exposed by the CLI). I'm not sure the naming is the best but @adzialocha mentioned that this can always be refactored later on when more has been implemented.

In a future PR, I'm quite keen to separate the swarm event loop into handlers to make it a bit neater. So, for example, there would be a handle_mdns() method which would encapsulate that functionality and be called when an mDNS event occurs. The same could be done for rendezvous and identify etc. Right now it's a bit time-consuming to comb through all the swarm event handlers and get a sense for what's happening. Here's an example from ursa: https://github.com/fleek-network/ursa/blob/9311c6d0a4514a1f6734ce22863373902551ef0d/crates/ursa-network/src/service.rs#L708

Copy link
Member

@adzialocha adzialocha left a comment

Choose a reason for hiding this comment

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

Looking all very good, mostly just small questions ☺️

aquadoggo/src/network/behaviour.rs Show resolved Hide resolved
@@ -13,6 +14,9 @@ use crate::network::identity::Identity;
/// Key pair file name.
const KEY_PAIR_FILE_NAME: &str = "private-key";

/// The namespace used by the `identify` network behaviour.
pub const NODE_NAMESPACE: &str = "aquadoggo";
Copy link
Member

Choose a reason for hiding this comment

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

Yes, ideally that parameter would be reused by other discovery mechanisms, like mDNS (probably not a "namespace" there but something similar?)

@@ -140,13 +110,34 @@ pub async fn network_service(
} => {
debug!("ConnectionClosed: {peer_id} {endpoint:?} {num_established} {cause:?}")
}
SwarmEvent::ConnectionEstablished { peer_id, .. }
if network_config.rendezvous_client
&& network_config.rendezvous_peer_id.unwrap() == peer_id =>
Copy link
Member

Choose a reason for hiding this comment

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

Give it a comment in the code 👍🏻

aquadoggo/src/network/service.rs Outdated Show resolved Hide resolved
@adzialocha adzialocha self-requested a review March 23, 2023 08:13
Copy link
Member

@sandreae sandreae left a comment

Choose a reason for hiding this comment

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

All looks really good to me, don't have any change requests!

👏 👏 👏

@@ -71,7 +75,23 @@ pub struct NetworkConfiguration {
pub quic_port: u16,

/// The addresses of remote peers to replicate from.
pub remote_peers: Vec<String>,
pub remote_peers: Vec<Multiaddr>,
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

@adzialocha adzialocha merged commit 76c1d44 into main Mar 23, 2023
adzialocha added a commit that referenced this pull request Mar 28, 2023
* main:
  Create and validate abstract queries (#302)
  Handle null value for viewId in nextArgs
  Introduce `identify` and `rendezvous` network protocols (#304)
  Restructure graphql modules (#307)
@adzialocha adzialocha deleted the add_rendezvous branch April 18, 2023 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants