Skip to content

Commit

Permalink
Merge pull request #3186 from AleoHQ/testnet3-staging-next
Browse files Browse the repository at this point in the history
Additional Hotfixes for `testnet3` branch
  • Loading branch information
howardwu committed Mar 24, 2024
2 parents 862d632 + ae6d79c commit 7d8b66b
Show file tree
Hide file tree
Showing 20 changed files with 830 additions and 281 deletions.
117 changes: 59 additions & 58 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions Cargo.toml
Expand Up @@ -55,7 +55,6 @@ name = "snarkos"
path = "snarkos/main.rs"

[features]
jemalloc = [ "tikv-jemallocator" ]
metrics = [ "snarkos-node-metrics", "snarkos-node/metrics" ]

[dependencies.anyhow]
Expand Down Expand Up @@ -120,9 +119,8 @@ version = "=2.2.7"
path = "./node/tcp"
version = "=2.2.7"

[dependencies.tikv-jemallocator]
version = "0.5"
optional = true
[target.'cfg(all(target_os = "linux", target_arch = "x86_64"))'.dependencies]
tikv-jemallocator = "0.5"

[dev-dependencies.rusty-hook]
version = "0.11.2"
Expand Down
23 changes: 6 additions & 17 deletions cli/src/commands/start.rs
Expand Up @@ -248,7 +248,7 @@ impl Start {
let _ = PrivateKey::<N>::new(&mut rng)?;
}
let private_key = PrivateKey::<N>::new(&mut rng)?;
println!("🔑 Your development private key for node {dev} is {}\n", private_key.to_string().bold());
println!("🔑 Your development private key for node {dev} is {}.\n", private_key.to_string().bold());
private_key
})
}
Expand Down Expand Up @@ -411,7 +411,7 @@ impl Start {
// If the display is not enabled, render the welcome message.
if self.nodisplay {
// Print the Aleo address.
println!("🪪 Your Aleo address is {}.\n", account.address().to_string().bold());
println!("👛 Your Aleo address is {}.\n", account.address().to_string().bold());
// Print the node type and network.
println!(
"🧭 Starting {} on {} {} at {}.\n",
Expand Down Expand Up @@ -465,23 +465,12 @@ impl Start {
fn runtime() -> Runtime {
// Retrieve the number of cores.
let num_cores = num_cpus::get();
// Determine the number of main cores.
let main_cores = match num_cores {
// Insufficient
0..=3 => {
eprintln!("The number of cores is insufficient, at least 4 are needed.");
std::process::exit(1);
}
// Efficiency mode
4..=8 => 2,
// Standard mode
9..=16 => 8,
// Performance mode
_ => 16,
};

// Initialize the number of tokio worker threads, max tokio blocking threads, and rayon cores.
// Note: We intentionally set the number of tokio worker threads and number of rayon cores to be
// more than the number of physical cores, because the node is expected to be I/O-bound.
let (num_tokio_worker_threads, max_tokio_blocking_threads, num_rayon_cores_global) =
{ (num_cores.min(main_cores), 512, num_cores.saturating_sub(main_cores).max(1)) };
(2 * num_cores, 512, num_cores);

// Initialize the parallelization parameters.
rayon::ThreadPoolBuilder::new()
Expand Down
8 changes: 4 additions & 4 deletions cli/src/helpers/mod.rs
Expand Up @@ -41,8 +41,8 @@ pub fn check_open_files_limit(minimum: u64) {
// Warn about too low limit.
let warning = [
format!("⚠️ The open files limit ({soft_limit}) for this process is lower than recommended."),
format!("⚠️ To ensure correct behavior of the node, please raise it to at least {minimum}."),
"⚠️ See the `ulimit` command and `/etc/security/limits.conf` for more details.".to_owned(),
format!(" To ensure correct behavior of the node, please raise it to at least {minimum}."),
" See the `ulimit` command and `/etc/security/limits.conf` for more details.".to_owned(),
]
.join("\n")
.yellow()
Expand All @@ -54,8 +54,8 @@ pub fn check_open_files_limit(minimum: u64) {
// Warn about unknown limit.
let warning = [
format!("⚠️ Unable to check the open files limit for this process due to {err}."),
format!("⚠️ To ensure correct behavior of the node, please ensure it is at least {minimum}."),
"⚠️ See the `ulimit` command and `/etc/security/limits.conf` for more details.".to_owned(),
format!(" To ensure correct behavior of the node, please ensure it is at least {minimum}."),
" See the `ulimit` command and `/etc/security/limits.conf` for more details.".to_owned(),
]
.join("\n")
.yellow()
Expand Down
5 changes: 4 additions & 1 deletion node/bft/ledger-service/Cargo.toml
Expand Up @@ -18,7 +18,7 @@ edition = "2021"

[features]
default = [ ]
ledger = [ "rand", "tokio", "tracing" ]
ledger = [ "parking_lot", "rand", "tokio", "tracing" ]
ledger-write = [ ]
mock = [ "parking_lot", "tracing" ]
prover = [ ]
Expand All @@ -32,6 +32,9 @@ version = "0.1"
version = "2.1"
features = [ "serde", "rayon" ]

[dependencies.lru]
version = "0.12"

[dependencies.parking_lot]
version = "0.12"
optional = true
Expand Down
31 changes: 27 additions & 4 deletions node/bft/ledger-service/src/ledger.rs
Expand Up @@ -26,6 +26,8 @@ use snarkvm::{
};

use indexmap::IndexMap;
use lru::LruCache;
use parking_lot::Mutex;
use std::{
fmt,
ops::Range,
Expand All @@ -35,18 +37,23 @@ use std::{
},
};

/// The capacity of the LRU holiding the recently queried committees.
const COMMITTEE_CACHE_SIZE: usize = 16;

/// A core ledger service.
pub struct CoreLedgerService<N: Network, C: ConsensusStorage<N>> {
ledger: Ledger<N, C>,
coinbase_verifying_key: Arc<CoinbaseVerifyingKey<N>>,
committee_cache: Arc<Mutex<LruCache<u64, Committee<N>>>>,
shutdown: Arc<AtomicBool>,
}

impl<N: Network, C: ConsensusStorage<N>> CoreLedgerService<N, C> {
/// Initializes a new core ledger service.
pub fn new(ledger: Ledger<N, C>, shutdown: Arc<AtomicBool>) -> Self {
let coinbase_verifying_key = Arc::new(ledger.coinbase_puzzle().coinbase_verifying_key().clone());
Self { ledger, coinbase_verifying_key, shutdown }
let committee_cache = Arc::new(Mutex::new(LruCache::new(COMMITTEE_CACHE_SIZE.try_into().unwrap())));
Self { ledger, coinbase_verifying_key, committee_cache, shutdown }
}
}

Expand Down Expand Up @@ -127,9 +134,19 @@ impl<N: Network, C: ConsensusStorage<N>> LedgerService<N> for CoreLedgerService<
/// Returns the committee for the given round.
/// If the given round is in the future, then the current committee is returned.
fn get_committee_for_round(&self, round: u64) -> Result<Committee<N>> {
// Check if the committee is already in the cache.
if let Some(committee) = self.committee_cache.lock().get(&round) {
return Ok(committee.clone());
}

match self.ledger.get_committee_for_round(round)? {
// Return the committee if it exists.
Some(committee) => Ok(committee),
Some(committee) => {
// Insert the committee into the cache.
self.committee_cache.lock().push(round, committee.clone());
// Return the committee.
Ok(committee)
}
// Return the current committee if the round is in the future.
None => {
// Retrieve the current committee.
Expand Down Expand Up @@ -174,8 +191,8 @@ impl<N: Network, C: ConsensusStorage<N>> LedgerService<N> for CoreLedgerService<
}
}

/// Ensures the given transmission ID matches the given transmission.
fn ensure_transmission_id_matches(
/// Ensures that the given transmission is not a fee and matches the given transmission ID.
fn ensure_transmission_is_well_formed(
&self,
transmission_id: TransmissionID<N>,
transmission: &mut Transmission<N>,
Expand All @@ -185,6 +202,7 @@ impl<N: Network, C: ConsensusStorage<N>> LedgerService<N> for CoreLedgerService<
(TransmissionID::Transaction(expected_transaction_id), Transmission::Transaction(transaction_data)) => {
match transaction_data.clone().deserialize_blocking() {
Ok(transaction) => {
// Ensure the transaction ID matches the expected transaction ID.
if transaction.id() != expected_transaction_id {
bail!(
"Received mismatching transaction ID - expected {}, found {}",
Expand All @@ -193,6 +211,11 @@ impl<N: Network, C: ConsensusStorage<N>> LedgerService<N> for CoreLedgerService<
);
}

// Ensure the transaction is not a fee transaction.
if transaction.is_fee() {
bail!("Received a fee transaction in a transmission");
}

// Update the transmission with the deserialized transaction.
*transaction_data = Data::Object(transaction);
}
Expand Down
4 changes: 2 additions & 2 deletions node/bft/ledger-service/src/mock.rs
Expand Up @@ -143,8 +143,8 @@ impl<N: Network> LedgerService<N> for MockLedgerService<N> {
Ok(false)
}

/// Ensures the given transmission ID matches the given transmission.
fn ensure_transmission_id_matches(
/// Ensures that the given transmission is not a fee and matches the given transmission ID.
fn ensure_transmission_is_well_formed(
&self,
transmission_id: TransmissionID<N>,
_transmission: &mut Transmission<N>,
Expand Down
4 changes: 2 additions & 2 deletions node/bft/ledger-service/src/prover.rs
Expand Up @@ -124,8 +124,8 @@ impl<N: Network> LedgerService<N> for ProverLedgerService<N> {
bail!("Transmission '{transmission_id}' does not exist in prover")
}

/// Ensures the given transmission ID matches the given transmission.
fn ensure_transmission_id_matches(
/// Ensures that the given transmission is not a fee and matches the given transmission ID.
fn ensure_transmission_is_well_formed(
&self,
_transmission_id: TransmissionID<N>,
_transmission: &mut Transmission<N>,
Expand Down
4 changes: 2 additions & 2 deletions node/bft/ledger-service/src/traits.rs
Expand Up @@ -78,8 +78,8 @@ pub trait LedgerService<N: Network>: Debug + Send + Sync {
/// Returns `true` if the ledger contains the given transmission ID.
fn contains_transmission(&self, transmission_id: &TransmissionID<N>) -> Result<bool>;

/// Ensures the given transmission ID matches the given transmission.
fn ensure_transmission_id_matches(
/// Ensures that the given transmission is not a fee and matches the given transmission ID.
fn ensure_transmission_is_well_formed(
&self,
transmission_id: TransmissionID<N>,
transmission: &mut Transmission<N>,
Expand Down
2 changes: 1 addition & 1 deletion node/bft/ledger-service/src/translucent.rs
Expand Up @@ -135,7 +135,7 @@ impl<N: Network, C: ConsensusStorage<N>> LedgerService<N> for TranslucentLedgerS
}

/// Always succeeds.
fn ensure_transmission_id_matches(
fn ensure_transmission_is_well_formed(
&self,
_transmission_id: TransmissionID<N>,
_transmission: &mut Transmission<N>,
Expand Down

0 comments on commit 7d8b66b

Please sign in to comment.