diff --git a/crates/era-downloader/src/fs.rs b/crates/era-downloader/src/fs.rs index 19532f01cf..eaab1f3f4b 100644 --- a/crates/era-downloader/src/fs.rs +++ b/crates/era-downloader/src/fs.rs @@ -12,6 +12,8 @@ pub fn read_dir( start_from: BlockNumber, ) -> eyre::Result> + Send + Sync + 'static + Unpin> { let mut checksums = None; + + // read all the files in the given dir and also read the checksums file let mut entries = fs::read_dir(dir)? .filter_map(|entry| { (|| { @@ -29,6 +31,7 @@ pub fn read_dir( return Ok(Some((number, path.into_boxed_path()))); } } + if path.file_name() == Some("checksums.txt".as_ref()) { let file = fs::open(path)?; let reader = io::BufReader::new(file); @@ -43,9 +46,15 @@ pub fn read_dir( .collect::>>()?; let mut checksums = checksums.ok_or_eyre("Missing file `checksums.txt` in the `dir`")?; + let start_index = start_from as usize / BLOCKS_PER_FILE; + for _ in 0..start_index { + // skip the first entries in the checksums iterator so that both iters align + checksums.next().transpose()?.ok_or_eyre("Got less checksums than ERA files")?; + } + entries.sort_by(|(left, _), (right, _)| left.cmp(right)); - Ok(stream::iter(entries.into_iter().skip(start_from as usize / BLOCKS_PER_FILE).map( + Ok(stream::iter(entries.into_iter().skip_while(move |(n, _)| *n < start_index).map( move |(_, path)| { let expected_checksum = checksums.next().transpose()?.ok_or_eyre("Got less checksums than ERA files")?;