Skip to content

Commit

Permalink
fix(forge): list cache files that are saved as block numbers for `cac…
Browse files Browse the repository at this point in the history
…he ls` (#7270)

* fix: forge cache ls should include blocknumber files

* fix: ignore files that are not numeric only

* chore: linting
  • Loading branch information
penandlim authored Feb 29, 2024
1 parent 2f432fb commit 576bb59
Showing 1 changed file with 29 additions and 7 deletions.
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

0 comments on commit 576bb59

Please sign in to comment.