Sort deps in blueprints before hashing
@@ -393,17 +393,22 @@ fn resolve_hash(
fn hash_dependencies(deps: Vec<Dependency>) -> Result<Hash> {
let mut hasher = blake3::Hasher::new();
- for d in deps.iter() {
- match d {
+ let mut deps: Vec<Hash> = deps
+ .into_iter()
+ .try_fold(vec![], |mut acc, dep| match dep {
Dependency::Hash(h) => {
- hasher.update(h.as_bytes());
+ acc.push(h);
+ Ok(acc)
}
- Dependency::Name(n) => {
- return Err(InvalidModuleReference {
+ Dependency::Name(n) => Err(InvalidModuleReference {
reference: n.to_string(),
- });
- }
- }
+ }),
+ })?;
+
+ deps.sort_by(|a, b| a.as_bytes().cmp(&b.as_bytes()));
+
+ for d in deps.iter() {
+ hasher.update(d.as_bytes());
}
let hash = hasher.finalize();
@@ -511,4 +516,17 @@ mod tests {
let result = get_interface(&repo, hash);
assert_eq!(0, result.ret_code);
}
+
+ #[test]
+ fn test_hash_dependency() {
+ use super::{hash_dependencies, Dependency};
+ use crate::modules::Hash;
+
+ let dep1 = Dependency::Hash(Hash::hash(&[1, 2, 3]));
+ let dep2 = Dependency::Hash(Hash::hash(&[3, 2, 1]));
+
+ let hash1 = hash_dependencies(vec![dep1.clone(), dep2.clone()]).unwrap();
+ let hash2 = hash_dependencies(vec![dep2.clone(), dep1.clone()]).unwrap();
+ assert_eq!(hash1.to_string(), hash2.to_string());
+ }
}
