Skip to content

Commit

Permalink
piecrust: tree pos with hash, pos and internal pos
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Nov 19, 2024
1 parent e69b42f commit e3af6c0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 47 deletions.
47 changes: 20 additions & 27 deletions piecrust/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod tree;
use std::cell::Ref;
use std::collections::btree_map::Entry::*;
use std::collections::btree_map::Keys;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{Debug, Formatter};
use std::fs::create_dir_all;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -510,7 +510,7 @@ fn index_merkle_from_path(
leaf_dir: impl AsRef<Path>,
maybe_commit_id: &Option<Hash>,
commit_store: Arc<Mutex<CommitStore>>,
maybe_tree_pos: Option<&HashMap<ContractId, u64>>,
maybe_tree_pos: Option<&Vec<(Hash, u64, u32)>>,
) -> io::Result<(NewContractIndex, ContractsMerkle)> {
let leaf_dir = leaf_dir.as_ref();

Expand Down Expand Up @@ -546,30 +546,6 @@ fn index_merkle_from_path(
),
)
})?;
if let Some(h) = element.hash() {
match maybe_tree_pos {
Some(tree_pos) => {
if let Some(pos) = tree_pos.get(&contract_id) {
merkle.insert_with_int_pos(
position_from_contract(&contract_id),
*pos,
h,
&contract_id,
);
}
}
None => {
merkle.insert_with_int_pos(
position_from_contract(&contract_id),
element
.int_pos()
.expect("int pos should be present"),
h,
&contract_id,
);
}
}
}
if element_depth != u32::MAX {
index.insert_contract_index(&contract_id, element);
} else {
Expand All @@ -582,6 +558,23 @@ fn index_merkle_from_path(
}
}

match maybe_tree_pos {
Some(tree_pos) => {
for (hash, pos, int_pos) in tree_pos.iter() {
merkle.insert_with_int_pos(*pos, *int_pos as u64, *hash);
}
}
None => {
unreachable!()
}
}

println!(
"COMMIT_FROM_DIR: merkle size={}, tree pos size={}",
merkle.len(),
merkle.tree_pos().len(),
);

Ok((index, merkle))
}

Expand Down Expand Up @@ -710,7 +703,7 @@ impl Commit {

let root = *element.tree().root();
let pos = position_from_contract(&contract_id);
let internal_pos = contracts_merkle.insert(pos, root, &contract_id);
let internal_pos = contracts_merkle.insert(pos, root);
element.set_hash(Some(root));
element.set_int_pos(Some(internal_pos));
}
Expand Down
32 changes: 12 additions & 20 deletions piecrust/src/store/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use std::collections::HashMap;
use std::{
cell::Ref,
collections::{BTreeMap, BTreeSet},
Expand Down Expand Up @@ -98,26 +97,21 @@ impl NewContractIndex {
pub struct ContractsMerkle {
inner_tree: Tree,
dict: BTreeMap<u64, u64>,
tree_pos: HashMap<ContractId, u64>,
tree_pos: Vec<(Hash, u64, u32)>,
}

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

impl ContractsMerkle {
pub fn insert(
&mut self,
pos: u64,
hash: Hash,
contract_id: &ContractId,
) -> u64 {
pub fn insert(&mut self, pos: u64, hash: Hash) -> u64 {
let new_pos = match self.dict.get(&pos) {
None => {
let new_pos = (self.dict.len() + 1) as u64;
Expand All @@ -127,20 +121,14 @@ impl ContractsMerkle {
Some(p) => *p,
};
self.inner_tree.insert(new_pos, hash);
self.tree_pos.insert(*contract_id, new_pos);
self.tree_pos.push((hash, pos, new_pos as u32));
new_pos
}

pub fn insert_with_int_pos(
&mut self,
pos: u64,
int_pos: u64,
hash: Hash,
contract_id: &ContractId,
) {
pub fn insert_with_int_pos(&mut self, pos: u64, int_pos: u64, hash: Hash) {
self.dict.insert(pos, int_pos);
self.inner_tree.insert(int_pos, hash);
self.tree_pos.insert(*contract_id, int_pos);
self.tree_pos.push((hash, pos, int_pos as u32));
}

pub fn opening(&self, pos: u64) -> Option<TreeOpening> {
Expand All @@ -152,9 +140,13 @@ impl ContractsMerkle {
self.inner_tree.root()
}

pub fn tree_pos(&self) -> &HashMap<ContractId, u64> {
pub fn tree_pos(&self) -> &Vec<(Hash, u64, u32)> {
&self.tree_pos
}

pub fn len(&self) -> u64 {
self.inner_tree.len()
}
}

#[derive(Debug, Clone, Archive, Deserialize, Serialize)]
Expand All @@ -176,7 +168,7 @@ pub struct BaseInfo {
#[derive(Debug, Clone, Default, Archive, Deserialize, Serialize)]
#[archive_attr(derive(CheckBytes))]
pub struct TreePos {
pub tree_pos: HashMap<ContractId, u64>,
pub tree_pos: Vec<(Hash, u64, u32)>,
}

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

0 comments on commit e3af6c0

Please sign in to comment.