Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
feat(network): addition of network modulr
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Feb 26, 2024
1 parent 34662b5 commit aa80c98
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.4.11", features = ["cargo"] }
config = { version = "*", path = "./config" }
futures = "0.3.30"

[workspace]

Expand Down
4 changes: 3 additions & 1 deletion p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ edition = "2021"

[dependencies]
config = { version = "*", path = "../config" }
libp2p = {version = "0.53.2", features = [ "async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux"]}
libp2p = {version = "0.53.2", features = [ "async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux", "ping", "tls"]}
futures = "0.3.21"
tokio = { version = "1.18", features = ["macros", "rt-multi-thread"] }
45 changes: 36 additions & 9 deletions p2p/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
use config::NetworkConfig;
use libp2p::kad;
use libp2p::kad::store::MemoryStore;
use libp2p::mdns;

pub struct Network {
pub config: NetworkConfig,
pub kademlia: kad::Behaviour<MemoryStore>,
pub mdns: mdns::async_io::Behaviour,

use libp2p::{identity, PeerId};
use std::time::Duration;

struct Network {
keys: identity::Keypair,
peer_id: PeerId,
}

pub fn start(_config: NetworkConfig) {}
impl Network {
#[tokio::main]
pub async fn start(config: NetworkConfig) {
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());

let net = Network {
keys: local_key,
peer_id: local_peer_id,
};

let mut swarm = libp2p::SwarmBuilder::with_existing_identity(net.keys)
.with_async_std()
.with_tcp(
libp2p::tcp::Config::default(),
libp2p::tls::Config::new,
libp2p::yamux::Config::default,
)
.unwrap()
.with_behaviour(|_| libp2p::ping::Behaviour::default())
.unwrap()
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(30)))
.build();

for b in config.bootstraps.iter() {
swarm.listen_on(b.parse().unwrap()).unwrap();
}
}
}

0 comments on commit aa80c98

Please sign in to comment.