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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
use config::{Parameters, SharedCommittee, SharedWorkerCache, WorkerId};
use consensus::{
bullshark::Bullshark,
dag::Dag,
metrics::{ChannelMetrics, ConsensusMetrics},
Consensus, ConsensusOutput,
};
use crypto::{KeyPair, NetworkKeyPair, PublicKey};
use executor::{get_restored_consensus_output, ExecutionState, Executor, SubscriberResult};
use fastcrypto::traits::{KeyPair as _, VerifyingKey};
use itertools::Itertools;
use network::P2pNetwork;
use primary::{NetworkModel, PayloadToken, Primary, PrimaryChannelMetrics};
use prometheus::{IntGauge, Registry};
use std::sync::Arc;
use storage::{CertificateStore, CertificateToken};
use store::{
reopen,
rocks::{open_cf, DBMap},
Store,
};
use tokio::sync::oneshot;
use tokio::{sync::watch, task::JoinHandle};
use tracing::{debug, info};
use types::{
metered_channel, Batch, BatchDigest, Certificate, CertificateDigest, ConsensusStore, Header,
HeaderDigest, ReconfigureNotification, Round, RoundVoteDigestPair, SequenceNumber,
};
use worker::{metrics::initialise_metrics, Worker};
pub mod execution_state;
pub mod metrics;
pub mod restarter;
pub struct NodeStorage {
pub vote_digest_store: Store<PublicKey, RoundVoteDigestPair>,
pub header_store: Store<HeaderDigest, Header>,
pub certificate_store: CertificateStore,
pub payload_store: Store<(BatchDigest, WorkerId), PayloadToken>,
pub batch_store: Store<BatchDigest, Batch>,
pub consensus_store: Arc<ConsensusStore>,
pub temp_batch_store: Store<(CertificateDigest, BatchDigest), Batch>,
}
impl NodeStorage {
const VOTES_CF: &'static str = "votes";
const HEADERS_CF: &'static str = "headers";
const CERTIFICATES_CF: &'static str = "certificates";
const CERTIFICATE_ID_BY_ROUND_CF: &'static str = "certificate_id_by_round";
const PAYLOAD_CF: &'static str = "payload";
const BATCHES_CF: &'static str = "batches";
const LAST_COMMITTED_CF: &'static str = "last_committed";
const SEQUENCE_CF: &'static str = "sequence";
const TEMP_BATCH_CF: &'static str = "temp_batches";
pub fn reopen<Path: AsRef<std::path::Path>>(store_path: Path) -> Self {
let rocksdb = open_cf(
store_path,
None,
&[
Self::VOTES_CF,
Self::HEADERS_CF,
Self::CERTIFICATES_CF,
Self::CERTIFICATE_ID_BY_ROUND_CF,
Self::PAYLOAD_CF,
Self::BATCHES_CF,
Self::LAST_COMMITTED_CF,
Self::SEQUENCE_CF,
Self::TEMP_BATCH_CF,
],
)
.expect("Cannot open database");
let (
votes_map,
header_map,
certificate_map,
certificate_id_by_round_map,
payload_map,
batch_map,
last_committed_map,
sequence_map,
temp_batch_map,
) = reopen!(&rocksdb,
Self::VOTES_CF;<PublicKey, RoundVoteDigestPair>,
Self::HEADERS_CF;<HeaderDigest, Header>,
Self::CERTIFICATES_CF;<CertificateDigest, Certificate>,
Self::CERTIFICATE_ID_BY_ROUND_CF;<(Round, CertificateDigest), CertificateToken>,
Self::PAYLOAD_CF;<(BatchDigest, WorkerId), PayloadToken>,
Self::BATCHES_CF;<BatchDigest, Batch>,
Self::LAST_COMMITTED_CF;<PublicKey, Round>,
Self::SEQUENCE_CF;<SequenceNumber, CertificateDigest>,
Self::TEMP_BATCH_CF;<(CertificateDigest, BatchDigest), Batch>
);
let vote_digest_store = Store::new(votes_map);
let header_store = Store::new(header_map);
let certificate_store = CertificateStore::new(certificate_map, certificate_id_by_round_map);
let payload_store = Store::new(payload_map);
let batch_store = Store::new(batch_map);
let consensus_store = Arc::new(ConsensusStore::new(last_committed_map, sequence_map));
let temp_batch_store = Store::new(temp_batch_map);
Self {
vote_digest_store,
header_store,
certificate_store,
payload_store,
batch_store,
consensus_store,
temp_batch_store,
}
}
}
pub struct Node;
impl Node {
pub const CHANNEL_CAPACITY: usize = 1_000;
pub async fn spawn_primary<State>(
keypair: KeyPair,
network_keypair: NetworkKeyPair,
committee: SharedCommittee,
worker_cache: SharedWorkerCache,
store: &NodeStorage,
parameters: Parameters,
internal_consensus: bool,
execution_state: Arc<State>,
registry: &Registry,
) -> SubscriberResult<Vec<JoinHandle<()>>>
where
State: ExecutionState + Send + Sync + 'static,
{
let initial_committee = ReconfigureNotification::NewEpoch((**committee.load()).clone());
let (tx_reconfigure, _rx_reconfigure) = watch::channel(initial_committee);
let new_certificates_counter = IntGauge::new(
PrimaryChannelMetrics::NAME_NEW_CERTS,
PrimaryChannelMetrics::DESC_NEW_CERTS,
)
.unwrap();
let (tx_new_certificates, rx_new_certificates) =
metered_channel::channel(Self::CHANNEL_CAPACITY, &new_certificates_counter);
let committed_certificates_counter = IntGauge::new(
PrimaryChannelMetrics::NAME_COMMITTED_CERTS,
PrimaryChannelMetrics::DESC_COMMITTED_CERTS,
)
.unwrap();
let (tx_consensus, rx_consensus) =
metered_channel::channel(Self::CHANNEL_CAPACITY, &committed_certificates_counter);
let tx_get_block_commands_counter = IntGauge::new(
PrimaryChannelMetrics::NAME_GET_BLOCK_COMMANDS,
PrimaryChannelMetrics::DESC_GET_BLOCK_COMMANDS,
)
.unwrap();
let (tx_get_block_commands, rx_get_block_commands) =
metered_channel::channel(Self::CHANNEL_CAPACITY, &tx_get_block_commands_counter);
let name = keypair.public().clone();
let mut handles = Vec::new();
let (rx_executor_network, tx_executor_network) = oneshot::channel();
let (dag, network_model) = if !internal_consensus {
debug!("Consensus is disabled: the primary will run w/o Tusk");
let consensus_metrics = Arc::new(ConsensusMetrics::new(registry));
let (handle, dag) = Dag::new(&committee.load(), rx_new_certificates, consensus_metrics);
handles.push(handle);
(Some(Arc::new(dag)), NetworkModel::Asynchronous)
} else {
let consensus_handles = Self::spawn_consensus(
name.clone(),
tx_executor_network,
worker_cache.clone(),
committee.clone(),
store,
parameters.clone(),
execution_state,
&tx_reconfigure,
rx_new_certificates,
tx_consensus.clone(),
registry,
)
.await?;
handles.extend(consensus_handles);
(None, NetworkModel::PartiallySynchronous)
};
#[cfg(feature = "dhat-heap")]
let profiler = {
use fastcrypto::traits::EncodeDecodeBase64;
use std::path::Path;
let heap_file = format!("dhat-heap-{}.json", name.encode_base64());
Arc::new(
dhat::Profiler::builder()
.file_name(Path::new(&heap_file))
.build(),
)
};
let primary_handles = Primary::spawn(
name.clone(),
keypair,
network_keypair,
committee.clone(),
worker_cache.clone(),
parameters.clone(),
store.header_store.clone(),
store.certificate_store.clone(),
store.payload_store.clone(),
store.vote_digest_store.clone(),
tx_new_certificates,
rx_consensus,
tx_get_block_commands,
rx_get_block_commands,
dag,
network_model,
tx_reconfigure,
tx_consensus,
registry,
Some(rx_executor_network),
);
handles.extend(primary_handles);
#[cfg(feature = "dhat-heap")]
{
use std::time::Duration;
#[allow(clippy::redundant_clone)]
let profiler2 = profiler.clone();
std::thread::spawn(|| {
std::thread::sleep(Duration::from_secs(240));
println!("Dropping DHAT profiler...");
drop(profiler2);
});
}
Ok(handles)
}
async fn spawn_consensus<State>(
name: PublicKey,
network: oneshot::Receiver<P2pNetwork>,
worker_cache: SharedWorkerCache,
committee: SharedCommittee,
store: &NodeStorage,
parameters: Parameters,
execution_state: State,
tx_reconfigure: &watch::Sender<ReconfigureNotification>,
rx_new_certificates: metered_channel::Receiver<Certificate>,
tx_feedback: metered_channel::Sender<Certificate>,
registry: &Registry,
) -> SubscriberResult<Vec<JoinHandle<()>>>
where
PublicKey: VerifyingKey,
State: ExecutionState + Send + Sync + 'static,
{
let consensus_metrics = Arc::new(ConsensusMetrics::new(registry));
let channel_metrics = ChannelMetrics::new(registry);
let (tx_sequence, rx_sequence) =
metered_channel::channel(Self::CHANNEL_CAPACITY, &channel_metrics.tx_sequence);
let restored_consensus_output = get_restored_consensus_output(
store.consensus_store.clone(),
store.certificate_store.clone(),
&execution_state,
)
.await?
.into_iter()
.sorted_by(|a, b| a.consensus_index.cmp(&b.consensus_index))
.collect::<Vec<ConsensusOutput>>();
let len_restored = restored_consensus_output.len() as u64;
if len_restored > 0 {
info!(
"Consensus output on its way to the executor was restored for {} certificates",
len_restored
);
}
consensus_metrics
.recovered_consensus_output
.inc_by(len_restored);
let ordering_engine = Bullshark::new(
(**committee.load()).clone(),
store.consensus_store.clone(),
parameters.gc_depth,
);
let consensus_handles = Consensus::spawn(
(**committee.load()).clone(),
store.consensus_store.clone(),
store.certificate_store.clone(),
tx_reconfigure.subscribe(),
rx_new_certificates,
tx_feedback,
tx_sequence,
ordering_engine,
consensus_metrics.clone(),
parameters.gc_depth,
);
let executor_handles = Executor::spawn(
name,
network,
worker_cache,
(**committee.load()).clone(),
execution_state,
tx_reconfigure,
rx_sequence,
registry,
restored_consensus_output,
)?;
Ok(executor_handles
.into_iter()
.chain(std::iter::once(consensus_handles))
.collect())
}
pub fn spawn_workers(
primary_name: PublicKey,
ids_and_keypairs: Vec<(WorkerId, NetworkKeyPair)>,
committee: SharedCommittee,
worker_cache: SharedWorkerCache,
store: &NodeStorage,
parameters: Parameters,
registry: &Registry,
) -> Vec<JoinHandle<()>> {
let mut handles = Vec::new();
let metrics = initialise_metrics(registry);
for (id, keypair) in ids_and_keypairs {
let worker_handles = Worker::spawn(
primary_name.clone(),
keypair,
id,
committee.clone(),
worker_cache.clone(),
parameters.clone(),
store.batch_store.clone(),
metrics.clone(),
);
handles.extend(worker_handles);
}
handles
}
}