Skip to content

Commit

Permalink
fix: remove first edges (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware authored Jun 7, 2024
1 parent 6b80ba9 commit a54ec2d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use crate::patricia_merkle_tree::types::{NodeIndex, SubTreeHeight};
use ethnum::U256;
use strum_macros::{EnumDiscriminants, EnumIter};

#[cfg(test)]
#[path = "inner_node_tests.rs"]
pub mod inner_node_test;

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(any(test, feature = "testing"), derive(EnumDiscriminants))]
#[cfg_attr(any(test, feature = "testing"), strum_discriminants(derive(EnumIter)))]
Expand Down Expand Up @@ -159,12 +163,15 @@ impl PathToBottom {

/// Returns the path after removing the first steps (the edges from the path's origin node).
pub(crate) fn remove_first_edges(&self, n_edges: EdgePathLength) -> PathToBottomResult {
if self.length <= n_edges {
if self.length < n_edges {
return Err(PathToBottomError::RemoveEdgesError {
length: self.length,
n_edges,
});
}
Self::new(EdgePath(self.path.0 >> n_edges.0), self.length - n_edges)
Self::new(
EdgePath(self.path.0 & ((U256::ONE << (self.length.0 - n_edges.0)) - 1)),
self.length - n_edges,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use ethnum::U256;
use rstest::rstest;

use crate::patricia_merkle_tree::node_data::inner_node::{EdgePathLength, PathToBottom};

#[rstest]
#[case(PathToBottom::from("1011"), 1, PathToBottom::from("011"))]
#[case(PathToBottom::from("1011"), 2, PathToBottom::from("11"))]
#[case(PathToBottom::from("1011"), 3, PathToBottom::from("1"))]
#[case(PathToBottom::from("1011"), 4, PathToBottom::new(U256::ZERO.into(), EdgePathLength::new(0).unwrap()).unwrap())]
#[should_panic]
#[case(PathToBottom::from("1011"), 5, PathToBottom::from("0"))]
fn test_remove_first_edges(
#[case] path_to_bottom: PathToBottom,
#[case] n_edges: u8,
#[case] expected: PathToBottom,
) {
assert_eq!(
path_to_bottom
.remove_first_edges(EdgePathLength::new(n_edges).unwrap())
.unwrap(),
expected
);
}

0 comments on commit a54ec2d

Please sign in to comment.