Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
0xForerunner committed Apr 16, 2024
1 parent 6eb1518 commit 2c83fdd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 64 deletions.
87 changes: 26 additions & 61 deletions src/cascading_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,7 @@ where
self.storage.validate(&self.empty_value)
}

pub fn extend_from_slice(&mut self, leaves: &[H::Hash])
where
S: std::fmt::Debug,
H: std::fmt::Debug,
{
pub fn extend_from_slice(&mut self, leaves: &[H::Hash]) {
if leaves.is_empty() {
return;
}
Expand All @@ -348,67 +344,41 @@ where
let total_leaves = current_leaves + num_leaves;
let new_last_leaf_index = storage_ops::index_from_leaf(total_leaves - 1);

println!(
"\n\nStorage len: {}, new last leaf index: {}, num_leaves: {}",
self.storage.len(),
new_last_leaf_index,
num_leaves
);

// If the index is out of bounds, we need to resize the storage
// we must always have 2^n leaves for any n
if new_last_leaf_index >= self.storage.len() {
if new_last_leaf_index >= storage_len {
let next_power_of_two = (new_last_leaf_index + 1).next_power_of_two();
let diff = next_power_of_two - storage_len;

self.storage
.extend(std::iter::repeat(self.empty_value).take(diff));
}

println!("storage len: {:?}", self.storage.len());

println!("Current leaves: {}", current_leaves);
let initial_subtree_power = ((current_leaves + 1).next_power_of_two()).ilog2();
println!("Initial subtree power: {}", initial_subtree_power);

let end_subtree_power = ((total_leaves).next_power_of_two()).ilog2();
println!("End subtree power: {}", end_subtree_power);

debug_storage::<H, S>(&self.storage);

let mut remaining_leaves = leaves;

// We iterate over subsequently larger subtrees
// let mut last_sub_root = self.storage[initial_subtree_root_index];
for subtree_power in initial_subtree_power..=end_subtree_power {
println!("\nSubtree power: {}", subtree_power);
let left_index = if subtree_power == 0 {
let (leaf_slice, remaining) = remaining_leaves.split_at(1);
remaining_leaves = remaining;
self.storage[1] = leaf_slice[0];
// last_sub_root = self.storage[1];
continue;
} else {
1 << subtree_power
};
// let left_index = 1 << height;
let storage_slice = &mut self.storage[left_index..(left_index << 1)];
println!("storage_slice: {:?}", storage_slice);
let (_depth, width) = storage_ops::subtree_depth_width(storage_slice);
let leaf_start = if subtree_power == initial_subtree_power {
let start_subtree_leaf_index = current_leaves
- ((current_leaves + 1).next_power_of_two() >> 1).min(current_leaves);
println!("Start subtree leaf index: {}", start_subtree_leaf_index);
start_subtree_leaf_index
current_leaves - ((current_leaves + 1).next_power_of_two() >> 1).min(current_leaves)
} else {
0
};
println!("Leaf start: {}", leaf_start);

let leaves_to_take = (width - leaf_start).min(remaining_leaves.len());
println!("Leaves to take: {}", leaves_to_take);
let (leaf_slice, remaining) = remaining_leaves.split_at(leaves_to_take);
println!("leaf_slice: {:?}", leaf_slice);
remaining_leaves = remaining;
let root = storage_ops::extend_subtree_with_leaves::<H>(
storage_slice,
Expand All @@ -417,40 +387,14 @@ where
leaf_slice,
);
let last_sub_root = self.storage[1 << (subtree_power - 1)];
println!("Root: {:?}", root);
println!("last_sub_root: {:?}", last_sub_root);
let hash = H::hash_node(&last_sub_root, &root);
self.storage[left_index] = hash;
// last_sub_root = hash;
println!("Result:");
debug_storage::<H, S>(&self.storage);

self.storage[left_index] = H::hash_node(&last_sub_root, &root);
}

self.storage.set_num_leaves(total_leaves);
}
}

pub fn debug_storage<H, S>(storage: &S)
where
H: Hasher + std::fmt::Debug,
S: std::ops::Deref<Target = [<H as Hasher>::Hash]> + std::fmt::Debug,
{
let storage_depth = storage.len().ilog2();
let storage_len = storage.len();
let root_index = storage_len >> 1;
let mut previous = vec![root_index];
println!("{:?}", vec![storage[root_index]]);
for _ in 1..storage_depth {
let next = previous
.iter()
.flat_map(|&i| storage_ops::children(i))
.collect::<Vec<_>>();
previous = next.iter().flat_map(|&(l, r)| [l, r]).collect();
let row = previous.iter().map(|&i| storage[i]).collect::<Vec<_>>();
println!("{row:?}");
}
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -483,6 +427,27 @@ mod tests {
debug_storage::<H, S>(&tree.storage);
}

pub fn debug_storage<H, S>(storage: &S)
where
H: Hasher + std::fmt::Debug,
S: std::ops::Deref<Target = [<H as Hasher>::Hash]> + std::fmt::Debug,
{
let storage_depth = storage.len().ilog2();
let storage_len = storage.len();
let root_index = storage_len >> 1;
let mut previous = vec![root_index];
println!("{:?}", vec![storage[root_index]]);
for _ in 1..storage_depth {
let next = previous
.iter()
.flat_map(|&i| storage_ops::children(i))
.collect::<Vec<_>>();
previous = next.iter().flat_map(|&(l, r)| [l, r]).collect();
let row = previous.iter().map(|&i| storage[i]).collect::<Vec<_>>();
println!("{row:?}");
}
}

#[test]
fn test_index_from_leaf() {
let mut leaf_indeces = Vec::new();
Expand Down
6 changes: 3 additions & 3 deletions src/cascading_merkle_tree/storage_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ pub fn index_height_offset(height: usize, offset: usize) -> usize {
offset_node + subtree_size
}

// #[cfg(test)]
#[cfg(test)]
pub fn children(i: usize) -> Option<(usize, usize)> {
let next_pow = i.next_power_of_two();
if i == next_pow {
Expand Down Expand Up @@ -335,7 +335,7 @@ pub fn extend_subtree_with_leaves<H: Hasher>(
storage[1]
}

/// Propogate hash up a subtree with leaves within a given range.
/// Propogate hashes up a subtree with leaves within a given range.
///
/// O(n) time complexity
///
Expand Down Expand Up @@ -382,7 +382,7 @@ pub fn propogate_partial_subtree<H: Hasher>(

/// Propogates empty hashes up the tree within a given range.
///
/// O(n) time complexity
/// O(log(n)) time complexity
///
/// Subtrees are 1 indexed and directly attached to the left most branch
/// of the main tree.
Expand Down

0 comments on commit 2c83fdd

Please sign in to comment.