Skip to content
Open
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
35 changes: 21 additions & 14 deletions crates/host/src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,24 @@ async fn get_cached_input<P: Provider>(
block_id: BlockId,
cache_dir: &Path,
) -> anyhow::Result<Input> {
// First, get the block header to determine the canonical hash for caching.
let header = processor
.provider()
.get_block(block_id)
.await?
.with_context(|| format!("block {block_id} not found"))?
.header;

let cache_file = cache_dir.join(format!("input_{}.json", header.hash));
let block_hash = match block_id {
BlockId::Hash(hash) => hash.block_hash,
_ => {
// First, get the block header to determine the canonical hash for caching.
let header = processor
.provider()
.get_block(block_id)
.await?
.with_context(|| format!("block {block_id} not found"))?
.header;

header.hash
}
};

let cache_file = cache_dir.join(format!("input_{block_hash}.json"));
let input: Input = if cache_file.exists() {
println!("Cache hit for block {}. Loading from file: {:?}", header.hash, &cache_file);
println!("Cache hit for block {block_hash}. Loading from file: {cache_file:?}");
let f = File::open(&cache_file).context("failed to open file")?;
let cached_input: serde_json::Value =
serde_json::from_reader(BufReader::new(f)).context("failed to parse file")?;
Expand All @@ -148,17 +155,17 @@ async fn get_cached_input<P: Provider>(
_ => bail!("failed to parse JSON"),
}
} else {
println!("Cache miss for block {}. Fetching from RPC.", header.hash);
let (input, _) = processor.create_input(header.hash).await?;
println!("Cache miss for block {block_hash}. Fetching from RPC.");
let (input, _) = processor.create_input(block_hash).await?;

// Save the newly fetched input to the cache.
println!("Writing new input to cache: {:?}", &cache_file);
println!("Writing new input to cache: {cache_file:?}");
let f = File::create(&cache_file).context("failed to create file")?;
serde_json::to_writer(BufWriter::new(f), &input).context("failed to write file")?;

input
};
ensure!(input.block.hash_slow() == header.hash);
ensure!(input.block.hash_slow() == block_hash);

Ok(input)
}