Skip to content

Commit 2844363

Browse files
committed
Improve documentation for consensus crate and remove unwrap() in production code
1 parent c7232f5 commit 2844363

File tree

7 files changed

+81
-68
lines changed

7 files changed

+81
-68
lines changed

node/bft/src/bft.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub struct BFT<N: Network> {
8080
leader_certificate_timer: Arc<AtomicI64>,
8181
/// The consensus sender.
8282
consensus_sender: Arc<OnceCell<ConsensusSender<N>>>,
83-
/// The spawned handles.
83+
/// Handles for all spawned tasks.
8484
handles: Arc<Mutex<Vec<JoinHandle<()>>>>,
8585
/// The BFT lock.
8686
lock: Arc<TMutex<()>>,
@@ -109,6 +109,9 @@ impl<N: Network> BFT<N> {
109109
}
110110

111111
/// Run the BFT instance.
112+
///
113+
/// This will return as soon as all required tasks are spawned.
114+
/// The function must not be called more than once per instance.
112115
pub async fn run(
113116
&mut self,
114117
consensus_sender: Option<ConsensusSender<N>>,

node/bft/src/helpers/ready.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use snarkvm::{
2525
use indexmap::{IndexMap, IndexSet};
2626
use std::collections::{HashMap, VecDeque, hash_map::Entry::Vacant};
2727

28+
/// Maintains a queue of verified ("ready") transactions.
2829
#[derive(Clone, Debug)]
2930
pub struct Ready<N: Network> {
3031
/// Maps each transmission ID to its logical index (physical index + offset)

node/bft/src/primary.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,13 @@ impl<N: Network> Primary<N> {
11331133

11341134
impl<N: Network> Primary<N> {
11351135
/// Starts the primary handlers.
1136+
///
1137+
/// For each receiver in the `primary_receiver` struct, there will be a dedicated tasks
1138+
/// that awaits new data and handles it accordingly.
1139+
/// Additionally, this spawns a tasks that periodically issues PrimaryPings and one that periodically
1140+
/// tries to move the the next round of batches.
1141+
///
1142+
/// This function is called exactly once, in `Self::run()`.
11361143
fn start_handlers(&self, primary_receiver: PrimaryReceiver<N>) {
11371144
let PrimaryReceiver {
11381145
mut rx_batch_propose,
@@ -1143,7 +1150,7 @@ impl<N: Network> Primary<N> {
11431150
mut rx_unconfirmed_transaction,
11441151
} = primary_receiver;
11451152

1146-
// Start the primary ping.
1153+
// Start the primary ping sender.
11471154
let self_ = self.clone();
11481155
self.spawn(async move {
11491156
loop {
@@ -1277,7 +1284,7 @@ impl<N: Network> Primary<N> {
12771284
}
12781285
});
12791286

1280-
// Process the proposed batch.
1287+
// Start the proposed batch handler.
12811288
let self_ = self.clone();
12821289
self.spawn(async move {
12831290
while let Some((peer_ip, batch_propose)) = rx_batch_propose.recv().await {
@@ -1298,7 +1305,7 @@ impl<N: Network> Primary<N> {
12981305
}
12991306
});
13001307

1301-
// Process the batch signature.
1308+
// Start the batch signature handler.
13021309
let self_ = self.clone();
13031310
self.spawn(async move {
13041311
while let Some((peer_ip, batch_signature)) = rx_batch_signature.recv().await {
@@ -1319,7 +1326,7 @@ impl<N: Network> Primary<N> {
13191326
}
13201327
});
13211328

1322-
// Process the certified batch.
1329+
// Start the certified batch handler.
13231330
let self_ = self.clone();
13241331
self.spawn(async move {
13251332
while let Some((peer_ip, batch_certificate)) = rx_batch_certified.recv().await {
@@ -1346,7 +1353,8 @@ impl<N: Network> Primary<N> {
13461353
}
13471354
});
13481355

1349-
// Periodically try to increment to the next round.
1356+
// This task periodically tries to move to the next round.
1357+
//
13501358
// Note: This is necessary to ensure that the primary is not stuck on a previous round
13511359
// despite having received enough certificates to advance to the next round.
13521360
let self_ = self.clone();
@@ -1385,7 +1393,7 @@ impl<N: Network> Primary<N> {
13851393
}
13861394
});
13871395

1388-
// Process the unconfirmed solutions.
1396+
// Start a handler to process new unconfirmed solutions.
13891397
let self_ = self.clone();
13901398
self.spawn(async move {
13911399
while let Some((solution_id, solution, callback)) = rx_unconfirmed_solution.recv().await {
@@ -1411,7 +1419,7 @@ impl<N: Network> Primary<N> {
14111419
}
14121420
});
14131421

1414-
// Process the unconfirmed transactions.
1422+
// Start a handler to process new unconfirmed transactions.
14151423
let self_ = self.clone();
14161424
self.spawn(async move {
14171425
while let Some((transaction_id, transaction, callback)) = rx_unconfirmed_transaction.recv().await {

node/bft/src/worker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ use rand::seq::IteratorRandom;
4242
use std::{future::Future, net::SocketAddr, sync::Arc, time::Duration};
4343
use tokio::{sync::oneshot, task::JoinHandle, time::timeout};
4444

45+
/// A worker's main role is maintaining a queue of verified ("ready") transmissions,
46+
/// which will eventually be fetched by the primary when the primary generates a new batch.
4547
#[derive(Clone)]
4648
pub struct Worker<N: Network> {
4749
/// The worker ID.
@@ -111,8 +113,6 @@ impl<N: Network> Worker<N> {
111113
/// The maximum number of transmissions allowed in a worker ping.
112114
pub const MAX_TRANSMISSIONS_PER_WORKER_PING: usize = BatchHeader::<N>::MAX_TRANSMISSIONS_PER_BATCH / 10;
113115

114-
// transmissions
115-
116116
/// Returns the number of transmissions in the ready queue.
117117
pub fn num_transmissions(&self) -> usize {
118118
self.ready.read().num_transmissions()

node/consensus/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
The `snarkos-node-consensus` crate provides the consensus layer for the snarkOS node.
88

9-
It builds on top of the `snarkos-node-bft` crate, which provides an abstract implementationv of the AleoBFT.
10-
More concretely, this crate provides the communication logic that allows multiple AleoBFT nodes to interact with each other.
9+
The crate builds on top of the `snarkos-node-bft`, which implements AleoBFT.
10+
It manages a ratelimiter/mempool for incoming transmissions, and manages construction of blcoks from batches that have been confirmed by the BFT layer.

0 commit comments

Comments
 (0)