Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forge): list cache files that are saved as block numbers for cache ls #7270

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1521,12 +1521,19 @@ impl Config {
if !chain_path.exists() {
return Ok(blocks)
}
for block in chain_path.read_dir()?.flatten().filter(|x| x.file_type().unwrap().is_dir()) {
let filepath = block.path().join("storage.json");
blocks.push((
block.file_name().to_string_lossy().into_owned(),
fs::metadata(filepath)?.len(),
));
for block in chain_path.read_dir()?.flatten() {
let file_type = block.file_type()?;
let file_name = block.file_name();
let filepath = if file_type.is_dir() {
block.path().join("storage.json")
} else if file_type.is_file() &&
file_name.to_string_lossy().chars().all(char::is_numeric)
{
block.path()
} else {
continue
};
blocks.push((file_name.to_string_lossy().into_owned(), fs::metadata(filepath)?.len()));
}
Ok(blocks)
}
Expand Down Expand Up @@ -4559,23 +4566,38 @@ mod tests {
writeln!(file, "{}", vec![' '; size_bytes - 1].iter().collect::<String>()).unwrap();
}

fn fake_block_cache_block_path_as_file(
chain_path: &Path,
block_number: &str,
size_bytes: usize,
) {
let block_path = chain_path.join(block_number);
let mut file = File::create(block_path).unwrap();
writeln!(file, "{}", vec![' '; size_bytes - 1].iter().collect::<String>()).unwrap();
}

let chain_dir = tempdir()?;

fake_block_cache(chain_dir.path(), "1", 100);
fake_block_cache(chain_dir.path(), "2", 500);
fake_block_cache_block_path_as_file(chain_dir.path(), "3", 900);
// Pollution file that should not show up in the cached block
let mut pol_file = File::create(chain_dir.path().join("pol.txt")).unwrap();
writeln!(pol_file, "{}", [' '; 10].iter().collect::<String>()).unwrap();

let result = Config::get_cached_blocks(chain_dir.path())?;

assert_eq!(result.len(), 2);
assert_eq!(result.len(), 3);
let block1 = &result.iter().find(|x| x.0 == "1").unwrap();
let block2 = &result.iter().find(|x| x.0 == "2").unwrap();
let block3 = &result.iter().find(|x| x.0 == "3").unwrap();

assert_eq!(block1.0, "1");
assert_eq!(block1.1, 100);
assert_eq!(block2.0, "2");
assert_eq!(block2.1, 500);
assert_eq!(block3.0, "3");
assert_eq!(block3.1, 900);

chain_dir.close()?;
Ok(())
Expand Down
Loading