Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Expose storage_prefix logic, and remove duplicate code #9621

Merged
6 commits merged into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions frame/support/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ impl<T: GetStorageVersion + PalletInfoAccess> 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(<T as PalletInfoAccess>::name()));
Expand Down
49 changes: 14 additions & 35 deletions frame/support/src/storage/generator/double_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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());

Expand Down Expand Up @@ -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());

Expand Down
39 changes: 11 additions & 28 deletions frame/support/src/storage/generator/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
45 changes: 12 additions & 33 deletions frame/support/src/storage/generator/nmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
//! be compromised.

use crate::{
hash::{StorageHasher, Twox128},
storage::{
self,
self, storage_prefix,
types::{
EncodeLikeTuple, HasKeyPrefix, HasReversibleKeyPrefix, KeyGenerator,
ReversibleKeyGenerator, TupleToEncodedIter,
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions frame/support/src/storage/generator/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// limitations under the License.

use crate::{
hash::{StorageHasher, Twox128},
storage::{self, unhashed, StorageAppend},
Never,
};
Expand Down Expand Up @@ -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())
}
}

Expand Down
Loading