Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Dec 17, 2024
1 parent b5cdbca commit 2ca4167
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
67 changes: 41 additions & 26 deletions anchor/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Instant;

use discv5::{Discv5, Enr};
use discv5::enr::{CombinedKey, NodeId};
use discv5::libp2p_identity::{Keypair, PeerId};
use discv5::multiaddr::Multiaddr;
use futures::{StreamExt, TryFutureExt};
use futures::FutureExt;
use discv5::{Discv5, Enr};
use futures::stream::FuturesUnordered;
use libp2p::core::Endpoint;
use futures::FutureExt;
use futures::{StreamExt, TryFutureExt};
use libp2p::core::transport::PortUse;
use libp2p::swarm::{ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm};
use libp2p::core::Endpoint;
use libp2p::swarm::dummy::ConnectionHandler;
use lighthouse_network::{CombinedKeyExt, Subnet};
use lighthouse_network::discovery::DiscoveredPeers;
use libp2p::swarm::{
ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler, THandlerInEvent,
THandlerOutEvent, ToSwarm,
};
use lighthouse_network::discovery::enr_ext::{QUIC6_ENR_KEY, QUIC_ENR_KEY};
use lighthouse_network::discovery::DiscoveredPeers;
use lighthouse_network::{CombinedKeyExt, Subnet};
use tokio::sync::mpsc;

use crate::Config;
Expand Down Expand Up @@ -86,11 +89,7 @@ pub struct Discovery {
}

impl Discovery {
pub async fn new(
local_keypair: Keypair,
network_config: &Config,
) -> Result<Self, String> {

pub async fn new(local_keypair: Keypair, network_config: &Config) -> Result<Self, String> {
let _enr_dir = match network_config.network_dir.to_str() {
Some(path) => String::from(path),
None => String::from(""),
Expand All @@ -101,7 +100,8 @@ impl Discovery {
// "quic4" => ?local_enr.quic4(), "quic6" => ?local_enr.quic6()
// );

let discv5_listen_config = discv5::ListenConfig::from_ip(Ipv4Addr::UNSPECIFIED.into(), 9000);
let discv5_listen_config =
discv5::ListenConfig::from_ip(Ipv4Addr::UNSPECIFIED.into(), 9000);

// discv5 configuration
let discv5_config = discv5::ConfigBuilder::new(discv5_listen_config).build();
Expand Down Expand Up @@ -296,7 +296,7 @@ impl Discovery {
//debug!(self.log, "Discovery query yielded no results.");
}
Ok(r) => {
// debug!(self.log, "Discovery query completed"; "peers_found" => r.len());
// debug!(self.log, "Discovery query completed"; "peers_found" => r.len());
let results = r
.into_iter()
.map(|enr| {
Expand Down Expand Up @@ -327,30 +327,48 @@ impl Discovery {
}
None
}

}

impl NetworkBehaviour for Discovery {
// Discovery is not a real NetworkBehaviour...
type ConnectionHandler = ConnectionHandler;
type ToSwarm = DiscoveredPeers;

fn handle_established_inbound_connection(&mut self, _connection_id: ConnectionId, _peer: PeerId, _local_addr: &Multiaddr, _remote_addr: &Multiaddr) -> Result<THandler<Self>, ConnectionDenied> {
fn handle_established_inbound_connection(
&mut self,
_connection_id: ConnectionId,
_peer: PeerId,
_local_addr: &Multiaddr,
_remote_addr: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(ConnectionHandler)
}

fn handle_established_outbound_connection(&mut self, _connection_id: ConnectionId, _peer: PeerId, _addr: &Multiaddr, _role_override: Endpoint, _port_use: PortUse) -> Result<THandler<Self>, ConnectionDenied> {
fn handle_established_outbound_connection(
&mut self,
_connection_id: ConnectionId,
_peer: PeerId,
_addr: &Multiaddr,
_role_override: Endpoint,
_port_use: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(ConnectionHandler)
}

fn on_swarm_event(&mut self, _event: FromSwarm) {
fn on_swarm_event(&mut self, _event: FromSwarm) {}

fn on_connection_handler_event(
&mut self,
_peer_id: PeerId,
_connection_id: ConnectionId,
_event: THandlerOutEvent<Self>,
) {
}

fn on_connection_handler_event(&mut self, _peer_id: PeerId, _connection_id: ConnectionId, _event: THandlerOutEvent<Self>) {
}

fn poll(&mut self, cx: &mut Context<'_>) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
fn poll(
&mut self,
cx: &mut Context<'_>,
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
if !self.started {
return Poll::Pending;
}
Expand All @@ -368,10 +386,7 @@ impl NetworkBehaviour for Discovery {
}

/// Builds a anchor ENR given a `network::Config`.
pub fn build_enr(
enr_key: &CombinedKey,
config: &Config,
) -> Result<Enr, String> {
pub fn build_enr(enr_key: &CombinedKey, config: &Config) -> Result<Enr, String> {
let mut builder = discv5::enr::Enr::builder();
let (maybe_ipv4_address, maybe_ipv6_address) = &config.enr_address;

Expand Down
2 changes: 1 addition & 1 deletion anchor/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ pub use config::Config;
pub use lighthouse_network::{ListenAddr, ListenAddress};
pub use network::Network;

pub type Enr = discv5::enr::Enr<discv5::enr::CombinedKey>;
pub type Enr = discv5::enr::Enr<discv5::enr::CombinedKey>;
18 changes: 10 additions & 8 deletions anchor/network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ use std::pin::Pin;
use std::time::Duration;

use futures::StreamExt;
use libp2p::{futures, gossipsub, identify, PeerId, ping, Swarm, SwarmBuilder};
use libp2p::core::muxing::StreamMuxerBox;
use libp2p::core::transport::Boxed;
use libp2p::gossipsub::{MessageAuthenticity, ValidationMode};
use libp2p::identity::Keypair;
use libp2p::multiaddr::Protocol;
use libp2p::swarm::SwarmEvent;
use libp2p::{futures, gossipsub, identify, ping, PeerId, Swarm, SwarmBuilder};
use lighthouse_network::discovery::DiscoveredPeers;
use lighthouse_network::discv5::enr::k256::sha2::{Digest, Sha256};
use task_executor::TaskExecutor;
use tracing::{info, log};

use crate::behaviour::AnchorBehaviour;
use crate::behaviour::AnchorBehaviourEvent;
use crate::Config;
use crate::discovery::{Discovery, FIND_NODE_QUERY_CLOSEST_PEERS};
use crate::keypair_utils::load_private_key;
use crate::transport::build_transport;
use crate::Config;

pub struct Network {
swarm: Swarm<AnchorBehaviour>,
Expand Down Expand Up @@ -115,7 +115,10 @@ impl Network {
}
}

async fn build_anchor_behaviour(local_keypair: Keypair, network_config: &Config) -> AnchorBehaviour {
async fn build_anchor_behaviour(
local_keypair: Keypair,
network_config: &Config,
) -> AnchorBehaviour {
// TODO setup discv5
let identify = {
let local_public_key = local_keypair.public();
Expand Down Expand Up @@ -158,10 +161,9 @@ async fn build_anchor_behaviour(local_keypair: Keypair, network_config: &Config)

let discovery = {
// Build and start the discovery sub-behaviour
let mut discovery = Discovery::new(
local_keypair.clone(),
&network_config,
).await.unwrap();
let mut discovery = Discovery::new(local_keypair.clone(), &network_config)
.await
.unwrap();
// start searching for peers
discovery.discover_peers(FIND_NODE_QUERY_CLOSEST_PEERS);
discovery
Expand Down Expand Up @@ -232,8 +234,8 @@ fn build_swarm(
mod test {
use task_executor::TaskExecutor;

use crate::Config;
use crate::network::Network;
use crate::Config;

#[tokio::test]
async fn create_network() {
Expand Down

0 comments on commit 2ca4167

Please sign in to comment.