Skip to content

Commit

Permalink
local cache for rpc test snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 committed Jan 10, 2025
1 parent b98c0ae commit e3a7ebf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ glob = "0.3"
http-range-header = "0.4"
insta = { version = "1", features = ["yaml"] }
libp2p-swarm-test = { workspace = true }
md5 = "0.7"
num-bigint = { version = "0.4", features = ['quickcheck'] }
petgraph = "0.7"
predicates = "3"
Expand Down
53 changes: 42 additions & 11 deletions src/tool/subcommands/api_cmd/test_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ async fn ctx(
#[cfg(test)]
mod tests {
use super::*;
use crate::daemon::db_util::download_to;
use crate::{daemon::db_util::download_to, utils::net::global_http_client};
use directories::ProjectDirs;
use itertools::Itertools as _;
use url::Url;

Expand All @@ -158,19 +159,49 @@ mod tests {
.as_str(),
)
.ok()
.map(|url| (n, url))
})
.collect_vec();
for url in urls {
print!("Testing {url} ...");
let tmp_dir = tempfile::tempdir().unwrap();
let tmp = tempfile::NamedTempFile::new_in(&tmp_dir)
.unwrap()
.into_temp_path();
println!("start downloading at {}", tmp.display());
download_to(&url, &tmp).await.unwrap();
println!("done downloading {}", tmp.display());
run_test_from_snapshot(&tmp).await.unwrap();
let project_dir = ProjectDirs::from("com", "ChainSafe", "Forest").unwrap();
let cache_dir = project_dir.cache_dir().join("test").join("rpc-snapshots");
for (filename, url) in urls {
let cache_file_path = cache_dir.join(filename);
let is_file_cached = match get_file_md5_etag(&cache_file_path) {
Some(file_etag) => {
let url_etag = get_digital_ocean_space_url_etag(url.clone()).await.unwrap();
if Some(&file_etag) == url_etag.as_ref() {
true
} else {
println!(
"etag mismatch, file: {filename}, local: {file_etag}, remote: {}",
url_etag.unwrap_or_default()
);
false
}
}
None => false,
};
if !is_file_cached {
println!("Downloading from {url} to {}", cache_file_path.display());
download_to(&url, &cache_file_path).await.unwrap();
}
print!("Testing {filename} ...");
run_test_from_snapshot(&cache_file_path).await.unwrap();
println!(" succeeded.");
}
}

async fn get_digital_ocean_space_url_etag(url: Url) -> anyhow::Result<Option<String>> {
let response = global_http_client().head(url).send().await?;
Ok(response
.headers()
.get("etag")
.and_then(|v| v.to_str().ok().map(|v| v.replace('"', "").to_string())))
}

fn get_file_md5_etag(path: &Path) -> Option<String> {
std::fs::read(path)
.ok()
.map(|bytes| format!("{:x}", md5::compute(bytes.as_slice())))
}
}

0 comments on commit e3a7ebf

Please sign in to comment.