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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#![warn(
future_incompatible,
nonstandard_style,
rust_2018_idioms,
rust_2021_compatibility
)]
use arc_swap::ArcSwap;
use clap::{crate_name, crate_version, App, AppSettings, ArgMatches, SubCommand};
use config::{Committee, Import, Parameters, WorkerCache, WorkerId};
use crypto::{KeyPair, NetworkKeyPair};
use executor::SerializedTransaction;
use eyre::Context;
use fastcrypto::{generate_production_keypair, traits::KeyPair as _};
use futures::future::join_all;
use narwhal_node as node;
use node::{
execution_state::SimpleExecutionState,
metrics::{primary_metrics_registry, start_prometheus_server, worker_metrics_registry},
Node, NodeStorage,
};
use prometheus::Registry;
use std::sync::Arc;
use telemetry_subscribers::TelemetryGuards;
use tokio::sync::mpsc::{channel, Receiver};
use tracing::info;
#[cfg(feature = "benchmark")]
use tracing::subscriber::set_global_default;
#[cfg(feature = "benchmark")]
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
#[tokio::main]
async fn main() -> Result<(), eyre::Report> {
let matches = App::new(crate_name!())
.version(crate_version!())
.about("A research implementation of Narwhal and Tusk.")
.args_from_usage("-v... 'Sets the level of verbosity'")
.subcommand(
SubCommand::with_name("generate_keys")
.about("Print a fresh key pair to file")
.args_from_usage("--filename=<FILE> 'The file where to print the new key pair'"),
)
.subcommand(
SubCommand::with_name("generate_network_keys")
.about("Print a fresh network key pair (ed25519) to file")
.args_from_usage("--filename=<FILE> 'The file where to print the new network key pair'"),
)
.subcommand(
SubCommand::with_name("run")
.about("Run a node")
.args_from_usage("--primary-keys=<FILE> 'The file containing the node's primary keys'")
.args_from_usage("--primary-network-keys=<FILE> 'The file containing the node's primary network keys'")
.args_from_usage("--worker-keys=<FILE> 'The file containing the node's worker keys'")
.args_from_usage("--committee=<FILE> 'The file containing committee information'")
.args_from_usage("--workers=<FILE> 'The file containing worker information'")
.args_from_usage("--parameters=[FILE] 'The file containing the node parameters'")
.args_from_usage("--store=<PATH> 'The path where to create the data store'")
.subcommand(SubCommand::with_name("primary")
.about("Run a single primary")
.args_from_usage("-d, --consensus-disabled 'Provide this flag to run a primary node without Tusk'")
)
.subcommand(
SubCommand::with_name("worker")
.about("Run a single worker")
.args_from_usage("--id=<INT> 'The worker id'"),
)
.setting(AppSettings::SubcommandRequiredElseHelp),
)
.setting(AppSettings::SubcommandRequiredElseHelp)
.get_matches();
let tracing_level = match matches.occurrences_of("v") {
0 => "error",
1 => "warn",
2 => "info",
3 => "debug",
_ => "trace",
};
let network_tracing_level = match matches.occurrences_of("v") {
0 | 1 => "error",
2 => "warn",
3 => "info",
4 => "debug",
_ => "trace",
};
match matches.subcommand() {
("generate_keys", Some(sub_matches)) => {
let _guard = setup_telemetry(tracing_level, network_tracing_level, None);
let kp = generate_production_keypair::<KeyPair>();
config::Export::export(&kp, sub_matches.value_of("filename").unwrap())
.context("Failed to generate key pair")?;
}
("generate_network_keys", Some(sub_matches)) => {
let _guard = setup_telemetry(tracing_level, network_tracing_level, None);
let network_kp = generate_production_keypair::<NetworkKeyPair>();
config::Export::export(&network_kp, sub_matches.value_of("filename").unwrap())
.context("Failed to generate network key pair")?
}
("run", Some(sub_matches)) => {
let primary_key_file = sub_matches.value_of("primary-keys").unwrap();
let primary_keypair = KeyPair::import(primary_key_file)
.context("Failed to load the node's primary keypair")?;
let primary_network_key_file = sub_matches.value_of("primary-network-keys").unwrap();
let primary_network_keypair = NetworkKeyPair::import(primary_network_key_file)
.context("Failed to load the node's primary network keypair")?;
let worker_key_file = sub_matches.value_of("worker-keys").unwrap();
let worker_keypair = NetworkKeyPair::import(worker_key_file)
.context("Failed to load the node's worker keypair")?;
let registry = match sub_matches.subcommand() {
("primary", _) => primary_metrics_registry(primary_keypair.public().clone()),
("worker", Some(worker_matches)) => {
let id = worker_matches
.value_of("id")
.unwrap()
.parse::<WorkerId>()
.context("The worker id must be a positive integer")?;
worker_metrics_registry(id, primary_keypair.public().clone())
}
_ => unreachable!(),
};
cfg_if::cfg_if! {
if #[cfg(feature = "benchmark")] {
setup_benchmark_telemetry(tracing_level, network_tracing_level)?;
} else {
let _guard = setup_telemetry(tracing_level, network_tracing_level, Some(®istry));
}
}
run(
sub_matches,
primary_keypair,
primary_network_keypair,
worker_keypair,
registry,
)
.await?
}
_ => unreachable!(),
}
Ok(())
}
fn setup_telemetry(
tracing_level: &str,
network_tracing_level: &str,
prom_registry: Option<&prometheus::Registry>,
) -> TelemetryGuards {
let log_filter = format!("{tracing_level},h2={network_tracing_level},tower={network_tracing_level},hyper={network_tracing_level},tonic::transport={network_tracing_level},quinn={network_tracing_level}");
let config = telemetry_subscribers::TelemetryConfig::new("narwhal")
.with_env()
.with_log_level(&log_filter);
let config = if let Some(reg) = prom_registry {
config.with_prom_registry(reg)
} else {
config
};
let (guard, _handle) = config.init();
guard
}
#[cfg(feature = "benchmark")]
fn setup_benchmark_telemetry(
tracing_level: &str,
network_tracing_level: &str,
) -> Result<(), eyre::Report> {
let custom_directive = "executor::core=info";
let filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.parse(format!(
"{tracing_level},h2={network_tracing_level},tower={network_tracing_level},hyper={network_tracing_level},tonic::transport={network_tracing_level},{custom_directive}"
))?;
let env_filter = EnvFilter::try_from_default_env().unwrap_or(filter);
let timer = tracing_subscriber::fmt::time::UtcTime::rfc_3339();
let subscriber_builder = tracing_subscriber::fmt::Subscriber::builder()
.with_env_filter(env_filter)
.with_timer(timer)
.with_ansi(false);
let subscriber = subscriber_builder.with_writer(std::io::stderr).finish();
set_global_default(subscriber).expect("Failed to set subscriber");
Ok(())
}
async fn run(
matches: &ArgMatches<'_>,
primary_keypair: KeyPair,
primary_network_keypair: NetworkKeyPair,
worker_keypair: NetworkKeyPair,
registry: Registry,
) -> Result<(), eyre::Report> {
let committee_file = matches.value_of("committee").unwrap();
let workers_file = matches.value_of("workers").unwrap();
let parameters_file = matches.value_of("parameters");
let store_path = matches.value_of("store").unwrap();
let committee = Arc::new(ArcSwap::from_pointee(
Committee::import(committee_file).context("Failed to load the committee information")?,
));
let worker_cache = Arc::new(ArcSwap::from_pointee(
WorkerCache::import(workers_file).context("Failed to load the worker information")?,
));
let parameters = match parameters_file {
Some(filename) => {
Parameters::import(filename).context("Failed to load the node's parameters")?
}
None => Parameters::default(),
};
let store = NodeStorage::reopen(store_path);
let (tx_transaction_confirmation, rx_transaction_confirmation) =
channel(Node::CHANNEL_CAPACITY);
let node_handles = match matches.subcommand() {
("primary", Some(sub_matches)) => {
Node::spawn_primary(
primary_keypair,
primary_network_keypair,
committee,
worker_cache,
&store,
parameters.clone(),
!sub_matches.is_present("consensus-disabled"),
Arc::new(SimpleExecutionState::new(tx_transaction_confirmation)),
®istry,
)
.await?
}
("worker", Some(sub_matches)) => {
let id = sub_matches
.value_of("id")
.unwrap()
.parse::<WorkerId>()
.context("The worker id must be a positive integer")?;
Node::spawn_workers(
primary_keypair.public().clone(),
vec![(id, worker_keypair)],
committee,
worker_cache,
&store,
parameters.clone(),
®istry,
)
}
_ => unreachable!(),
};
let prom_address = parameters.prometheus_metrics.socket_addr;
info!(
"Starting Prometheus HTTP metrics endpoint at {}",
prom_address
);
let _metrics_server_handle = start_prometheus_server(prom_address, ®istry);
analyze(rx_transaction_confirmation).await;
join_all(node_handles).await;
Ok(())
}
async fn analyze(mut rx_output: Receiver<SerializedTransaction>) {
while let Some(_message) = rx_output.recv().await {
}
}