-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
247 lines (222 loc) · 9.14 KB
/
main.rs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use clap::Parser;
use futures::StreamExt;
use libp2p::swarm::SwarmEvent;
use libp2p::Multiaddr;
use libp2p::{mdns, PeerId};
use std::error::Error;
use tracing::{debug, error, info, warn};
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{
prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Layer,
};
use types::DATA_SHARDS_COUNT;
#[cfg(feature = "console-log")]
use console_subscriber::ConsoleLayer;
#[cfg(feature = "console-log")]
use std::net::SocketAddr;
use crate::network::CombinedBehaviourEvent;
// "modules" as in `module::Module`
mod behaviour;
mod consensus;
mod data_memory;
mod instruction_storage;
mod request_response;
mod encoding;
mod io;
mod logging_helpers;
mod module;
mod network;
mod processor;
mod protocol;
mod signatures;
mod types;
mod ui;
const CHANNEL_BUFFER_LIMIT: usize = 100;
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
#[clap(short, long)]
interactive: bool,
#[clap(long)]
generate_input: bool,
#[clap(long, default_value = "/ip4/0.0.0.0/tcp/0")]
listen_address: String,
#[clap(short, long)]
dial_address: Option<String>,
#[clap(long, default_value_t = 1)]
parity_shards: u64,
#[cfg(feature = "console-log")]
#[clap(short, long)]
console_subscriber_addr: Option<String>,
/// Seed to generate key
#[clap(long)]
key_seed: Option<u8>,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();
if args.generate_input {
crate::io::test_write_input(
"input/performance/data.json",
"input/performance/program.json",
)
.await
.unwrap();
return Ok(());
}
let encoding_settings = encoding::reed_solomon::Settings {
data_shards_total: DATA_SHARDS_COUNT + args.parity_shards,
data_shards_sufficient: DATA_SHARDS_COUNT,
};
let listen_address: Multiaddr = args.listen_address.parse().unwrap();
let (mut swarm, mut request_response_server, join_handles, shutdown_token) =
network::new(None, encoding_settings, args.interactive, listen_address)
.await
.unwrap();
#[cfg(feature = "console-log")]
let console_subscriber_addr = args.console_subscriber_addr;
#[cfg(not(feature = "console-log"))]
let console_subscriber_addr = None;
let _guard = configure_logs(*swarm.local_peer_id(), console_subscriber_addr);
// Dial the peer identified by the multi-address given as the second
// command-line argument, if any.
if let Some(addr) = args.dial_address {
let remote: Multiaddr = addr.parse()?;
swarm.dial(remote)?;
info!("Dialed {}", addr)
}
// todo: send sigterm
loop {
tokio::select! {
event = swarm.select_next_some() => {
match event {
SwarmEvent::Behaviour(CombinedBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => {
for (peer, address) in list {
swarm.behaviour_mut().main.inject_peer_discovered(peer);
swarm.behaviour_mut().request_response.add_address(&peer, address);
}
}
SwarmEvent::Behaviour(CombinedBehaviourEvent::Mdns(mdns::Event::Expired(list))) => {
for (peer, address) in list {
if !swarm.behaviour_mut().mdns.has_node(&peer) {
swarm.behaviour_mut().main.inject_peer_expired(&peer);
swarm.behaviour_mut().request_response.remove_address(&peer, &address);
}
}
}
SwarmEvent::Behaviour(CombinedBehaviourEvent::RequestResponse(e)) => {
let handle_result = request_response::handle_request_response_event(
&mut request_response_server, e
).await;
if let Err(_) = handle_result {
error!("Shutting down...");
shutdown_token.cancel();
break;
}
},
SwarmEvent::Behaviour(CombinedBehaviourEvent::Main(Err(behaviour::Error::CancelSignal))) => {
info!("{}", behaviour::Error::CancelSignal);
shutdown_token.cancel();
break;
},
SwarmEvent::Behaviour(CombinedBehaviourEvent::Main(Err(behaviour::Error::UnableToOperate))) => {
error!("Shutting down...");
shutdown_token.cancel();
break;
},
SwarmEvent::Behaviour(event) => info!("{:?}", event),
SwarmEvent::NewListenAddr { address, .. } => {
let local_peer_id = *swarm.local_peer_id();
eprintln!(
"Local node is listening on {:?}",
address.with(libp2p::multiaddr::Protocol::P2p(local_peer_id.into()))
);
}
SwarmEvent::IncomingConnection { .. } => {}
SwarmEvent::ConnectionEstablished { .. } => {}
SwarmEvent::ConnectionClosed { .. } => {}
SwarmEvent::OutgoingConnectionError { .. } => {}
SwarmEvent::IncomingConnectionError { .. } => {}
SwarmEvent::Dialing(peer_id) => eprintln!("Dialing {peer_id}"),
other => debug!("{:?}", other),
}
}
action = request_response_server.input.recv() => {
let Some(action) = action else {
error!("other half of `request_response_server.input` was closed. no reason to operate without main behaviour.");
shutdown_token.cancel();
break;
};
debug!("{:?}", action);
match action {
request_response::InEvent::MakeRequest { request, to } => {
// todo: check if `to` is local id, reroute manually if needed
// https://github.com/libp2p/go-libp2p/issues/328
let request_id = swarm.behaviour_mut().request_response.send_request(&to, request.clone());
let send_result = request_response_server.output.send(request_response::OutEvent::AssignedRequestId { request_id, request }).await;
if let Err(_) = send_result {
error!("other half of `request_response_server.output` was closed. no reason to operate without main behaviour.");
shutdown_token.cancel();
break;
}
},
request_response::InEvent::Respond { request_id, channel, response } => {
let send_result = swarm.behaviour_mut().request_response.send_response(channel, response);
if let Err(_) = &send_result {
warn!("Could not send response to {:?}: {:?}", request_id, send_result);
}
},
}
}
}
}
for handle in join_handles {
handle.await.unwrap()
}
Ok(())
}
/// Returned guard should be dropped at the end of program execution
/// (see docs for details)
fn configure_logs(
#[allow(unused)] local_id: PeerId,
#[allow(unused)] console_subscriber_addr: Option<String>,
) -> Option<WorkerGuard> {
#[allow(unused_assignments, unused_mut)]
let mut guard = None;
#[cfg(feature = "file-log")]
let file_layer = {
let filename =
format!("./logs/{:?}-{}.log", chrono::offset::Utc::now(), local_id).to_string();
let path = std::path::Path::new(&filename);
let prefix = path.parent().unwrap();
std::fs::create_dir_all(prefix).unwrap();
let (non_blocking, _guard) =
tracing_appender::non_blocking(std::fs::File::create(path).unwrap());
guard = Some(_guard);
let file_layer = tracing_subscriber::fmt::Layer::new()
.with_ansi(false)
.with_writer(non_blocking);
file_layer
};
#[cfg(feature = "console-log")]
let console_layer = {
let mut layer = ConsoleLayer::builder().with_default_env();
match console_subscriber_addr {
Some(addr) => {
let addr: SocketAddr = addr.parse().unwrap();
layer = layer.server_addr(addr);
}
None => (),
}
layer.spawn()
};
let stdout_layer = tracing_subscriber::fmt::layer()
.with_filter(tracing_subscriber::EnvFilter::from_default_env());
let registry = tracing_subscriber::registry();
#[cfg(feature = "file-log")]
let registry = registry.with(file_layer);
#[cfg(feature = "console-log")]
let registry = registry.with(console_layer);
registry.with(stdout_layer).init();
guard
}