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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::time::Duration;
use libp2p::identity::Keypair;
use libp2p::kad::KademliaConfig;
use libp2p::mdns::{Mdns, MdnsConfig};
use libp2p::swarm::SwarmBuilder;
use libp2p::{Multiaddr, PeerId, Transport};
use crate::graph::persist::PersistentGraph;
pub use super::control::NodeFacade;
use super::GlycosSwarm;
pub struct NodeBuilder {
pub db: PersistentGraph,
pub listen_on: Vec<Multiaddr>,
pub key_pair: Keypair,
}
impl Default for NodeBuilder {
fn default() -> Self {
NodeBuilder {
db: PersistentGraph::temp(),
listen_on: Vec::new(),
key_pair: Keypair::generate_ed25519(),
}
.listen_port(0)
}
}
impl NodeBuilder {
pub fn listen_port(mut self, p: u16) -> Self {
self.listen_on.clear();
let v4: Multiaddr = format!("/ip4/0.0.0.0/tcp/{}", p).parse().unwrap();
let v6: Multiaddr = format!("/ip6/::/tcp/{}", p).parse().unwrap();
self.listen_on.push(v4);
self.listen_on.push(v6);
self
}
pub fn graph(mut self, db: PersistentGraph) -> Self {
self.db = db;
self
}
pub fn key_pair(mut self, pair: Keypair) -> Self {
self.key_pair = pair;
self
}
pub async fn into_facade(self) -> Result<NodeFacade, anyhow::Error> {
Ok(NodeFacade::spawn(self.build().await?).await?)
}
pub async fn build(self) -> Result<GlycosSwarm, anyhow::Error> {
let local_peer_id = PeerId::from(self.key_pair.public());
log::info!("Starting peer {:?}", local_peer_id);
let transport = {
let tcp = libp2p::tcp::TokioTcpConfig::new()
.nodelay(true)
.port_reuse(false);
let dns_tcp = libp2p::dns::TokioDnsConfig::system(tcp)?;
let ws_dns_tcp = libp2p::websocket::WsConfig::new(dns_tcp.clone());
dns_tcp.or_transport(ws_dns_tcp)
};
let relay_config = libp2p::relay::RelayConfig::default();
let (transport, relay) =
libp2p::relay::new_transport_and_behaviour(relay_config, transport);
let noise_keys = libp2p::noise::Keypair::<libp2p::noise::X25519Spec>::new()
.into_authentic(&self.key_pair)
.expect("Signing libp2p-noise static DH keypair failed.");
let transport = transport
.upgrade(libp2p::core::upgrade::Version::V1)
.authenticate(libp2p::noise::NoiseConfig::xx(noise_keys).into_authenticated())
.multiplex(libp2p::core::upgrade::SelectUpgrade::new(
libp2p::yamux::YamuxConfig::default(),
libp2p::mplex::MplexConfig::default(),
))
.timeout(Duration::from_secs(20))
.boxed();
let mut swarm = {
let mut cfg = KademliaConfig::default();
cfg.set_protocol_name(b"/glycos/1.0.0".to_vec());
let behaviour = super::GlycosBehaviour::new(
cfg,
relay,
self.key_pair.public(),
self.db,
Mdns::new(MdnsConfig::default()).await?,
);
SwarmBuilder::new(transport, behaviour, local_peer_id)
.executor(Box::new(|fut| {
tokio::spawn(fut);
}))
.build()
};
for addr in self.listen_on {
swarm.listen_on(addr)?;
}
swarm.listen_on(
Multiaddr::empty()
.with(libp2p::multiaddr::Protocol::P2pCircuit)
.with(libp2p::multiaddr::Protocol::P2p(local_peer_id.into())),
)?;
if !cfg!(feature = "no-relay") {
}
Ok(swarm)
}
}
pub async fn new(db: PersistentGraph) -> Result<NodeFacade, anyhow::Error> {
Ok(NodeBuilder::default().graph(db).into_facade().await?)
}
pub async fn new_in_ram() -> Result<NodeFacade, anyhow::Error> {
Ok(NodeBuilder::default().into_facade().await?)
}
pub async fn new_in_ram_with_port(port: u16) -> Result<NodeFacade, anyhow::Error> {
Ok(NodeBuilder::default()
.listen_port(port)
.into_facade()
.await?)
}
pub async fn new_with_port(
port: u16,
storage: PersistentGraph,
) -> Result<NodeFacade, anyhow::Error> {
let swarm = NodeBuilder::default()
.listen_port(port)
.graph(storage)
.into_facade()
.await?;
Ok(swarm)
}