Skip to content

fix(skeleton): add missing NodeIndex::new ctor calls #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
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
6 changes: 3 additions & 3 deletions crates/committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl From<TreeHeight> for u8 {
#[derive(
Clone, Copy, Debug, PartialEq, Eq, Hash, derive_more::BitAnd, derive_more::Sub, PartialOrd, Ord,
)]
pub struct NodeIndex(pub U256);
pub struct NodeIndex(U256);

// Wraps a U256. Maximal possible value is the largest index in a tree of height 251 (2 ^ 252 - 1).
impl NodeIndex {
Expand Down Expand Up @@ -108,9 +108,9 @@ impl NodeIndex {

let xor = adapted_self.0 ^ other.0;
// The length of the remainder after removing the common prefix of the two nodes.
let post_common_prefix_len = NodeIndex(xor).bit_length();
let post_common_prefix_len = NodeIndex::new(xor).bit_length();
let lca = adapted_self.0 >> post_common_prefix_len;
NodeIndex(lca)
NodeIndex::new(lca)
}

/// Returns the path from the node to its given descendant.
Expand Down
16 changes: 8 additions & 8 deletions crates/committer/src/patricia_merkle_tree/types_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ fn test_cast_to_node_index(
#[case(8, 10, 2)]
#[case(9, 12, 1)]
fn test_get_lca(#[case] node_index: u8, #[case] other: u8, #[case] expected: u8) {
let root_index = NodeIndex(node_index.into());
let other_index = NodeIndex(other.into());
let root_index = NodeIndex::new(node_index.into());
let other_index = NodeIndex::new(other.into());
let lca = root_index.get_lca(&other_index);
let expected = NodeIndex(expected.into());
let expected = NodeIndex::new(expected.into());
assert_eq!(lca, expected);
}

Expand All @@ -87,7 +87,7 @@ fn test_get_lca_big(mut random: ThreadRng) {
let mut random_extension = |index: NodeIndex| {
let extension_bits = index.leading_zeros();
let extension: u128 = random.gen_range(0..(1 << extension_bits));
(index << extension_bits) + NodeIndex(U256::from(extension))
(index << extension_bits) + NodeIndex::new(U256::from(extension))
};

let left_descendant = random_extension(left_child);
Expand All @@ -110,19 +110,19 @@ fn test_get_path_to_descendant(
#[case] expected_path: u8,
#[case] expected_length: u8,
) {
let root_index = NodeIndex(root_index.into());
let descendant = NodeIndex(descendant.into());
let root_index = NodeIndex::new(root_index.into());
let descendant = NodeIndex::new(descendant.into());
let path_to_bottom = root_index.get_path_to_descendant(descendant);
assert_eq!(path_to_bottom.path, U256::from(expected_path).into());
assert_eq!(path_to_bottom.length, EdgePathLength(expected_length));
}

#[rstest]
fn test_get_path_to_descendant_big() {
let root_index = NodeIndex(U256::from(rand::thread_rng().gen::<u128>()));
let root_index = NodeIndex::new(U256::from(rand::thread_rng().gen::<u128>()));
let max_bits = NodeIndex::BITS - 128;
let extension: u128 = rand::thread_rng().gen_range(0..1 << max_bits);
let extension_index = NodeIndex(U256::from(extension));
let extension_index = NodeIndex::new(U256::from(extension));

let descendant = (root_index << extension_index.bit_length()) + extension_index;
let path_to_bottom = root_index.get_path_to_descendant(descendant);
Expand Down
4 changes: 2 additions & 2 deletions crates/committer_cli/src/tests/utils/random_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ impl RandomValue for NodeIndex {
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
// The maximum value is the maximum between max and NodeIndex::MAX.
let max_value = match max {
Some(m) => min(m, NodeIndex::MAX.0),
None => NodeIndex::MAX.0,
Some(m) => min(m, U256::from(NodeIndex::MAX)),
None => U256::from(NodeIndex::MAX),
};

Self::new(get_random_u256(rng, U256::ONE, max_value + 1))
Expand Down
Loading