Skip to content

Commit 1f45c10

Browse files
committed
logs(ledger/store): add context for errors in ledger
1 parent 79ff81c commit 1f45c10

File tree

4 files changed

+45
-53
lines changed

4 files changed

+45
-53
lines changed

ledger/src/check_next_block.rs

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use super::*;
1717

1818
use crate::narwhal::BatchHeader;
1919

20+
use anyhow::{Context, bail};
21+
2022
impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
2123
/// Checks the given block is valid next block.
2224
pub fn check_next_block<R: CryptoRng + Rng>(&self, block: &Block<N>, rng: &mut R) -> Result<()> {
@@ -40,29 +42,6 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
4042
}
4143
}
4244

43-
// TODO (howardwu): Remove this after moving the total supply into credits.aleo.
44-
{
45-
// // Retrieve the latest total supply.
46-
// let latest_total_supply = self.latest_total_supply_in_microcredits();
47-
// // Retrieve the block reward from the first block ratification.
48-
// let block_reward = match block.ratifications()[0] {
49-
// Ratify::BlockReward(block_reward) => block_reward,
50-
// _ => bail!("Block {height} is invalid - the first ratification must be a block reward"),
51-
// };
52-
// // Retrieve the puzzle reward from the second block ratification.
53-
// let puzzle_reward = match block.ratifications()[1] {
54-
// Ratify::PuzzleReward(puzzle_reward) => puzzle_reward,
55-
// _ => bail!("Block {height} is invalid - the second ratification must be a puzzle reward"),
56-
// };
57-
// // Compute the next total supply in microcredits.
58-
// let next_total_supply_in_microcredits =
59-
// update_total_supply(latest_total_supply, block_reward, puzzle_reward, block.transactions())?;
60-
// // Ensure the total supply in microcredits is correct.
61-
// if next_total_supply_in_microcredits != block.total_supply_in_microcredits() {
62-
// bail!("Invalid total supply in microcredits")
63-
// }
64-
}
65-
6645
// Construct the finalize state.
6746
let state = FinalizeGlobalState::new::<N>(
6847
block.round(),
@@ -74,14 +53,17 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
7453

7554
// Ensure speculation over the unconfirmed transactions is correct and ensure each transaction is well-formed and unique.
7655
let time_since_last_block = block.timestamp().saturating_sub(self.latest_timestamp());
77-
let ratified_finalize_operations = self.vm.check_speculate(
78-
state,
79-
time_since_last_block,
80-
block.ratifications(),
81-
block.solutions(),
82-
block.transactions(),
83-
rng,
84-
)?;
56+
let ratified_finalize_operations = self
57+
.vm
58+
.check_speculate(
59+
state,
60+
time_since_last_block,
61+
block.ratifications(),
62+
block.solutions(),
63+
block.transactions(),
64+
rng,
65+
)
66+
.with_context(|| "Failed to speculate over unconfirmed transactions")?;
8567

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

10082
// Ensure the block is correct.
101-
let (expected_existing_solution_ids, expected_existing_transaction_ids) = block.verify(
102-
&latest_block,
103-
self.latest_state_root(),
104-
&previous_committee_lookback,
105-
&committee_lookback,
106-
self.puzzle(),
107-
self.latest_epoch_hash()?,
108-
OffsetDateTime::now_utc().unix_timestamp(),
109-
ratified_finalize_operations,
110-
)?;
83+
let (expected_existing_solution_ids, expected_existing_transaction_ids) = block
84+
.verify(
85+
&latest_block,
86+
self.latest_state_root(),
87+
&previous_committee_lookback,
88+
&committee_lookback,
89+
self.puzzle(),
90+
self.latest_epoch_hash()?,
91+
OffsetDateTime::now_utc().unix_timestamp(),
92+
ratified_finalize_operations,
93+
)
94+
.with_context(|| "Failed to verify block")?;
11195

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

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

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

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

278263
// Retrieve the previous leader certificates.
279264
let previous_certificate = match subdag.get(&round).and_then(|certificates| {

ledger/store/src/helpers/memory/internal/nested_map.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use crate::helpers::{NestedMap, NestedMapRead};
1919
use console::network::prelude::*;
2020

21+
use anyhow::Context;
2122
use core::hash::Hash;
2223
#[cfg(feature = "locktick")]
2324
use locktick::parking_lot::{Mutex, RwLock};
@@ -282,9 +283,9 @@ impl<
282283
///
283284
fn contains_key_confirmed(&self, map: &M, key: &K) -> Result<bool> {
284285
// Serialize 'm'.
285-
let m = bincode::serialize(map)?;
286+
let m = bincode::serialize(map).with_context(|| "Failed to serialize map")?;
286287
// Concatenate 'm' and 'k' with a 0-byte separator.
287-
let mk = to_map_key(&m, &bincode::serialize(key)?);
288+
let mk = to_map_key(&m, &bincode::serialize(key).with_context(|| "Failed to serialize map key")?);
288289
// Return whether the concatenated key exists in the map.
289290
Ok(self.map_inner.read().contains_key(&mk))
290291
}

ledger/store/src/helpers/rocksdb/internal/nested_map.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use super::*;
1919
use crate::helpers::{NestedMap, NestedMapRead};
2020
use console::prelude::{FromBytes, anyhow, cfg_into_iter};
2121

22+
use anyhow::Context;
2223
use core::{fmt, fmt::Debug, hash::Hash, mem};
2324
use std::{borrow::Cow, sync::atomic::Ordering};
2425
use tracing::error;
@@ -59,10 +60,11 @@ impl<M: Serialize + DeserializeOwned, K: Serialize + DeserializeOwned, V: Serial
5960
fn create_prefixed_map(&self, map: &M) -> Result<Vec<u8>> {
6061
let mut raw_map = self.context.clone();
6162

62-
let map_size: u32 = bincode::serialized_size(&map)?.try_into()?;
63+
let map_size: u32 =
64+
bincode::serialized_size(&map).with_context(|| "Failed to get size of serialize map")?.try_into()?;
6365
raw_map.extend_from_slice(&map_size.to_le_bytes());
6466

65-
bincode::serialize_into(&mut raw_map, map)?;
67+
bincode::serialize_into(&mut raw_map, map).with_context(|| "Failed to serialize map")?;
6668
Ok(raw_map)
6769
}
6870

@@ -463,14 +465,15 @@ impl<
463465
}
464466

465467
// Possibly deserialize the entries in parallel.
466-
Ok(cfg_into_iter!(entries)
468+
cfg_into_iter!(entries)
467469
.map(|(k, v)| {
468470
let k = bincode::deserialize::<K>(&k);
469471
let v = bincode::deserialize::<V>(&v);
470472

471473
k.and_then(|k| v.map(|v| (k, v)))
472474
})
473-
.collect::<Result<_, bincode::Error>>()?)
475+
.collect::<Result<_, bincode::Error>>()
476+
.with_context(|| "Failed to deserialize map entries")
474477
}
475478

476479
///
@@ -520,7 +523,10 @@ impl<
520523
///
521524
fn get_value_confirmed(&'a self, map: &M, key: &K) -> Result<Option<Cow<'a, V>>> {
522525
match self.get_map_key_raw(map, key) {
523-
Ok(Some(bytes)) => Ok(Some(Cow::Owned(bincode::deserialize(&bytes)?))),
526+
Ok(Some(bytes)) => {
527+
let v = bincode::deserialize(&bytes).with_context(|| "Failed to deserialize value")?;
528+
Ok(Some(Cow::Owned(v)))
529+
}
524530
Ok(None) => Ok(None),
525531
Err(e) => Err(e),
526532
}

utilities/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ macro_rules! ensure_equals {
8989
};
9090
}
9191

92-
/// A trait to provide a way to turn an `anyhow::Result` into a log message.
92+
/// A trait that allows turning an Error into a log message.
9393
pub trait LoggableError {
9494
/// Log the error with the given context and log level `ERROR`.
9595
fn log_error<S: Send + Sync + Display + 'static>(self, context: S);

0 commit comments

Comments
 (0)