Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
add tests, implement StoragePrefixedMap on types
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 committed Sep 2, 2020
1 parent fae5be6 commit cd12184
Show file tree
Hide file tree
Showing 7 changed files with 1,039 additions and 81 deletions.
2 changes: 1 addition & 1 deletion frame/support/procedural/src/pallet/expand/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn expand_module(def: &mut Def) -> proc_macro2::TokenStream {

quote::quote_spanned!(fn_deposit_event_span =>
impl<#type_impl_gen> Module<#type_use_gen> {
fn deposit_event(event: Event<#event_use_gen>) {
pub fn deposit_event(event: Event<#event_use_gen>) {
let event = <
<T as Trait#trait_use_gen>::Event as
From<Event<#event_use_gen>>
Expand Down
6 changes: 5 additions & 1 deletion frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,9 @@ pub mod pallet_prelude {
pub use sp_std::marker::PhantomData;
pub use frame_support::traits::{Get, Instance, ModuleInterface, GenesisBuilder, IsType};
pub use frame_support::dispatch::{DispatchResultWithPostInfo, Parameter};
pub use frame_support::storage::types::*;
pub use frame_support::{EqNoBound, PartialEqNoBound, DebugStripped, DebugNoBound, CloneNoBound};
pub use codec::{Encode, Decode};
pub use sp_inherents::ProvideInherent;
pub use sp_inherents::InherentData;
pub use sp_inherents::InherentIdentifier;
Expand All @@ -846,7 +849,6 @@ pub mod pallet_prelude {
UnknownTransaction,
},
};
pub use frame_support::storage::types::*;
pub use crate::{
StorageValue, StorageMap, StorageDoubleMap, StoragePrefixedMap, IterableStorageMap,
IterableStorageDoubleMap,
Expand Down Expand Up @@ -1146,13 +1148,15 @@ pub mod pallet_prelude {
/// // Define the module struct placeholder, various pallet function are implemented on it.
/// // The macro checks struct generics: is expected `T` or `T, I = DefaultInstance`
/// #[pallet::module]
/// #[pallet::generate(fn deposit_event)]
/// pub struct Module<T>(PhantomData<T>);
///
/// // Implement on the module interface on module.
/// // The macro checks:
/// // * trait is `ModuleInterface` (imported from pallet_prelude)
/// // * struct is `Module<T>` or `Module<T, I>`
/// #[pallet::module_interface]
/// #[pallet::generate(fn deposit_event)]
/// impl<T: Trait> ModuleInterface<BlockNumberFor<T>> for Module<T> {
/// }
///
Expand Down
39 changes: 39 additions & 0 deletions frame/support/src/storage/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ where
}
}

impl<Prefix, Hasher, Key, Value, QueryKind, OnEmpty> super::StoragePrefixedMap<Value> for
StorageMapType<Prefix, Hasher, Key, Value, QueryKind, OnEmpty>
where
Prefix: StorageInstance,
Hasher: crate::hash::StorageHasher,
Key: FullEncode,
Value: FullCodec,
QueryKind: QueryKindTrait<Value>,
OnEmpty: crate::traits::Get<QueryKind::Query> + 'static,
{
fn module_prefix() -> &'static [u8] {
<Self as super::generator::StorageMap<Key, Value>>::module_prefix()
}
fn storage_prefix() -> &'static [u8] {
<Self as super::generator::StorageMap<Key, Value>>::storage_prefix()
}
}

/// A type that implements StorageDoubleMap when generics are correctly set:
/// * Prefix must implement StorageInstance, when used inside pallet macro with
/// `#[pallet::storage]` just write `_` the macro will expand with an automatically generated
Expand Down Expand Up @@ -210,6 +228,27 @@ where
}
}

impl<Prefix, Hasher1, Key1, Hasher2, Key2, Value, QueryKind, OnEmpty>
super::StoragePrefixedMap<Value> for
StorageDoubleMapType<Prefix, Hasher1, Key1, Hasher2, Key2, Value, QueryKind, OnEmpty>
where
Prefix: StorageInstance,
Hasher1: crate::hash::StorageHasher,
Hasher2: crate::hash::StorageHasher,
Key1: FullEncode,
Key2: FullEncode,
Value: FullCodec,
QueryKind: QueryKindTrait<Value>,
OnEmpty: crate::traits::Get<QueryKind::Query> + 'static
{
fn module_prefix() -> &'static [u8] {
<Self as super::generator::StorageDoubleMap<Key1, Key2, Value>>::module_prefix()
}
fn storage_prefix() -> &'static [u8] {
<Self as super::generator::StorageDoubleMap<Key1, Key2, Value>>::storage_prefix()
}
}

/// Part of storage metadata for storage value.
pub trait StorageValueMetadata {
const MODIFIER: StorageEntryModifier;
Expand Down
Loading

0 comments on commit cd12184

Please sign in to comment.