Skip to content

Commit

Permalink
Move cargo version out of hash_files
Browse files Browse the repository at this point in the history
This makes the unit tests more resilient to changes in the rust
environment.
  • Loading branch information
gligneul committed Nov 4, 2024
1 parent b57487f commit bcfb703
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion main/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl CheckConfig {
let cfg = BuildConfig::new(rust_stable);
let wasm = project::build_dylib(cfg.clone())?;
let project_hash =
project::hash_files(self.common_cfg.source_files_for_project_hash.clone(), cfg)?;
project::hash_project(self.common_cfg.source_files_for_project_hash.clone(), cfg)?;
Ok((wasm, project_hash))
}
}
Expand Down
28 changes: 22 additions & 6 deletions main/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ pub fn read_file_preimage(filename: &Path) -> Result<Vec<u8>> {
Ok(contents)
}

pub fn hash_files(source_file_patterns: Vec<String>, cfg: BuildConfig) -> Result<[u8; 32]> {
let mut keccak = Keccak::v256();
pub fn hash_project(source_file_patterns: Vec<String>, cfg: BuildConfig) -> Result<[u8; 32]> {
let mut cmd = Command::new("cargo");
cmd.arg("--version");
let output = cmd
Expand All @@ -255,7 +254,21 @@ pub fn hash_files(source_file_patterns: Vec<String>, cfg: BuildConfig) -> Result
if !output.status.success() {
bail!("cargo version command failed");
}
keccak.update(&output.stdout);

hash_files(&output.stdout, source_file_patterns, cfg)
}

pub fn hash_files(
cargo_version_output: &[u8],
source_file_patterns: Vec<String>,
cfg: BuildConfig,
) -> Result<[u8; 32]> {
let mut keccak = Keccak::v256();
println!(
"> {}",
String::from_utf8(cargo_version_output.into()).unwrap()
);
keccak.update(cargo_version_output);
if cfg.opt_level == OptLevel::Z {
keccak.update(&[0]);
} else {
Expand All @@ -282,7 +295,7 @@ pub fn hash_files(source_file_patterns: Vec<String>, cfg: BuildConfig) -> Result
"File used for deployment hash: {}",
filename.as_os_str().to_string_lossy()
);
tx.send(read_file_preimage(&filename))
tx.send(read_file_preimage(filename))
.expect("failed to send preimage (impossible)");
}
});
Expand Down Expand Up @@ -538,7 +551,8 @@ mod test {
#[test]
pub fn test_hash_files() -> Result<()> {
let _dir = write_hash_files(10, 100)?;
let hash = hash_files(vec![], BuildConfig::new(false))?;
let rust_version = "cargo 1.80.0 (376290515 2024-07-16)\n".as_bytes();
let hash = hash_files(rust_version, vec![], BuildConfig::new(false))?;
assert_eq!(
hex::encode(hash),
"06b50fcc53e0804f043eac3257c825226e59123018b73895cb946676148cb262"
Expand All @@ -550,8 +564,10 @@ mod test {
#[bench]
pub fn bench_hash_files(b: &mut test::Bencher) -> Result<()> {
let _dir = write_hash_files(1000, 10000)?;
let rust_version = "cargo 1.80.0 (376290515 2024-07-16)\n".as_bytes();
b.iter(|| {
hash_files(vec![], BuildConfig::new(false)).expect("failed to hash files");
hash_files(rust_version, vec![], BuildConfig::new(false))
.expect("failed to hash files");
});
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion main/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub async fn verify(cfg: VerifyConfig) -> eyre::Result<()> {
let wasm_file: PathBuf = project::build_dylib(build_cfg.clone())
.map_err(|e| eyre!("could not build project to WASM: {e}"))?;
let project_hash =
project::hash_files(cfg.common_cfg.source_files_for_project_hash, build_cfg)?;
project::hash_project(cfg.common_cfg.source_files_for_project_hash, build_cfg)?;
let (_, init_code) = project::compress_wasm(&wasm_file, project_hash)?;
let deployment_data = deploy::contract_deployment_calldata(&init_code);
if deployment_data == *result.input {
Expand Down

0 comments on commit bcfb703

Please sign in to comment.