Skip to content

Commit

Permalink
piecrust: corrections, clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Oct 14, 2024
1 parent 217a71b commit e15464c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 100 deletions.
51 changes: 22 additions & 29 deletions piecrust/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommitId>) ->
// ContractIndexElement

let loop_root_dir = root_dir.to_path_buf();

// The thread is given a name to allow for easily identifying it while
Expand Down Expand Up @@ -377,10 +371,10 @@ fn commit_from_dir<P: AsRef<Path>>(
})
}

fn index_merkle_from_path<P1: AsRef<Path>, P2: AsRef<Path>, P3: AsRef<Path>>(
main_path: P1,
merkle_path: P2,
leaf_dir: P3,
fn index_merkle_from_path(
main_path: impl AsRef<Path>,
merkle_path: impl AsRef<Path>,
leaf_dir: impl AsRef<Path>,
maybe_commit_id: &Option<Hash>,
) -> io::Result<(NewContractIndex, ContractsMerkle)> {
let merkle_path = merkle_path.as_ref();
Expand All @@ -400,21 +394,23 @@ fn index_merkle_from_path<P1: AsRef<Path>, P2: AsRef<Path>, P3: AsRef<Path>>(
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)
}
}
}

Expand Down Expand Up @@ -467,11 +463,8 @@ impl Commit {
pub fn inclusion_proofs(
mut self,
contract_id: &ContractId,
maybe_commit_id: Option<Hash>,
) -> Option<impl Iterator<Item = (usize, PageOpening)>> {
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);

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -738,7 +731,7 @@ fn write_commit<P: AsRef<Path>>(
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 {
Expand Down
4 changes: 1 addition & 3 deletions piecrust/src/store/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)| {
Expand Down
74 changes: 7 additions & 67 deletions piecrust/src/store/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,70 +75,6 @@ impl PageTree {

pub type Tree = dusk_merkle::Tree<Hash, C_HEIGHT, C_ARITY>;

// struct ReverseInsert {
// pos: u64,
// value: Option<Hash> // if Some, insert, if None, remove
// }
//

// struct Insert {
// pos: u64,
// value: Hash,
// }

// fn perform_inserts(maybe_commit_id: Option<Hash>, reverse_inserts: &mut
// Vec<ReverseInsert>, &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<ReverseInsert>, &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<Insert>
//
// 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::<ReverseInsert>::new();
// perform_inserts(maybe_base, &mut reverse_inserts,
// &central_merkle_tree); let root =
// calc_the_actual_root(&central_merkle_tree); perform_reversals(&
// reverse_inserts, &central_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::<ReverseInsert>::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<Self> {
// }
// 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 {
Expand Down Expand Up @@ -175,7 +111,7 @@ impl ContractsMerkle {
}

pub fn opening(&self, pos: u64) -> Option<TreeOpening> {
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)
}

Expand Down Expand Up @@ -208,6 +144,12 @@ pub struct ContractIndexElement {
pub page_indices: BTreeSet<usize>,
}

impl Default for NewContractIndex {
fn default() -> Self {
Self::new()
}
}

impl NewContractIndex {
pub fn new() -> Self {
Self {
Expand All @@ -218,8 +160,6 @@ impl NewContractIndex {
pub fn remove_contract_index(
&mut self,
contract_id: &ContractId,
_maybe_commit_id: Option<Hash>,
// _path: impl AsRef<Path>,
) -> Option<ContractIndexElement> {
self.inner_contracts.remove(contract_id)
}
Expand Down
2 changes: 1 addition & 1 deletion piecrust/tests/callcenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn cc_passthrough() -> Result<(), Error> {
LIMIT,
)?;

let raw = (String::from("read_va"), Vec::<u8>::new());
let raw = (String::from("read_value"), Vec::<u8>::new());

let res: (String, Vec<u8>) = session
.call(center_id, "query_passthrough", &raw, LIMIT)?
Expand Down

0 comments on commit e15464c

Please sign in to comment.