Skip to content

Commit

Permalink
add query types to generate_storage_alias (paritytech#9659)
Browse files Browse the repository at this point in the history
* add query types to generate_storage_alias

* adjust comment

* use ValueQuery explicitly for generate_storage_alias with generic value type

* bump impl_version

* adjust line width and add import

* more compilation and formatting fixes

* formatting
  • Loading branch information
apopiak authored Sep 8, 2021
1 parent e45dab3 commit 89cd02d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 267,
impl_version: 0,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
};
Expand Down
16 changes: 13 additions & 3 deletions frame/elections-phragmen/src/migrations/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use codec::{Decode, Encode, FullCodec};
use frame_support::{
pallet_prelude::ValueQuery,
traits::{PalletInfoAccess, StorageVersion},
weights::Weight,
RuntimeDebug, Twox64Concat,
Expand Down Expand Up @@ -52,13 +53,22 @@ pub trait V2ToV3 {
}

frame_support::generate_storage_alias!(
PhragmenElection, Candidates<T: V2ToV3> => Value<Vec<(T::AccountId, T::Balance)>>
PhragmenElection, Candidates<T: V2ToV3> => Value<
Vec<(T::AccountId, T::Balance)>,
ValueQuery
>
);
frame_support::generate_storage_alias!(
PhragmenElection, Members<T: V2ToV3> => Value<Vec<SeatHolder<T::AccountId, T::Balance>>>
PhragmenElection, Members<T: V2ToV3> => Value<
Vec<SeatHolder<T::AccountId, T::Balance>>,
ValueQuery
>
);
frame_support::generate_storage_alias!(
PhragmenElection, RunnersUp<T: V2ToV3> => Value<Vec<SeatHolder<T::AccountId, T::Balance>>>
PhragmenElection, RunnersUp<T: V2ToV3> => Value<
Vec<SeatHolder<T::AccountId, T::Balance>>,
ValueQuery
>
);
frame_support::generate_storage_alias!(
PhragmenElection, Voting<T: V2ToV3> => Map<
Expand Down
6 changes: 4 additions & 2 deletions frame/offences/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
// limitations under the License.

use super::{Config, OffenceDetails, Perbill, SessionIndex};
use frame_support::{generate_storage_alias, traits::Get, weights::Weight};
use frame_support::{
generate_storage_alias, pallet_prelude::ValueQuery, traits::Get, weights::Weight,
};
use sp_staking::offence::OnOffenceHandler;
use sp_std::vec::Vec;

Expand All @@ -31,7 +33,7 @@ type DeferredOffenceOf<T> = (
// at a later time.
generate_storage_alias!(
Offences,
DeferredOffences<T: Config> => Value<Vec<DeferredOffenceOf<T>>>
DeferredOffences<T: Config> => Value<Vec<DeferredOffenceOf<T>>, ValueQuery>
);

pub fn remove_deferred_storage<T: Config>() -> Weight {
Expand Down
51 changes: 42 additions & 9 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ impl TypeId for PalletId {
/// >
/// );
///
/// // optionally specify the query type
/// use frame_support::pallet_prelude::{ValueQuery, OptionQuery};
/// generate_storage_alias!(Prefix, ValueName => Value<u32, OptionQuery>);
/// generate_storage_alias!(
/// Prefix, SomeStorageName => DoubleMap<
/// (u32, Twox64Concat),
/// (u32, Twox64Concat),
/// Vec<u8>,
/// ValueQuery
/// >
/// );
///
/// // generate a map from `Config::AccountId` (with hasher `Twox64Concat`) to `Vec<u8>`
/// trait Config { type AccountId: codec::FullCodec; }
/// generate_storage_alias!(
Expand All @@ -140,18 +152,23 @@ impl TypeId for PalletId {
#[macro_export]
macro_rules! generate_storage_alias {
// without generic for $name.
($pallet:ident, $name:ident => Map<($key:ty, $hasher:ty), $value:ty>) => {
($pallet:ident, $name:ident => Map<($key:ty, $hasher:ty), $value:ty $(, $querytype:ty)?>) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
type $name = $crate::storage::types::StorageMap<
[<$name Instance>],
$hasher,
$key,
$value,
$( $querytype )?
>;
}
};
($pallet:ident, $name:ident => DoubleMap<($key1:ty, $hasher1:ty), ($key2:ty, $hasher2:ty), $value:ty>) => {
(
$pallet:ident,
$name:ident
=> DoubleMap<($key1:ty, $hasher1:ty), ($key2:ty, $hasher2:ty), $value:ty $(, $querytype:ty)?>
) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
type $name = $crate::storage::types::StorageDoubleMap<
Expand All @@ -161,10 +178,15 @@ macro_rules! generate_storage_alias {
$hasher2,
$key2,
$value,
$( $querytype )?
>;
}
};
($pallet:ident, $name:ident => NMap<Key<$(($key:ty, $hasher:ty)),+>, $value:ty>) => {
(
$pallet:ident,
$name:ident
=> NMap<Key<$(($key:ty, $hasher:ty)),+>, $value:ty $(, $querytype:ty)?>
) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
type $name = $crate::storage::types::StorageNMap<
Expand All @@ -173,20 +195,26 @@ macro_rules! generate_storage_alias {
$( $crate::storage::types::Key<$hasher, $key>, )+
),
$value,
$( $querytype )?
>;
}
};
($pallet:ident, $name:ident => Value<$value:ty>) => {
($pallet:ident, $name:ident => Value<$value:ty $(, $querytype:ty)?>) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
type $name = $crate::storage::types::StorageValue<
[<$name Instance>],
$value,
$( $querytype )?
>;
}
};
// with generic for $name.
($pallet:ident, $name:ident<$t:ident : $bounds:tt> => Map<($key:ty, $hasher:ty), $value:ty>) => {
(
$pallet:ident,
$name:ident<$t:ident : $bounds:tt>
=> Map<($key:ty, $hasher:ty), $value:ty $(, $querytype:ty)?>
) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
#[allow(type_alias_bounds)]
Expand All @@ -195,13 +223,15 @@ macro_rules! generate_storage_alias {
$key,
$hasher,
$value,
$( $querytype )?
>;
}
};
(
$pallet:ident,
$name:ident<$t:ident : $bounds:tt>
=> DoubleMap<($key1:ty, $hasher1:ty), ($key2:ty, $hasher2:ty), $value:ty>) => {
=> DoubleMap<($key1:ty, $hasher1:ty), ($key2:ty, $hasher2:ty), $value:ty $(, $querytype:ty)?>
) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
#[allow(type_alias_bounds)]
Expand All @@ -212,12 +242,14 @@ macro_rules! generate_storage_alias {
$key2,
$hasher2,
$value,
$( $querytype )?
>;
}
};
(
$pallet:ident,
$name:ident<$t:ident : $bounds:tt> => NMap<$(($key:ty, $hasher:ty),)+ $value:ty>
$name:ident<$t:ident : $bounds:tt>
=> NMap<$(($key:ty, $hasher:ty),)+ $value:ty $(, $querytype:ty)?>
) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
Expand All @@ -228,17 +260,18 @@ macro_rules! generate_storage_alias {
$( $crate::storage::types::Key<$hasher, $key>, )+
),
$value,
$( $querytype )?
>;
}
};
($pallet:ident, $name:ident<$t:ident : $bounds:tt> => Value<$value:ty>) => {
($pallet:ident, $name:ident<$t:ident : $bounds:tt> => Value<$value:ty $(, $querytype:ty)?>) => {
$crate::paste::paste! {
$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
#[allow(type_alias_bounds)]
type $name<$t : $bounds> = $crate::storage::types::StorageValue<
[<$name Instance>],
$value,
$crate::storage::types::ValueQuery,
$( $querytype )?
>;
}
};
Expand Down

0 comments on commit 89cd02d

Please sign in to comment.