Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ version = "0.14"
version = "1.4"

[workspace.dependencies.locktick]
version = "0.3"
version = "0.4"

[workspace.dependencies.lru]
version = "0.16"
Expand Down
6 changes: 3 additions & 3 deletions ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ optional = true
[dependencies.snarkvm-synthesizer]
workspace = true

[dependencies.snarkvm-utilities]
workspace = true

[dependencies.aleo-std]
workspace = true
features = [ "storage" ]
Expand Down Expand Up @@ -191,9 +194,6 @@ features = [ "preserve_order" ]
[dev-dependencies.snarkvm-circuit]
workspace = true

[dev-dependencies.snarkvm-utilities]
workspace = true

[dev-dependencies.snarkvm-synthesizer]
workspace = true
features = [ "test" ]
75 changes: 30 additions & 45 deletions ledger/src/check_next_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use super::*;

use crate::narwhal::BatchHeader;

use anyhow::{Context, bail};

impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
/// Checks the given block is valid next block.
pub fn check_next_block<R: CryptoRng + Rng>(&self, block: &Block<N>, rng: &mut R) -> Result<()> {
Expand All @@ -40,29 +42,6 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
}
}

// TODO (howardwu): Remove this after moving the total supply into credits.aleo.
{
// // Retrieve the latest total supply.
// let latest_total_supply = self.latest_total_supply_in_microcredits();
// // Retrieve the block reward from the first block ratification.
// let block_reward = match block.ratifications()[0] {
// Ratify::BlockReward(block_reward) => block_reward,
// _ => bail!("Block {height} is invalid - the first ratification must be a block reward"),
// };
// // Retrieve the puzzle reward from the second block ratification.
// let puzzle_reward = match block.ratifications()[1] {
// Ratify::PuzzleReward(puzzle_reward) => puzzle_reward,
// _ => bail!("Block {height} is invalid - the second ratification must be a puzzle reward"),
// };
// // Compute the next total supply in microcredits.
// let next_total_supply_in_microcredits =
// update_total_supply(latest_total_supply, block_reward, puzzle_reward, block.transactions())?;
// // Ensure the total supply in microcredits is correct.
// if next_total_supply_in_microcredits != block.total_supply_in_microcredits() {
// bail!("Invalid total supply in microcredits")
// }
}

// Construct the finalize state.
let state = FinalizeGlobalState::new::<N>(
block.round(),
Expand All @@ -74,14 +53,17 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {

// Ensure speculation over the unconfirmed transactions is correct and ensure each transaction is well-formed and unique.
let time_since_last_block = block.timestamp().saturating_sub(self.latest_timestamp());
let ratified_finalize_operations = self.vm.check_speculate(
state,
time_since_last_block,
block.ratifications(),
block.solutions(),
block.transactions(),
rng,
)?;
let ratified_finalize_operations = self
.vm
.check_speculate(
state,
time_since_last_block,
block.ratifications(),
block.solutions(),
block.transactions(),
rng,
)
.with_context(|| "Failed to speculate over unconfirmed transactions")?;

// Retrieve the committee lookback.
let committee_lookback = self
Expand All @@ -98,16 +80,18 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
};

// Ensure the block is correct.
let (expected_existing_solution_ids, expected_existing_transaction_ids) = block.verify(
&latest_block,
self.latest_state_root(),
&previous_committee_lookback,
&committee_lookback,
self.puzzle(),
self.latest_epoch_hash()?,
OffsetDateTime::now_utc().unix_timestamp(),
ratified_finalize_operations,
)?;
let (expected_existing_solution_ids, expected_existing_transaction_ids) = block
.verify(
&latest_block,
self.latest_state_root(),
&previous_committee_lookback,
&committee_lookback,
self.puzzle(),
self.latest_epoch_hash()?,
OffsetDateTime::now_utc().unix_timestamp(),
ratified_finalize_operations,
)
.with_context(|| "Failed to verify block")?;

// Ensure that the provers are within their stake bounds.
if let Some(solutions) = block.solutions().deref() {
Expand All @@ -130,7 +114,7 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
// Determine if the block subdag is correctly constructed and is not a combination of multiple subdags.
self.check_block_subdag_atomicity(block)?;

// Ensure that all leafs of the subdag point to valid batches in other subdags/blocks.
// Ensure that all leaves of the subdag point to valid batches in other subdags/blocks.
self.check_block_subdag_leaves(block)?;

// Ensure that each existing solution ID from the block exists in the ledger.
Expand Down Expand Up @@ -204,8 +188,9 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
cfg_iter!(subdag).try_for_each(|(round, certificates)| {
// Retrieve the committee lookback for the round.
let committee_lookback = self
.get_committee_lookback_for_round(*round)?
.ok_or_else(|| anyhow!("No committee lookback found for round {round}"))?;
.get_committee_lookback_for_round(*round)
.with_context(|| format!("Failed to get committee lookback for round {round}"))?
.ok_or_else(|| anyhow!("No committee lookback for round {round}"))?;

// Check that each certificate for this round has met quorum requirements.
// Note that we do not need to check the quorum requirement for the previous certificates
Expand Down Expand Up @@ -273,7 +258,7 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
// Compute the leader for the commit round.
let computed_leader = previous_committee_lookback
.get_leader(round)
.map_err(|e| anyhow!("Failed to compute leader for round {round}: {e}"))?;
.with_context(|| format!("Failed to compute leader for round {round}"))?;

// Retrieve the previous leader certificates.
let previous_certificate = match subdag.get(&round).and_then(|certificates| {
Expand Down
10 changes: 6 additions & 4 deletions ledger/src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use super::*;

use snarkvm_utilities::LoggableError;

impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
/// Returns the block height that contains the given `state root`.
pub fn find_block_height_from_state_root(&self, state_root: N::StateRoot) -> Result<Option<u32>> {
Expand Down Expand Up @@ -128,8 +130,8 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
match commitment {
Ok(Some(commitment)) => Some((commitment, record)),
Ok(None) => None,
Err(e) => {
warn!("Failed to process 'find_record_ciphertexts({:?})': {e}", filter);
Err(err) => {
err.log_warning(format!("Failed to process 'find_record_ciphertexts({filter:?})'"));
None
}
}
Expand All @@ -146,8 +148,8 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
self.find_record_ciphertexts(view_key, filter).map(|iter| {
iter.flat_map(|(commitment, record)| match record.decrypt(view_key) {
Ok(record) => Some((commitment, record)),
Err(e) => {
warn!("Failed to decrypt the record: {e}");
Err(err) => {
err.log_warning("Failed to decrypt record");
None
}
})
Expand Down
9 changes: 4 additions & 5 deletions ledger/store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ edition = "2024"
[features]
default = [ "indexmap/rayon" ]
locktick = [ "dep:locktick", "snarkvm-ledger-puzzle/locktick" ]
rocks = [ "rocksdb", "smallvec", "tracing" ]
rocks = [ "rocksdb", "smallvec" ]
serial = [
"snarkvm-console/serial",
"snarkvm-ledger-block/serial",
Expand Down Expand Up @@ -109,10 +109,6 @@ workspace = true
features = [ "write" ]
optional = true

[dependencies.tracing]
workspace = true
optional = true

[dev-dependencies.aleo-std]
workspace = true

Expand All @@ -131,3 +127,6 @@ workspace = true

[dev-dependencies.tracing-test]
version = "0.2.5"

[dev-dependencies.tracing]
workspace = true
5 changes: 4 additions & 1 deletion ledger/store/src/helpers/memory/internal/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ impl<
// Set the atomic batch flag to `true`.
self.batch_in_progress.store(true, Ordering::SeqCst);
// Ensure that the atomic batch is empty.
assert!(self.atomic_batch.lock().is_empty());
assert!(
self.atomic_batch.lock().is_empty(),
"Cannot start an atomic batch operation while another one is already in progress"
);
}

///
Expand Down
13 changes: 8 additions & 5 deletions ledger/store/src/helpers/memory/internal/nested_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

use crate::helpers::{NestedMap, NestedMapRead};
use console::network::prelude::*;

use snarkvm_utilities::bytes::unchecked_deserialize;

use core::hash::Hash;
use anyhow::Context;
#[cfg(feature = "locktick")]
use locktick::parking_lot::{Mutex, RwLock};
#[cfg(not(feature = "locktick"))]
use parking_lot::{Mutex, RwLock};
use std::{
borrow::Cow,
collections::{BTreeMap, BTreeSet, btree_map},
hash::Hash,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
Expand Down Expand Up @@ -152,7 +152,10 @@ impl<
// Set the atomic batch flag to `true`.
self.batch_in_progress.store(true, Ordering::SeqCst);
// Ensure that the atomic batch is empty.
assert!(self.atomic_batch.lock().is_empty());
assert!(
self.atomic_batch.lock().is_empty(),
"Cannot start an atomic operation while another one is already in progress"
);
}

///
Expand Down Expand Up @@ -284,9 +287,9 @@ impl<
///
fn contains_key_confirmed(&self, map: &M, key: &K) -> Result<bool> {
// Serialize 'm'.
let m = bincode::serialize(map)?;
let m = bincode::serialize(map).with_context(|| "Failed to serialize map")?;
// Concatenate 'm' and 'k' with a 0-byte separator.
let mk = to_map_key(&m, &bincode::serialize(key)?);
let mk = to_map_key(&m, &bincode::serialize(key).with_context(|| "Failed to serialize map key")?);
// Return whether the concatenated key exists in the map.
Ok(self.map_inner.read().contains_key(&mk))
}
Expand Down
Loading