diff --git a/packages/vm-derive-impl/Cargo.toml b/packages/vm-derive-impl/Cargo.toml index 75b506026..0af3c1788 100644 --- a/packages/vm-derive-impl/Cargo.toml +++ b/packages/vm-derive-impl/Cargo.toml @@ -11,4 +11,4 @@ blake3 = "1.5.4" cargo-manifest = "0.15.2" proc-macro2 = "1.0.86" quote = "1.0.37" -syn = { version = "2.0.77", features = ["full"] } +syn = { version = "2.0.77", features = ["extra-traits", "full"] } diff --git a/packages/vm-derive-impl/src/hash_function.rs b/packages/vm-derive-impl/src/hash_function.rs index abc5a2c35..6b6ff76a6 100644 --- a/packages/vm-derive-impl/src/hash_function.rs +++ b/packages/vm-derive-impl/src/hash_function.rs @@ -1,19 +1,48 @@ +use std::hash::{Hash, Hasher}; + use proc_macro2::TokenStream; use quote::quote; use super::{bail, maybe}; +struct Blake3Hasher { + hasher: blake3::Hasher, +} + +impl Blake3Hasher { + fn new() -> Self { + Self { + hasher: blake3::Hasher::new(), + } + } + + fn consume(self) -> String { + self.hasher.finalize().to_hex().to_string() + } +} + +impl Hasher for Blake3Hasher { + fn write(&mut self, bytes: &[u8]) { + self.hasher.update(bytes); + } + + fn finish(&self) -> u64 { + unimplemented!(); + } +} + pub fn hash_function_impl(attr: TokenStream, input: TokenStream) -> TokenStream { if !attr.is_empty() { bail!(attr, "Unexpected parameters"); } // Just verify that this is actually a function - let _: syn::ItemFn = maybe!(syn::parse2(input.clone())); + let function: syn::ItemFn = maybe!(syn::parse2(input.clone())); + + let mut hasher = Blake3Hasher::new(); + function.hash(&mut hasher); - let display = input.to_string(); - let hex_hash = blake3::hash(display.as_bytes()).to_hex(); - let hex_hash = hex_hash.as_str(); + let hex_hash = hasher.consume(); quote! { #input diff --git a/packages/vm/src/modules/file_system_cache.rs b/packages/vm/src/modules/file_system_cache.rs index c3bd55b8b..eee976d6d 100644 --- a/packages/vm/src/modules/file_system_cache.rs +++ b/packages/vm/src/modules/file_system_cache.rs @@ -465,4 +465,13 @@ mod tests { assert_eq!(v2, v3); assert_eq!(v3, v4); } + + #[test] + fn module_version_static() { + let version = raw_module_version_discriminator(); + assert_eq!( + version, + "9ef3de8cb5fb770171ae3ea14db67fe25d946fef383472a18ddd75ced7bfcd4b" + ); + } }