|
1 | 1 | // Copyright 2025, Offchain Labs, Inc. |
2 | 2 | // For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/main/licenses/COPYRIGHT.md |
3 | 3 |
|
4 | | -use tiny_keccak::{Hasher, Keccak}; |
| 4 | +use std::{ |
| 5 | + fs, |
| 6 | + io::Read, |
| 7 | + path::{Path, PathBuf}, |
| 8 | + sync::mpsc, |
| 9 | + thread, |
| 10 | +}; |
5 | 11 |
|
6 | | -use crate::utils::cargo; |
| 12 | +use glob::glob; |
| 13 | +use tiny_keccak::{Hasher, Keccak}; |
7 | 14 |
|
8 | 15 | use super::{ProjectConfig, ProjectError}; |
| 16 | +use crate::{ |
| 17 | + core::build::{BuildConfig, OptLevel}, |
| 18 | + utils::{cargo, toolchain::find_toolchain_file}, |
| 19 | +}; |
9 | 20 |
|
10 | 21 | pub type ProjectHash = [u8; 32]; |
11 | 22 |
|
12 | | -pub fn hash_project(_config: &ProjectConfig) -> Result<ProjectHash, ProjectError> { |
| 23 | +pub fn hash_project( |
| 24 | + dir: impl AsRef<Path>, |
| 25 | + config: &ProjectConfig, |
| 26 | + build: &BuildConfig, |
| 27 | +) -> Result<ProjectHash, ProjectError> { |
13 | 28 | let cargo_version = cargo::version()?; |
14 | 29 |
|
15 | 30 | let mut keccak = Keccak::v256(); |
16 | 31 | keccak.update(cargo_version.as_bytes()); |
17 | | - // TODO: hash project files |
| 32 | + if matches!(build.opt_level, OptLevel::Z) { |
| 33 | + keccak.update(&[0]); |
| 34 | + } else { |
| 35 | + keccak.update(&[1]); |
| 36 | + } |
| 37 | + |
| 38 | + // Fetch the Rust toolchain toml file from the project root. Assert that it exists and add it to the |
| 39 | + // files in the directory to hash. |
| 40 | + let toolchain_file_path = find_toolchain_file(dir.as_ref())?; |
| 41 | + |
| 42 | + let mut paths = all_paths(dir, config.source_file_patterns.clone())?; |
| 43 | + paths.push(toolchain_file_path); |
| 44 | + paths.sort(); |
| 45 | + |
| 46 | + // Read the file contents in another thread and process the keccak in the main thread. |
| 47 | + let (tx, rx) = mpsc::channel(); |
| 48 | + thread::spawn(move || { |
| 49 | + for filename in paths.iter() { |
| 50 | + greyln!( |
| 51 | + "File used for deployment hash: {}", |
| 52 | + filename.as_os_str().to_string_lossy() |
| 53 | + ); |
| 54 | + tx.send(read_file_preimage(filename)) |
| 55 | + .expect("failed to send preimage (impossible)"); |
| 56 | + } |
| 57 | + }); |
| 58 | + for result in rx { |
| 59 | + keccak.update(result?.as_slice()); |
| 60 | + } |
| 61 | + |
| 62 | + let mut project_hash = ProjectHash::default(); |
| 63 | + keccak.finalize(&mut project_hash); |
| 64 | + greyln!( |
| 65 | + "project metadata hash computed on deployment: {:?}", |
| 66 | + hex::encode(project_hash) |
| 67 | + ); |
| 68 | + Ok(project_hash) |
| 69 | +} |
| 70 | + |
| 71 | +fn all_paths( |
| 72 | + root_dir: impl AsRef<Path>, |
| 73 | + source_file_patterns: Vec<String>, |
| 74 | +) -> Result<Vec<PathBuf>, ProjectError> { |
| 75 | + let mut files = Vec::<PathBuf>::new(); |
| 76 | + let mut directories = Vec::<PathBuf>::new(); |
| 77 | + directories.push(root_dir.as_ref().to_path_buf()); // Using `from` directly |
| 78 | + |
| 79 | + let glob_paths = expand_glob_patterns(source_file_patterns)?; |
| 80 | + |
| 81 | + while let Some(dir) = directories.pop() { |
| 82 | + for entry in fs::read_dir(&dir).map_err(|e| ProjectError::DirectoryRead(dir.clone(), e))? { |
| 83 | + let entry = entry.map_err(|e| ProjectError::DirectoryEntry(dir.clone(), e))?; |
| 84 | + let path = entry.path(); |
| 85 | + |
| 86 | + if path.is_dir() { |
| 87 | + if path.ends_with("target") || path.ends_with(".git") { |
| 88 | + continue; // Skip "target" and ".git" directories |
| 89 | + } |
| 90 | + directories.push(path); |
| 91 | + } else if path.file_name().is_some_and(|f| { |
| 92 | + // If the user has has specified a list of source file patterns, check if the file |
| 93 | + // matches the pattern. |
| 94 | + if !glob_paths.is_empty() { |
| 95 | + for glob_path in glob_paths.iter() { |
| 96 | + if glob_path == &path { |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | + false |
| 101 | + } else { |
| 102 | + // Otherwise, by default include all rust files, Cargo.toml and Cargo.lock files. |
| 103 | + f == "Cargo.toml" || f == "Cargo.lock" || f.to_string_lossy().ends_with(".rs") |
| 104 | + } |
| 105 | + }) { |
| 106 | + files.push(path); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + Ok(files) |
| 111 | +} |
| 112 | + |
| 113 | +fn expand_glob_patterns(patterns: Vec<String>) -> Result<Vec<PathBuf>, ProjectError> { |
| 114 | + let mut files_to_include = Vec::new(); |
| 115 | + for pattern in patterns { |
| 116 | + let paths = glob(&pattern).map_err(|e| ProjectError::GlobPattern(pattern.clone(), e))?; |
| 117 | + for path_result in paths { |
| 118 | + let path = path_result?; |
| 119 | + files_to_include.push(path); |
| 120 | + } |
| 121 | + } |
| 122 | + Ok(files_to_include) |
| 123 | +} |
18 | 124 |
|
19 | | - Ok(ProjectHash::default()) |
| 125 | +fn read_file_preimage(filename: &Path) -> Result<Vec<u8>, ProjectError> { |
| 126 | + let mut contents = Vec::with_capacity(1024); |
| 127 | + { |
| 128 | + let filename = filename.as_os_str(); |
| 129 | + contents.extend_from_slice(&(filename.len() as u64).to_be_bytes()); |
| 130 | + contents.extend_from_slice(filename.as_encoded_bytes()); |
| 131 | + } |
| 132 | + let mut file = std::fs::File::open(filename) |
| 133 | + .map_err(|e| ProjectError::FileOpen(filename.to_path_buf(), e))?; |
| 134 | + contents.extend_from_slice(&file.metadata().unwrap().len().to_be_bytes()); |
| 135 | + file.read_to_end(&mut contents) |
| 136 | + .map_err(|e| ProjectError::FileRead(filename.to_path_buf(), e))?; |
| 137 | + Ok(contents) |
20 | 138 | } |
0 commit comments