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
@@ -31,6 +31,8 @@ pub trait PalletInfo {
fn index<P: 'static>() -> Option<usize>;
/// Convert the given pallet `P` into its name as configured in the runtime.
fn name<P: 'static>() -> Option<&'static str>;
/// The two128 hash of name.
fn name_hash<P: 'static>() -> Option<[u8; 16]>;
/// Convert the given pallet `P` into its Rust module name as used in `construct_runtime!`.
fn module_name<P: 'static>() -> Option<&'static str>;
/// Convert the given pallet `P` into its containing crate version.
@@ -59,6 +61,8 @@ pub trait PalletInfoAccess {
fn index() -> usize;
/// Name of the pallet as configured in the runtime.
fn name() -> &'static str;
/// Two128 hash of name.
fn name_hash() -> [u8; 16];
/// Name of the Rust module containing the pallet.
fn module_name() -> &'static str;
/// Version of the crate containing the pallet.
@@ -281,6 +285,7 @@ pub trait GetStorageVersion {
#[cfg(test)]
mod tests {
use super::*;
use sp_core::twox_128;
struct Pallet1;
impl PalletInfoAccess for Pallet1 {
@@ -290,6 +295,9 @@ mod tests {
fn name() -> &'static str {
"Pallet1"
}
fn name_hash() -> [u8; 16] {
twox_128(Self::name().as_bytes())
}
fn module_name() -> &'static str {
"pallet1"
}
@@ -305,6 +313,11 @@ mod tests {
fn name() -> &'static str {
"Pallet2"
}
fn name_hash() -> [u8; 16] {
twox_128(Self::name().as_bytes())
}
fn module_name() -> &'static str {
"pallet2"
}
@@ -61,8 +61,35 @@ pub trait StorageInstance {
/// Prefix of a pallet to isolate it from other pallets.
fn pallet_prefix() -> &'static str;
/// Return the prefix hash of pallet instance.
///
/// NOTE: This hash must be `twox_128(pallet_prefix())`.
/// Should not impl this function by hand. Only use the default or macro generated impls.
fn pallet_prefix_hash() -> [u8; 16] {
sp_io::hashing::twox_128(Self::pallet_prefix().as_bytes())
}
/// Prefix given to a storage to isolate from other storages in the pallet.
const STORAGE_PREFIX: &'static str;
/// Return the prefix hash of storage instance.
///
/// NOTE: This hash must be `twox_128(STORAGE_PREFIX)`.
fn storage_prefix_hash() -> [u8; 16] {
sp_io::hashing::twox_128(Self::STORAGE_PREFIX.as_bytes())
}
/// Return the prefix hash of instance.
///
/// NOTE: This hash must be `twox_128(pallet_prefix())++twox_128(STORAGE_PREFIX)`.
/// Should not impl this function by hand. Only use the default or macro generated impls.
fn prefix_hash() -> [u8; 32] {
let mut final_key = [0u8; 32];
final_key[..16].copy_from_slice(&Self::pallet_prefix_hash());
final_key[16..].copy_from_slice(&Self::storage_prefix_hash());
final_key
}
}
/// Metadata about storage from the runtime.