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:
Shawn Tabrizi
2021-08-25 15:16:47 -04:00
committed by GitHub
parent 531fd70e22
commit 7c3890c652
12 changed files with 89 additions and 191 deletions
+17 -6
View File
@@ -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<Value: FullCodec> {
/// 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 _;