Skip to content

Commit

Permalink
Rename constructors + new
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Apr 12, 2024
1 parent b65bd08 commit f20b9f8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion benches/cascading_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ fn bench_cascading_restore_dense_mmap_tree(criterion: &mut Criterion) {
let storage =
unsafe { MmapVec::open_create(format!("./testfile{}", id)).unwrap() };
let _tree: CascadingMerkleTree<PoseidonHash, _> =
CascadingMerkleTree::new(storage, value.depth, &value.empty_value).unwrap();
CascadingMerkleTree::restore(storage, value.depth, &value.empty_value)
.unwrap();
let _root = _tree.root();
});
},
Expand Down
2 changes: 1 addition & 1 deletion examples/abort_test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() -> Result<()> {
let mmap_vec: MmapVec<<TestHasher as Hasher>::Hash> = unsafe { MmapVec::new(tempfile)? };

println!("restoring");
let mut tree = CascadingMerkleTree::<TestHasher, _>::new(mmap_vec, 30, &1)?;
let mut tree = CascadingMerkleTree::<TestHasher, _>::restore(mmap_vec, 30, &1)?;
tree.push(2).unwrap();

println!("tree length: {}", tree.num_leaves());
Expand Down
14 changes: 11 additions & 3 deletions src/cascading_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ where
S: StorageOps<H>,
{
/// Use to open a previously initialized tree
pub fn new(
pub fn restore(
storage: S,
depth: usize,
empty_value: &H::Hash,
) -> Result<CascadingMerkleTree<H, S>> {
// # Safety
// Safe because we're calling `.validate` on the tree later
let tree = unsafe { Self::new_unchecked(storage, depth, empty_value)? };
let tree = unsafe { Self::restore_unchecked(storage, depth, empty_value)? };

tree.validate()?;

Expand All @@ -71,7 +71,7 @@ where
/// This method is unsafe as it does not validate the contents of storage.
/// Use this only if you're sure that the contents of storage are valid -
/// i.e. have not been modified since last use.
pub unsafe fn new_unchecked(
pub unsafe fn restore_unchecked(
storage: S,
depth: usize,
empty_value: &H::Hash,
Expand All @@ -95,6 +95,14 @@ where
Ok(tree)
}

/// Create and initialize a tree in the provided storage
///
/// initializes an empty tree
#[must_use]
pub fn new(storage: S, depth: usize, empty_value: &H::Hash) -> CascadingMerkleTree<H, S> {
Self::new_with_leaves(storage, depth, empty_value, &[])
}

/// Create and initialize a tree in the provided storage
#[must_use]
pub fn new_with_leaves(
Expand Down

0 comments on commit f20b9f8

Please sign in to comment.