mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-20 14:01:02 +00:00
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:
@@ -246,6 +246,14 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
implemented by the runtime")
|
||||
}
|
||||
|
||||
fn name_hash() -> [u8; 16] {
|
||||
<
|
||||
<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo
|
||||
>::name_hash::<Self>()
|
||||
.expect("Pallet is part of the runtime because pallet `Config` trait is \
|
||||
implemented by the runtime")
|
||||
}
|
||||
|
||||
fn module_name() -> &'static str {
|
||||
<
|
||||
<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo
|
||||
|
||||
@@ -18,7 +18,10 @@
|
||||
use crate::{
|
||||
counter_prefix,
|
||||
pallet::{
|
||||
parse::storage::{Metadata, QueryKind, StorageDef, StorageGenerics},
|
||||
parse::{
|
||||
helper::two128_str,
|
||||
storage::{Metadata, QueryKind, StorageDef, StorageGenerics},
|
||||
},
|
||||
Def,
|
||||
},
|
||||
};
|
||||
@@ -638,6 +641,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
Metadata::CountedMap { .. } => {
|
||||
let counter_prefix_struct_ident = counter_prefix_ident(&storage_def.ident);
|
||||
let counter_prefix_struct_const = counter_prefix(&prefix_struct_const);
|
||||
let storage_prefix_hash = two128_str(&counter_prefix_struct_const);
|
||||
quote::quote_spanned!(storage_def.attr_span =>
|
||||
#(#cfg_attrs)*
|
||||
#[doc(hidden)]
|
||||
@@ -656,7 +660,19 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
>::name::<Pallet<#type_use_gen>>()
|
||||
.expect("No name found for the pallet in the runtime! This usually means that the pallet wasn't added to `construct_runtime!`.")
|
||||
}
|
||||
|
||||
fn pallet_prefix_hash() -> [u8; 16] {
|
||||
<
|
||||
<T as #frame_system::Config>::PalletInfo
|
||||
as #frame_support::traits::PalletInfo
|
||||
>::name_hash::<Pallet<#type_use_gen>>()
|
||||
.expect("No name_hash found for the pallet in the runtime! This usually means that the pallet wasn't added to `construct_runtime!`.")
|
||||
}
|
||||
|
||||
const STORAGE_PREFIX: &'static str = #counter_prefix_struct_const;
|
||||
fn storage_prefix_hash() -> [u8; 16] {
|
||||
#storage_prefix_hash
|
||||
}
|
||||
}
|
||||
#(#cfg_attrs)*
|
||||
impl<#type_impl_gen> #frame_support::storage::types::CountedStorageMapInstance
|
||||
@@ -670,6 +686,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
Metadata::CountedNMap { .. } => {
|
||||
let counter_prefix_struct_ident = counter_prefix_ident(&storage_def.ident);
|
||||
let counter_prefix_struct_const = counter_prefix(&prefix_struct_const);
|
||||
let storage_prefix_hash = two128_str(&counter_prefix_struct_const);
|
||||
quote::quote_spanned!(storage_def.attr_span =>
|
||||
#(#cfg_attrs)*
|
||||
#[doc(hidden)]
|
||||
@@ -688,7 +705,17 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
>::name::<Pallet<#type_use_gen>>()
|
||||
.expect("No name found for the pallet in the runtime! This usually means that the pallet wasn't added to `construct_runtime!`.")
|
||||
}
|
||||
fn pallet_prefix_hash() -> [u8; 16] {
|
||||
<
|
||||
<T as #frame_system::Config>::PalletInfo
|
||||
as #frame_support::traits::PalletInfo
|
||||
>::name_hash::<Pallet<#type_use_gen>>()
|
||||
.expect("No name_hash found for the pallet in the runtime! This usually means that the pallet wasn't added to `construct_runtime!`.")
|
||||
}
|
||||
const STORAGE_PREFIX: &'static str = #counter_prefix_struct_const;
|
||||
fn storage_prefix_hash() -> [u8; 16] {
|
||||
#storage_prefix_hash
|
||||
}
|
||||
}
|
||||
#(#cfg_attrs)*
|
||||
impl<#type_impl_gen> #frame_support::storage::types::CountedStorageNMapInstance
|
||||
@@ -702,6 +729,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
_ => proc_macro2::TokenStream::default(),
|
||||
};
|
||||
|
||||
let storage_prefix_hash = two128_str(&prefix_struct_const);
|
||||
quote::quote_spanned!(storage_def.attr_span =>
|
||||
#maybe_counter
|
||||
|
||||
@@ -722,7 +750,19 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
>::name::<Pallet<#type_use_gen>>()
|
||||
.expect("No name found for the pallet in the runtime! This usually means that the pallet wasn't added to `construct_runtime!`.")
|
||||
}
|
||||
|
||||
fn pallet_prefix_hash() -> [u8; 16] {
|
||||
<
|
||||
<T as #frame_system::Config>::PalletInfo
|
||||
as #frame_support::traits::PalletInfo
|
||||
>::name_hash::<Pallet<#type_use_gen>>()
|
||||
.expect("No name_hash found for the pallet in the runtime! This usually means that the pallet wasn't added to `construct_runtime!`.")
|
||||
}
|
||||
|
||||
const STORAGE_PREFIX: &'static str = #prefix_struct_const;
|
||||
fn storage_prefix_hash() -> [u8; 16] {
|
||||
#storage_prefix_hash
|
||||
}
|
||||
}
|
||||
)
|
||||
});
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
//! to user defined types. And also crate new types and implement block.
|
||||
|
||||
mod expand;
|
||||
mod parse;
|
||||
pub(crate) mod parse;
|
||||
|
||||
pub use parse::{composite::keyword::CompositeKeyword, Def};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
@@ -15,7 +15,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use quote::ToTokens;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
/// List of additional token to be used for parsing.
|
||||
@@ -610,3 +611,16 @@ pub fn check_pallet_call_return_type(type_: &syn::Type) -> syn::Result<()> {
|
||||
|
||||
syn::parse2::<Checker>(type_.to_token_stream()).map(|_| ())
|
||||
}
|
||||
|
||||
pub(crate) fn two128_str(s: &str) -> TokenStream {
|
||||
bytes_to_array(sp_core_hashing::twox_128(s.as_bytes()).into_iter())
|
||||
}
|
||||
|
||||
pub(crate) fn bytes_to_array(bytes: impl IntoIterator<Item = u8>) -> TokenStream {
|
||||
let bytes = bytes.into_iter();
|
||||
|
||||
quote!(
|
||||
[ #( #bytes ),* ]
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user