Skip to content

Commit

Permalink
Move network service relay initialization into main event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
sandreae committed Jun 22, 2024
1 parent 962f453 commit b8e7991
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 301 deletions.
1 change: 1 addition & 0 deletions aquadoggo/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod behaviour;
mod config;
pub mod identity;
mod peers;
mod relay;
mod service;
mod shutdown;
mod swarm;
Expand Down
9 changes: 0 additions & 9 deletions aquadoggo/src/network/peers/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,6 @@ impl Behaviour {
false
}

/// Disable the behaviour, it won't handle any connection events or received messages.
pub fn disable(&mut self) {
self.enabled = false
}

pub fn enable(&mut self) {
self.enabled = true
}

pub fn send_message(&mut self, peer: Peer, message: PeerMessage) {
self.push_event(ToSwarm::NotifyHandler {
peer_id: peer.id(),
Expand Down
110 changes: 110 additions & 0 deletions aquadoggo/src/network/relay.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use libp2p::multiaddr::Protocol;
use libp2p::{rendezvous, Multiaddr, PeerId, Swarm};

use crate::network::behaviour::P2pandaBehaviour;
use crate::network::config::NODE_NAMESPACE;

/// A relay node.
pub struct Relay {
/// PeerId of the relay node.
pub(crate) peer_id: PeerId,

/// A single Multiaddr which we know the relay node to be accessible at.
pub(crate) addr: Multiaddr,

/// The namespace we discover peers at on this relay.
pub(crate) namespace: String,

/// Did we tell the relay it's observed address yet.
pub(crate) told_addr: bool,

/// Are we currently discovering peers.
pub(crate) discovering: bool,

/// Are we in the process of registering at this relay.
pub(crate) registering: bool,

/// Have we successfully registered.
pub(crate) registered: bool,

/// Was our relay circuit reservation accepted.
pub(crate) reservation_accepted: bool,
}

impl Relay {
pub fn new(peer_id: PeerId, addr: Multiaddr) -> Self {
Relay {
peer_id,
addr,
namespace: NODE_NAMESPACE.to_string(),
told_addr: false,
discovering: false,
registering: false,
registered: false,
reservation_accepted: false,
}
}

/// The circuit address we should listen at for this relay.
pub fn circuit_addr(&self) -> Multiaddr {
self.addr
.clone()
.with(Protocol::P2p(self.peer_id))
.with(Protocol::P2pCircuit)
}

/// Start listening on the relay circuit address and register on our discovery namespace.
pub fn register(&mut self, swarm: &mut Swarm<P2pandaBehaviour>) -> Result<bool, anyhow::Error> {
if self.registered || self.registering {
return Ok(false);
}

self.registering = true;

// Start listening on the circuit relay address.
let circuit_address = self.circuit_addr();
swarm.listen_on(circuit_address.clone())?;

// Register in the `NODE_NAMESPACE` using the rendezvous network behaviour.
swarm
.behaviour_mut()
.rendezvous_client
.as_mut()
.unwrap()
.register(
rendezvous::Namespace::from_static(NODE_NAMESPACE),
self.peer_id.clone(),
None, // Default ttl is 7200s
)?;

Ok(true)
}

/// Start discovering peers also registered at the same namespace.
pub fn discover(&mut self, swarm: &mut Swarm<P2pandaBehaviour>) -> bool {
if self.reservation_accepted && self.registered && !self.discovering {
self.discovering = true;

swarm
.behaviour_mut()
.rendezvous_client
.as_mut()
.expect("Relay client behaviour exists")
.discover(
Some(
rendezvous::Namespace::new(NODE_NAMESPACE.to_string())
.expect("Valid namespace"),
),
None,
None,
self.peer_id,
);

true
} else {
false
}
}
}
Loading

0 comments on commit b8e7991

Please sign in to comment.