mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-25 10:41:08 +00:00
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 <gui.thiolliere@gmail.com> Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
@@ -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<K1: FullEncode, K2: FullEncode, V: FullCodec> {
|
||||
/// The full prefix; just the hash of `module_prefix` concatenated to the hash of
|
||||
/// `storage_prefix`.
|
||||
fn prefix_hash() -> Vec<u8> {
|
||||
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<K1: FullEncode, K2: FullEncode, V: FullCodec> {
|
||||
where
|
||||
KArg1: EncodeLike<K1>,
|
||||
{
|
||||
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<K1: FullEncode, K2: FullEncode, V: FullCodec> {
|
||||
KArg1: EncodeLike<K1>,
|
||||
KArg2: EncodeLike<K2>,
|
||||
{
|
||||
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<V> {
|
||||
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());
|
||||
|
||||
|
||||
@@ -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<K: FullEncode, V: FullCodec> {
|
||||
/// The full prefix; just the hash of `module_prefix` concatenated to the hash of
|
||||
/// `storage_prefix`.
|
||||
fn prefix_hash() -> Vec<u8> {
|
||||
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<K: FullEncode, V: FullCodec> {
|
||||
where
|
||||
KeyArg: EncodeLike<K>,
|
||||
{
|
||||
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<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 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
|
||||
|
||||
@@ -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<K: KeyGenerator, V: FullCodec> {
|
||||
/// The full prefix; just the hash of `module_prefix` concatenated to the hash of
|
||||
/// `storage_prefix`.
|
||||
fn prefix_hash() -> Vec<u8> {
|
||||
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<K: KeyGenerator, V: FullCodec> {
|
||||
where
|
||||
K: HasKeyPrefix<KP>,
|
||||
{
|
||||
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 as HasKeyPrefix<KP>>::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<K: KeyGenerator, V: FullCodec> {
|
||||
KG: KeyGenerator,
|
||||
KArg: EncodeLikeTuple<KG::KArg> + 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<K::KArg> + 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
|
||||
|
||||
@@ -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<T: FullCodec> {
|
||||
|
||||
/// 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())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user