mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 00:31:07 +00:00
Adds ability to use default hasher in dev_mode for explicit key binding (#14164)
* Initial setup * Minor update * Minor update * Addresses review comments * Addresses review comments * Updates doc * ".git/.scripts/commands/fmt/fmt.sh" * Renames file * Updates path in test --------- Co-authored-by: command-bot <>
This commit is contained in:
@@ -468,7 +468,8 @@ pub fn construct_runtime(input: TokenStream) -> TokenStream {
|
|||||||
/// storage types. This is equivalent to specifying `#[pallet::unbounded]` on all storage type
|
/// storage types. This is equivalent to specifying `#[pallet::unbounded]` on all storage type
|
||||||
/// definitions.
|
/// definitions.
|
||||||
/// * Storage hashers no longer need to be specified and can be replaced by `_`. In dev mode, these
|
/// * Storage hashers no longer need to be specified and can be replaced by `_`. In dev mode, these
|
||||||
/// will be replaced by `Blake2_128Concat`.
|
/// will be replaced by `Blake2_128Concat`. In case of explicit key-binding, `Hasher` can simply
|
||||||
|
/// be ignored when in `dev_mode`.
|
||||||
///
|
///
|
||||||
/// Note that the `dev_mode` argument can only be supplied to the `#[pallet]` or
|
/// Note that the `dev_mode` argument can only be supplied to the `#[pallet]` or
|
||||||
/// `#[frame_support::pallet]` attribute macro that encloses your pallet module. This argument
|
/// `#[frame_support::pallet]` attribute macro that encloses your pallet module. This argument
|
||||||
|
|||||||
@@ -332,6 +332,7 @@ fn process_named_generics(
|
|||||||
storage: &StorageKind,
|
storage: &StorageKind,
|
||||||
args_span: proc_macro2::Span,
|
args_span: proc_macro2::Span,
|
||||||
args: &[syn::AssocType],
|
args: &[syn::AssocType],
|
||||||
|
dev_mode: bool,
|
||||||
) -> syn::Result<(Option<StorageGenerics>, Metadata, Option<syn::Type>, bool)> {
|
) -> syn::Result<(Option<StorageGenerics>, Metadata, Option<syn::Type>, bool)> {
|
||||||
let mut parsed = HashMap::<String, syn::AssocType>::new();
|
let mut parsed = HashMap::<String, syn::AssocType>::new();
|
||||||
|
|
||||||
@@ -346,6 +347,14 @@ fn process_named_generics(
|
|||||||
parsed.insert(arg.ident.to_string(), arg.clone());
|
parsed.insert(arg.ident.to_string(), arg.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut map_mandatory_generics = vec!["Key", "Value"];
|
||||||
|
let mut map_optional_generics = vec!["QueryKind", "OnEmpty", "MaxValues"];
|
||||||
|
if dev_mode {
|
||||||
|
map_optional_generics.push("Hasher");
|
||||||
|
} else {
|
||||||
|
map_mandatory_generics.push("Hasher");
|
||||||
|
}
|
||||||
|
|
||||||
let generics = match storage {
|
let generics = match storage {
|
||||||
StorageKind::Value => {
|
StorageKind::Value => {
|
||||||
check_generics(
|
check_generics(
|
||||||
@@ -368,8 +377,8 @@ fn process_named_generics(
|
|||||||
StorageKind::Map => {
|
StorageKind::Map => {
|
||||||
check_generics(
|
check_generics(
|
||||||
&parsed,
|
&parsed,
|
||||||
&["Hasher", "Key", "Value"],
|
&map_mandatory_generics,
|
||||||
&["QueryKind", "OnEmpty", "MaxValues"],
|
&map_optional_generics,
|
||||||
"StorageMap",
|
"StorageMap",
|
||||||
args_span,
|
args_span,
|
||||||
)?;
|
)?;
|
||||||
@@ -378,7 +387,7 @@ fn process_named_generics(
|
|||||||
hasher: parsed
|
hasher: parsed
|
||||||
.remove("Hasher")
|
.remove("Hasher")
|
||||||
.map(|binding| binding.ty)
|
.map(|binding| binding.ty)
|
||||||
.expect("checked above as mandatory generic"),
|
.unwrap_or(syn::parse_quote!(Blake2_128Concat)),
|
||||||
key: parsed
|
key: parsed
|
||||||
.remove("Key")
|
.remove("Key")
|
||||||
.map(|binding| binding.ty)
|
.map(|binding| binding.ty)
|
||||||
@@ -395,8 +404,8 @@ fn process_named_generics(
|
|||||||
StorageKind::CountedMap => {
|
StorageKind::CountedMap => {
|
||||||
check_generics(
|
check_generics(
|
||||||
&parsed,
|
&parsed,
|
||||||
&["Hasher", "Key", "Value"],
|
&map_mandatory_generics,
|
||||||
&["QueryKind", "OnEmpty", "MaxValues"],
|
&map_optional_generics,
|
||||||
"CountedStorageMap",
|
"CountedStorageMap",
|
||||||
args_span,
|
args_span,
|
||||||
)?;
|
)?;
|
||||||
@@ -405,7 +414,7 @@ fn process_named_generics(
|
|||||||
hasher: parsed
|
hasher: parsed
|
||||||
.remove("Hasher")
|
.remove("Hasher")
|
||||||
.map(|binding| binding.ty)
|
.map(|binding| binding.ty)
|
||||||
.expect("checked above as mandatory generic"),
|
.unwrap_or(syn::Type::Verbatim(quote::quote! { Blake2_128Concat })),
|
||||||
key: parsed
|
key: parsed
|
||||||
.remove("Key")
|
.remove("Key")
|
||||||
.map(|binding| binding.ty)
|
.map(|binding| binding.ty)
|
||||||
@@ -420,10 +429,17 @@ fn process_named_generics(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
StorageKind::DoubleMap => {
|
StorageKind::DoubleMap => {
|
||||||
|
let mut double_map_mandatory_generics = vec!["Key1", "Key2", "Value"];
|
||||||
|
if dev_mode {
|
||||||
|
map_optional_generics.extend(["Hasher1", "Hasher2"]);
|
||||||
|
} else {
|
||||||
|
double_map_mandatory_generics.extend(["Hasher1", "Hasher2"]);
|
||||||
|
}
|
||||||
|
|
||||||
check_generics(
|
check_generics(
|
||||||
&parsed,
|
&parsed,
|
||||||
&["Hasher1", "Key1", "Hasher2", "Key2", "Value"],
|
&double_map_mandatory_generics,
|
||||||
&["QueryKind", "OnEmpty", "MaxValues"],
|
&map_optional_generics,
|
||||||
"StorageDoubleMap",
|
"StorageDoubleMap",
|
||||||
args_span,
|
args_span,
|
||||||
)?;
|
)?;
|
||||||
@@ -432,7 +448,7 @@ fn process_named_generics(
|
|||||||
hasher1: parsed
|
hasher1: parsed
|
||||||
.remove("Hasher1")
|
.remove("Hasher1")
|
||||||
.map(|binding| binding.ty)
|
.map(|binding| binding.ty)
|
||||||
.expect("checked above as mandatory generic"),
|
.unwrap_or(syn::parse_quote!(Blake2_128Concat)),
|
||||||
key1: parsed
|
key1: parsed
|
||||||
.remove("Key1")
|
.remove("Key1")
|
||||||
.map(|binding| binding.ty)
|
.map(|binding| binding.ty)
|
||||||
@@ -440,7 +456,7 @@ fn process_named_generics(
|
|||||||
hasher2: parsed
|
hasher2: parsed
|
||||||
.remove("Hasher2")
|
.remove("Hasher2")
|
||||||
.map(|binding| binding.ty)
|
.map(|binding| binding.ty)
|
||||||
.expect("checked above as mandatory generic"),
|
.unwrap_or(syn::parse_quote!(Blake2_128Concat)),
|
||||||
key2: parsed
|
key2: parsed
|
||||||
.remove("Key2")
|
.remove("Key2")
|
||||||
.map(|binding| binding.ty)
|
.map(|binding| binding.ty)
|
||||||
@@ -619,7 +635,7 @@ fn process_generics(
|
|||||||
_ => unreachable!("It is asserted above that all generics are bindings"),
|
_ => unreachable!("It is asserted above that all generics are bindings"),
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
process_named_generics(&storage_kind, args_span, &args)
|
process_named_generics(&storage_kind, args_span, &args, dev_mode)
|
||||||
} else {
|
} else {
|
||||||
let msg = "Invalid pallet::storage, invalid generic declaration for storage. Expect only \
|
let msg = "Invalid pallet::storage, invalid generic declaration for storage. Expect only \
|
||||||
type generics or binding generics, e.g. `<Name1 = Gen1, Name2 = Gen2, ..>` or \
|
type generics or binding generics, e.g. `<Name1 = Gen1, Name2 = Gen2, ..>` or \
|
||||||
|
|||||||
+33
@@ -0,0 +1,33 @@
|
|||||||
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
|
|
||||||
|
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() {}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
error: Invalid pallet::storage, cannot find `Hasher` generic, required for `StorageMap`.
|
||||||
|
--> tests/pallet_ui/non_dev_mode_storage_map_explicit_key_default_hasher.rs:21:43
|
||||||
|
|
|
||||||
|
21 | type MyStorageMap<T: Config> = StorageMap<Key = u32, Value = u64>;
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error[E0432]: unresolved import `pallet`
|
||||||
|
--> tests/pallet_ui/non_dev_mode_storage_map_explicit_key_default_hasher.rs:3:9
|
||||||
|
|
|
||||||
|
3 | pub use pallet::*;
|
||||||
|
| ^^^^^^ help: a similar path exists: `test_pallet::pallet`
|
||||||
@@ -35,6 +35,15 @@ pub mod pallet {
|
|||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
type MyCountedStorageMap<T: Config> = CountedStorageMap<_, _, u32, u64>;
|
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.
|
// Your Pallet's callable functions.
|
||||||
#[pallet::call]
|
#[pallet::call]
|
||||||
impl<T: Config> Pallet<T> {
|
impl<T: Config> Pallet<T> {
|
||||||
|
|||||||
Reference in New Issue
Block a user