Skip to content

Commit

Permalink
Add back constant test
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Sep 13, 2024
1 parent 0cfa86a commit 4dc02fb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/vm-derive-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
37 changes: 33 additions & 4 deletions packages/vm-derive-impl/src/hash_function.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions packages/vm/src/modules/file_system_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
}

0 comments on commit 4dc02fb

Please sign in to comment.