Define StorageDoubleMapKeyProvider

Signed-off-by: Serban Iorga <serban@parity.io>
This commit is contained in:
Serban Iorga
2022-07-26 22:50:04 +03:00
committed by Bastian Köcher
parent f9af3f2d6e
commit cd36d1e15f
5 changed files with 100 additions and 65 deletions
+43 -32
View File
@@ -246,38 +246,6 @@ pub fn storage_map_final_key<H: StorageHasher>(
StorageKey(final_key)
}
/// This is a copy of the
/// `frame_support::storage::generator::StorageDoubleMap::storage_double_map_final_key` for maps
/// based on selected hashers.
///
/// We're using it because to call `storage_double_map_final_key` directly, we need access to the
/// runtime and pallet instance, which (sometimes) is impossible.
pub fn storage_double_map_final_key<H1: StorageHasher, H2: StorageHasher>(
pallet_prefix: &str,
map_name: &str,
key1: &[u8],
key2: &[u8],
) -> StorageKey {
let key1_hashed = H1::hash(key1);
let key2_hashed = H2::hash(key2);
let pallet_prefix_hashed = frame_support::Twox128::hash(pallet_prefix.as_bytes());
let storage_prefix_hashed = frame_support::Twox128::hash(map_name.as_bytes());
let mut final_key = Vec::with_capacity(
pallet_prefix_hashed.len() +
storage_prefix_hashed.len() +
key1_hashed.as_ref().len() +
key2_hashed.as_ref().len(),
);
final_key.extend_from_slice(&pallet_prefix_hashed[..]);
final_key.extend_from_slice(&storage_prefix_hashed[..]);
final_key.extend_from_slice(key1_hashed.as_ref());
final_key.extend_from_slice(key2_hashed.as_ref());
StorageKey(final_key)
}
/// This is how a storage key of storage parameter (`parameter_types! { storage Param: bool = false;
/// }`) is computed.
///
@@ -304,6 +272,49 @@ pub fn storage_value_key(pallet_prefix: &str, value_name: &str) -> StorageKey {
StorageKey(final_key)
}
/// Can be use to access the runtime storage key of a `StorageDoubleMap`.
pub trait StorageDoubleMapKeyProvider {
// The name of the variable that holds the `StorageDoubleMap`
const MAP_NAME: &'static str;
// The same as `StorageDoubleMap::Hasher1`
type Hasher1: StorageHasher;
// The same as `StorageDoubleMap::Key1`
type Key1: FullCodec;
// The same as `StorageDoubleMap::Hasher2`
type Hasher2: StorageHasher;
// The same as `StorageDoubleMap::Key2`
type Key2: FullCodec;
// The same as `StorageDoubleMap::Value`
type Value: FullCodec;
/// This is a copy of the
/// `frame_support::storage::generator::StorageDoubleMap::storage_double_map_final_key`.
///
/// We're using it because to call `storage_double_map_final_key` directly, we need access
/// to the runtime and pallet instance, which (sometimes) is impossible.
fn final_key(pallet_prefix: &str, key1: &Self::Key1, key2: &Self::Key2) -> StorageKey {
let key1_hashed = Self::Hasher1::hash(&key1.encode());
let key2_hashed = Self::Hasher2::hash(&key2.encode());
let pallet_prefix_hashed = frame_support::Twox128::hash(pallet_prefix.as_bytes());
let storage_prefix_hashed = frame_support::Twox128::hash(Self::MAP_NAME.as_bytes());
let mut final_key = Vec::with_capacity(
pallet_prefix_hashed.len() +
storage_prefix_hashed.len() +
key1_hashed.as_ref().len() +
key2_hashed.as_ref().len(),
);
final_key.extend_from_slice(&pallet_prefix_hashed[..]);
final_key.extend_from_slice(&storage_prefix_hashed[..]);
final_key.extend_from_slice(key1_hashed.as_ref());
final_key.extend_from_slice(key2_hashed.as_ref());
StorageKey(final_key)
}
}
/// Error generated by the `OwnedBridgeModule` trait.
#[derive(Encode, Decode, TypeInfo, PalletError)]
pub enum OwnedBridgeModuleError {