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

Adds ability to use default hasher in dev_mode for explicit key binding #14164

Merged
merged 11 commits into from
May 19, 2023
53 changes: 42 additions & 11 deletions frame/support/procedural/src/pallet/parse/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ fn process_named_generics(
storage: &StorageKind,
args_span: proc_macro2::Span,
args: &[syn::AssocType],
dev_mode: bool,
) -> syn::Result<(Option<StorageGenerics>, Metadata, Option<syn::Type>, bool)> {
let mut parsed = HashMap::<String, syn::AssocType>::new();

Expand Down Expand Up @@ -366,10 +367,20 @@ fn process_named_generics(
}
},
StorageKind::Map => {
let mandatory_generics: &[&str];
let optional_generics: &[&str];
if dev_mode {
mandatory_generics = &["Key", "Value"];
optional_generics = &["Hasher", "QueryKind", "OnEmpty", "MaxValues"];
} else {
mandatory_generics = &["Hasher", "Key", "Value"];
optional_generics = &["QueryKind", "OnEmpty", "MaxValues"];
}

check_generics(
&parsed,
&["Hasher", "Key", "Value"],
&["QueryKind", "OnEmpty", "MaxValues"],
mandatory_generics,
optional_generics,
"StorageMap",
args_span,
)?;
Expand All @@ -378,7 +389,7 @@ fn process_named_generics(
hasher: parsed
.remove("Hasher")
.map(|binding| binding.ty)
.expect("checked above as mandatory generic"),
.unwrap_or(syn::parse_quote!(Blake2_128Concat)),
key: parsed
.remove("Key")
.map(|binding| binding.ty)
Expand All @@ -393,10 +404,20 @@ fn process_named_generics(
}
},
StorageKind::CountedMap => {
let mandatory_generics: &[&str];
let optional_generics: &[&str];
if dev_mode {
mandatory_generics = &["Key", "Value"];
optional_generics = &["Hasher", "QueryKind", "OnEmpty", "MaxValues"];
} else {
mandatory_generics = &["Hasher", "Key", "Value"];
optional_generics = &["QueryKind", "OnEmpty", "MaxValues"];
}
gupnik marked this conversation as resolved.
Show resolved Hide resolved

check_generics(
&parsed,
&["Hasher", "Key", "Value"],
&["QueryKind", "OnEmpty", "MaxValues"],
mandatory_generics,
optional_generics,
"CountedStorageMap",
args_span,
)?;
Expand All @@ -405,7 +426,7 @@ fn process_named_generics(
hasher: parsed
.remove("Hasher")
.map(|binding| binding.ty)
.expect("checked above as mandatory generic"),
.unwrap_or(syn::Type::Verbatim(quote::quote! { Blake2_128Concat })),
key: parsed
.remove("Key")
.map(|binding| binding.ty)
Expand All @@ -420,10 +441,20 @@ fn process_named_generics(
}
},
StorageKind::DoubleMap => {
let mandatory_generics: &[&str];
let optional_generics: &[&str];
if dev_mode {
mandatory_generics = &["Key1", "Key2", "Value"];
optional_generics = &["Hasher1", "Hasher2", "QueryKind", "OnEmpty", "MaxValues"];
} else {
mandatory_generics = &["Hasher1", "Key1", "Hasher2", "Key2", "Value"];
optional_generics = &["QueryKind", "OnEmpty", "MaxValues"];
gupnik marked this conversation as resolved.
Show resolved Hide resolved
}

check_generics(
&parsed,
&["Hasher1", "Key1", "Hasher2", "Key2", "Value"],
&["QueryKind", "OnEmpty", "MaxValues"],
mandatory_generics,
optional_generics,
"StorageDoubleMap",
args_span,
)?;
Expand All @@ -432,15 +463,15 @@ fn process_named_generics(
hasher1: parsed
.remove("Hasher1")
.map(|binding| binding.ty)
.expect("checked above as mandatory generic"),
.unwrap_or(syn::parse_quote!(Blake2_128Concat)),
key1: parsed
.remove("Key1")
.map(|binding| binding.ty)
.expect("checked above as mandatory generic"),
hasher2: parsed
.remove("Hasher2")
.map(|binding| binding.ty)
.expect("checked above as mandatory generic"),
.unwrap_or(syn::parse_quote!(Blake2_128Concat)),
key2: parsed
.remove("Key2")
.map(|binding| binding.ty)
Expand Down Expand Up @@ -619,7 +650,7 @@ fn process_generics(
_ => unreachable!("It is asserted above that all generics are bindings"),
})
.collect::<Vec<_>>();
process_named_generics(&storage_kind, args_span, &args)
process_named_generics(&storage_kind, args_span, &args, dev_mode)
} else {
let msg = "Invalid pallet::storage, invalid generic declaration for storage. Expect only \
type generics or binding generics, e.g. `<Name1 = Gen1, Name2 = Gen2, ..>` or \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![cfg_attr(not(feature = "std"), no_std)]
gupnik marked this conversation as resolved.
Show resolved Hide resolved

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;

// The struct on which we build all of our Pallet logic.
#[pallet::pallet]
pub struct Pallet<T>(_);

// Your Pallet's configuration trait, representing custom external types and interfaces.
#[pallet::config]
pub trait Config: frame_system::Config {}

#[pallet::storage]
type MyStorage<T: Config> = StorageValue<_, Vec<u8>>;

#[pallet::storage]
type MyStorageMap<T: Config> = StorageMap<Key = u32, Value = u64>;

#[pallet::storage]
type MyStorageDoubleMap<T: Config> = StorageDoubleMap<Key1 = u32, Key2 = u64, Value = u64>;

#[pallet::storage]
type MyCountedStorageMap<T: Config> = CountedStorageMap<Key = u32, Value = u64>;

// Your Pallet's internal functions.
impl<T: Config> Pallet<T> {}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: Invalid pallet::storage, cannot find `Hasher` generic, required for `StorageMap`.
--> tests/pallet_ui/dev_mode_explicit_key_without_arg_default_hasher.rs:21:43
|
21 | type MyStorageMap<T: Config> = StorageMap<Key = u32, Value = u64>;
| ^

error[E0432]: unresolved import `pallet`
--> tests/pallet_ui/dev_mode_explicit_key_without_arg_default_hasher.rs:3:9
|
3 | pub use pallet::*;
| ^^^^^^ help: a similar path exists: `test_pallet::pallet`
9 changes: 9 additions & 0 deletions frame/support/test/tests/pallet_ui/pass/dev_mode_valid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ pub mod pallet {
#[pallet::storage]
type MyCountedStorageMap<T: Config> = CountedStorageMap<_, _, u32, u64>;

#[pallet::storage]
pub type MyStorageMap2<T: Config> = StorageMap<Key = u32, Value = u64>;

#[pallet::storage]
type MyStorageDoubleMap2<T: Config> = StorageDoubleMap<Key1 = u32, Key2 = u64, Value = u64>;

#[pallet::storage]
type MyCountedStorageMap2<T: Config> = CountedStorageMap<Key = u32, Value = u64>;

// Your Pallet's callable functions.
#[pallet::call]
impl<T: Config> Pallet<T> {
Expand Down