This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network): addition of network modulr
- Loading branch information
Showing
3 changed files
with
40 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |