Skip to content
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

fix(restore-unchecked): restore root #72

Merged
merged 3 commits into from
Apr 12, 2024
Merged
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
36 changes: 34 additions & 2 deletions src/cascading_merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::ops::{Deref, DerefMut};

Check warning on line 1 in src/cascading_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / Test

unused imports: `DerefMut`, `Deref`

Check warning on line 1 in src/cascading_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `DerefMut`, `Deref`

warning: unused imports: `DerefMut`, `Deref` --> src/cascading_merkle_tree.rs:1:16 | 1 | use std::ops::{Deref, DerefMut}; | ^^^^^ ^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

use color_eyre::eyre::{bail, ensure, Result};
use itertools::Itertools;

Check warning on line 4 in src/cascading_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `itertools::Itertools`

Check warning on line 4 in src/cascading_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `itertools::Itertools`

warning: unused import: `itertools::Itertools` --> src/cascading_merkle_tree.rs:4:5 | 4 | use itertools::Itertools; | ^^^^^^^^^^^^^^^^^^^^
use rayon::prelude::*;

Check warning on line 5 in src/cascading_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `rayon::prelude`

Check warning on line 5 in src/cascading_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `rayon::prelude`

warning: unused import: `rayon::prelude` --> src/cascading_merkle_tree.rs:5:5 | 5 | use rayon::prelude::*; | ^^^^^^^^^^^^^^

use crate::merkle_tree::{Branch, Hasher, Proof};

Expand Down Expand Up @@ -83,9 +83,9 @@

let sparse_column = Self::sparse_column(depth, empty_value);

let tree = CascadingMerkleTree {
let mut tree = CascadingMerkleTree {

Check warning on line 86 in src/cascading_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / Test

variable does not need to be mutable

Check warning on line 86 in src/cascading_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

variable does not need to be mutable

warning: variable does not need to be mutable --> src/cascading_merkle_tree.rs:86:13 | 86 | let mut tree = CascadingMerkleTree { | ----^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default
depth,
root: *empty_value,
root: storage.storage_root(),
empty_value: *empty_value,
sparse_column,
storage,
Expand Down Expand Up @@ -877,4 +877,36 @@
elapsed.as_millis()
);
}

#[test]
#[serial]
fn test_restore_from_cache() -> color_eyre::Result<()> {
let empty = 0;
let leaves = vec![1; 1 << 20];

// Create a new tmp file for mmap storage
let tempfile = tempfile::NamedTempFile::new()?;
let file_path = tempfile.path().to_owned();

// Initialize the expected tree
let mmap_vec: MmapVec<_> = unsafe { MmapVec::new(tempfile.reopen()?).unwrap() };
let expected_tree = CascadingMerkleTree::<TestHasher, MmapVec<_>>::new_with_leaves(
mmap_vec, 30, &empty, &leaves,
);

let expected_root = expected_tree.root();
let expected_leaves = expected_tree.leaves().collect::<Vec<usize>>();

drop(expected_tree);

// Restore the tree
let mmap_vec: MmapVec<_> = unsafe { MmapVec::restore(file_path).unwrap() };
let tree = CascadingMerkleTree::<TestHasher, MmapVec<_>>::restore(mmap_vec, 30, &empty)?;

// Assert that the root and the leaves are as expected
assert_eq!(tree.root(), expected_root);
assert_eq!(tree.leaves().collect::<Vec<usize>>(), expected_leaves);

Ok(())
}
}
Loading