Skip to content

Commit

Permalink
Made sub-trie errors better (#520)
Browse files Browse the repository at this point in the history
* Made sub-trie errors better

- Now shows the path in the trie where we encountered the `hash` node.

* Requested PR changes for #520
  • Loading branch information
BGluth authored Aug 22, 2024
1 parent b2006bf commit c7a1641
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
6 changes: 3 additions & 3 deletions mpt_trie/src/debug_tools/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ pub struct DebugQueryParamsBuilder {

impl DebugQueryParamsBuilder {
/// Defaults to `true`.
pub const fn print_key_pieces(mut self, enabled: bool) -> Self {
pub const fn include_key_pieces(mut self, enabled: bool) -> Self {
self.params.include_key_piece_per_node = enabled;
self
}

/// Defaults to `true`.
pub const fn print_node_type(mut self, enabled: bool) -> Self {
pub const fn include_node_type(mut self, enabled: bool) -> Self {
self.params.include_node_type = enabled;
self
}

/// Defaults to `false`.
pub const fn print_node_specific_values(mut self, enabled: bool) -> Self {
pub const fn include_node_specific_values(mut self, enabled: bool) -> Self {
self.params.include_node_specific_values = enabled;
self
}
Expand Down
34 changes: 21 additions & 13 deletions mpt_trie/src/trie_subsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use log::trace;
use thiserror::Error;

use crate::{
debug_tools::query::{get_path_from_query, DebugQueryOutput, DebugQueryParamsBuilder},
nibbles::Nibbles,
partial_trie::{Node, PartialTrie, WrappedNode},
trie_hashing::EncodedNode,
Expand All @@ -21,13 +22,10 @@ use crate::{
/// The output type of trie_subset operations.
pub type SubsetTrieResult<T> = Result<T, SubsetTrieError>;

/// Errors that may occur when creating a subset [`PartialTrie`].
/// We encountered a `hash` node when marking nodes during sub-trie creation.
#[derive(Clone, Debug, Error, Hash)]
pub enum SubsetTrieError {
#[error("Tried to mark nodes in a tracked trie for a key that does not exist! (Key: {0}, trie: {1})")]
/// The key does not exist in the trie.
UnexpectedKey(Nibbles, String),
}
#[error("Encountered a hash node when marking nodes to not hash when traversing a key to not hash!\nPath: {0}")]
pub struct SubsetTrieError(DebugQueryOutput);

#[derive(Debug)]
enum TrackedNodeIntern<N: PartialTrie> {
Expand Down Expand Up @@ -256,8 +254,17 @@ where
N: PartialTrie,
K: Into<Nibbles>,
{
for k in keys_involved {
mark_nodes_that_are_needed(tracked_trie, &mut k.into())?;
for mut k in keys_involved.map(|k| k.into()) {
mark_nodes_that_are_needed(tracked_trie, &mut k).map_err(|_| {
// We need to unwind back to this callsite in order to produce the actual error.
let query = DebugQueryParamsBuilder::default()
.include_node_specific_values(true)
.build(k);

let res = get_path_from_query(&tracked_trie.info.underlying_node, query);

SubsetTrieError(res)
})?;
}

Ok(create_partial_trie_subset_from_tracked_trie(tracked_trie))
Expand All @@ -270,10 +277,14 @@ where
/// - For the key `0x1`, the marked nodes would be [B(0x), B(0x1)].
/// - For the key `0x12`, the marked nodes still would be [B(0x), B(0x1)].
/// - For the key `0x123`, the marked nodes would be [B(0x), B(0x1), L(0x123)].
///
/// Also note that we can't construct the error until we back out of this
/// recursive function. We need to know the full key that hit the hash
/// node, and that's only available at the initial call site.
fn mark_nodes_that_are_needed<N: PartialTrie>(
trie: &mut TrackedNode<N>,
curr_nibbles: &mut Nibbles,
) -> SubsetTrieResult<()> {
) -> Result<(), ()> {
trace!(
"Sub-trie marking at {:x}, (type: {})",
curr_nibbles,
Expand All @@ -286,10 +297,7 @@ fn mark_nodes_that_are_needed<N: PartialTrie>(
}
TrackedNodeIntern::Hash => match curr_nibbles.is_empty() {
false => {
return Err(SubsetTrieError::UnexpectedKey(
*curr_nibbles,
format!("{:?}", trie),
));
return Err(());
}
true => {
trie.info.touched = true;
Expand Down

0 comments on commit c7a1641

Please sign in to comment.