Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 17 additions & 6 deletions srml/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ pub enum StorageHasher {
Twox64Concat,
}

/// The kind of map for a map storage entry type.
#[derive(Clone, PartialEq, Eq, Encode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub enum StorageMapKind {
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: StorageMapKind,
},
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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ impl BuilderDef {
<#storage_struct as #scrate::#storage_trait>::put::<&#value_type>(v);
}}
},
StorageLineTypeDef::Map(map) | StorageLineTypeDef::LinkedMap(map) => {
StorageLineTypeDef::Map(map)
| StorageLineTypeDef::LinkedMap(map)
| StorageLineTypeDef::PrefixedMap(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 @@ -91,7 +91,10 @@ impl GenesisConfigDef {

let typ = match &line.storage_type {
StorageLineTypeDef::Simple(_) => (*value_type).clone(),
StorageLineTypeDef::Map(map) | StorageLineTypeDef::LinkedMap(map) => {
StorageLineTypeDef::Map(map)
| StorageLineTypeDef::LinkedMap(map)
| StorageLineTypeDef::PrefixedMap(map)
=> {
let key = &map.key;
parse_quote!( Vec<(#key, #value_type)> )
},
Expand Down
5 changes: 4 additions & 1 deletion srml/support/procedural/src/storage/getters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ pub fn impl_getters(scrate: &TokenStream, def: &DeclStorageDefExt) -> TokenStrea
}
}
},
StorageLineTypeDef::Map(map) | StorageLineTypeDef::LinkedMap(map) => {
StorageLineTypeDef::Map(map)
| StorageLineTypeDef::LinkedMap(map)
| StorageLineTypeDef::PrefixedMap(map)
=> {
let key = &map.key;
let value = &map.value;
quote!{
Expand Down
17 changes: 15 additions & 2 deletions srml/support/procedural/src/storage/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn storage_line_metadata_type(scrate: &TokenStream, line: &StorageLineDefExt) ->
hasher: #scrate::metadata::#hasher,
key: #scrate::metadata::DecodeDifferent::Encode(#key),
value: #scrate::metadata::DecodeDifferent::Encode(#value_type),
is_linked: false,
kind: #scrate::metadata::StorageMapKind::Map,
}
}
},
Expand All @@ -54,7 +54,20 @@ fn storage_line_metadata_type(scrate: &TokenStream, line: &StorageLineDefExt) ->
hasher: #scrate::metadata::#hasher,
key: #scrate::metadata::DecodeDifferent::Encode(#key),
value: #scrate::metadata::DecodeDifferent::Encode(#value_type),
is_linked: true,
kind: #scrate::metadata::StorageMapKind::LinkedMap,
}
}
},
StorageLineTypeDef::PrefixedMap(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),
kind: #scrate::metadata::StorageMapKind::PrefixedMap,
}
}
},
Expand Down
11 changes: 11 additions & 0 deletions srml/support/procedural/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ impl StorageLineDefExt {
ext::type_contains_ident(&map.key, &def.module_runtime_generic)
|| ext::type_contains_ident(&map.value, &def.module_runtime_generic)
}
StorageLineTypeDef::PrefixedMap(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 @@ -250,6 +254,7 @@ impl StorageLineDefExt {
StorageLineTypeDef::Simple(value) => value.clone(),
StorageLineTypeDef::Map(map) => map.value.clone(),
StorageLineTypeDef::LinkedMap(map) => map.value.clone(),
StorageLineTypeDef::PrefixedMap(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 @@ -295,6 +300,10 @@ impl StorageLineDefExt {
let key = &map.key;
quote!( StorageLinkedMap<#key, #value_type> )
},
StorageLineTypeDef::PrefixedMap(map) => {
let key = &map.key;
quote!( StoragePrefixedMap<#key, #value_type> )
},
StorageLineTypeDef::DoubleMap(map) => {
let key1 = &map.key1;
let key2 = &map.key2;
Expand Down Expand Up @@ -337,6 +346,7 @@ impl StorageLineDefExt {
pub enum StorageLineTypeDef {
Map(MapDef),
LinkedMap(MapDef),
PrefixedMap(MapDef),
DoubleMap(DoubleMapDef),
Simple(syn::Type),
}
Expand Down Expand Up @@ -418,6 +428,7 @@ pub fn decl_storage_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStr
StorageValue as _,
StorageMap as _,
StorageLinkedMap as _,
StoragePrefixedMap as _,
StorageDoubleMap as _
};

Expand Down
19 changes: 19 additions & 0 deletions srml/support/procedural/src/storage/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod keyword {
syn::custom_keyword!(get);
syn::custom_keyword!(map);
syn::custom_keyword!(linked_map);
syn::custom_keyword!(prefixed_map);
syn::custom_keyword!(double_map);
syn::custom_keyword!(blake2_256);
syn::custom_keyword!(blake2_128);
Expand Down Expand Up @@ -141,6 +142,7 @@ struct DeclStorageBuild {
enum DeclStorageType {
Map(DeclStorageMap),
LinkedMap(DeclStorageLinkedMap),
PrefixedMap(DeclStoragePrefixedMap),
DoubleMap(DeclStorageDoubleMap),
Simple(syn::Type),
}
Expand All @@ -163,6 +165,15 @@ struct DeclStorageLinkedMap {
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 DeclStorageDoubleMap {
pub map_keyword: keyword::double_map,
Expand Down Expand Up @@ -375,6 +386,14 @@ fn parse_storage_line_defs(
value: map.value,
}
),
DeclStorageType::PrefixedMap(map) => super::StorageLineTypeDef::PrefixedMap(
super::MapDef {
hasher: map.hasher.inner.map(Into::into)
.unwrap_or(super::HasherKind::Blake2_256),
key: map.key,
value: map.value,
}
),
DeclStorageType::DoubleMap(map) => super::StorageLineTypeDef::DoubleMap(
super::DoubleMapDef {
hasher1: map.hasher.inner.map(Into::into)
Expand Down
23 changes: 23 additions & 0 deletions srml/support/procedural/src/storage/storage_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,29 @@ pub fn decl_and_impl(scrate: &TokenStream, def: &DeclStorageDefExt) -> TokenStre
}
)
},
StorageLineTypeDef::PrefixedMap(map) => {
let hasher = map.hasher.to_storage_hasher_struct();
quote!(
impl<#impl_trait> #scrate::#storage_generator_trait for #storage_struct
#optional_storage_where_clause
{
type Query = #query_type;
type Hasher = #scrate::#hasher;

fn prefix() -> &'static [u8] {
#final_prefix
}

fn from_optional_value_to_query(v: Option<#value_type>) -> Self::Query {
#from_optional_value_to_query
}

fn from_query_to_optional_value(v: Self::Query) -> Option<#value_type> {
#from_query_to_optional_value
}
}
)
},
StorageLineTypeDef::DoubleMap(map) => {
let hasher1 = map.hasher1.to_storage_hasher_struct();
let hasher2 = map.hasher2.to_storage_hasher_struct();
Expand Down
12 changes: 6 additions & 6 deletions srml/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub mod error;
pub mod traits;

pub use self::hash::{Twox256, Twox128, Blake2_256, Blake2_128, Twox64Concat, Hashable};
pub use self::storage::{StorageValue, StorageMap, StorageLinkedMap, StorageDoubleMap};
pub use self::storage::{StorageValue, StorageMap, StorageLinkedMap, StoragePrefixedMap, StorageDoubleMap};
pub use self::dispatch::{Parameter, Callable, IsSubType};
pub use sr_primitives::{self, ConsensusEngineId, print, traits::Printable};

Expand Down Expand Up @@ -240,7 +240,7 @@ mod tests {
use codec::{Codec, EncodeLike};
use srml_metadata::{
DecodeDifferent, StorageEntryMetadata, StorageMetadata, StorageEntryType,
StorageEntryModifier, DefaultByteGetter, StorageHasher,
StorageEntryModifier, DefaultByteGetter, StorageHasher, StorageMapKind,
};
use rstd::marker::PhantomData;

Expand Down Expand Up @@ -470,7 +470,7 @@ mod tests {
hasher: StorageHasher::Twox64Concat,
key: DecodeDifferent::Encode("u32"),
value: DecodeDifferent::Encode("u64"),
is_linked: true,
kind: StorageMapKind::LinkedMap,
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructData(PhantomData::<Test>))
Expand All @@ -484,7 +484,7 @@ mod tests {
hasher: StorageHasher::Blake2_256,
key: DecodeDifferent::Encode("u32"),
value: DecodeDifferent::Encode("u32"),
is_linked: true,
kind: StorageMapKind::LinkedMap,
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructOptionLinkedMap(PhantomData::<Test>))
Expand All @@ -498,7 +498,7 @@ mod tests {
hasher: StorageHasher::Twox128,
key: DecodeDifferent::Encode("T::BlockNumber"),
value: DecodeDifferent::Encode("T::BlockNumber"),
is_linked: true
kind: StorageMapKind::LinkedMap
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructGenericData(PhantomData::<Test>))
Expand All @@ -512,7 +512,7 @@ mod tests {
hasher: StorageHasher::Blake2_256,
key: DecodeDifferent::Encode("T::BlockNumber"),
value: DecodeDifferent::Encode("T::BlockNumber"),
is_linked: true
kind: StorageMapKind::LinkedMap
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructGenericData2(PhantomData::<Test>))
Expand Down
3 changes: 2 additions & 1 deletion srml/support/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
pub use srml_metadata::{
DecodeDifferent, FnEncode, RuntimeMetadata, ModuleMetadata, RuntimeMetadataLastVersion,
DefaultByteGetter, RuntimeMetadataPrefixed, StorageEntryMetadata, StorageMetadata,
StorageEntryType, StorageEntryModifier, DefaultByte, StorageHasher, ModuleErrorMetadata
StorageEntryType, StorageEntryModifier, DefaultByte, StorageHasher, ModuleErrorMetadata,
StorageMapKind
};

/// Implements the metadata support for the given runtime and all its modules.
Expand Down
3 changes: 2 additions & 1 deletion srml/support/src/storage/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@

mod linked_map;
mod map;
mod prefixed_map;
mod double_map;
mod value;

pub use linked_map::{StorageLinkedMap, Enumerator, Linkage};
pub use map::StorageMap;
pub use prefixed_map::StoragePrefixedMap;
pub use double_map::StorageDoubleMap;
pub use value::StorageValue;


#[cfg(test)]
#[allow(dead_code)]
mod tests {
Expand Down
Loading