Skip to content

Commit 4480a04

Browse files
gui1117bkchr
authored andcommitted
Refactor StorageInstance trait to be usable more easily (paritytech#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 <[email protected]> Co-authored-by: Bastian Köcher <[email protected]>
1 parent 55f0420 commit 4480a04

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

frame/support/src/storage/types/double_map.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use sp_std::vec::Vec;
3434
///
3535
/// Each value is stored at:
3636
/// ```nocompile
37-
/// Twox128(<Prefix::Pallet as PalletInfo>::name())
37+
/// Twox128(Prefix::pallet_prefix())
3838
/// ++ Twox128(Prefix::STORAGE_PREFIX)
3939
/// ++ Hasher1(encode(key1))
4040
/// ++ Hasher2(encode(key2))
@@ -68,8 +68,7 @@ where
6868
type Hasher1 = Hasher1;
6969
type Hasher2 = Hasher2;
7070
fn module_prefix() -> &'static [u8] {
71-
<Prefix::PalletInfo as crate::traits::PalletInfo>::name::<Prefix::Pallet>()
72-
.expect("Every active pallet has a name in the runtime; qed").as_bytes()
71+
Prefix::pallet_prefix().as_bytes()
7372
}
7473
fn storage_prefix() -> &'static [u8] {
7574
Prefix::STORAGE_PREFIX.as_bytes()
@@ -415,8 +414,7 @@ mod test {
415414

416415
struct Prefix;
417416
impl StorageInstance for Prefix {
418-
type Pallet = ();
419-
type PalletInfo = ();
417+
fn pallet_prefix() -> &'static str { "test" }
420418
const STORAGE_PREFIX: &'static str = "foo";
421419
}
422420

frame/support/src/storage/types/map.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use sp_std::prelude::*;
3333
///
3434
/// Each value is stored at:
3535
/// ```nocompile
36-
/// Twox128(<Prefix::Pallet as PalletInfo>::name())
36+
/// Twox128(Prefix::pallet_prefix())
3737
/// ++ Twox128(Prefix::STORAGE_PREFIX)
3838
/// ++ Hasher1(encode(key))
3939
/// ```
@@ -60,8 +60,7 @@ where
6060
type Query = QueryKind::Query;
6161
type Hasher = Hasher;
6262
fn module_prefix() -> &'static [u8] {
63-
<Prefix::PalletInfo as crate::traits::PalletInfo>::name::<Prefix::Pallet>()
64-
.expect("Every active pallet has a name in the runtime; qed").as_bytes()
63+
Prefix::pallet_prefix().as_bytes()
6564
}
6665
fn storage_prefix() -> &'static [u8] {
6766
Prefix::STORAGE_PREFIX.as_bytes()
@@ -318,8 +317,7 @@ mod test {
318317

319318
struct Prefix;
320319
impl StorageInstance for Prefix {
321-
type Pallet = ();
322-
type PalletInfo = ();
320+
fn pallet_prefix() -> &'static str { "test" }
323321
const STORAGE_PREFIX: &'static str = "foo";
324322
}
325323

frame/support/src/storage/types/value.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use frame_metadata::{DefaultByteGetter, StorageEntryModifier};
3131
///
3232
/// Each value is stored at:
3333
/// ```nocompile
34-
/// Twox128(<Prefix::Pallet as PalletInfo>::name()) ++ Twox128(Prefix::STORAGE_PREFIX)
34+
/// Twox128(Prefix::pallet_prefix()) ++ Twox128(Prefix::STORAGE_PREFIX)
3535
/// ```
3636
pub struct StorageValue<Prefix, Value, QueryKind=OptionQuery, OnEmpty=GetDefault>(
3737
core::marker::PhantomData<(Prefix, Value, QueryKind, OnEmpty)>
@@ -47,8 +47,7 @@ where
4747
{
4848
type Query = QueryKind::Query;
4949
fn module_prefix() -> &'static [u8] {
50-
<Prefix::PalletInfo as crate::traits::PalletInfo>::name::<Prefix::Pallet>()
51-
.expect("Every active pallet has a name in the runtime; qed").as_bytes()
50+
Prefix::pallet_prefix().as_bytes()
5251
}
5352
fn storage_prefix() -> &'static [u8] {
5453
Prefix::STORAGE_PREFIX.as_bytes()
@@ -201,8 +200,7 @@ mod test {
201200

202201
struct Prefix;
203202
impl StorageInstance for Prefix {
204-
type Pallet = ();
205-
type PalletInfo = ();
203+
fn pallet_prefix() -> &'static str { "test" }
206204
const STORAGE_PREFIX: &'static str = "foo";
207205
}
208206

frame/support/src/traits.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,13 +1731,19 @@ pub trait Instance: 'static {
17311731
const PREFIX: &'static str;
17321732
}
17331733

1734-
/// An instance of a storage.
1734+
/// An instance of a storage in a pallet.
17351735
///
1736-
/// It is required the the couple `(PalletInfo::name<Pallet>(), STORAGE_PREFIX)` is unique.
1737-
/// Any storage with same couple will collide.
1736+
/// Define an instance for an individual storage inside a pallet.
1737+
/// The pallet prefix is used to isolate the storage between pallets, and the storage prefix is
1738+
/// used to isolate storages inside a pallet.
1739+
///
1740+
/// NOTE: These information can be used to define storages in pallet such as a `StorageMap` which
1741+
/// can use keys after `twox_128(pallet_prefix())++twox_128(STORAGE_PREFIX)`
17381742
pub trait StorageInstance {
1739-
type Pallet: 'static;
1740-
type PalletInfo: PalletInfo;
1743+
/// Prefix of a pallet to isolate it from other pallets.
1744+
fn pallet_prefix() -> &'static str;
1745+
1746+
/// Prefix given to a storage to isolate from other storages in the pallet.
17411747
const STORAGE_PREFIX: &'static str;
17421748
}
17431749

0 commit comments

Comments
 (0)