From 8e352f0ede89132ac061e34bd7796278c115b4ac Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Sun, 13 Oct 2024 03:19:06 +0200 Subject: [PATCH] piecrust: corrections, clean-up --- piecrust/src/store.rs | 51 +++++++++++------------- piecrust/src/store/session.rs | 4 +- piecrust/src/store/tree.rs | 74 ++++------------------------------- piecrust/tests/callcenter.rs | 2 +- 4 files changed, 31 insertions(+), 100 deletions(-) diff --git a/piecrust/src/store.rs b/piecrust/src/store.rs index 3f45a7a9..8861d8c9 100644 --- a/piecrust/src/store.rs +++ b/piecrust/src/store.rs @@ -85,12 +85,6 @@ impl ContractStore { let (call, calls) = mpsc::channel(); let commits = read_all_commits(&engine, root_dir)?; - // here is a place where central objects should be read from disk - // central objects are: - // 1) merkle tree - // 2) contracts map (ContractId, Option) -> - // ContractIndexElement - let loop_root_dir = root_dir.to_path_buf(); // The thread is given a name to allow for easily identifying it while @@ -377,10 +371,10 @@ fn commit_from_dir>( }) } -fn index_merkle_from_path, P2: AsRef, P3: AsRef>( - main_path: P1, - merkle_path: P2, - leaf_dir: P3, +fn index_merkle_from_path( + main_path: impl AsRef, + merkle_path: impl AsRef, + leaf_dir: impl AsRef, maybe_commit_id: &Option, ) -> io::Result<(NewContractIndex, ContractsMerkle)> { let merkle_path = merkle_path.as_ref(); @@ -400,21 +394,23 @@ fn index_merkle_from_path, P2: AsRef, P3: AsRef>( main_path.as_ref(), ) .unwrap_or(contract_leaf_path.join(ELEMENT_FILE)); - let element_bytes = fs::read(element_path.clone())?; - let element: ContractIndexElement = - rkyv::from_bytes(&element_bytes).map_err(|err| { - tracing::trace!( - "deserializing element file failed {}", - err - ); - io::Error::new( - io::ErrorKind::InvalidData, - format!( + if element_path.is_file() { + let element_bytes = fs::read(element_path.clone())?; + let element: ContractIndexElement = + rkyv::from_bytes(&element_bytes).map_err(|err| { + tracing::trace!( + "deserializing element file failed {}", + err + ); + io::Error::new( + io::ErrorKind::InvalidData, + format!( "Invalid element file \"{element_path:?}\": {err}" ), - ) - })?; - index.insert_contract_index(&contract_id, element) + ) + })?; + index.insert_contract_index(&contract_id, element) + } } } @@ -467,11 +463,8 @@ impl Commit { pub fn inclusion_proofs( mut self, contract_id: &ContractId, - maybe_commit_id: Option, ) -> Option> { - let contract = self - .index - .remove_contract_index(contract_id, maybe_commit_id)?; + let contract = self.index.remove_contract_index(contract_id)?; let pos = position_from_contract(contract_id); @@ -523,7 +516,7 @@ impl Commit { } pub fn remove_and_insert(&mut self, contract: ContractId, memory: &Memory) { - self.index.remove_contract_index(&contract, self.maybe_hash); + self.index.remove_contract_index(&contract); self.insert(contract, memory); } @@ -738,7 +731,7 @@ fn write_commit>( let mut commit = Commit { index, contracts_merkle, - maybe_hash: base.as_ref().map_or(None, |base| base.maybe_hash), + maybe_hash: base.as_ref().and_then(|base| base.maybe_hash), }; for (contract_id, contract_data) in &commit_contracts { diff --git a/piecrust/src/store/session.rs b/piecrust/src/store/session.rs index 21520e5f..bab933da 100644 --- a/piecrust/src/store/session.rs +++ b/piecrust/src/store/session.rs @@ -108,9 +108,7 @@ impl ContractSession { } let contract_data = self.contracts.get(&contract)?; - let maybe_hash = commit.maybe_hash; - let inclusion_proofs = - commit.inclusion_proofs(&contract, maybe_hash)?; + let inclusion_proofs = commit.inclusion_proofs(&contract)?; let inclusion_proofs = inclusion_proofs.map(move |(page_index, opening)| { diff --git a/piecrust/src/store/tree.rs b/piecrust/src/store/tree.rs index 6eecdf47..e3791276 100644 --- a/piecrust/src/store/tree.rs +++ b/piecrust/src/store/tree.rs @@ -75,70 +75,6 @@ impl PageTree { pub type Tree = dusk_merkle::Tree; -// struct ReverseInsert { -// pos: u64, -// value: Option // if Some, insert, if None, remove -// } -// - -// struct Insert { -// pos: u64, -// value: Hash, -// } - -// fn perform_inserts(maybe_commit_id: Option, reverse_inserts: &mut -// Vec, &merkle_tree) { match maybe_commit_id { -// Some(commit_id) => { -// let maybe_base = base_of_commit(commit_id) -// perform_inserts(maybe_base, reverse_inserts, merkle_tree); -// let inserts = read_inserts_for_commit_id(&commit_id); -// for every insert { -// let reverse_insert = apply_insert(insert, &merkle_tree); -// reverse_inserts.push(reverse_insert); -// } -// }, -// None => { -// return; -// } -// } -// - -// fn perform_reversals(reverse_inserts: &Vec, &merkle_tree) { -// from end to the beginning of reverse_inserts, for every reverse_insert do -// { apply_reverse_insert(reverse_insert, merkle_tree) // meaning either -// insert or removal } -// } - -// struct CommitInserts -// -// data members: -// inserts: Vec -// -// methods: -// fn calc_root(&self, &mut central_merkle_tree, commit_id) -> root { -// let maybe_base = base_of_commit(commit_id); -// let mut reverse_inserts = Vec::::new(); -// perform_inserts(maybe_base, &mut reverse_inserts, -// ¢ral_merkle_tree); let root = -// calc_the_actual_root(¢ral_merkle_tree); perform_reversals(& -// reverse_inserts, ¢ral_merkle_tree); root -// } -// fn finalize(&self, &mut central_merkle_tree, commit_id) { -// let maybe_base = base_of_commit(commit_id); -// let mut reverse_inserts = Vec::::new(); -// perform_inserts(maybe_base, &mut reverse_inserts); -// save central_merkle_tree to disk -// remove yourself on Disk or make sure it is being done as part of a -// greater finalization of commit commit_id -// } -// fn read_from_file_repr() -> io::Result { -// } -// fn remove_file_repr(&self) -> io::Result<()>{ -// } - -// this should only contain a collection of merkle tree inserts (CommitInserts) -// this should really be a class which is able to calc root -// and finalize #[derive(Debug, Clone, Archive, Deserialize, Serialize)] #[archive_attr(derive(CheckBytes))] pub struct NewContractIndex { @@ -175,7 +111,7 @@ impl ContractsMerkle { } pub fn opening(&self, pos: u64) -> Option { - let new_pos = self.dict.get(&pos).expect("pos should exist in dict"); + let new_pos = self.dict.get(&pos)?; self.inner_tree.opening(*new_pos) } @@ -208,6 +144,12 @@ pub struct ContractIndexElement { pub page_indices: BTreeSet, } +impl Default for NewContractIndex { + fn default() -> Self { + Self::new() + } +} + impl NewContractIndex { pub fn new() -> Self { Self { @@ -218,8 +160,6 @@ impl NewContractIndex { pub fn remove_contract_index( &mut self, contract_id: &ContractId, - _maybe_commit_id: Option, - // _path: impl AsRef, ) -> Option { self.inner_contracts.remove(contract_id) } diff --git a/piecrust/tests/callcenter.rs b/piecrust/tests/callcenter.rs index 880d26de..5d8545b8 100644 --- a/piecrust/tests/callcenter.rs +++ b/piecrust/tests/callcenter.rs @@ -103,7 +103,7 @@ pub fn cc_passthrough() -> Result<(), Error> { LIMIT, )?; - let raw = (String::from("read_va"), Vec::::new()); + let raw = (String::from("read_value"), Vec::::new()); let res: (String, Vec) = session .call(center_id, "query_passthrough", &raw, LIMIT)?