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

Commit

Permalink
merge both StorageInfoTrait and PalletStorageInfo
Browse files Browse the repository at this point in the history
I think it is more future proof. In the future some storage could make
use of multiple prefix. Like one to store how much value has been
inserted, etc...
  • Loading branch information
gui1117 committed May 14, 2021
1 parent ada719b commit 341d3f5
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 122 deletions.
2 changes: 1 addition & 1 deletion frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ use proc_macro::TokenStream;
/// Will include the item in `GenesisConfig`.
/// * \[optional\] `build(#closure)`: Closure called with storage overlays.
/// * \[optional\] `max_values(#expr)`: `expr` is an expression returning a `u32`. It is used to
/// implement `PalletStorageInfo`. Note this attribute is not available for storage value as the maximum
/// implement `StorageInfoTrait`. Note this attribute is not available for storage value as the maximum
/// number of values is 1.
/// * `#type`: Storage type.
/// * \[optional\] `#default`: Value returned when none.
Expand Down
29 changes: 16 additions & 13 deletions frame/support/procedural/src/pallet/expand/pallet_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::pallet::{Def, expand::merge_where_clauses, parse::helper::get_doc_lit
/// * declare Module type alias for construct_runtime
/// * replace the first field type of `struct Pallet` with `PhantomData` if it is `_`
/// * implementation of `PalletInfoAccess` information
/// * implementation of `PalletStorageInfo` on Pallet
/// * implementation of `StorageInfoTrait` on Pallet
pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
let frame_support = &def.frame_support;
let frame_system = &def.frame_system;
Expand Down Expand Up @@ -109,24 +109,27 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
.collect::<Vec<_>>();

quote::quote_spanned!(storage_info_span =>
impl<#type_impl_gen> #frame_support::traits::PalletStorageInfo
impl<#type_impl_gen> #frame_support::traits::StorageInfoTrait
for #pallet_ident<#type_use_gen>
#storages_where_clauses
{
fn storage_info()
-> #frame_support::sp_std::vec::Vec<#frame_support::traits::StorageInfo>
{
#frame_support::sp_std::vec![
#(
#(#storage_cfg_attrs)*
{
<
#storage_names<#type_use_gen>
as #frame_support::traits::StorageInfoTrait
>::storage_info()
},
)*
]
let mut res = #frame_support::sp_std::vec![];

#(
#(#storage_cfg_attrs)*
{
let mut storage_info = <
#storage_names<#type_use_gen>
as #frame_support::traits::StorageInfoTrait
>::storage_info();
res.append(&mut storage_info);
}
)*

res
}
}
)
Expand Down
10 changes: 5 additions & 5 deletions frame/support/procedural/src/pallet/parse/pallet_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct PalletStructDef {
pub store: Option<(syn::Visibility, keyword::Store)>,
/// The span of the pallet::pallet attribute.
pub attr_span: proc_macro2::Span,
/// Whether to specify the storages max encoded len when implementing `PalletStorageInfo`.
/// Whether to specify the storages max encoded len when implementing `StorageInfoTrait`.
/// Contains the span of the attribute.
pub generate_storage_info: Option<proc_macro2::Span>,
}
Expand All @@ -54,14 +54,14 @@ pub enum PalletStructAttr {
vis: syn::Visibility,
keyword: keyword::Store,
},
GeneratePalletStorageInfo(proc_macro2::Span),
GenerateStorageInfoTrait(proc_macro2::Span),
}

impl PalletStructAttr {
fn span(&self) -> proc_macro2::Span {
match self {
Self::GenerateStore { span, .. } => *span,
Self::GeneratePalletStorageInfo(span) => *span,
Self::GenerateStorageInfoTrait(span) => *span,
}
}
}
Expand All @@ -86,7 +86,7 @@ impl syn::parse::Parse for PalletStructAttr {
Ok(Self::GenerateStore { vis, keyword, span })
} else if lookahead.peek(keyword::generate_storage_info) {
let span = content.parse::<keyword::generate_storage_info>()?.span();
Ok(Self::GeneratePalletStorageInfo(span))
Ok(Self::GenerateStorageInfoTrait(span))
} else {
Err(lookahead.error())
}
Expand Down Expand Up @@ -115,7 +115,7 @@ impl PalletStructDef {
PalletStructAttr::GenerateStore { vis, keyword, .. } if store.is_none() => {
store = Some((vis, keyword));
},
PalletStructAttr::GeneratePalletStorageInfo(span) if generate_storage_info.is_none() => {
PalletStructAttr::GenerateStorageInfoTrait(span) if generate_storage_info.is_none() => {
generate_storage_info = Some(span);
},
attr => {
Expand Down
22 changes: 11 additions & 11 deletions frame/support/procedural/src/storage/storage_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Implementation of trait `PalletStorageInfo` on module structure.
//! Implementation of trait `StorageInfoTrait` on module structure.

use proc_macro2::TokenStream;
use quote::quote;
Expand All @@ -28,29 +28,29 @@ pub fn impl_storage_info(def: &DeclStorageDefExt) -> TokenStream {

let scrate = &def.hidden_crate;

let mut entries = TokenStream::new();
let mut res_append_storage = TokenStream::new();

for line in def.storage_lines.iter() {
let storage_struct = &line.storage_struct;

let entry = quote!(
<
res_append_storage.extend(quote!(
let mut storage_info = <
#storage_struct as #scrate::traits::StorageInfoTrait
>::storage_info(),
);
entries.extend(entry);
>::storage_info();
res.append(&mut storage_info);
));
}

let module_struct = &def.module_struct;
let module_impl = &def.module_impl;
let where_clause = &def.where_clause;

quote!(
impl#module_impl #scrate::traits::PalletStorageInfo for #module_struct #where_clause {
impl#module_impl #scrate::traits::StorageInfoTrait for #module_struct #where_clause {
fn storage_info() -> #scrate::sp_std::vec::Vec<#scrate::traits::StorageInfo> {
#scrate::sp_std::vec![
#entries
]
let mut res = #scrate::sp_std::vec![];
#res_append_storage
res
}
}
)
Expand Down
90 changes: 55 additions & 35 deletions frame/support/procedural/src/storage/storage_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,20 +261,25 @@ pub fn decl_and_impl(def: &DeclStorageDefExt) -> TokenStream {
impl<#impl_trait> #scrate::traits::StorageInfoTrait for #storage_struct
#optional_storage_where_clause
{
fn storage_info() -> #scrate::traits::StorageInfo {
fn storage_info()
-> #scrate::sp_std::vec::Vec<#scrate::traits::StorageInfo>
{
use #scrate::sp_runtime::SaturatedConversion;

let max_size = <
#value_type as #scrate::traits::MaxEncodedLen
>::max_encoded_len()
.saturated_into();
#scrate::traits::StorageInfo {
prefix: <
#storage_struct as #scrate::#storage_generator_trait
>::storage_value_final_key(),
max_values: Some(1),
max_size: Some(max_size),
}

#scrate::sp_std::vec![
#scrate::traits::StorageInfo {
prefix: <
#storage_struct as #scrate::#storage_generator_trait
>::storage_value_final_key(),
max_values: Some(1),
max_size: Some(max_size),
}
]
}
}
)
Expand All @@ -285,7 +290,9 @@ pub fn decl_and_impl(def: &DeclStorageDefExt) -> TokenStream {
impl<#impl_trait> #scrate::traits::StorageInfoTrait for #storage_struct
#optional_storage_where_clause
{
fn storage_info() -> #scrate::traits::StorageInfo {
fn storage_info()
-> #scrate::sp_std::vec::Vec<#scrate::traits::StorageInfo>
{
use #scrate::sp_runtime::SaturatedConversion;
let max_size = <
#value_type as #scrate::traits::MaxEncodedLen
Expand All @@ -294,14 +301,17 @@ pub fn decl_and_impl(def: &DeclStorageDefExt) -> TokenStream {
<#key as #scrate::traits::MaxEncodedLen>::max_encoded_len()
)
.saturated_into();
#scrate::traits::StorageInfo {
prefix: <
#storage_struct
as #scrate::storage::StoragePrefixedMap<#value_type>
>::final_prefix(),
max_values: #max_values,
max_size: Some(max_size),
}

#scrate::sp_std::vec![
#scrate::traits::StorageInfo {
prefix: <
#storage_struct
as #scrate::storage::StoragePrefixedMap<#value_type>
>::final_prefix(),
max_values: #max_values,
max_size: Some(max_size),
}
]
}
}
)
Expand All @@ -313,7 +323,9 @@ pub fn decl_and_impl(def: &DeclStorageDefExt) -> TokenStream {
impl<#impl_trait> #scrate::traits::StorageInfoTrait for #storage_struct
#optional_storage_where_clause
{
fn storage_info() -> #scrate::traits::StorageInfo {
fn storage_info()
-> #scrate::sp_std::vec::Vec<#scrate::traits::StorageInfo>
{
use #scrate::sp_runtime::SaturatedConversion;
let max_size = <
#value_type as #scrate::traits::MaxEncodedLen
Expand All @@ -325,14 +337,17 @@ pub fn decl_and_impl(def: &DeclStorageDefExt) -> TokenStream {
<#key2 as #scrate::traits::MaxEncodedLen>::max_encoded_len()
)
.saturated_into();
#scrate::traits::StorageInfo {
prefix: <
#storage_struct
as #scrate::storage::StoragePrefixedMap<#value_type>
>::final_prefix(),
max_values: #max_values,
max_size: Some(max_size),
}

#scrate::sp_std::vec![
#scrate::traits::StorageInfo {
prefix: <
#storage_struct
as #scrate::storage::StoragePrefixedMap<#value_type>
>::final_prefix(),
max_values: #max_values,
max_size: Some(max_size),
}
]
}
}
)
Expand All @@ -343,7 +358,9 @@ pub fn decl_and_impl(def: &DeclStorageDefExt) -> TokenStream {
impl<#impl_trait> #scrate::traits::StorageInfoTrait for #storage_struct
#optional_storage_where_clause
{
fn storage_info() -> #scrate::traits::StorageInfo {
fn storage_info()
-> #scrate::sp_std::vec::Vec<#scrate::traits::StorageInfo>
{
use #scrate::sp_runtime::SaturatedConversion;
let max_size = <
#value_type as #scrate::traits::MaxEncodedLen
Expand All @@ -357,14 +374,17 @@ pub fn decl_and_impl(def: &DeclStorageDefExt) -> TokenStream {
)
)*
.saturated_into();
#scrate::traits::StorageInfo {
prefix: <
#storage_struct
as #scrate::storage::StoragePrefixedMap<#value_type>
>::final_prefix(),
max_values: #max_values,
max_size: Some(max_size),
}

#scrate::sp_std::vec![
#scrate::traits::StorageInfo {
prefix: <
#storage_struct
as #scrate::storage::StoragePrefixedMap<#value_type>
>::final_prefix(),
max_values: #max_values,
max_size: Some(max_size),
}
]
}
}
)
Expand Down
9 changes: 5 additions & 4 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ pub mod pallet_prelude {
Twox128, Blake2_256, Blake2_128, Identity, Twox64Concat, Blake2_128Concat, ensure,
RuntimeDebug, storage,
traits::{
Get, Hooks, IsType, GetPalletVersion, EnsureOrigin, PalletInfoAccess, PalletStorageInfo,
Get, Hooks, IsType, GetPalletVersion, EnsureOrigin, PalletInfoAccess, StorageInfoTrait,
ConstU32, GetDefault,
},
dispatch::{DispatchResultWithPostInfo, Parameter, DispatchError, DispatchResult},
Expand Down Expand Up @@ -1357,7 +1357,7 @@ pub mod pallet_prelude {
/// pub struct Pallet<T>(_);
/// ```
///
/// This require all storage to implement the trait [`traits::StorageMaxEncodedLen`], thus all keys
/// This require all storage to implement the trait [`traits::StorageInfoTrait`], thus all keys
/// and value types must bound [`traits::MaxEncodedLen`].
///
/// ### Macro expansion:
Expand All @@ -1384,13 +1384,14 @@ pub mod pallet_prelude {
/// given by [`frame_support::traits::PalletInfo`].
/// (The implementation use the associated type `frame_system::Config::PalletInfo`).
///
/// It implements [`traits::PalletStorageInfo`] on `Pallet` which give information about all storages.
/// It implements [`traits::StorageInfoTrait`] on `Pallet` which give information about all storages.
///
/// If the attribute generate_store is set then the macro creates the trait `Store` and implements
/// it on `Pallet`.
///
/// If the attribute set_storage_max_encoded_len is set then the macro call
/// [`traits::StorageMaxEncodedLen`] in the implementation of [`PalletStorageInfo`].
/// [`traits::StorageInfoTrait`] for each storage in the implementation of
/// [`traits::StorageInfoTrait`] for the pallet.
///
/// # Hooks: `#[pallet::hooks]` mandatory
///
Expand Down
26 changes: 14 additions & 12 deletions frame/support/src/storage/types/double_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
};
use frame_metadata::{DefaultByteGetter, StorageEntryModifier};
use sp_arithmetic::traits::SaturatedConversion;
use sp_std::vec::Vec;
use sp_std::prelude::*;

/// A type that allow to store values for `(key1, key2)` couple. Similar to `StorageMap` but allow
/// to iterate and remove value associated to first key.
Expand Down Expand Up @@ -493,17 +493,19 @@ where
OnEmpty: Get<QueryKind::Query> + 'static,
MaxValues: Get<Option<u32>>,
{
fn storage_info() -> StorageInfo {
StorageInfo {
prefix: Self::final_prefix(),
max_values: MaxValues::get(),
max_size: Some(
Key1::max_encoded_len()
.saturating_add(Key2::max_encoded_len())
.saturating_add(Value::max_encoded_len())
.saturated_into(),
),
}
fn storage_info() -> Vec<StorageInfo> {
vec![
StorageInfo {
prefix: Self::final_prefix(),
max_values: MaxValues::get(),
max_size: Some(
Key1::max_encoded_len()
.saturating_add(Key2::max_encoded_len())
.saturating_add(Value::max_encoded_len())
.saturated_into(),
),
}
]
}
}

Expand Down
22 changes: 12 additions & 10 deletions frame/support/src/storage/types/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,18 @@ where
OnEmpty: Get<QueryKind::Query> + 'static,
MaxValues: Get<Option<u32>>,
{
fn storage_info() -> StorageInfo {
StorageInfo {
prefix: Self::final_prefix(),
max_values: MaxValues::get(),
max_size: Some(
Key::max_encoded_len()
.saturating_add(Value::max_encoded_len())
.saturated_into(),
),
}
fn storage_info() -> Vec<StorageInfo> {
vec![
StorageInfo {
prefix: Self::final_prefix(),
max_values: MaxValues::get(),
max_size: Some(
Key::max_encoded_len()
.saturating_add(Value::max_encoded_len())
.saturated_into(),
),
}
]
}
}

Expand Down
Loading

0 comments on commit 341d3f5

Please sign in to comment.