Refactor StorageInstance trait to be usable more easily (#7659)

* refactor StorageInstance to be usable without macros

* better description

* update types  doc

* Update frame/support/src/traits.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Guillaume Thiolliere
2020-12-03 11:51:08 +01:00
committed by GitHub
parent b149b1fdc0
commit 7e83b7a7f2
4 changed files with 20 additions and 20 deletions
@@ -34,7 +34,7 @@ use sp_std::vec::Vec;
///
/// Each value is stored at:
/// ```nocompile
/// Twox128(<Prefix::Pallet as PalletInfo>::name())
/// Twox128(Prefix::pallet_prefix())
/// ++ Twox128(Prefix::STORAGE_PREFIX)
/// ++ Hasher1(encode(key1))
/// ++ Hasher2(encode(key2))
@@ -68,8 +68,7 @@ where
type Hasher1 = Hasher1;
type Hasher2 = Hasher2;
fn module_prefix() -> &'static [u8] {
<Prefix::PalletInfo as crate::traits::PalletInfo>::name::<Prefix::Pallet>()
.expect("Every active pallet has a name in the runtime; qed").as_bytes()
Prefix::pallet_prefix().as_bytes()
}
fn storage_prefix() -> &'static [u8] {
Prefix::STORAGE_PREFIX.as_bytes()
@@ -415,8 +414,7 @@ mod test {
struct Prefix;
impl StorageInstance for Prefix {
type Pallet = ();
type PalletInfo = ();
fn pallet_prefix() -> &'static str { "test" }
const STORAGE_PREFIX: &'static str = "foo";
}
@@ -33,7 +33,7 @@ use sp_std::prelude::*;
///
/// Each value is stored at:
/// ```nocompile
/// Twox128(<Prefix::Pallet as PalletInfo>::name())
/// Twox128(Prefix::pallet_prefix())
/// ++ Twox128(Prefix::STORAGE_PREFIX)
/// ++ Hasher1(encode(key))
/// ```
@@ -60,8 +60,7 @@ where
type Query = QueryKind::Query;
type Hasher = Hasher;
fn module_prefix() -> &'static [u8] {
<Prefix::PalletInfo as crate::traits::PalletInfo>::name::<Prefix::Pallet>()
.expect("Every active pallet has a name in the runtime; qed").as_bytes()
Prefix::pallet_prefix().as_bytes()
}
fn storage_prefix() -> &'static [u8] {
Prefix::STORAGE_PREFIX.as_bytes()
@@ -318,8 +317,7 @@ mod test {
struct Prefix;
impl StorageInstance for Prefix {
type Pallet = ();
type PalletInfo = ();
fn pallet_prefix() -> &'static str { "test" }
const STORAGE_PREFIX: &'static str = "foo";
}
@@ -31,7 +31,7 @@ use frame_metadata::{DefaultByteGetter, StorageEntryModifier};
///
/// Each value is stored at:
/// ```nocompile
/// Twox128(<Prefix::Pallet as PalletInfo>::name()) ++ Twox128(Prefix::STORAGE_PREFIX)
/// Twox128(Prefix::pallet_prefix()) ++ Twox128(Prefix::STORAGE_PREFIX)
/// ```
pub struct StorageValue<Prefix, Value, QueryKind=OptionQuery, OnEmpty=GetDefault>(
core::marker::PhantomData<(Prefix, Value, QueryKind, OnEmpty)>
@@ -47,8 +47,7 @@ where
{
type Query = QueryKind::Query;
fn module_prefix() -> &'static [u8] {
<Prefix::PalletInfo as crate::traits::PalletInfo>::name::<Prefix::Pallet>()
.expect("Every active pallet has a name in the runtime; qed").as_bytes()
Prefix::pallet_prefix().as_bytes()
}
fn storage_prefix() -> &'static [u8] {
Prefix::STORAGE_PREFIX.as_bytes()
@@ -201,8 +200,7 @@ mod test {
struct Prefix;
impl StorageInstance for Prefix {
type Pallet = ();
type PalletInfo = ();
fn pallet_prefix() -> &'static str { "test" }
const STORAGE_PREFIX: &'static str = "foo";
}
+11 -5
View File
@@ -1731,13 +1731,19 @@ pub trait Instance: 'static {
const PREFIX: &'static str;
}
/// An instance of a storage.
/// An instance of a storage in a pallet.
///
/// It is required the the couple `(PalletInfo::name<Pallet>(), STORAGE_PREFIX)` is unique.
/// Any storage with same couple will collide.
/// Define an instance for an individual storage inside a pallet.
/// The pallet prefix is used to isolate the storage between pallets, and the storage prefix is
/// used to isolate storages inside a pallet.
///
/// NOTE: These information can be used to define storages in pallet such as a `StorageMap` which
/// can use keys after `twox_128(pallet_prefix())++twox_128(STORAGE_PREFIX)`
pub trait StorageInstance {
type Pallet: 'static;
type PalletInfo: PalletInfo;
/// Prefix of a pallet to isolate it from other pallets.
fn pallet_prefix() -> &'static str;
/// Prefix given to a storage to isolate from other storages in the pallet.
const STORAGE_PREFIX: &'static str;
}