Skip to content

Commit

Permalink
piecurst: dictionary for contracts merkle
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Oct 14, 2024
1 parent 56e97ae commit 2d7f4d7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
4 changes: 1 addition & 3 deletions piecrust/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ impl Commit {
Some(contract.page_indices.into_iter().map(move |page_index| {
let tree_opening = self
.contracts_merkle
.tree
.opening(pos)
.expect("There must be a leaf for the contract");

Expand Down Expand Up @@ -520,7 +519,6 @@ impl Commit {
}

self.contracts_merkle
.tree
.insert(position_from_contract(&contract_id), *element.tree.root());
}

Expand All @@ -530,7 +528,7 @@ impl Commit {
}

pub fn root(&self) -> Ref<Hash> {
self.contracts_merkle.tree.root()
self.contracts_merkle.root()
}
}

Expand Down
31 changes: 29 additions & 2 deletions piecrust/src/store/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,39 @@ pub struct NewContractIndex {
#[derive(Debug, Clone, Archive, Deserialize, Serialize)]
#[archive_attr(derive(CheckBytes))]
pub struct ContractsMerkle {
pub tree: Tree,
inner_tree: Tree,
dict: BTreeMap<u64, u64>,
}

impl Default for ContractsMerkle {
fn default() -> Self {
Self { tree: Tree::new() }
Self {
inner_tree: Tree::new(),
dict: BTreeMap::new(),
}
}
}

impl ContractsMerkle {
pub fn insert(&mut self, pos: u64, hash: Hash) {
let new_pos = match self.dict.get(&pos) {
None => {
let new_pos = (self.dict.len() + 1) as u64;
self.dict.insert(pos, new_pos);
new_pos
}
Some(p) => *p,
};
self.inner_tree.insert(new_pos, hash);
}

pub fn opening(&self, pos: u64) -> Option<TreeOpening> {
let new_pos = self.dict.get(&pos).expect("pos should exist in dict");
self.inner_tree.opening(*new_pos)
}

pub fn root(&self) -> Ref<Hash> {
self.inner_tree.root()
}
}

Expand Down

0 comments on commit 2d7f4d7

Please sign in to comment.