Skip to content

Commit

Permalink
piecrust: storing entire merkle
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Nov 17, 2024
1 parent 247a3eb commit 4479446
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
32 changes: 12 additions & 20 deletions piecrust/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,9 @@ fn commit_from_dir<P: AsRef<Path>>(
// let contracts_merkle_path = dir.join(MERKLE_FILE);
let leaf_dir = main_dir.join(LEAF_DIR);
tracing::trace!("before index_merkle_from_path");
let (index, contracts_merkle) = index_merkle_from_path(
main_dir,
leaf_dir,
&maybe_hash,
commit_store.clone(),
)?;

let index =
index_from_path(main_dir, leaf_dir, &maybe_hash, commit_store.clone())?;
tracing::trace!("after index_merkle_from_path");

let bytecode_dir = main_dir.join(BYTECODE_DIR);
Expand Down Expand Up @@ -468,11 +465,12 @@ fn commit_from_dir<P: AsRef<Path>>(
}
}

let base = if let Some(hash_hex) = commit_id {
let (base, contracts_merkle) = if let Some(hash_hex) = commit_id {
let base_info_path = main_dir.join(hash_hex).join(BASE_FILE);
base_from_path(base_info_path)?.maybe_base
let base_info = base_from_path(base_info_path)?;
(base_info.maybe_base, base_info.contracts_merkle)
} else {
None
(None, ContractsMerkle::default())
};

Ok(Commit {
Expand All @@ -484,16 +482,15 @@ fn commit_from_dir<P: AsRef<Path>>(
})
}

fn index_merkle_from_path(
fn index_from_path(
main_path: impl AsRef<Path>,
leaf_dir: impl AsRef<Path>,
maybe_commit_id: &Option<Hash>,
commit_store: Arc<Mutex<CommitStore>>,
) -> io::Result<(NewContractIndex, ContractsMerkle)> {
) -> io::Result<NewContractIndex> {
let leaf_dir = leaf_dir.as_ref();

let mut index: NewContractIndex = NewContractIndex::new();
let mut merkle: ContractsMerkle = ContractsMerkle::default();

for entry in fs::read_dir(leaf_dir)? {
let entry = entry?;
Expand Down Expand Up @@ -524,13 +521,6 @@ fn index_merkle_from_path(
),
)
})?;
if let Some(h) = element.hash() {
merkle.insert_with_int_pos(
position_from_contract(&contract_id),
element.int_pos().expect("int pos should be present"),
h,
);
}
if element_depth != u32::MAX {
index.insert_contract_index(&contract_id, element);
} else {
Expand All @@ -543,7 +533,7 @@ fn index_merkle_from_path(
}
}

Ok((index, merkle))
Ok(index)
}

fn base_from_path<P: AsRef<Path>>(path: P) -> io::Result<BaseInfo> {
Expand Down Expand Up @@ -1058,6 +1048,8 @@ fn write_commit_inner<P: AsRef<Path>, S: AsRef<str>>(
}
tracing::trace!("persisting index finished");

base_info.contracts_merkle = commit.contracts_merkle.clone(); //todo: clone

let base_main_path =
base_path_main(directories.main_dir, commit_id.as_ref())?;
let base_info_bytes =
Expand Down
1 change: 1 addition & 0 deletions piecrust/src/store/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub struct ContractIndex {
pub struct BaseInfo {
pub contract_hints: Vec<ContractId>,
pub maybe_base: Option<Hash>,
pub contracts_merkle: ContractsMerkle,
}

#[derive(Debug, Clone, Archive, Deserialize, Serialize)]
Expand Down

0 comments on commit 4479446

Please sign in to comment.