Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement prefixed storage
  • Loading branch information
gui1117 committed Nov 27, 2019
commit 4931088126a427082d7310ed7e83b8eea966bc20
23 changes: 17 additions & 6 deletions frame/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ pub enum StorageHasher {
Twox64Concat,
}

/// A storage map type.
#[derive(Clone, PartialEq, Eq, Encode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub enum StorageMapType {
Map,
LinkedMap,
PrefixedMap,
}

/// A storage entry type.
#[derive(Clone, PartialEq, Eq, Encode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
Expand All @@ -287,7 +296,7 @@ pub enum StorageEntryType {
hasher: StorageHasher,
key: DecodeDifferentStr,
value: DecodeDifferentStr,
is_linked: bool,
kind: StorageMapType,
},
DoubleMap {
hasher: StorageHasher,
Expand Down Expand Up @@ -342,8 +351,10 @@ pub enum RuntimeMetadata {
V6(RuntimeMetadataDeprecated),
/// Version 7 for runtime metadata. No longer used.
V7(RuntimeMetadataDeprecated),
/// Version 8 for runtime metadata.
V8(RuntimeMetadataV8),
/// Version 8 for runtime metadata. No longer used.
V8(RuntimeMetadataDeprecated),
/// Version 9 for runtime metadata.
V9(RuntimeMetadataV9),
}

/// Enum that should fail.
Expand All @@ -367,12 +378,12 @@ impl Decode for RuntimeMetadataDeprecated {
/// The metadata of a runtime.
#[derive(Eq, Encode, PartialEq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct RuntimeMetadataV8 {
pub struct RuntimeMetadataV9 {
pub modules: DecodeDifferentArray<ModuleMetadata>,
}

/// The latest version of the metadata.
pub type RuntimeMetadataLastVersion = RuntimeMetadataV8;
pub type RuntimeMetadataLastVersion = RuntimeMetadataV9;

/// All metadata about an runtime module.
#[derive(Clone, PartialEq, Eq, Encode, RuntimeDebug)]
Expand All @@ -397,6 +408,6 @@ impl Into<primitives::OpaqueMetadata> for RuntimeMetadataPrefixed {

impl Into<RuntimeMetadataPrefixed> for RuntimeMetadataLastVersion {
fn into(self) -> RuntimeMetadataPrefixed {
RuntimeMetadataPrefixed(META_RESERVED, RuntimeMetadata::V8(self))
RuntimeMetadataPrefixed(META_RESERVED, RuntimeMetadata::V9(self))
}
}
24 changes: 20 additions & 4 deletions frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,27 @@ use proc_macro::TokenStream;
/// [`Hashable`](../frame_support/trait.Hashable.html) trait.
///
/// `hasher($hash)` is optional and its default is `blake2_256`. One should use another hasher
/// with care, see generator documentation.
/// with care.
///
/// The generator is implemented with:
/// * `prefix`: `$module_prefix ++ " " ++ $storage_name`
/// * `Hasher`: $hash
/// The generator is implemented so the value is inserted in the trie at:
/// ```nocompile
/// $hash($module_prefix ++ " " ++ $storage_name ++ encode($key))
/// ```
///
/// * PrefixedMap: `Foo: prefixed_map hasher($hash) type => type`: Implements the
/// [`StorageMap`](../palette_support/storage/trait.StorageMap.html) trait using the
/// [`StorageMap generator`](../palette_support/storage/generator/trait.StorageMap.html).
///
/// `$hash` representing a choice of hashing algorithms available in the
/// [`Hashable`](../palette_support/trait.Hashable.html) trait.
///
/// `hasher($hash)` is optional and its default is `blake2_256`. One should use another hasher
/// with care.
///
/// The generator is implemented so the value is inserted in the trie at:
/// ```nocompile
/// twox_128($module_prefix)++twox_128($storage_name)++$hash(encode($key))
/// ```
///
/// * Linked map: `Foo: linked_map hasher($hash) type => type`: Implements the
/// [`StorageLinkedMap`](../frame_support/storage/trait.StorageLinkedMap.html) trait using the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl BuilderDef {
<#storage_struct as #scrate::#storage_trait>::put::<&#value_type>(v);
}}
},
StorageLineTypeDef::Map(map) | StorageLineTypeDef::LinkedMap(map) => {
StorageLineTypeDef::Map(map) => {
let key = &map.key;
quote!{{
let data: &#scrate::rstd::vec::Vec<(#key, #value_type)> = #data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl GenesisConfigDef {

let typ = match &line.storage_type {
StorageLineTypeDef::Simple(_) => (*value_type).clone(),
StorageLineTypeDef::Map(map) | StorageLineTypeDef::LinkedMap(map) => {
StorageLineTypeDef::Map(map) => {
let key = &map.key;
parse_quote!( Vec<(#key, #value_type)> )
},
Expand Down
2 changes: 1 addition & 1 deletion frame/support/procedural/src/storage/getters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn impl_getters(scrate: &TokenStream, def: &DeclStorageDefExt) -> TokenStrea
}
}
},
StorageLineTypeDef::Map(map) | StorageLineTypeDef::LinkedMap(map) => {
StorageLineTypeDef::Map(map) => {
let key = &map.key;
let value = &map.value;
quote!{
Expand Down
17 changes: 13 additions & 4 deletions frame/support/procedural/src/storage/instance_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,26 @@

//! Implementation of the trait instance and the instance structures implementing it.
//! (For not instantiable traits there is still the inherent instance implemented).
//!
//! The instance trait defines the prefix to be used by storages.

use proc_macro2::{TokenStream, Span};
use quote::quote;
use super::{DeclStorageDefExt, StorageLineTypeDef};
use super::{DeclStorageDefExt, StorageLineTypeDef, MapKind, MapDef};

const NUMBER_OF_INSTANCE: usize = 16;
const INHERENT_INSTANCE_NAME: &str = "__InherentHiddenInstance";
pub(crate) const INHERENT_INSTANCE_NAME: &str = "__InherentHiddenInstance";
pub(crate) const DEFAULT_INSTANTIABLE_TRAIT_NAME: &str = "__GeneratedInstantiable";

// prefix for consts in trait Instance
pub(crate) const PREFIX_FOR: &str = "PREFIX_FOR_";
// prefix for the consts for the head of a linkedmap
pub(crate) const HEAD_KEY_FOR: &str = "HEAD_KEY_FOR_";

// Used to generate the const:
// `const $name: &'static str = $value_prefix ++ instance_prefix ++ $value_suffix`
struct InstanceConstDef {
// The name of the const.
name: syn::Ident,
value_prefix: String,
value_suffix: String,
Expand All @@ -50,6 +54,11 @@ pub fn decl_and_impl(scrate: &TokenStream, def: &DeclStorageDefExt) -> TokenStre
let mut const_defs = vec![];

for line in def.storage_lines.iter() {
// Do not generate additional const for prefixed map
if let StorageLineTypeDef::Map(MapDef { kind: MapKind::PrefixedMap, .. }) = line.storage_type {
continue;
}

let storage_prefix = format!("{} {}", def.crate_name, line.name);

let const_name = syn::Ident::new(
Expand All @@ -61,7 +70,7 @@ pub fn decl_and_impl(scrate: &TokenStream, def: &DeclStorageDefExt) -> TokenStre
value_suffix: storage_prefix.clone(),
});

if let StorageLineTypeDef::LinkedMap(_) = line.storage_type {
if let StorageLineTypeDef::Map(MapDef { kind: MapKind::LinkedMap, .. }) = line.storage_type {
let const_name = syn::Ident::new(
&format!("{}{}", HEAD_KEY_FOR, line.name.to_string()), proc_macro2::Span::call_site()
);
Expand Down Expand Up @@ -149,7 +158,7 @@ fn create_instance_trait(
/// Defines storage prefixes, they must be unique.
#optional_hide
pub trait #instance_trait: 'static {
/// The prefix used by any storage entry of an instance.
/// The module prefix used by any storage entry of an instance.
const PREFIX: &'static str;
#const_impls
}
Expand Down
16 changes: 2 additions & 14 deletions frame/support/procedural/src/storage/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,13 @@ fn storage_line_metadata_type(scrate: &TokenStream, line: &StorageLineDefExt) ->
let hasher = map.hasher.into_metadata();
let key = &map.key;
let key = clean_type_string(&quote!(#key).to_string());
let kind = map.kind.into_metadata();
quote!{
#scrate::metadata::StorageEntryType::Map {
hasher: #scrate::metadata::#hasher,
key: #scrate::metadata::DecodeDifferent::Encode(#key),
value: #scrate::metadata::DecodeDifferent::Encode(#value_type),
is_linked: false,
}
}
},
StorageLineTypeDef::LinkedMap(map) => {
let hasher = map.hasher.into_metadata();
let key = &map.key;
let key = clean_type_string(&quote!(#key).to_string());
quote!{
#scrate::metadata::StorageEntryType::Map {
hasher: #scrate::metadata::#hasher,
key: #scrate::metadata::DecodeDifferent::Encode(#key),
value: #scrate::metadata::DecodeDifferent::Encode(#value_type),
is_linked: true,
kind: #scrate::metadata::#kind,
}
}
},
Expand Down
37 changes: 25 additions & 12 deletions frame/support/procedural/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,6 @@ impl StorageLineDefExt {
ext::type_contains_ident(&map.key, &def.module_runtime_generic)
|| ext::type_contains_ident(&map.value, &def.module_runtime_generic)
}
StorageLineTypeDef::LinkedMap(map) => {
ext::type_contains_ident(&map.key, &def.module_runtime_generic)
|| ext::type_contains_ident(&map.value, &def.module_runtime_generic)
}
StorageLineTypeDef::DoubleMap(map) => {
ext::type_contains_ident(&map.key1, &def.module_runtime_generic)
|| ext::type_contains_ident(&map.key2, &def.module_runtime_generic)
Expand All @@ -249,7 +245,6 @@ impl StorageLineDefExt {
let query_type = match &storage_def.storage_type {
StorageLineTypeDef::Simple(value) => value.clone(),
StorageLineTypeDef::Map(map) => map.value.clone(),
StorageLineTypeDef::LinkedMap(map) => map.value.clone(),
StorageLineTypeDef::DoubleMap(map) => map.value.clone(),
};
let is_option = ext::extract_type_option(&query_type).is_some();
Expand Down Expand Up @@ -289,11 +284,11 @@ impl StorageLineDefExt {
},
StorageLineTypeDef::Map(map) => {
let key = &map.key;
quote!( StorageMap<#key, #value_type> )
},
StorageLineTypeDef::LinkedMap(map) => {
let key = &map.key;
quote!( StorageLinkedMap<#key, #value_type> )
match map.kind {
MapKind::Map => quote!( StorageMap<#key, #value_type> ),
MapKind::LinkedMap => quote!( StorageLinkedMap<#key, #value_type> ),
MapKind::PrefixedMap => quote!( StorageMap<#key, #value_type> ),
}
},
StorageLineTypeDef::DoubleMap(map) => {
let key1 = &map.key1;
Expand Down Expand Up @@ -336,7 +331,6 @@ impl StorageLineDefExt {

pub enum StorageLineTypeDef {
Map(MapDef),
LinkedMap(MapDef),
DoubleMap(DoubleMapDef),
Simple(syn::Type),
}
Expand All @@ -346,6 +340,24 @@ pub struct MapDef {
pub key: syn::Type,
/// This is the query value not the inner value used in storage trait implementation.
pub value: syn::Type,
pub kind: MapKind,
}

#[derive(PartialEq, Eq)]
pub enum MapKind {
Map,
LinkedMap,
PrefixedMap,
}

impl MapKind {
fn into_metadata(&self) -> proc_macro2::TokenStream {
match self {
MapKind::Map => quote!( StorageMapType::Map ),
MapKind::LinkedMap => quote!( StorageMapType::LinkedMap ),
MapKind::PrefixedMap => quote!( StorageMapType::PrefixedMap ),
}
}
}

pub struct DoubleMapDef {
Expand Down Expand Up @@ -418,7 +430,8 @@ pub fn decl_storage_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStr
StorageValue as _,
StorageMap as _,
StorageLinkedMap as _,
StorageDoubleMap as _
StorageDoubleMap as _,
StoragePrefixedMap as _,
};

#scrate_decl
Expand Down
24 changes: 23 additions & 1 deletion frame/support/procedural/src/storage/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod keyword {
syn::custom_keyword!(build);
syn::custom_keyword!(get);
syn::custom_keyword!(map);
syn::custom_keyword!(prefixed_map);
syn::custom_keyword!(linked_map);
syn::custom_keyword!(double_map);
syn::custom_keyword!(blake2_256);
Expand Down Expand Up @@ -140,6 +141,7 @@ struct DeclStorageBuild {
#[derive(Parse, ToTokens, Debug)]
enum DeclStorageType {
Map(DeclStorageMap),
PrefixedMap(DeclStoragePrefixedMap),
LinkedMap(DeclStorageLinkedMap),
DoubleMap(DeclStorageDoubleMap),
Simple(syn::Type),
Expand All @@ -154,6 +156,15 @@ struct DeclStorageMap {
pub value: syn::Type,
}

#[derive(Parse, ToTokens, Debug)]
struct DeclStoragePrefixedMap {
pub map_keyword: keyword::prefixed_map,
pub hasher: ext::Opt<SetHasher>,
pub key: syn::Type,
pub ass_keyword: Token![=>],
pub value: syn::Type,
}

#[derive(Parse, ToTokens, Debug)]
struct DeclStorageLinkedMap {
pub map_keyword: keyword::linked_map,
Expand Down Expand Up @@ -365,14 +376,25 @@ fn parse_storage_line_defs(
.unwrap_or(super::HasherKind::Blake2_256),
key: map.key,
value: map.value,
kind: super::MapKind::Map,
}
),
DeclStorageType::PrefixedMap(map) => super::StorageLineTypeDef::Map(
super::MapDef {
hasher: map.hasher.inner.map(Into::into)
.unwrap_or(super::HasherKind::Blake2_256),
key: map.key,
value: map.value,
kind: super::MapKind::PrefixedMap,
}
),
DeclStorageType::LinkedMap(map) => super::StorageLineTypeDef::LinkedMap(
DeclStorageType::LinkedMap(map) => super::StorageLineTypeDef::Map(
super::MapDef {
hasher: map.hasher.inner.map(Into::into)
.unwrap_or(super::HasherKind::Blake2_256),
key: map.key,
value: map.value,
kind: super::MapKind::LinkedMap,
}
),
DeclStorageType::DoubleMap(map) => super::StorageLineTypeDef::DoubleMap(
Expand Down
Loading