mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-06 02:07:25 +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:
@@ -33,7 +33,7 @@ use sp_std::prelude::*;
|
||||
///
|
||||
/// Thus value for (key1, key2) is stored at:
|
||||
/// ```nocompile
|
||||
/// Twox128(module_prefix) ++ Twox128(storage_prefix) ++ Hasher1(encode(key1)) ++ Hasher2(encode(key2))
|
||||
/// Twox128(pallet_prefix) ++ Twox128(storage_prefix) ++ Hasher1(encode(key1)) ++ Hasher2(encode(key2))
|
||||
/// ```
|
||||
///
|
||||
/// # Warning
|
||||
@@ -53,18 +53,15 @@ pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
|
||||
/// Hasher for the second key.
|
||||
type Hasher2: StorageHasher;
|
||||
|
||||
/// Module prefix. Used for generating final key.
|
||||
fn module_prefix() -> &'static [u8];
|
||||
/// Pallet prefix. Used for generating final key.
|
||||
fn pallet_prefix() -> &'static [u8];
|
||||
|
||||
/// Storage prefix. Used for generating final key.
|
||||
fn storage_prefix() -> &'static [u8];
|
||||
|
||||
/// The full prefix; just the hash of `module_prefix` concatenated to the hash of
|
||||
/// The full prefix; just the hash of `pallet_prefix` concatenated to the hash of
|
||||
/// `storage_prefix`.
|
||||
fn prefix_hash() -> Vec<u8> {
|
||||
let result = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
result.to_vec()
|
||||
}
|
||||
fn prefix_hash() -> [u8; 32];
|
||||
|
||||
/// Convert an optional value retrieved from storage to the type queried.
|
||||
fn from_optional_value_to_query(v: Option<V>) -> Self::Query;
|
||||
@@ -77,7 +74,7 @@ pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
|
||||
where
|
||||
KArg1: EncodeLike<K1>,
|
||||
{
|
||||
let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
let storage_prefix = storage_prefix(Self::pallet_prefix(), Self::storage_prefix());
|
||||
let key_hashed = k1.using_encoded(Self::Hasher1::hash);
|
||||
|
||||
let mut final_key = Vec::with_capacity(storage_prefix.len() + key_hashed.as_ref().len());
|
||||
@@ -94,7 +91,7 @@ pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
|
||||
KArg1: EncodeLike<K1>,
|
||||
KArg2: EncodeLike<K2>,
|
||||
{
|
||||
let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
let storage_prefix = storage_prefix(Self::pallet_prefix(), Self::storage_prefix());
|
||||
let key1_hashed = k1.using_encoded(Self::Hasher1::hash);
|
||||
let key2_hashed = k2.using_encoded(Self::Hasher2::hash);
|
||||
|
||||
@@ -334,7 +331,7 @@ where
|
||||
key2: KeyArg2,
|
||||
) -> Option<V> {
|
||||
let old_key = {
|
||||
let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
let storage_prefix = storage_prefix(Self::pallet_prefix(), Self::storage_prefix());
|
||||
|
||||
let key1_hashed = key1.using_encoded(OldHasher1::hash);
|
||||
let key2_hashed = key2.using_encoded(OldHasher2::hash);
|
||||
@@ -419,7 +416,7 @@ where
|
||||
}
|
||||
|
||||
fn iter() -> Self::Iterator {
|
||||
let prefix = G::prefix_hash();
|
||||
let prefix = G::prefix_hash().to_vec();
|
||||
Self::Iterator {
|
||||
prefix: prefix.clone(),
|
||||
previous_key: prefix,
|
||||
@@ -442,7 +439,7 @@ where
|
||||
}
|
||||
|
||||
fn iter_keys() -> Self::FullKeyIterator {
|
||||
let prefix = G::prefix_hash();
|
||||
let prefix = G::prefix_hash().to_vec();
|
||||
Self::FullKeyIterator {
|
||||
prefix: prefix.clone(),
|
||||
previous_key: prefix,
|
||||
@@ -470,7 +467,7 @@ where
|
||||
}
|
||||
|
||||
fn translate<O: Decode, F: FnMut(K1, K2, O) -> Option<V>>(mut f: F) {
|
||||
let prefix = G::prefix_hash();
|
||||
let prefix = G::prefix_hash().to_vec();
|
||||
let mut previous_key = prefix.clone();
|
||||
while let Some(next) =
|
||||
sp_io::storage::next_key(&previous_key).filter(|n| n.starts_with(&prefix))
|
||||
@@ -561,7 +558,7 @@ mod test_iterators {
|
||||
type DoubleMap = self::frame_system::DoubleMap<Runtime>;
|
||||
|
||||
// All map iterator
|
||||
let prefix = DoubleMap::prefix_hash();
|
||||
let prefix = DoubleMap::prefix_hash().to_vec();
|
||||
|
||||
unhashed::put(&key_before_prefix(prefix.clone()), &1u64);
|
||||
unhashed::put(&key_after_prefix(prefix.clone()), &1u64);
|
||||
@@ -621,7 +618,7 @@ mod test_iterators {
|
||||
assert_eq!(unhashed::get(&key_after_prefix(prefix.clone())), Some(1u64));
|
||||
|
||||
// Translate
|
||||
let prefix = DoubleMap::prefix_hash();
|
||||
let prefix = DoubleMap::prefix_hash().to_vec();
|
||||
|
||||
unhashed::put(&key_before_prefix(prefix.clone()), &1u64);
|
||||
unhashed::put(&key_after_prefix(prefix.clone()), &1u64);
|
||||
|
||||
@@ -28,7 +28,7 @@ use sp_std::prelude::*;
|
||||
///
|
||||
/// By default each key value is stored at:
|
||||
/// ```nocompile
|
||||
/// Twox128(module_prefix) ++ Twox128(storage_prefix) ++ Hasher(encode(key))
|
||||
/// Twox128(pallet_prefix) ++ Twox128(storage_prefix) ++ Hasher(encode(key))
|
||||
/// ```
|
||||
///
|
||||
/// # Warning
|
||||
@@ -42,18 +42,15 @@ pub trait StorageMap<K: FullEncode, V: FullCodec> {
|
||||
/// Hasher. Used for generating final key.
|
||||
type Hasher: StorageHasher;
|
||||
|
||||
/// Module prefix. Used for generating final key.
|
||||
fn module_prefix() -> &'static [u8];
|
||||
/// Pallet prefix. Used for generating final key.
|
||||
fn pallet_prefix() -> &'static [u8];
|
||||
|
||||
/// Storage prefix. Used for generating final key.
|
||||
fn storage_prefix() -> &'static [u8];
|
||||
|
||||
/// The full prefix; just the hash of `module_prefix` concatenated to the hash of
|
||||
/// The full prefix; just the hash of `pallet_prefix` concatenated to the hash of
|
||||
/// `storage_prefix`.
|
||||
fn prefix_hash() -> Vec<u8> {
|
||||
let result = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
result.to_vec()
|
||||
}
|
||||
fn prefix_hash() -> [u8; 32];
|
||||
|
||||
/// Convert an optional value retrieved from storage to the type queried.
|
||||
fn from_optional_value_to_query(v: Option<V>) -> Self::Query;
|
||||
@@ -66,7 +63,7 @@ pub trait StorageMap<K: FullEncode, V: FullCodec> {
|
||||
where
|
||||
KeyArg: EncodeLike<K>,
|
||||
{
|
||||
let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
let storage_prefix = storage_prefix(Self::pallet_prefix(), Self::storage_prefix());
|
||||
let key_hashed = key.using_encoded(Self::Hasher::hash);
|
||||
|
||||
let mut final_key = Vec::with_capacity(storage_prefix.len() + key_hashed.as_ref().len());
|
||||
@@ -128,7 +125,7 @@ where
|
||||
|
||||
/// Enumerate all elements in the map.
|
||||
fn iter() -> Self::Iterator {
|
||||
let prefix = G::prefix_hash();
|
||||
let prefix = G::prefix_hash().to_vec();
|
||||
PrefixIterator {
|
||||
prefix: prefix.clone(),
|
||||
previous_key: prefix,
|
||||
@@ -150,7 +147,7 @@ where
|
||||
|
||||
/// Enumerate all keys in the map.
|
||||
fn iter_keys() -> Self::KeyIterator {
|
||||
let prefix = G::prefix_hash();
|
||||
let prefix = G::prefix_hash().to_vec();
|
||||
KeyPrefixIterator {
|
||||
prefix: prefix.clone(),
|
||||
previous_key: prefix,
|
||||
@@ -190,7 +187,7 @@ where
|
||||
previous_key: Option<Vec<u8>>,
|
||||
mut f: F,
|
||||
) -> Option<Vec<u8>> {
|
||||
let prefix = G::prefix_hash();
|
||||
let prefix = G::prefix_hash().to_vec();
|
||||
let previous_key = previous_key.unwrap_or_else(|| prefix.clone());
|
||||
|
||||
let current_key =
|
||||
@@ -339,7 +336,7 @@ impl<K: FullEncode, V: FullCodec, G: StorageMap<K, V>> storage::StorageMap<K, V>
|
||||
|
||||
fn migrate_key<OldHasher: StorageHasher, KeyArg: EncodeLike<K>>(key: KeyArg) -> Option<V> {
|
||||
let old_key = {
|
||||
let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
let storage_prefix = storage_prefix(Self::pallet_prefix(), Self::storage_prefix());
|
||||
let key_hashed = key.using_encoded(OldHasher::hash);
|
||||
|
||||
let mut final_key =
|
||||
@@ -398,7 +395,7 @@ mod test_iterators {
|
||||
type Map = self::frame_system::Map<Runtime>;
|
||||
|
||||
// All map iterator
|
||||
let prefix = Map::prefix_hash();
|
||||
let prefix = Map::prefix_hash().to_vec();
|
||||
|
||||
unhashed::put(&key_before_prefix(prefix.clone()), &1u64);
|
||||
unhashed::put(&key_after_prefix(prefix.clone()), &1u64);
|
||||
@@ -420,7 +417,7 @@ mod test_iterators {
|
||||
assert_eq!(unhashed::get(&key_after_prefix(prefix.clone())), Some(1u64));
|
||||
|
||||
// Translate
|
||||
let prefix = Map::prefix_hash();
|
||||
let prefix = Map::prefix_hash().to_vec();
|
||||
|
||||
unhashed::put(&key_before_prefix(prefix.clone()), &1u64);
|
||||
unhashed::put(&key_after_prefix(prefix.clone()), &1u64);
|
||||
|
||||
@@ -61,18 +61,15 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
|
||||
/// The type that get/take returns.
|
||||
type Query;
|
||||
|
||||
/// Module prefix. Used for generating final key.
|
||||
fn module_prefix() -> &'static [u8];
|
||||
/// Pallet prefix. Used for generating final key.
|
||||
fn pallet_prefix() -> &'static [u8];
|
||||
|
||||
/// Storage prefix. Used for generating final key.
|
||||
fn storage_prefix() -> &'static [u8];
|
||||
|
||||
/// The full prefix; just the hash of `module_prefix` concatenated to the hash of
|
||||
/// The full prefix; just the hash of `pallet_prefix` concatenated to the hash of
|
||||
/// `storage_prefix`.
|
||||
fn prefix_hash() -> Vec<u8> {
|
||||
let result = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
result.to_vec()
|
||||
}
|
||||
fn prefix_hash() -> [u8; 32];
|
||||
|
||||
/// Convert an optional value retrieved from storage to the type queried.
|
||||
fn from_optional_value_to_query(v: Option<V>) -> Self::Query;
|
||||
@@ -85,7 +82,7 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
|
||||
where
|
||||
K: HasKeyPrefix<KP>,
|
||||
{
|
||||
let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
let storage_prefix = storage_prefix(Self::pallet_prefix(), Self::storage_prefix());
|
||||
let key_hashed = <K as HasKeyPrefix<KP>>::partial_key(key);
|
||||
|
||||
let mut final_key = Vec::with_capacity(storage_prefix.len() + key_hashed.len());
|
||||
@@ -102,7 +99,7 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
|
||||
KG: KeyGenerator,
|
||||
KArg: EncodeLikeTuple<KG::KArg> + TupleToEncodedIter,
|
||||
{
|
||||
let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
let storage_prefix = storage_prefix(Self::pallet_prefix(), Self::storage_prefix());
|
||||
let key_hashed = KG::final_key(key);
|
||||
|
||||
let mut final_key = Vec::with_capacity(storage_prefix.len() + key_hashed.len());
|
||||
@@ -299,7 +296,7 @@ where
|
||||
KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter,
|
||||
{
|
||||
let old_key = {
|
||||
let storage_prefix = storage_prefix(Self::module_prefix(), Self::storage_prefix());
|
||||
let storage_prefix = storage_prefix(Self::pallet_prefix(), Self::storage_prefix());
|
||||
let key_hashed = K::migrate_key(&key, hash_fns);
|
||||
|
||||
let mut final_key = Vec::with_capacity(storage_prefix.len() + key_hashed.len());
|
||||
@@ -386,11 +383,11 @@ impl<K: ReversibleKeyGenerator, V: FullCodec, G: StorageNMap<K, V>>
|
||||
}
|
||||
|
||||
fn iter() -> Self::Iterator {
|
||||
Self::iter_from(G::prefix_hash())
|
||||
Self::iter_from(G::prefix_hash().to_vec())
|
||||
}
|
||||
|
||||
fn iter_from(starting_raw_key: Vec<u8>) -> Self::Iterator {
|
||||
let prefix = G::prefix_hash();
|
||||
let prefix = G::prefix_hash().to_vec();
|
||||
Self::Iterator {
|
||||
prefix,
|
||||
previous_key: starting_raw_key,
|
||||
@@ -404,11 +401,11 @@ impl<K: ReversibleKeyGenerator, V: FullCodec, G: StorageNMap<K, V>>
|
||||
}
|
||||
|
||||
fn iter_keys() -> Self::KeyIterator {
|
||||
Self::iter_keys_from(G::prefix_hash())
|
||||
Self::iter_keys_from(G::prefix_hash().to_vec())
|
||||
}
|
||||
|
||||
fn iter_keys_from(starting_raw_key: Vec<u8>) -> Self::KeyIterator {
|
||||
let prefix = G::prefix_hash();
|
||||
let prefix = G::prefix_hash().to_vec();
|
||||
Self::KeyIterator {
|
||||
prefix,
|
||||
previous_key: starting_raw_key,
|
||||
@@ -427,7 +424,7 @@ impl<K: ReversibleKeyGenerator, V: FullCodec, G: StorageNMap<K, V>>
|
||||
}
|
||||
|
||||
fn translate<O: Decode, F: FnMut(K::Key, O) -> Option<V>>(mut f: F) {
|
||||
let prefix = G::prefix_hash();
|
||||
let prefix = G::prefix_hash().to_vec();
|
||||
let mut previous_key = prefix.clone();
|
||||
while let Some(next) =
|
||||
sp_io::storage::next_key(&previous_key).filter(|n| n.starts_with(&prefix))
|
||||
@@ -537,7 +534,7 @@ mod test_iterators {
|
||||
type NMap = self::frame_system::NMap<Runtime>;
|
||||
|
||||
// All map iterator
|
||||
let prefix = NMap::prefix_hash();
|
||||
let prefix = NMap::prefix_hash().to_vec();
|
||||
|
||||
unhashed::put(&key_before_prefix(prefix.clone()), &1u64);
|
||||
unhashed::put(&key_after_prefix(prefix.clone()), &1u64);
|
||||
@@ -594,7 +591,7 @@ mod test_iterators {
|
||||
assert_eq!(unhashed::get(&key_after_prefix(prefix.clone())), Some(1u64));
|
||||
|
||||
// Translate
|
||||
let prefix = NMap::prefix_hash();
|
||||
let prefix = NMap::prefix_hash().to_vec();
|
||||
|
||||
unhashed::put(&key_before_prefix(prefix.clone()), &1u64);
|
||||
unhashed::put(&key_after_prefix(prefix.clone()), &1u64);
|
||||
|
||||
@@ -25,14 +25,14 @@ use codec::{Decode, Encode, EncodeLike, FullCodec};
|
||||
///
|
||||
/// By default value is stored at:
|
||||
/// ```nocompile
|
||||
/// Twox128(module_prefix) ++ Twox128(storage_prefix)
|
||||
/// Twox128(pallet_prefix) ++ Twox128(storage_prefix)
|
||||
/// ```
|
||||
pub trait StorageValue<T: FullCodec> {
|
||||
/// The type that get/take returns.
|
||||
type Query;
|
||||
|
||||
/// Module prefix. Used for generating final key.
|
||||
fn module_prefix() -> &'static [u8];
|
||||
/// Pallet prefix. Used for generating final key.
|
||||
fn pallet_prefix() -> &'static [u8];
|
||||
|
||||
/// Storage prefix. Used for generating final key.
|
||||
fn storage_prefix() -> &'static [u8];
|
||||
@@ -44,9 +44,7 @@ pub trait StorageValue<T: FullCodec> {
|
||||
fn from_query_to_optional_value(v: Self::Query) -> Option<T>;
|
||||
|
||||
/// Generate the full key used in top storage.
|
||||
fn storage_value_final_key() -> [u8; 32] {
|
||||
crate::storage::storage_prefix(Self::module_prefix(), Self::storage_prefix())
|
||||
}
|
||||
fn storage_value_final_key() -> [u8; 32];
|
||||
}
|
||||
|
||||
impl<T: FullCodec, G: StorageValue<T>> storage::StorageValue<T> for G {
|
||||
@@ -97,10 +95,6 @@ impl<T: FullCodec, G: StorageValue<T>> storage::StorageValue<T> for G {
|
||||
}
|
||||
}
|
||||
|
||||
fn kill() {
|
||||
unhashed::kill(&Self::storage_value_final_key())
|
||||
}
|
||||
|
||||
fn mutate<R, F: FnOnce(&mut G::Query) -> R>(f: F) -> R {
|
||||
Self::try_mutate(|v| Ok::<R, Never>(f(v))).expect("`Never` can not be constructed; qed")
|
||||
}
|
||||
@@ -142,6 +136,10 @@ impl<T: FullCodec, G: StorageValue<T>> storage::StorageValue<T> for G {
|
||||
ret
|
||||
}
|
||||
|
||||
fn kill() {
|
||||
unhashed::kill(&Self::storage_value_final_key())
|
||||
}
|
||||
|
||||
fn take() -> G::Query {
|
||||
let key = Self::storage_value_final_key();
|
||||
let value = unhashed::get(&key);
|
||||
|
||||
Reference in New Issue
Block a user