mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 12:48:00 +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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user