Skip to content

Commit

Permalink
Requested PR changes for #455
Browse files Browse the repository at this point in the history
  • Loading branch information
BGluth committed Aug 9, 2024
1 parent c202542 commit 610c00b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 24 deletions.
8 changes: 4 additions & 4 deletions mpt_trie/src/debug_tools/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::{
/// [branch][`Node::Branch`]s have no [`Nibble`] directly associated with them.
fn get_key_piece_from_node<T: PartialTrie>(n: &Node<T>) -> Nibbles {
match n {
Node::Empty | Node::Hash(_) | Node::Branch { .. } => Nibbles::empty(),
Node::Empty | Node::Hash(_) | Node::Branch { .. } => Nibbles::default(),
Node::Extension { nibbles, child: _ } | Node::Leaf { nibbles, value: _ } => *nibbles,
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ fn find_latest_diff_point_between_tries(
a: &HashedPartialTrie,
b: &HashedPartialTrie,
) -> Option<DiffPoint> {
let state = DepthDiffPerCallState::new(a, b, Nibbles::empty(), 0);
let state = DepthDiffPerCallState::new(a, b, Nibbles::default(), 0);
let mut longest_state = DepthNodeDiffState::default();

find_latest_diff_point_between_tries_rec(&state, &mut longest_state);
Expand Down Expand Up @@ -327,7 +327,7 @@ fn find_latest_diff_point_between_tries_rec(
create_diff_detection_state_based_from_hashes(
a_hash,
b_hash,
&state.new_from_parent(state.a, state.b, &Nibbles::empty()),
&state.new_from_parent(state.a, state.b, &Nibbles::default()),
depth_state,
)
}
Expand Down Expand Up @@ -461,7 +461,7 @@ mod tests {
let expected = DiffPoint {
depth: 0,
path: TriePath(vec![]),
key: Nibbles::empty(),
key: Nibbles::default(),
a_info: expected_a,
b_info: expected_b,
};
Expand Down
2 changes: 1 addition & 1 deletion mpt_trie/src/debug_tools/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn get_key_piece_from_node_pulling_from_key_for_branches<T: PartialTrie>(
curr_key: &Nibbles,
) -> Nibbles {
match n {
Node::Empty | Node::Hash(_) => Nibbles::empty(),
Node::Empty | Node::Hash(_) => Nibbles::default(),
Node::Branch { .. } => curr_key.get_next_nibbles(1),
Node::Extension { nibbles, child: _ } | Node::Leaf { nibbles, value: _ } => *nibbles,
}
Expand Down
18 changes: 9 additions & 9 deletions mpt_trie/src/nibbles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ impl Debug for Nibbles {
}

/// While we could just derive `Default` and it would be correct, it's a bit
/// cleaner to instead call [`Nibbles::empty`] explicitly.
/// cleaner to instead call [`Nibbles::new`] explicitly.
impl Default for Nibbles {
fn default() -> Self {
Self::empty()
Self::new()
}
}

Expand Down Expand Up @@ -367,7 +367,7 @@ impl Nibbles {
///
/// Note that mean that the key size is `0` and does not mean that the key
/// contains the `0` [`Nibble`].
pub fn empty() -> Self {
pub fn new() -> Self {
Self {
count: 0,
packed: NibblesIntern::default(),
Expand Down Expand Up @@ -1145,7 +1145,7 @@ mod tests {

#[test]
fn push_nibble_front_works() {
test_and_assert_nib_push_func(Nibbles::empty(), 0x1, |n| n.push_nibble_front(0x1));
test_and_assert_nib_push_func(Nibbles::default(), 0x1, |n| n.push_nibble_front(0x1));
test_and_assert_nib_push_func(0x1, 0x21, |n| n.push_nibble_front(0x2));
test_and_assert_nib_push_func(
Nibbles::from_str(ZERO_NIBS_63).unwrap(),
Expand All @@ -1156,7 +1156,7 @@ mod tests {

#[test]
fn push_nibble_back_works() {
test_and_assert_nib_push_func(Nibbles::empty(), 0x1, |n| n.push_nibble_back(0x1));
test_and_assert_nib_push_func(Nibbles::default(), 0x1, |n| n.push_nibble_back(0x1));
test_and_assert_nib_push_func(0x1, 0x12, |n| n.push_nibble_back(0x2));
test_and_assert_nib_push_func(
Nibbles::from_str(ZERO_NIBS_63).unwrap(),
Expand All @@ -1167,7 +1167,7 @@ mod tests {

#[test]
fn push_nibbles_front_works() {
test_and_assert_nib_push_func(Nibbles::empty(), 0x1234, |n| {
test_and_assert_nib_push_func(Nibbles::default(), 0x1234, |n| {
n.push_nibbles_front(&0x1234.into())
});
test_and_assert_nib_push_func(0x1234, 0x5671234, |n| n.push_nibbles_front(&0x567.into()));
Expand All @@ -1180,7 +1180,7 @@ mod tests {

#[test]
fn push_nibbles_back_works() {
test_and_assert_nib_push_func(Nibbles::empty(), 0x1234, |n| {
test_and_assert_nib_push_func(Nibbles::default(), 0x1234, |n| {
n.push_nibbles_back(&0x1234.into())
});
test_and_assert_nib_push_func(0x1234, 0x1234567, |n| n.push_nibbles_back(&0x567.into()));
Expand All @@ -1206,13 +1206,13 @@ mod tests {
fn get_next_nibbles_works() -> Result<(), StrToNibblesError> {
let n: Nibbles = 0x1234.into();

assert_eq!(n.get_next_nibbles(0), Nibbles::empty());
assert_eq!(n.get_next_nibbles(0), Nibbles::default());
assert_eq!(n.get_next_nibbles(1), Nibbles::from(0x1));
assert_eq!(n.get_next_nibbles(2), Nibbles::from(0x12));
assert_eq!(n.get_next_nibbles(3), Nibbles::from(0x123));
assert_eq!(n.get_next_nibbles(4), Nibbles::from(0x1234));

assert_eq!(Nibbles::from(0x0).get_next_nibbles(0), Nibbles::empty());
assert_eq!(Nibbles::from(0x0).get_next_nibbles(0), Nibbles::default());

let n = Nibbles::from_str(
"0x3ab76c381c0f8ea617ea96780ffd1e165c754b28a41a95922f9f70682c581353",
Expand Down
4 changes: 2 additions & 2 deletions mpt_trie/src/special_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ mod test {
TrieSegment::Branch(2),
TrieSegment::Extension(Nibbles::from_str("0x00").unwrap()),
TrieSegment::Branch(0x1),
TrieSegment::Leaf(Nibbles::empty()),
TrieSegment::Leaf(Nibbles::default()),
],
vec![
TrieSegment::Branch(2),
TrieSegment::Extension(Nibbles::from_str("0x00").unwrap()),
TrieSegment::Branch(0x2),
TrieSegment::Leaf(Nibbles::empty()),
TrieSegment::Leaf(Nibbles::default()),
],
];

Expand Down
6 changes: 3 additions & 3 deletions mpt_trie/src/trie_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl<N: PartialTrie> Iterator for PartialTrieIter<N> {

next_iter_item = match stack_entry {
IterStackEntry::Root(root) => {
self.advance_iter_to_next_empty_leaf_or_hash_node(&root, Nibbles::empty())
self.advance_iter_to_next_empty_leaf_or_hash_node(&root, Nibbles::default())
}
IterStackEntry::Extension(num_nibbles) => {
// Drop nibbles that extension added since we are going back up the trie.
Expand Down Expand Up @@ -389,7 +389,7 @@ impl<T: PartialTrie> Node<T> {
// Final check at the root if we have an extension node. While this check also
// exists as we recursively traverse down the trie, it can not perform this
// check on the root node.
let wrapped_node = try_collapse_if_extension(updated_root, &Nibbles::empty())?;
let wrapped_node = try_collapse_if_extension(updated_root, &Nibbles::default())?;
let node_ref: &Node<T> = &wrapped_node;
*self = node_ref.clone();

Expand All @@ -399,7 +399,7 @@ impl<T: PartialTrie> Node<T> {

pub(crate) fn trie_items(&self) -> impl Iterator<Item = (Nibbles, ValOrHash)> {
PartialTrieIter {
curr_key_after_last_branch: Nibbles::empty(),
curr_key_after_last_branch: Nibbles::default(),
trie_stack: vec![IterStackEntry::Root(self.clone().into())],
}
}
Expand Down
13 changes: 9 additions & 4 deletions mpt_trie/src/trie_subsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,12 @@ mod tests {
return_on_empty_or_hash: bool,
) -> Vec<NodeFullNibbles> {
let mut nodes = Vec::new();
get_nodes_in_trie_intern_rec(trie, Nibbles::empty(), &mut nodes, return_on_empty_or_hash);
get_nodes_in_trie_intern_rec(
trie,
Nibbles::default(),
&mut nodes,
return_on_empty_or_hash,
);

nodes
}
Expand Down Expand Up @@ -561,7 +566,7 @@ mod tests {
assert_node_exists(
&all_non_empty_and_hash_nodes,
TrieNodeType::Branch,
Nibbles::empty(),
Nibbles::default(),
);
assert_node_exists(&all_non_empty_and_hash_nodes, TrieNodeType::Branch, 0x1);
assert_node_exists(&all_non_empty_and_hash_nodes, TrieNodeType::Leaf, 0x1234);
Expand All @@ -588,7 +593,7 @@ mod tests {
assert_node_exists(
&all_non_empty_and_hash_nodes_partial,
TrieNodeType::Branch,
Nibbles::empty(),
Nibbles::default(),
);
assert_node_exists(
&all_non_empty_and_hash_nodes_partial,
Expand All @@ -613,7 +618,7 @@ mod tests {
assert_node_exists(
&all_non_empty_and_hash_nodes_partial,
TrieNodeType::Branch,
Nibbles::empty(),
Nibbles::default(),
);
assert_node_exists(
&all_non_empty_and_hash_nodes_partial,
Expand Down
2 changes: 1 addition & 1 deletion mpt_trie/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub trait IntoTrieKey {

impl<P: Borrow<TrieSegment>, T: Iterator<Item = P>> IntoTrieKey for T {
fn into_key(self) -> Nibbles {
let mut key = Nibbles::empty();
let mut key = Nibbles::default();

for seg in self {
match seg.borrow() {
Expand Down

0 comments on commit 610c00b

Please sign in to comment.