Skip to content

Commit 7eda5b5

Browse files
committed
cargo fmt
1 parent 5bbfb57 commit 7eda5b5

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

anchor/network/src/discovery.rs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ use std::pin::Pin;
55
use std::task::{Context, Poll};
66
use std::time::Instant;
77

8-
use discv5::{Discv5, Enr};
98
use discv5::enr::{CombinedKey, NodeId};
109
use discv5::libp2p_identity::{Keypair, PeerId};
1110
use discv5::multiaddr::Multiaddr;
12-
use futures::{StreamExt, TryFutureExt};
13-
use futures::FutureExt;
11+
use discv5::{Discv5, Enr};
1412
use futures::stream::FuturesUnordered;
15-
use libp2p::core::Endpoint;
13+
use futures::FutureExt;
14+
use futures::{StreamExt, TryFutureExt};
1615
use libp2p::core::transport::PortUse;
17-
use libp2p::swarm::{ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm};
16+
use libp2p::core::Endpoint;
1817
use libp2p::swarm::dummy::ConnectionHandler;
19-
use lighthouse_network::{CombinedKeyExt, Subnet};
20-
use lighthouse_network::discovery::DiscoveredPeers;
18+
use libp2p::swarm::{
19+
ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler, THandlerInEvent,
20+
THandlerOutEvent, ToSwarm,
21+
};
2122
use lighthouse_network::discovery::enr_ext::{QUIC6_ENR_KEY, QUIC_ENR_KEY};
23+
use lighthouse_network::discovery::DiscoveredPeers;
24+
use lighthouse_network::{CombinedKeyExt, Subnet};
2225
use tokio::sync::mpsc;
2326

2427
use crate::Config;
@@ -86,11 +89,7 @@ pub struct Discovery {
8689
}
8790

8891
impl Discovery {
89-
pub async fn new(
90-
local_keypair: Keypair,
91-
network_config: &Config,
92-
) -> Result<Self, String> {
93-
92+
pub async fn new(local_keypair: Keypair, network_config: &Config) -> Result<Self, String> {
9493
let _enr_dir = match network_config.network_dir.to_str() {
9594
Some(path) => String::from(path),
9695
None => String::from(""),
@@ -101,7 +100,8 @@ impl Discovery {
101100
// "quic4" => ?local_enr.quic4(), "quic6" => ?local_enr.quic6()
102101
// );
103102

104-
let discv5_listen_config = discv5::ListenConfig::from_ip(Ipv4Addr::UNSPECIFIED.into(), 9000);
103+
let discv5_listen_config =
104+
discv5::ListenConfig::from_ip(Ipv4Addr::UNSPECIFIED.into(), 9000);
105105

106106
// discv5 configuration
107107
let discv5_config = discv5::ConfigBuilder::new(discv5_listen_config).build();
@@ -296,7 +296,7 @@ impl Discovery {
296296
//debug!(self.log, "Discovery query yielded no results.");
297297
}
298298
Ok(r) => {
299-
// debug!(self.log, "Discovery query completed"; "peers_found" => r.len());
299+
// debug!(self.log, "Discovery query completed"; "peers_found" => r.len());
300300
let results = r
301301
.into_iter()
302302
.map(|enr| {
@@ -327,30 +327,48 @@ impl Discovery {
327327
}
328328
None
329329
}
330-
331330
}
332331

333332
impl NetworkBehaviour for Discovery {
334333
// Discovery is not a real NetworkBehaviour...
335334
type ConnectionHandler = ConnectionHandler;
336335
type ToSwarm = DiscoveredPeers;
337336

338-
fn handle_established_inbound_connection(&mut self, _connection_id: ConnectionId, _peer: PeerId, _local_addr: &Multiaddr, _remote_addr: &Multiaddr) -> Result<THandler<Self>, ConnectionDenied> {
337+
fn handle_established_inbound_connection(
338+
&mut self,
339+
_connection_id: ConnectionId,
340+
_peer: PeerId,
341+
_local_addr: &Multiaddr,
342+
_remote_addr: &Multiaddr,
343+
) -> Result<THandler<Self>, ConnectionDenied> {
339344
Ok(ConnectionHandler)
340345
}
341346

342-
fn handle_established_outbound_connection(&mut self, _connection_id: ConnectionId, _peer: PeerId, _addr: &Multiaddr, _role_override: Endpoint, _port_use: PortUse) -> Result<THandler<Self>, ConnectionDenied> {
347+
fn handle_established_outbound_connection(
348+
&mut self,
349+
_connection_id: ConnectionId,
350+
_peer: PeerId,
351+
_addr: &Multiaddr,
352+
_role_override: Endpoint,
353+
_port_use: PortUse,
354+
) -> Result<THandler<Self>, ConnectionDenied> {
343355
Ok(ConnectionHandler)
344356
}
345357

346-
fn on_swarm_event(&mut self, _event: FromSwarm) {
358+
fn on_swarm_event(&mut self, _event: FromSwarm) {}
347359

360+
fn on_connection_handler_event(
361+
&mut self,
362+
_peer_id: PeerId,
363+
_connection_id: ConnectionId,
364+
_event: THandlerOutEvent<Self>,
365+
) {
348366
}
349367

350-
fn on_connection_handler_event(&mut self, _peer_id: PeerId, _connection_id: ConnectionId, _event: THandlerOutEvent<Self>) {
351-
}
352-
353-
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
368+
fn poll(
369+
&mut self,
370+
cx: &mut Context<'_>,
371+
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
354372
if !self.started {
355373
return Poll::Pending;
356374
}
@@ -368,10 +386,7 @@ impl NetworkBehaviour for Discovery {
368386
}
369387

370388
/// Builds a anchor ENR given a `network::Config`.
371-
pub fn build_enr(
372-
enr_key: &CombinedKey,
373-
config: &Config,
374-
) -> Result<Enr, String> {
389+
pub fn build_enr(enr_key: &CombinedKey, config: &Config) -> Result<Enr, String> {
375390
let mut builder = discv5::enr::Enr::builder();
376391
let (maybe_ipv4_address, maybe_ipv6_address) = &config.enr_address;
377392

anchor/network/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ pub use config::Config;
1212
pub use lighthouse_network::{ListenAddr, ListenAddress};
1313
pub use network::Network;
1414

15-
pub type Enr = discv5::enr::Enr<discv5::enr::CombinedKey>;
15+
pub type Enr = discv5::enr::Enr<discv5::enr::CombinedKey>;

anchor/network/src/network.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ use std::pin::Pin;
33
use std::time::Duration;
44

55
use futures::StreamExt;
6-
use libp2p::{futures, gossipsub, identify, PeerId, ping, Swarm, SwarmBuilder};
76
use libp2p::core::muxing::StreamMuxerBox;
87
use libp2p::core::transport::Boxed;
98
use libp2p::gossipsub::{MessageAuthenticity, ValidationMode};
109
use libp2p::identity::Keypair;
1110
use libp2p::multiaddr::Protocol;
1211
use libp2p::swarm::SwarmEvent;
12+
use libp2p::{futures, gossipsub, identify, ping, PeerId, Swarm, SwarmBuilder};
1313
use lighthouse_network::discovery::DiscoveredPeers;
1414
use lighthouse_network::discv5::enr::k256::sha2::{Digest, Sha256};
1515
use task_executor::TaskExecutor;
1616
use tracing::{info, log};
1717

1818
use crate::behaviour::AnchorBehaviour;
1919
use crate::behaviour::AnchorBehaviourEvent;
20-
use crate::Config;
2120
use crate::discovery::{Discovery, FIND_NODE_QUERY_CLOSEST_PEERS};
2221
use crate::keypair_utils::load_private_key;
2322
use crate::transport::build_transport;
23+
use crate::Config;
2424

2525
pub struct Network {
2626
swarm: Swarm<AnchorBehaviour>,
@@ -115,7 +115,10 @@ impl Network {
115115
}
116116
}
117117

118-
async fn build_anchor_behaviour(local_keypair: Keypair, network_config: &Config) -> AnchorBehaviour {
118+
async fn build_anchor_behaviour(
119+
local_keypair: Keypair,
120+
network_config: &Config,
121+
) -> AnchorBehaviour {
119122
// TODO setup discv5
120123
let identify = {
121124
let local_public_key = local_keypair.public();
@@ -158,10 +161,9 @@ async fn build_anchor_behaviour(local_keypair: Keypair, network_config: &Config)
158161

159162
let discovery = {
160163
// Build and start the discovery sub-behaviour
161-
let mut discovery = Discovery::new(
162-
local_keypair.clone(),
163-
&network_config,
164-
).await.unwrap();
164+
let mut discovery = Discovery::new(local_keypair.clone(), &network_config)
165+
.await
166+
.unwrap();
165167
// start searching for peers
166168
discovery.discover_peers(FIND_NODE_QUERY_CLOSEST_PEERS);
167169
discovery
@@ -232,8 +234,8 @@ fn build_swarm(
232234
mod test {
233235
use task_executor::TaskExecutor;
234236

235-
use crate::Config;
236237
use crate::network::Network;
238+
use crate::Config;
237239

238240
#[tokio::test]
239241
async fn create_network() {

0 commit comments

Comments
 (0)