1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use libp2p::core::PeerId;
use libp2p::gossipsub::GossipsubConfig;
use libp2p::identity::{Keypair, PublicKey};
use libp2p::ping::PingConfig;
use libp2p::pnet::PreSharedKey;
use libp2p_bitswap::BitswapConfig;
#[derive(Clone)]
pub struct NetworkConfig {
pub node_key: Keypair,
pub node_name: String,
pub enable_mdns: bool,
pub enable_kad: bool,
pub allow_non_globals_in_dht: bool,
pub psk: Option<PreSharedKey>,
pub ping: PingConfig,
pub gossipsub: GossipsubConfig,
pub bitswap: BitswapConfig,
}
impl NetworkConfig {
pub fn new() -> Self {
Self {
enable_mdns: true,
enable_kad: true,
allow_non_globals_in_dht: false,
node_key: Keypair::generate_ed25519(),
node_name: names::Generator::with_naming(names::Name::Numbered)
.next()
.unwrap(),
psk: None,
ping: PingConfig::new().with_keep_alive(true),
gossipsub: GossipsubConfig::default(),
bitswap: BitswapConfig::default(),
}
}
pub fn public(&self) -> PublicKey {
self.node_key.public()
}
pub fn peer_id(&self) -> PeerId {
self.node_key.public().into_peer_id()
}
}
impl Default for NetworkConfig {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for NetworkConfig {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("NetworkConfig")
.field("node_key", &self.peer_id().to_string())
.field("node_name", &self.node_name)
.field("enable_mdns", &self.enable_mdns)
.field("enable_kad", &self.enable_kad)
.field("allow_non_globals_in_dht", &self.allow_non_globals_in_dht)
.field("psk", &self.psk.is_some())
.field("ping", &self.ping)
.field("gossipsub", &self.gossipsub)
.field("bitswap", &self.bitswap)
.finish()
}
}