Skip to content

Commit

Permalink
Refactor StorageInstance trait to be usable more easily (paritytech…
Browse files Browse the repository at this point in the history
…#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>
  • Loading branch information
2 people authored and darkfriend77 committed Jan 11, 2021
1 parent 55f0420 commit 4480a04
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
8 changes: 3 additions & 5 deletions frame/support/src/storage/types/double_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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";
}

Expand Down
8 changes: 3 additions & 5 deletions frame/support/src/storage/types/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
/// ```
Expand All @@ -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()
Expand Down Expand Up @@ -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";
}

Expand Down
8 changes: 3 additions & 5 deletions frame/support/src/storage/types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)>
Expand All @@ -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()
Expand Down Expand Up @@ -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";
}

Expand Down
16 changes: 11 additions & 5 deletions frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 4480a04

Please sign in to comment.