Skip to content

Commit

Permalink
piecurst: recursive finding elements of base commits
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Oct 14, 2024
1 parent d6944a2 commit 56e97ae
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 74 deletions.
89 changes: 17 additions & 72 deletions piecrust/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub use tree::PageOpening;
const BYTECODE_DIR: &str = "bytecode";
const MEMORY_DIR: &str = "memory";
const LEAF_DIR: &str = "leaf";
// const INDEX_FILE: &str = "index";
const MERKLE_FILE: &str = "merkle";
const BASE_FILE: &str = "base";
const ELEMENT_FILE: &str = "element";
Expand Down Expand Up @@ -205,12 +204,6 @@ fn read_all_commits<P: AsRef<Path>>(
let root_dir = root_dir.join(MAIN_DIR);
fs::create_dir_all(root_dir.clone())?;

// if root_dir.join(INDEX_FILE).is_file() {
// let commit = read_commit(engine, root_dir.clone())?;
// let root = *commit.root();
// commits.insert(root, commit);
// }

for entry in fs::read_dir(root_dir)? {
let entry = entry?;
if entry.path().is_dir() {
Expand Down Expand Up @@ -254,16 +247,6 @@ fn page_path_main<P: AsRef<Path>, S: AsRef<str>>(
Ok(dir.join(format!("{page_index}")))
}

// fn index_path_main<P: AsRef<Path>, S: AsRef<str>>(
// main_dir: P,
// commit_id: S,
// ) -> io::Result<PathBuf> {
// let commit_id = commit_id.as_ref();
// let dir = main_dir.as_ref().join(commit_id);
// fs::create_dir_all(&dir)?;
// Ok(dir.join(INDEX_FILE))
// }

fn merkle_path_main<P: AsRef<Path>, S: AsRef<str>>(
main_dir: P,
commit_id: S,
Expand Down Expand Up @@ -327,8 +310,12 @@ fn commit_from_dir<P: AsRef<Path>>(

let contracts_merkle_path = dir.join(MERKLE_FILE);
let leaf_dir = main_dir.join(LEAF_DIR);
let (index, contracts_merkle) =
index_merkle_from_path(contracts_merkle_path, leaf_dir, &maybe_hash)?;
let (index, contracts_merkle) = index_merkle_from_path(
main_dir,
contracts_merkle_path,
leaf_dir,
&maybe_hash,
)?;

let bytecode_dir = main_dir.join(BYTECODE_DIR);
let memory_dir = main_dir.join(MEMORY_DIR);
Expand Down Expand Up @@ -390,54 +377,29 @@ fn commit_from_dir<P: AsRef<Path>>(
})
}

fn index_merkle_from_path<P1: AsRef<Path>, P2: AsRef<Path>>(
merkle_path: P1,
leaf_dir: P2,
fn index_merkle_from_path<P1: AsRef<Path>, P2: AsRef<Path>, P3: AsRef<Path>>(
main_path: P1,
merkle_path: P2,
leaf_dir: P3,
maybe_commit_id: &Option<Hash>,
) -> io::Result<(NewContractIndex, ContractsMerkle)> {
// let path = path.as_ref();
let merkle_path = merkle_path.as_ref();
let leaf_dir = leaf_dir.as_ref();

// tracing::trace!("reading index file started");
// let index_bytes = fs::read(path)?;
// tracing::trace!("reading index file finished");
// tracing::trace!("deserializing index file started");
let mut index: NewContractIndex = NewContractIndex::new();
// rkyv::from_bytes(&index_bytes).map_err(|err| {
// tracing::trace!("deserializing index file failed {}", err);
// io::Error::new(
// io::ErrorKind::InvalidData,
// format!("Invalid index file \"{path:?}\": {err}"),
// )
// })?;
// tracing::trace!("deserializing index file finished");
// compare index with disk representation
for entry in fs::read_dir(leaf_dir)? {
let entry = entry?;
if entry.path().is_dir() {
let contract_id_hex =
entry.file_name().to_string_lossy().to_string();
let contract_id = contract_id_from_hex(contract_id_hex.clone());
let element_path = match maybe_commit_id {
Some(commit_id) => {
let contract_leaf_dir = leaf_dir.join(contract_id_hex);
let leaf_file_path = contract_leaf_dir
.join(hex::encode(commit_id.as_bytes()))
.join(ELEMENT_FILE);
if leaf_file_path.is_file() {
leaf_file_path
} else {
contract_leaf_dir.join(ELEMENT_FILE)
}
}
None => leaf_dir.join(contract_id_hex).join(ELEMENT_FILE),
};
println!(
"element_path={:?} is_file={}",
element_path,
element_path.is_file()
);
let contract_leaf_path = leaf_dir.join(contract_id_hex);
let element_path = ContractSession::find_element(
*maybe_commit_id,
contract_leaf_path.clone(),
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| {
Expand Down Expand Up @@ -896,23 +858,9 @@ fn write_commit_inner<P: AsRef<Path>, S: AsRef<str>>(
}
}

// let index_main_path =
// index_path_main(directories.main_dir.clone(), commit_id.as_ref())?;
let merkle_main_path =
merkle_path_main(directories.main_dir.clone(), commit_id.as_ref())?;

// tracing::trace!("serializing index started");
// let index_bytes =
// rkyv::to_bytes::<_, 128>(&commit.index).map_err(|err| {
// io::Error::new(
// io::ErrorKind::InvalidData,
// format!("Failed serializing index file: {err}"),
// )
// })?;
// tracing::trace!("serializing index finished");
// tracing::trace!("writing index file started");
// fs::write(index_main_path.clone(), index_bytes)?;
// tracing::trace!("writing index file finished");
for (contract_id, element) in commit.index.iter() {
if commit_contracts.contains_key(contract_id) {
// todo: write element to disk at
Expand Down Expand Up @@ -1033,9 +981,6 @@ fn finalize_commit<P: AsRef<Path>>(
}
fs::remove_dir(src_leaf_path.clone())?;
}
// let index_path = commit_path.join(INDEX_FILE);
// let dst_index_path = main_dir.join(INDEX_FILE);
// fs::rename(index_path.clone(), dst_index_path.clone())?;

let merkle_path = commit_path.join(MERKLE_FILE);
let dst_merkle_path = main_dir.join(MERKLE_FILE);
Expand Down
37 changes: 35 additions & 2 deletions piecrust/src/store/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::contract::ContractMetadata;
use crate::store::tree::{Hash, PageOpening};
use crate::store::{
base_from_path, Bytecode, Call, Commit, Memory, Metadata, Module,
BASE_FILE, BYTECODE_DIR, MAIN_DIR, MEMORY_DIR, METADATA_EXTENSION,
OBJECTCODE_EXTENSION, PAGE_SIZE,
BASE_FILE, BYTECODE_DIR, ELEMENT_FILE, MAIN_DIR, MEMORY_DIR,
METADATA_EXTENSION, OBJECTCODE_EXTENSION, PAGE_SIZE,
};
use crate::Error;

Expand Down Expand Up @@ -196,6 +196,39 @@ impl ContractSession {
}
}

/// Returns path to a file representing a given commit and element.
///
/// Requires a contract's leaf path and a main state path.
/// Progresses recursively via bases of commits.
pub fn find_element(
commit: Option<Hash>,
leaf_path: impl AsRef<Path>,
main_path: impl AsRef<Path>,
) -> Option<PathBuf> {
match commit {
None => None,
Some(hash) => {
let hash_hex = hex::encode(hash.as_bytes());
let path = leaf_path
.as_ref()
.join(hash_hex.clone())
.join(ELEMENT_FILE);
if path.is_file() {
Some(path)
} else {
let base_info_path =
main_path.as_ref().join(hash_hex).join(BASE_FILE);
let index = base_from_path(base_info_path).ok()?;
Self::find_element(
index.maybe_base,
leaf_path.as_ref(),
main_path.as_ref(),
)
}
}
}
}

/// Return the bytecode and memory belonging to the given `contract`, if it
/// exists.
///
Expand Down

0 comments on commit 56e97ae

Please sign in to comment.