From e6787aefa12416ef7ab5811acb0dbb270225252b Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 25 Aug 2021 15:16:47 -0400 Subject: [PATCH] Expose `storage_prefix` logic, and remove duplicate code (#9621) * expose storage prefix generation, remove duplicate code * remove more duplicate code * clean up import * fix io test * remove slicing * Update frame/support/src/storage/mod.rs Co-authored-by: Guillaume Thiolliere Co-authored-by: Guillaume Thiolliere fix: revert unneccessary changes --- frame/support/src/migrations.rs | 9 +--- .../src/storage/generator/double_map.rs | 49 ++++++------------- frame/support/src/storage/generator/map.rs | 39 +++++---------- frame/support/src/storage/generator/nmap.rs | 45 +++++------------ frame/support/src/storage/generator/value.rs | 6 +-- frame/support/src/storage/migration.rs | 48 +++++++++--------- frame/support/src/storage/mod.rs | 23 ++++++--- frame/support/src/traits/hooks.rs | 9 +--- frame/support/src/traits/metadata.rs | 10 +--- frame/support/test/tests/decl_storage.rs | 19 ++----- frame/support/test/tests/pallet.rs | 19 +------ .../tests/pallet_ui/hooks_invalid_item.stderr | 4 +- 12 files changed, 89 insertions(+), 191 deletions(-) diff --git a/frame/support/src/migrations.rs b/frame/support/src/migrations.rs index cf1ba81982424..dc3402440fdd4 100644 --- a/frame/support/src/migrations.rs +++ b/frame/support/src/migrations.rs @@ -30,14 +30,7 @@ impl PalletVersionToStorageVersionHelpe const PALLET_VERSION_STORAGE_KEY_POSTFIX: &[u8] = b":__PALLET_VERSION__:"; fn pallet_version_key(name: &str) -> [u8; 32] { - let pallet_name = sp_io::hashing::twox_128(name.as_bytes()); - let postfix = sp_io::hashing::twox_128(PALLET_VERSION_STORAGE_KEY_POSTFIX); - - let mut final_key = [0u8; 32]; - final_key[..16].copy_from_slice(&pallet_name); - final_key[16..].copy_from_slice(&postfix); - - final_key + crate::storage::storage_prefix(name.as_bytes(), PALLET_VERSION_STORAGE_KEY_POSTFIX) } sp_io::storage::clear(&pallet_version_key(::name())); diff --git a/frame/support/src/storage/generator/double_map.rs b/frame/support/src/storage/generator/double_map.rs index cec5bf57e50ce..d28e42028de53 100644 --- a/frame/support/src/storage/generator/double_map.rs +++ b/frame/support/src/storage/generator/double_map.rs @@ -16,8 +16,8 @@ // limitations under the License. use crate::{ - hash::{ReversibleStorageHasher, StorageHasher, Twox128}, - storage::{self, unhashed, KeyPrefixIterator, PrefixIterator, StorageAppend}, + hash::{ReversibleStorageHasher, StorageHasher}, + storage::{self, storage_prefix, unhashed, KeyPrefixIterator, PrefixIterator, StorageAppend}, Never, }; use codec::{Decode, Encode, EncodeLike, FullCodec, FullEncode}; @@ -62,16 +62,8 @@ pub trait StorageDoubleMap { /// The full prefix; just the hash of `module_prefix` concatenated to the hash of /// `storage_prefix`. fn prefix_hash() -> Vec { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); - - let mut result = - Vec::with_capacity(module_prefix_hashed.len() + storage_prefix_hashed.len()); - - result.extend_from_slice(&module_prefix_hashed[..]); - result.extend_from_slice(&storage_prefix_hashed[..]); - - result + let result = storage_prefix(Self::module_prefix(), Self::storage_prefix()); + result.to_vec() } /// Convert an optional value retrieved from storage to the type queried. @@ -85,16 +77,12 @@ pub trait StorageDoubleMap { where KArg1: EncodeLike, { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); + let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix()); let key_hashed = k1.borrow().using_encoded(Self::Hasher1::hash); - let mut final_key = Vec::with_capacity( - module_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.as_ref().len(), - ); + let mut final_key = Vec::with_capacity(storage_prefix.len() + key_hashed.as_ref().len()); - final_key.extend_from_slice(&module_prefix_hashed[..]); - final_key.extend_from_slice(&storage_prefix_hashed[..]); + final_key.extend_from_slice(&storage_prefix); final_key.extend_from_slice(key_hashed.as_ref()); final_key @@ -106,20 +94,15 @@ pub trait StorageDoubleMap { KArg1: EncodeLike, KArg2: EncodeLike, { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); + let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix()); let key1_hashed = k1.borrow().using_encoded(Self::Hasher1::hash); let key2_hashed = k2.borrow().using_encoded(Self::Hasher2::hash); let mut final_key = Vec::with_capacity( - module_prefix_hashed.len() + - storage_prefix_hashed.len() + - key1_hashed.as_ref().len() + - key2_hashed.as_ref().len(), + storage_prefix.len() + key1_hashed.as_ref().len() + key2_hashed.as_ref().len(), ); - final_key.extend_from_slice(&module_prefix_hashed[..]); - final_key.extend_from_slice(&storage_prefix_hashed[..]); + final_key.extend_from_slice(&storage_prefix); final_key.extend_from_slice(key1_hashed.as_ref()); final_key.extend_from_slice(key2_hashed.as_ref()); @@ -319,20 +302,16 @@ where key2: KeyArg2, ) -> Option { let old_key = { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); + let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix()); + let key1_hashed = key1.borrow().using_encoded(OldHasher1::hash); let key2_hashed = key2.borrow().using_encoded(OldHasher2::hash); let mut final_key = Vec::with_capacity( - module_prefix_hashed.len() + - storage_prefix_hashed.len() + - key1_hashed.as_ref().len() + - key2_hashed.as_ref().len(), + storage_prefix.len() + key1_hashed.as_ref().len() + key2_hashed.as_ref().len(), ); - final_key.extend_from_slice(&module_prefix_hashed[..]); - final_key.extend_from_slice(&storage_prefix_hashed[..]); + final_key.extend_from_slice(&storage_prefix); final_key.extend_from_slice(key1_hashed.as_ref()); final_key.extend_from_slice(key2_hashed.as_ref()); diff --git a/frame/support/src/storage/generator/map.rs b/frame/support/src/storage/generator/map.rs index b78e9f96496fa..3fd3b9a0ea7b8 100644 --- a/frame/support/src/storage/generator/map.rs +++ b/frame/support/src/storage/generator/map.rs @@ -16,8 +16,8 @@ // limitations under the License. use crate::{ - hash::{ReversibleStorageHasher, StorageHasher, Twox128}, - storage::{self, unhashed, KeyPrefixIterator, PrefixIterator, StorageAppend}, + hash::{ReversibleStorageHasher, StorageHasher}, + storage::{self, storage_prefix, unhashed, KeyPrefixIterator, PrefixIterator, StorageAppend}, Never, }; use codec::{Decode, Encode, EncodeLike, FullCodec, FullEncode}; @@ -52,16 +52,8 @@ pub trait StorageMap { /// The full prefix; just the hash of `module_prefix` concatenated to the hash of /// `storage_prefix`. fn prefix_hash() -> Vec { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); - - let mut result = - Vec::with_capacity(module_prefix_hashed.len() + storage_prefix_hashed.len()); - - result.extend_from_slice(&module_prefix_hashed[..]); - result.extend_from_slice(&storage_prefix_hashed[..]); - - result + let result = storage_prefix(Self::module_prefix(), Self::storage_prefix()); + result.to_vec() } /// Convert an optional value retrieved from storage to the type queried. @@ -75,16 +67,12 @@ pub trait StorageMap { where KeyArg: EncodeLike, { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); + let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix()); let key_hashed = key.borrow().using_encoded(Self::Hasher::hash); - let mut final_key = Vec::with_capacity( - module_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.as_ref().len(), - ); + let mut final_key = Vec::with_capacity(storage_prefix.len() + key_hashed.as_ref().len()); - final_key.extend_from_slice(&module_prefix_hashed[..]); - final_key.extend_from_slice(&storage_prefix_hashed[..]); + final_key.extend_from_slice(&storage_prefix); final_key.extend_from_slice(key_hashed.as_ref()); final_key @@ -330,18 +318,13 @@ impl> storage::StorageMap fn migrate_key>(key: KeyArg) -> Option { let old_key = { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); + let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix()); let key_hashed = key.borrow().using_encoded(OldHasher::hash); - let mut final_key = Vec::with_capacity( - module_prefix_hashed.len() + - storage_prefix_hashed.len() + - key_hashed.as_ref().len(), - ); + let mut final_key = + Vec::with_capacity(storage_prefix.len() + key_hashed.as_ref().len()); - final_key.extend_from_slice(&module_prefix_hashed[..]); - final_key.extend_from_slice(&storage_prefix_hashed[..]); + final_key.extend_from_slice(&storage_prefix); final_key.extend_from_slice(key_hashed.as_ref()); final_key diff --git a/frame/support/src/storage/generator/nmap.rs b/frame/support/src/storage/generator/nmap.rs index 2ea401f44e96f..592bcc81341bf 100755 --- a/frame/support/src/storage/generator/nmap.rs +++ b/frame/support/src/storage/generator/nmap.rs @@ -30,9 +30,8 @@ //! be compromised. use crate::{ - hash::{StorageHasher, Twox128}, storage::{ - self, + self, storage_prefix, types::{ EncodeLikeTuple, HasKeyPrefix, HasReversibleKeyPrefix, KeyGenerator, ReversibleKeyGenerator, TupleToEncodedIter, @@ -71,16 +70,8 @@ pub trait StorageNMap { /// The full prefix; just the hash of `module_prefix` concatenated to the hash of /// `storage_prefix`. fn prefix_hash() -> Vec { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); - - let mut result = - Vec::with_capacity(module_prefix_hashed.len() + storage_prefix_hashed.len()); - - result.extend_from_slice(&module_prefix_hashed[..]); - result.extend_from_slice(&storage_prefix_hashed[..]); - - result + let result = storage_prefix(Self::module_prefix(), Self::storage_prefix()); + result.to_vec() } /// Convert an optional value retrieved from storage to the type queried. @@ -94,16 +85,12 @@ pub trait StorageNMap { where K: HasKeyPrefix, { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); + let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix()); let key_hashed = >::partial_key(key); - let mut final_key = Vec::with_capacity( - module_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.len(), - ); + let mut final_key = Vec::with_capacity(storage_prefix.len() + key_hashed.len()); - final_key.extend_from_slice(&module_prefix_hashed[..]); - final_key.extend_from_slice(&storage_prefix_hashed[..]); + final_key.extend_from_slice(&storage_prefix); final_key.extend_from_slice(key_hashed.as_ref()); final_key @@ -115,16 +102,12 @@ pub trait StorageNMap { KG: KeyGenerator, KArg: EncodeLikeTuple + TupleToEncodedIter, { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); + let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix()); let key_hashed = KG::final_key(key); - let mut final_key = Vec::with_capacity( - module_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.len(), - ); + let mut final_key = Vec::with_capacity(storage_prefix.len() + key_hashed.len()); - final_key.extend_from_slice(&module_prefix_hashed[..]); - final_key.extend_from_slice(&storage_prefix_hashed[..]); + final_key.extend_from_slice(&storage_prefix); final_key.extend_from_slice(key_hashed.as_ref()); final_key @@ -286,16 +269,12 @@ where KArg: EncodeLikeTuple + TupleToEncodedIter, { let old_key = { - let module_prefix_hashed = Twox128::hash(Self::module_prefix()); - let storage_prefix_hashed = Twox128::hash(Self::storage_prefix()); + let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix()); let key_hashed = K::migrate_key(&key, hash_fns); - let mut final_key = Vec::with_capacity( - module_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.len(), - ); + let mut final_key = Vec::with_capacity(storage_prefix.len() + key_hashed.len()); - final_key.extend_from_slice(&module_prefix_hashed[..]); - final_key.extend_from_slice(&storage_prefix_hashed[..]); + final_key.extend_from_slice(&storage_prefix); final_key.extend_from_slice(key_hashed.as_ref()); final_key diff --git a/frame/support/src/storage/generator/value.rs b/frame/support/src/storage/generator/value.rs index c765e059ec149..3486eaa005c06 100644 --- a/frame/support/src/storage/generator/value.rs +++ b/frame/support/src/storage/generator/value.rs @@ -16,7 +16,6 @@ // limitations under the License. use crate::{ - hash::{StorageHasher, Twox128}, storage::{self, unhashed, StorageAppend}, Never, }; @@ -46,10 +45,7 @@ pub trait StorageValue { /// Generate the full key used in top storage. fn storage_value_final_key() -> [u8; 32] { - let mut final_key = [0u8; 32]; - final_key[0..16].copy_from_slice(&Twox128::hash(Self::module_prefix())); - final_key[16..32].copy_from_slice(&Twox128::hash(Self::storage_prefix())); - final_key + crate::storage::storage_prefix(Self::module_prefix(), Self::storage_prefix()) } } diff --git a/frame/support/src/storage/migration.rs b/frame/support/src/storage/migration.rs index 0f10c5cbb47d3..eae45b1e96ad0 100644 --- a/frame/support/src/storage/migration.rs +++ b/frame/support/src/storage/migration.rs @@ -17,7 +17,11 @@ //! Some utilities for helping access storage with arbitrary key types. -use crate::{hash::ReversibleStorageHasher, storage::unhashed, StorageHasher, Twox128}; +use crate::{ + hash::ReversibleStorageHasher, + storage::{storage_prefix, unhashed}, + StorageHasher, Twox128, +}; use codec::{Decode, Encode}; use sp_std::prelude::*; @@ -47,8 +51,8 @@ impl StorageIterator { )] pub fn with_suffix(module: &[u8], item: &[u8], suffix: &[u8]) -> Self { let mut prefix = Vec::new(); - prefix.extend_from_slice(&Twox128::hash(module)); - prefix.extend_from_slice(&Twox128::hash(item)); + let storage_prefix = storage_prefix(module, item); + prefix.extend_from_slice(&storage_prefix); prefix.extend_from_slice(suffix); let previous_key = prefix.clone(); Self { prefix, previous_key, drain: false, _phantom: Default::default() } @@ -112,8 +116,8 @@ impl StorageKeyIterator { )] pub fn with_suffix(module: &[u8], item: &[u8], suffix: &[u8]) -> Self { let mut prefix = Vec::new(); - prefix.extend_from_slice(&Twox128::hash(module)); - prefix.extend_from_slice(&Twox128::hash(item)); + let storage_prefix = storage_prefix(module, item); + prefix.extend_from_slice(&storage_prefix); prefix.extend_from_slice(suffix); let previous_key = prefix.clone(); Self { prefix, previous_key, drain: false, _phantom: Default::default() } @@ -173,8 +177,8 @@ pub fn storage_iter_with_suffix( suffix: &[u8], ) -> PrefixIterator<(Vec, T)> { let mut prefix = Vec::new(); - prefix.extend_from_slice(&Twox128::hash(module)); - prefix.extend_from_slice(&Twox128::hash(item)); + let storage_prefix = storage_prefix(module, item); + prefix.extend_from_slice(&storage_prefix); prefix.extend_from_slice(suffix); let previous_key = prefix.clone(); let closure = |raw_key_without_prefix: &[u8], raw_value: &[u8]| { @@ -204,8 +208,9 @@ pub fn storage_key_iter_with_suffix< suffix: &[u8], ) -> PrefixIterator<(K, T)> { let mut prefix = Vec::new(); - prefix.extend_from_slice(&Twox128::hash(module)); - prefix.extend_from_slice(&Twox128::hash(item)); + let storage_prefix = storage_prefix(module, item); + + prefix.extend_from_slice(&storage_prefix); prefix.extend_from_slice(suffix); let previous_key = prefix.clone(); let closure = |raw_key_without_prefix: &[u8], raw_value: &[u8]| { @@ -225,8 +230,8 @@ pub fn have_storage_value(module: &[u8], item: &[u8], hash: &[u8]) -> bool { /// Get a particular value in storage by the `module`, the map's `item` name and the key `hash`. pub fn get_storage_value(module: &[u8], item: &[u8], hash: &[u8]) -> Option { let mut key = vec![0u8; 32 + hash.len()]; - key[0..16].copy_from_slice(&Twox128::hash(module)); - key[16..32].copy_from_slice(&Twox128::hash(item)); + let storage_prefix = storage_prefix(module, item); + key[0..32].copy_from_slice(&storage_prefix); key[32..].copy_from_slice(hash); frame_support::storage::unhashed::get::(&key) } @@ -234,8 +239,8 @@ pub fn get_storage_value(module: &[u8], item: &[u8], hash: &[ /// Take a particular value in storage by the `module`, the map's `item` name and the key `hash`. pub fn take_storage_value(module: &[u8], item: &[u8], hash: &[u8]) -> Option { let mut key = vec![0u8; 32 + hash.len()]; - key[0..16].copy_from_slice(&Twox128::hash(module)); - key[16..32].copy_from_slice(&Twox128::hash(item)); + let storage_prefix = storage_prefix(module, item); + key[0..32].copy_from_slice(&storage_prefix); key[32..].copy_from_slice(hash); frame_support::storage::unhashed::take::(&key) } @@ -243,8 +248,8 @@ pub fn take_storage_value(module: &[u8], item: &[u8], hash: & /// Put a particular value into storage by the `module`, the map's `item` name and the key `hash`. pub fn put_storage_value(module: &[u8], item: &[u8], hash: &[u8], value: T) { let mut key = vec![0u8; 32 + hash.len()]; - key[0..16].copy_from_slice(&Twox128::hash(module)); - key[16..32].copy_from_slice(&Twox128::hash(item)); + let storage_prefix = storage_prefix(module, item); + key[0..32].copy_from_slice(&storage_prefix); key[32..].copy_from_slice(hash); frame_support::storage::unhashed::put(&key, &value); } @@ -253,8 +258,8 @@ pub fn put_storage_value(module: &[u8], item: &[u8], hash: &[u8], val /// `hash`. pub fn remove_storage_prefix(module: &[u8], item: &[u8], hash: &[u8]) { let mut key = vec![0u8; 32 + hash.len()]; - key[0..16].copy_from_slice(&Twox128::hash(module)); - key[16..32].copy_from_slice(&Twox128::hash(item)); + let storage_prefix = storage_prefix(module, item); + key[0..32].copy_from_slice(&storage_prefix); key[32..].copy_from_slice(hash); frame_support::storage::unhashed::kill_prefix(&key, None); } @@ -293,13 +298,8 @@ pub fn move_storage_from_pallet( old_pallet_name: &[u8], new_pallet_name: &[u8], ) { - let mut new_prefix = Vec::new(); - new_prefix.extend_from_slice(&Twox128::hash(new_pallet_name)); - new_prefix.extend_from_slice(&Twox128::hash(storage_name)); - - let mut old_prefix = Vec::new(); - old_prefix.extend_from_slice(&Twox128::hash(old_pallet_name)); - old_prefix.extend_from_slice(&Twox128::hash(storage_name)); + let new_prefix = storage_prefix(new_pallet_name, storage_name); + let old_prefix = storage_prefix(old_pallet_name, storage_name); move_prefix(&old_prefix, &new_prefix); diff --git a/frame/support/src/storage/mod.rs b/frame/support/src/storage/mod.rs index ac2ddaa73c3b6..8cee9faf6e81d 100644 --- a/frame/support/src/storage/mod.rs +++ b/frame/support/src/storage/mod.rs @@ -18,7 +18,7 @@ //! Stuff to do with the runtime's storage. use crate::{ - hash::{ReversibleStorageHasher, StorageHasher, Twox128}, + hash::{ReversibleStorageHasher, StorageHasher}, storage::types::{ EncodeLikeTuple, HasKeyPrefix, HasReversibleKeyPrefix, KeyGenerator, ReversibleKeyGenerator, TupleToEncodedIter, @@ -1108,10 +1108,7 @@ pub trait StoragePrefixedMap { /// Final full prefix that prefixes all keys. fn final_prefix() -> [u8; 32] { - let mut final_key = [0u8; 32]; - final_key[0..16].copy_from_slice(&Twox128::hash(Self::module_prefix())); - final_key[16..32].copy_from_slice(&Twox128::hash(Self::storage_prefix())); - final_key + crate::storage::storage_prefix(Self::module_prefix(), Self::storage_prefix()) } /// Remove all value of the storage. @@ -1361,10 +1358,24 @@ where } } +/// Returns the storage prefix for a specific pallet name and storage name. +/// +/// The storage prefix is `concat(twox_128(pallet_name), twox_128(storage_name))`. +pub fn storage_prefix(pallet_name: &[u8], storage_name: &[u8]) -> [u8; 32] { + let pallet_hash = sp_io::hashing::twox_128(pallet_name); + let storage_hash = sp_io::hashing::twox_128(storage_name); + + let mut final_key = [0u8; 32]; + final_key[..16].copy_from_slice(&pallet_hash); + final_key[16..].copy_from_slice(&storage_hash); + + final_key +} + #[cfg(test)] mod test { use super::*; - use crate::{assert_ok, hash::Identity}; + use crate::{assert_ok, hash::Identity, Twox128}; use bounded_vec::BoundedVec; use core::convert::{TryFrom, TryInto}; use generator::StorageValue as _; diff --git a/frame/support/src/traits/hooks.rs b/frame/support/src/traits/hooks.rs index 965cce234288f..adba88e5acbf3 100644 --- a/frame/support/src/traits/hooks.rs +++ b/frame/support/src/traits/hooks.rs @@ -124,14 +124,7 @@ pub trait OnRuntimeUpgradeHelpersExt { /// them. See [`Self::set_temp_storage`] and [`Self::get_temp_storage`]. #[cfg(feature = "try-runtime")] fn storage_key(ident: &str) -> [u8; 32] { - let prefix = sp_io::hashing::twox_128(ON_RUNTIME_UPGRADE_PREFIX); - let ident = sp_io::hashing::twox_128(ident.as_bytes()); - - let mut final_key = [0u8; 32]; - final_key[..16].copy_from_slice(&prefix); - final_key[16..].copy_from_slice(&ident); - - final_key + crate::storage::storage_prefix(ON_RUNTIME_UPGRADE_PREFIX, ident.as_bytes()) } /// Get temporary storage data written by [`Self::set_temp_storage`]. diff --git a/frame/support/src/traits/metadata.rs b/frame/support/src/traits/metadata.rs index 8b1707855f7b0..e877f29e0a137 100644 --- a/frame/support/src/traits/metadata.rs +++ b/frame/support/src/traits/metadata.rs @@ -92,15 +92,7 @@ impl StorageVersion { /// See [`STORAGE_VERSION_STORAGE_KEY_POSTFIX`] on how this key is built. pub fn storage_key() -> [u8; 32] { let pallet_name = P::name(); - - let pallet_name = sp_io::hashing::twox_128(pallet_name.as_bytes()); - let postfix = sp_io::hashing::twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX); - - let mut final_key = [0u8; 32]; - final_key[..16].copy_from_slice(&pallet_name); - final_key[16..].copy_from_slice(&postfix); - - final_key + crate::storage::storage_prefix(pallet_name.as_bytes(), STORAGE_VERSION_STORAGE_KEY_POSTFIX) } /// Put this storage version for the given pallet into the storage. diff --git a/frame/support/test/tests/decl_storage.rs b/frame/support/test/tests/decl_storage.rs index 666dda49935ef..50c8387bca555 100644 --- a/frame/support/test/tests/decl_storage.rs +++ b/frame/support/test/tests/decl_storage.rs @@ -428,16 +428,10 @@ mod tests { #[test] fn storage_info() { use frame_support::{ - pallet_prelude::*, + storage::storage_prefix as prefix, traits::{StorageInfo, StorageInfoTrait}, - StorageHasher, - }; - let prefix = |pallet_name, storage_name| { - let mut res = [0u8; 32]; - res[0..16].copy_from_slice(&Twox128::hash(pallet_name)); - res[16..32].copy_from_slice(&Twox128::hash(storage_name)); - res }; + pretty_assertions::assert_eq!( >::storage_info(), vec![ @@ -717,15 +711,8 @@ mod test2 { #[test] fn storage_info() { use frame_support::{ - pallet_prelude::*, + storage::storage_prefix as prefix, traits::{StorageInfo, StorageInfoTrait}, - StorageHasher, - }; - let prefix = |pallet_name, storage_name| { - let mut res = [0u8; 32]; - res[0..16].copy_from_slice(&Twox128::hash(pallet_name)); - res[16..32].copy_from_slice(&Twox128::hash(storage_name)); - res }; pretty_assertions::assert_eq!( >::storage_info(), diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index 00af4d261c659..80bae000b6c77 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -934,14 +934,7 @@ fn migrate_from_pallet_version_to_storage_version() { const PALLET_VERSION_STORAGE_KEY_POSTFIX: &[u8] = b":__PALLET_VERSION__:"; fn pallet_version_key(name: &str) -> [u8; 32] { - let pallet_name = sp_io::hashing::twox_128(name.as_bytes()); - let postfix = sp_io::hashing::twox_128(PALLET_VERSION_STORAGE_KEY_POSTFIX); - - let mut final_key = [0u8; 32]; - final_key[..16].copy_from_slice(&pallet_name); - final_key[16..].copy_from_slice(&postfix); - - final_key + frame_support::storage::storage_prefix(name.as_bytes(), PALLET_VERSION_STORAGE_KEY_POSTFIX) } TestExternalities::default().execute_with(|| { @@ -1274,16 +1267,8 @@ fn test_pallet_info_access() { #[test] fn test_storage_info() { use frame_support::{ - pallet_prelude::*, + storage::storage_prefix as prefix, traits::{StorageInfo, StorageInfoTrait}, - StorageHasher, - }; - - let prefix = |pallet_name, storage_name| { - let mut res = [0u8; 32]; - res[0..16].copy_from_slice(&Twox128::hash(pallet_name)); - res[16..32].copy_from_slice(&Twox128::hash(storage_name)); - res }; assert_eq!( diff --git a/frame/support/test/tests/pallet_ui/hooks_invalid_item.stderr b/frame/support/test/tests/pallet_ui/hooks_invalid_item.stderr index f3677113dabeb..3d7303fafdcf5 100644 --- a/frame/support/test/tests/pallet_ui/hooks_invalid_item.stderr +++ b/frame/support/test/tests/pallet_ui/hooks_invalid_item.stderr @@ -5,9 +5,9 @@ error[E0107]: missing generics for trait `Hooks` | ^^^^^ expected 1 type argument | note: trait defined here, with 1 type parameter: `BlockNumber` - --> $DIR/hooks.rs:221:11 + --> $DIR/hooks.rs:214:11 | -221 | pub trait Hooks { +214 | pub trait Hooks { | ^^^^^ ----------- help: use angle brackets to add missing type argument |