-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
lib.rs
464 lines (422 loc) · 19.9 KB
/
lib.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkOS library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![forbid(unsafe_code)]
#[macro_use]
extern crate tracing;
use snarkos_account::Account;
use snarkos_node_bft::{
helpers::{
fmt_id,
init_consensus_channels,
ConsensusReceiver,
PrimaryReceiver,
PrimarySender,
Storage as NarwhalStorage,
},
spawn_blocking,
BFT,
};
use snarkos_node_bft_ledger_service::LedgerService;
use snarkos_node_bft_storage_service::BFTPersistentStorage;
use snarkvm::{
ledger::{
block::Transaction,
coinbase::{ProverSolution, PuzzleCommitment},
narwhal::{BatchHeader, Data, Subdag, Transmission, TransmissionID},
},
prelude::*,
};
use aleo_std::StorageMode;
use anyhow::Result;
use colored::Colorize;
use indexmap::IndexMap;
use lru::LruCache;
use parking_lot::Mutex;
use std::{future::Future, net::SocketAddr, num::NonZeroUsize, sync::Arc};
use tokio::{
sync::{oneshot, OnceCell},
task::JoinHandle,
};
/// The capacity of the mempool reserved for deployments.
const CAPACITY_FOR_DEPLOYMENTS: usize = 1 << 5;
/// The capacity of the mempool reserved for executions.
const CAPACITY_FOR_EXECUTIONS: usize = 1 << 10;
/// The capacity of the mempool reserved for solutions.
const CAPACITY_FOR_SOLUTIONS: usize = 1 << 10;
/// The percentage of the transmissions being processed that are deployments.
const DEPLOYMENT_VERIFICATION_RATE: usize = 15;
/// Helper struct to track incoming transactions.
struct TransactionsQueue<N: Network> {
pub deployments: LruCache<N::TransactionID, Transaction<N>>,
pub executions: LruCache<N::TransactionID, Transaction<N>>,
}
impl<N: Network> Default for TransactionsQueue<N> {
fn default() -> Self {
Self {
deployments: LruCache::new(NonZeroUsize::new(CAPACITY_FOR_DEPLOYMENTS).unwrap()),
executions: LruCache::new(NonZeroUsize::new(CAPACITY_FOR_EXECUTIONS).unwrap()),
}
}
}
#[derive(Clone)]
pub struct Consensus<N: Network> {
/// The ledger.
ledger: Arc<dyn LedgerService<N>>,
/// The BFT.
bft: BFT<N>,
/// The primary sender.
primary_sender: Arc<OnceCell<PrimarySender<N>>>,
/// The unconfirmed solutions queue.
solutions_queue: Arc<Mutex<LruCache<PuzzleCommitment<N>, ProverSolution<N>>>>,
/// The unconfirmed transactions queue.
transactions_queue: Arc<Mutex<TransactionsQueue<N>>>,
/// The recently-seen unconfirmed solutions.
seen_solutions: Arc<Mutex<LruCache<PuzzleCommitment<N>, ()>>>,
/// The recently-seen unconfirmed transactions.
seen_transactions: Arc<Mutex<LruCache<N::TransactionID, ()>>>,
/// The spawned handles.
handles: Arc<Mutex<Vec<JoinHandle<()>>>>,
}
impl<N: Network> Consensus<N> {
/// Initializes a new instance of consensus.
pub fn new(
account: Account<N>,
ledger: Arc<dyn LedgerService<N>>,
ip: Option<SocketAddr>,
trusted_validators: &[SocketAddr],
storage_mode: StorageMode,
) -> Result<Self> {
// Recover the development ID, if it is present.
let dev = match storage_mode {
StorageMode::Development(id) => Some(id),
StorageMode::Production | StorageMode::Custom(..) => None,
};
// Initialize the Narwhal transmissions.
let transmissions = Arc::new(BFTPersistentStorage::open(storage_mode)?);
// Initialize the Narwhal storage.
let storage = NarwhalStorage::new(ledger.clone(), transmissions, BatchHeader::<N>::MAX_GC_ROUNDS as u64);
// Initialize the BFT.
let bft = BFT::new(account, storage, ledger.clone(), ip, trusted_validators, dev)?;
// Return the consensus.
Ok(Self {
ledger,
bft,
primary_sender: Default::default(),
solutions_queue: Arc::new(Mutex::new(LruCache::new(NonZeroUsize::new(CAPACITY_FOR_SOLUTIONS).unwrap()))),
transactions_queue: Default::default(),
seen_solutions: Arc::new(Mutex::new(LruCache::new(NonZeroUsize::new(1 << 16).unwrap()))),
seen_transactions: Arc::new(Mutex::new(LruCache::new(NonZeroUsize::new(1 << 16).unwrap()))),
handles: Default::default(),
})
}
/// Run the consensus instance.
pub async fn run(&mut self, primary_sender: PrimarySender<N>, primary_receiver: PrimaryReceiver<N>) -> Result<()> {
info!("Starting the consensus instance...");
// Set the primary sender.
self.primary_sender.set(primary_sender.clone()).expect("Primary sender already set");
// First, initialize the consensus channels.
let (consensus_sender, consensus_receiver) = init_consensus_channels();
// Then, start the consensus handlers.
self.start_handlers(consensus_receiver);
// Lastly, the consensus.
self.bft.run(Some(consensus_sender), primary_sender, primary_receiver).await?;
Ok(())
}
/// Returns the ledger.
pub const fn ledger(&self) -> &Arc<dyn LedgerService<N>> {
&self.ledger
}
/// Returns the BFT.
pub const fn bft(&self) -> &BFT<N> {
&self.bft
}
/// Returns the primary sender.
pub fn primary_sender(&self) -> &PrimarySender<N> {
self.primary_sender.get().expect("Primary sender not set")
}
}
impl<N: Network> Consensus<N> {
/// Returns the number of unconfirmed transmissions.
pub fn num_unconfirmed_transmissions(&self) -> usize {
self.bft.num_unconfirmed_transmissions()
}
/// Returns the number of unconfirmed ratifications.
pub fn num_unconfirmed_ratifications(&self) -> usize {
self.bft.num_unconfirmed_ratifications()
}
/// Returns the number of solutions.
pub fn num_unconfirmed_solutions(&self) -> usize {
self.bft.num_unconfirmed_solutions()
}
/// Returns the number of unconfirmed transactions.
pub fn num_unconfirmed_transactions(&self) -> usize {
self.bft.num_unconfirmed_transactions()
}
}
impl<N: Network> Consensus<N> {
/// Returns the unconfirmed transmission IDs.
pub fn unconfirmed_transmission_ids(&self) -> impl '_ + Iterator<Item = TransmissionID<N>> {
self.bft.unconfirmed_transmission_ids()
}
/// Returns the unconfirmed transmissions.
pub fn unconfirmed_transmissions(&self) -> impl '_ + Iterator<Item = (TransmissionID<N>, Transmission<N>)> {
self.bft.unconfirmed_transmissions()
}
/// Returns the unconfirmed solutions.
pub fn unconfirmed_solutions(&self) -> impl '_ + Iterator<Item = (PuzzleCommitment<N>, Data<ProverSolution<N>>)> {
self.bft.unconfirmed_solutions()
}
/// Returns the unconfirmed transactions.
pub fn unconfirmed_transactions(&self) -> impl '_ + Iterator<Item = (N::TransactionID, Data<Transaction<N>>)> {
self.bft.unconfirmed_transactions()
}
}
impl<N: Network> Consensus<N> {
/// Adds the given unconfirmed solution to the memory pool.
pub async fn add_unconfirmed_solution(&self, solution: ProverSolution<N>) -> Result<()> {
// Process the unconfirmed solution.
{
let solution_id = solution.commitment();
// Check if the transaction was recently seen.
if self.seen_solutions.lock().put(solution_id, ()).is_some() {
// If the transaction was recently seen, return early.
return Ok(());
}
// Check if the solution already exists in the ledger.
if self.ledger.contains_transmission(&TransmissionID::from(solution_id))? {
bail!("Solution '{}' exists in the ledger {}", fmt_id(solution_id), "(skipping)".dimmed());
}
// Add the solution to the memory pool.
trace!("Received unconfirmed solution '{}' in the queue", fmt_id(solution_id));
if self.solutions_queue.lock().put(solution_id, solution).is_some() {
bail!("Solution '{}' exists in the memory pool", fmt_id(solution_id));
}
}
// If the memory pool of this node is full, return early.
let num_unconfirmed = self.num_unconfirmed_transmissions();
if num_unconfirmed > N::MAX_SOLUTIONS || num_unconfirmed > BatchHeader::<N>::MAX_TRANSMISSIONS_PER_BATCH {
return Ok(());
}
// Retrieve the solutions.
let solutions = {
// Determine the available capacity.
let capacity = N::MAX_SOLUTIONS.saturating_sub(num_unconfirmed);
// Acquire the lock on the queue.
let mut queue = self.solutions_queue.lock();
// Determine the number of solutions to send.
let num_solutions = queue.len().min(capacity);
// Drain the solutions from the queue.
(0..num_solutions).filter_map(|_| queue.pop_lru().map(|(_, solution)| solution)).collect::<Vec<_>>()
};
// Iterate over the solutions.
for solution in solutions.into_iter() {
let solution_id = solution.commitment();
trace!("Adding unconfirmed solution '{}' to the memory pool...", fmt_id(solution_id));
// Send the unconfirmed solution to the primary.
if let Err(e) = self.primary_sender().send_unconfirmed_solution(solution_id, Data::Object(solution)).await {
// If the BFT is synced, then log the warning.
if self.bft.is_synced() {
warn!("Failed to add unconfirmed solution '{}' to the memory pool - {e}", fmt_id(solution_id));
}
}
}
Ok(())
}
/// Adds the given unconfirmed transaction to the memory pool.
pub async fn add_unconfirmed_transaction(&self, transaction: Transaction<N>) -> Result<()> {
// Process the unconfirmed transaction.
{
let transaction_id = transaction.id();
// Check that the transaction is not a fee transaction.
if transaction.is_fee() {
bail!("Transaction '{}' is a fee transaction {}", fmt_id(transaction_id), "(skipping)".dimmed());
}
// Check if the transaction was recently seen.
if self.seen_transactions.lock().put(transaction_id, ()).is_some() {
// If the transaction was recently seen, return early.
return Ok(());
}
// Check if the transaction already exists in the ledger.
if self.ledger.contains_transmission(&TransmissionID::from(&transaction_id))? {
bail!("Transaction '{}' exists in the ledger {}", fmt_id(transaction_id), "(skipping)".dimmed());
}
// Add the transaction to the memory pool.
trace!("Received unconfirmed transaction '{}' in the queue", fmt_id(transaction_id));
if transaction.is_deploy() {
if self.transactions_queue.lock().deployments.put(transaction_id, transaction).is_some() {
bail!("Transaction '{}' exists in the memory pool", fmt_id(transaction_id));
}
} else if self.transactions_queue.lock().executions.put(transaction_id, transaction).is_some() {
bail!("Transaction '{}' exists in the memory pool", fmt_id(transaction_id));
}
}
// If the memory pool of this node is full, return early.
let num_unconfirmed = self.num_unconfirmed_transmissions();
if num_unconfirmed > BatchHeader::<N>::MAX_TRANSMISSIONS_PER_BATCH {
return Ok(());
}
// Retrieve the transactions.
let transactions = {
// Determine the available capacity.
let capacity = BatchHeader::<N>::MAX_TRANSMISSIONS_PER_BATCH.saturating_sub(num_unconfirmed);
// Acquire the lock on the transactions queue.
let mut tx_queue = self.transactions_queue.lock();
// Determine the number of deployments to send.
let num_deployments = tx_queue.deployments.len().min(capacity * DEPLOYMENT_VERIFICATION_RATE / 100);
// Determine the number of executions to send.
let num_executions = tx_queue.executions.len().min(capacity.saturating_sub(num_deployments));
// Create an iterator which will select interleaved deployments and executions within the capacity.
// Note: interleaving ensures we will never have consecutive invalid deployments blocking the queue.
let selector_iter = (0..num_deployments).map(|_| true).interleave((0..num_executions).map(|_| false));
// Drain the transactions from the queue, interleaving deployments and executions.
selector_iter
.filter_map(|select_deployment| {
if select_deployment {
tx_queue.deployments.pop_lru().map(|(_, tx)| tx)
} else {
tx_queue.executions.pop_lru().map(|(_, tx)| tx)
}
})
.collect_vec()
};
// Iterate over the transactions.
for transaction in transactions.into_iter() {
let transaction_id = transaction.id();
trace!("Adding unconfirmed transaction '{}' to the memory pool...", fmt_id(transaction_id));
// Send the unconfirmed transaction to the primary.
if let Err(e) =
self.primary_sender().send_unconfirmed_transaction(transaction_id, Data::Object(transaction)).await
{
// If the BFT is synced, then log the warning.
if self.bft.is_synced() {
warn!(
"Failed to add unconfirmed transaction '{}' to the memory pool - {e}",
fmt_id(transaction_id)
);
}
}
}
Ok(())
}
}
impl<N: Network> Consensus<N> {
/// Starts the consensus handlers.
fn start_handlers(&self, consensus_receiver: ConsensusReceiver<N>) {
let ConsensusReceiver { mut rx_consensus_subdag } = consensus_receiver;
// Process the committed subdag and transmissions from the BFT.
let self_ = self.clone();
self.spawn(async move {
while let Some((committed_subdag, transmissions, callback)) = rx_consensus_subdag.recv().await {
self_.process_bft_subdag(committed_subdag, transmissions, callback).await;
}
});
}
/// Processes the committed subdag and transmissions from the BFT.
async fn process_bft_subdag(
&self,
subdag: Subdag<N>,
transmissions: IndexMap<TransmissionID<N>, Transmission<N>>,
callback: oneshot::Sender<Result<()>>,
) {
// Try to advance to the next block.
let self_ = self.clone();
let transmissions_ = transmissions.clone();
let result = spawn_blocking! { self_.try_advance_to_next_block(subdag, transmissions_) };
// If the block failed to advance, reinsert the transmissions into the memory pool.
if let Err(e) = &result {
error!("Unable to advance to the next block - {e}");
// On failure, reinsert the transmissions into the memory pool.
self.reinsert_transmissions(transmissions).await;
}
// Send the callback **after** advancing to the next block.
// Note: We must await the block to be advanced before sending the callback.
callback.send(result).ok();
}
/// Attempts to advance to the next block.
fn try_advance_to_next_block(
&self,
subdag: Subdag<N>,
transmissions: IndexMap<TransmissionID<N>, Transmission<N>>,
) -> Result<()> {
#[cfg(feature = "metrics")]
let start = subdag.leader_certificate().batch_header().timestamp();
#[cfg(feature = "metrics")]
let num_committed_certificates = subdag.values().map(|c| c.len()).sum::<usize>();
#[cfg(feature = "metrics")]
let current_block_timestamp = self.ledger.latest_block().header().metadata().timestamp();
// Create the candidate next block.
let next_block = self.ledger.prepare_advance_to_next_quorum_block(subdag, transmissions)?;
// Check that the block is well-formed.
self.ledger.check_next_block(&next_block)?;
// Advance to the next block.
self.ledger.advance_to_next_block(&next_block)?;
#[cfg(feature = "metrics")]
{
let elapsed = std::time::Duration::from_secs((snarkos_node_bft::helpers::now() - start) as u64);
let next_block_timestamp = next_block.header().metadata().timestamp();
let block_latency = next_block_timestamp - current_block_timestamp;
metrics::gauge(metrics::blocks::HEIGHT, next_block.height() as f64);
metrics::increment_gauge(metrics::blocks::TRANSACTIONS, next_block.transactions().len() as f64);
metrics::gauge(metrics::consensus::LAST_COMMITTED_ROUND, next_block.round() as f64);
metrics::gauge(metrics::consensus::COMMITTED_CERTIFICATES, num_committed_certificates as f64);
metrics::histogram(metrics::consensus::CERTIFICATE_COMMIT_LATENCY, elapsed.as_secs_f64());
metrics::histogram(metrics::consensus::BLOCK_LATENCY, block_latency as f64);
}
Ok(())
}
/// Reinserts the given transmissions into the memory pool.
async fn reinsert_transmissions(&self, transmissions: IndexMap<TransmissionID<N>, Transmission<N>>) {
// Iterate over the transmissions.
for (transmission_id, transmission) in transmissions.into_iter() {
// Reinsert the transmission into the memory pool.
if let Err(e) = self.reinsert_transmission(transmission_id, transmission).await {
warn!("Unable to reinsert transmission {} into the memory pool - {e}", fmt_id(transmission_id));
}
}
}
/// Reinserts the given transmission into the memory pool.
async fn reinsert_transmission(
&self,
transmission_id: TransmissionID<N>,
transmission: Transmission<N>,
) -> Result<()> {
// Initialize a callback sender and receiver.
let (callback, callback_receiver) = oneshot::channel();
// Send the transmission to the primary.
match (transmission_id, transmission) {
(TransmissionID::Ratification, Transmission::Ratification) => return Ok(()),
(TransmissionID::Solution(commitment), Transmission::Solution(solution)) => {
// Send the solution to the primary.
self.primary_sender().tx_unconfirmed_solution.send((commitment, solution, callback)).await?;
}
(TransmissionID::Transaction(transaction_id), Transmission::Transaction(transaction)) => {
// Send the transaction to the primary.
self.primary_sender().tx_unconfirmed_transaction.send((transaction_id, transaction, callback)).await?;
}
_ => bail!("Mismatching `(transmission_id, transmission)` pair in consensus"),
}
// Await the callback.
callback_receiver.await?
}
/// Spawns a task with the given future; it should only be used for long-running tasks.
fn spawn<T: Future<Output = ()> + Send + 'static>(&self, future: T) {
self.handles.lock().push(tokio::spawn(future));
}
/// Shuts down the BFT.
pub async fn shut_down(&self) {
info!("Shutting down consensus...");
// Shut down the BFT.
self.bft.shut_down().await;
// Abort the tasks.
self.handles.lock().iter().for_each(|handle| handle.abort());
}
}