Skip to content

Commit

Permalink
modify hash calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
indirection42 committed Jun 25, 2024
1 parent 3c0d9b4 commit f1af976
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions xcq-extension/procedural/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ proc-macro = true
syn = { version = "2", features = ["full", "extra-traits"] }
quote = "1"
proc-macro2 = "1"
twox-hash = "1.6.3"
19 changes: 11 additions & 8 deletions xcq-extension/procedural/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::TokenStream;
use quote::quote;
use std::hash::Hasher;
use std::hash::{Hash, Hasher};
use syn::token::Comma;
use syn::{parse_macro_input, parse_quote, parse_str, spanned::Spanned};
use syn::{punctuated::Punctuated, ExprCall, Field, Ident, ItemImpl, Pat, TraitItem, Variant};
Expand Down Expand Up @@ -116,7 +116,7 @@ fn dispatchable_impl(trait_ident: &Ident, methods: &[Method]) -> TokenStream {
}

fn extension_id_impl(trait_ident: &Ident, trait_items: &[TraitItem]) -> ItemImpl {
let extension_id = calculate_hash(trait_items);
let extension_id = calculate_hash(trait_ident, trait_items);
parse_quote! {
impl<Impl: #trait_ident> xcq_extension::ExtensionId for Call<Impl> {
const EXTENSION_ID: xcq_extension::ExtensionIdTy = #extension_id;
Expand Down Expand Up @@ -165,11 +165,14 @@ fn replace_self_to_impl(ty: &syn::Type) -> syn::Result<Box<syn::Type>> {
Ok(Box::new(modified_ty))
}

// TODO: may rely on whole syn::File where we can replace the type alias to get stable hash results
// Or we rejects the type alias
// TODO: check hasher implementation is collision resistant and stable
fn calculate_hash(trait_items: &[TraitItem]) -> u64 {
let mut hasher = std::hash::DefaultHasher::new();
std::hash::Hash::hash_slice(trait_items, &mut hasher);
// TODO: currently we only hash on trait ident and function names,
fn calculate_hash(trait_ident: &Ident, trait_items: &[TraitItem]) -> u64 {
let mut hasher = twox_hash::XxHash64::default();
trait_ident.hash(&mut hasher);
for trait_item in trait_items {
if let TraitItem::Fn(method) = trait_item {
method.sig.ident.hash(&mut hasher);
}
}
hasher.finish()
}
4 changes: 2 additions & 2 deletions xcq-extension/tests/extension_executor_works.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn call_core_works() {
let mut executor = ExtensionsExecutor::<Extensions, ()>::new(InvokeSource::RuntimeAPI);
let guest = GuestImpl { program: blob.to_vec() };
let method = CoreMethod::HasExtension { id: 0 };
let mut input_data = 16086776565154421753u64.encode();
let mut input_data = 980409741042475474u64.encode();
input_data.extend_from_slice(&method.encode());
let input = InputImpl {
method: "main".to_string(),
Expand All @@ -137,7 +137,7 @@ fn call_fungibles_works() {
let mut executor = ExtensionsExecutor::<Extensions, ()>::new(InvokeSource::RuntimeAPI);
let guest = GuestImpl { program: blob.to_vec() };
let method = FungiblesMethod::TotalSupply { asset: 1u64 };
let mut input_data = 18438207628271534596u64.encode();
let mut input_data = 10604941536775757594u64.encode();
input_data.extend_from_slice(&method.encode());
let input = InputImpl {
method: "main".to_string(),
Expand Down

0 comments on commit f1af976

Please sign in to comment.