feat: compute pallet/storage prefix hash at compile time (#1539)

Since the hash rules of this part of the `pallet_prefix/storage_prefix`
are always fixed, we can put the runtime calculation into compile time.

---
polkadot address: 15ouFh2SHpGbHtDPsJ6cXQfes9Cx1gEFnJJsJVqPGzBSTudr

---------

Co-authored-by: Juan <juangirini@gmail.com>
Co-authored-by: command-bot <>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
yjh
2023-10-03 23:16:46 +08:00
committed by GitHub
parent 3ea497b5a0
commit aad80cce31
28 changed files with 263 additions and 123 deletions
@@ -17,7 +17,7 @@
//! Implementation of the `storage_alias` attribute macro.
use crate::counter_prefix;
use crate::{counter_prefix, pallet::parse::helper};
use frame_support_procedural_tools::generate_crate_access_2018;
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
@@ -619,6 +619,7 @@ fn generate_storage_instance(
let counter_code = is_counted_map.then(|| {
let counter_name = Ident::new(&counter_prefix(&name_str), Span::call_site());
let counter_storage_name_str = counter_prefix(&storage_name_str);
let storage_prefix_hash = helper::two128_str(&counter_storage_name_str);
quote! {
#visibility struct #counter_name< #impl_generics >(
@@ -633,6 +634,9 @@ fn generate_storage_instance(
}
const STORAGE_PREFIX: &'static str = #counter_storage_name_str;
fn storage_prefix_hash() -> [u8; 16] {
#storage_prefix_hash
}
}
impl<#impl_generics> #crate_::storage::types::CountedStorageMapInstance
@@ -643,6 +647,8 @@ fn generate_storage_instance(
}
});
let storage_prefix_hash = helper::two128_str(&storage_name_str);
// Implement `StorageInstance` trait.
let code = quote! {
#[allow(non_camel_case_types)]
@@ -658,6 +664,9 @@ fn generate_storage_instance(
}
const STORAGE_PREFIX: &'static str = #storage_name_str;
fn storage_prefix_hash() -> [u8; 16] {
#storage_prefix_hash
}
}
#counter_code