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

generate_storage_alias: Rewrite as proc macro attribute #11387

Merged
merged 11 commits into from
May 17, 2022
10 changes: 4 additions & 6 deletions frame/bags-list/src/list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,10 @@ mod list {
assert_eq!(crate::ListNodes::<Runtime>::count(), 4);
// we do some wacky stuff here to get access to the counter, since it is (reasonably)
// not exposed as mutable in any sense.
frame_support::generate_storage_alias!(
BagsList,
CounterForListNodes
=> Value<u32, frame_support::pallet_prelude::ValueQuery>
);
CounterForListNodes::mutate(|counter| *counter += 1);
#[frame_support::storage_alias]
type CounterForListNodes<T: Config> =
StorageValue<crate::Pallet<T>, u32, frame_support::pallet_prelude::ValueQuery>;
CounterForListNodes::<Runtime>::mutate(|counter| *counter += 1);
assert_eq!(crate::ListNodes::<Runtime>::count(), 5);

assert_eq!(List::<Runtime>::sanity_check(), Err("iter_count != stored_count"));
Expand Down
5 changes: 3 additions & 2 deletions frame/bags-list/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ impl<T: crate::Config> OnRuntimeUpgrade for CheckCounterPrefix<T> {
fn pre_upgrade() -> Result<(), &'static str> {
use frame_support::ensure;
// The old explicit storage item.
frame_support::generate_storage_alias!(BagsList, CounterForListNodes => Value<u32>);
#[frame_support::storage_alias]
type CounterForListNodes<T: crate::Config> = StorageValue<crate::Pallet<T>, u32>;

// ensure that a value exists in the counter struct.
ensure!(
crate::ListNodes::<T>::count() == CounterForListNodes::get().unwrap(),
crate::ListNodes::<T>::count() == CounterForListNodes::<T>::get().unwrap(),
"wrong list node counter"
);

Expand Down
64 changes: 29 additions & 35 deletions frame/contracts/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
use crate::{BalanceOf, CodeHash, Config, Pallet, TrieId, Weight};
use codec::{Decode, Encode};
use frame_support::{
codec, generate_storage_alias,
pallet_prelude::*,
storage::migration,
traits::{Get, PalletInfoAccess},
Identity, Twox64Concat,
codec, pallet_prelude::*, storage::migration, storage_alias, traits::Get, Identity,
Twox64Concat,
};
use sp_std::{marker::PhantomData, prelude::*};

Expand Down Expand Up @@ -119,15 +116,16 @@ mod v5 {
trie_id: TrieId,
}

generate_storage_alias!(
Contracts,
ContractInfoOf<T: Config> => Map<(Twox64Concat, T::AccountId), ContractInfo<T>>
);
#[storage_alias]
type ContractInfoOf<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
ContractInfo<T>,
>;

generate_storage_alias!(
Contracts,
DeletionQueue => Value<Vec<DeletedContract>>
);
#[storage_alias]
type DeletionQueue<T: Config> = StorageValue<Pallet<T>, Vec<DeletedContract>>;

pub fn migrate<T: Config>() -> Weight {
let mut weight: Weight = 0;
Expand All @@ -144,7 +142,7 @@ mod v5 {
}
});

DeletionQueue::translate(|old: Option<Vec<OldDeletedContract>>| {
DeletionQueue::<T>::translate(|old: Option<Vec<OldDeletedContract>>| {
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
old.map(|old| old.into_iter().map(|o| DeletedContract { trie_id: o.trie_id }).collect())
})
Expand Down Expand Up @@ -204,20 +202,19 @@ mod v6 {

type ContractInfo<T> = RawContractInfo<CodeHash<T>, BalanceOf<T>>;

generate_storage_alias!(
Contracts,
ContractInfoOf<T: Config> => Map<(Twox64Concat, T::AccountId), ContractInfo<T>>
);
#[storage_alias]
type ContractInfoOf<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
ContractInfo<T>,
>;

generate_storage_alias!(
Contracts,
CodeStorage<T: Config> => Map<(Identity, CodeHash<T>), PrefabWasmModule>
);
#[storage_alias]
type CodeStorage<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, PrefabWasmModule>;

generate_storage_alias!(
Contracts,
OwnerInfoOf<T: Config> => Map<(Identity, CodeHash<T>), OwnerInfo<T>>
);
#[storage_alias]
type OwnerInfoOf<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, OwnerInfo<T>>;

pub fn migrate<T: Config>() -> Weight {
let mut weight: Weight = 0;
Expand Down Expand Up @@ -261,15 +258,12 @@ mod v7 {
use super::*;

pub fn migrate<T: Config>() -> Weight {
generate_storage_alias!(
Contracts,
AccountCounter => Value<u64, ValueQuery>
);
generate_storage_alias!(
Contracts,
Nonce => Value<u64, ValueQuery>
);
Nonce::set(AccountCounter::take());
#[storage_alias]
type AccountCounter<T: Config> = StorageValue<Pallet<T>, u64, ValueQuery>;
#[storage_alias]
type Nonce<T: Config> = StorageValue<Pallet<T>, u64, ValueQuery>;

Nonce::<T>::set(AccountCounter::<T>::take());
T::DbWeight::get().reads_writes(1, 2)
}
}
98 changes: 49 additions & 49 deletions frame/elections-phragmen/src/migrations/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

//! Migrations to version [`3.0.0`], as denoted by the changelog.

use crate::{Config, Pallet};
use codec::{Decode, Encode, FullCodec};
use frame_support::{
pallet_prelude::ValueQuery,
traits::{PalletInfoAccess, StorageVersion},
weights::Weight,
RuntimeDebug, Twox64Concat,
pallet_prelude::ValueQuery, traits::StorageVersion, weights::Weight, RuntimeDebug, Twox64Concat,
};
use sp_std::prelude::*;

Expand All @@ -42,40 +40,38 @@ struct Voter<AccountId, Balance> {

/// Trait to implement to give information about types used for migration
pub trait V2ToV3 {
/// The elections-phragmen pallet.
type Pallet: 'static + PalletInfoAccess;

/// System config account id
type AccountId: 'static + FullCodec;

/// Elections-phragmen currency balance.
type Balance: 'static + FullCodec + Copy;
}

frame_support::generate_storage_alias!(
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>>,
ValueQuery
>
);
frame_support::generate_storage_alias!(
PhragmenElection, RunnersUp<T: V2ToV3> => Value<
Vec<SeatHolder<T::AccountId, T::Balance>>,
ValueQuery
>
);
frame_support::generate_storage_alias!(
PhragmenElection, Voting<T: V2ToV3> => Map<
(Twox64Concat, T::AccountId),
Voter<T::AccountId, T::Balance>
>
);
#[frame_support::storage_alias]
type Candidates<V, T: Config> =
StorageValue<Pallet<T>, Vec<(<V as V2ToV3>::AccountId, <V as V2ToV3>::Balance)>, ValueQuery>;

#[frame_support::storage_alias]
type Members<V, T: Config> = StorageValue<
Pallet<T>,
Vec<SeatHolder<<V as V2ToV3>::AccountId, <V as V2ToV3>::Balance>>,
ValueQuery,
>;

#[frame_support::storage_alias]
type RunnersUp<V, T: Config> = StorageValue<
Pallet<T>,
Vec<SeatHolder<<V as V2ToV3>::AccountId, <V as V2ToV3>::Balance>>,
ValueQuery,
>;

#[frame_support::storage_alias]
type Voting<V, T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<V as V2ToV3>::AccountId,
Voter<<V as V2ToV3>::AccountId, <V as V2ToV3>::Balance>,
>;

/// Apply all of the migrations from 2 to 3.
///
Expand All @@ -86,21 +82,24 @@ frame_support::generate_storage_alias!(
///
/// Be aware that this migration is intended to be used only for the mentioned versions. Use
/// with care and run at your own risk.
pub fn apply<T: V2ToV3>(old_voter_bond: T::Balance, old_candidacy_bond: T::Balance) -> Weight {
let storage_version = StorageVersion::get::<T::Pallet>();
pub fn apply<V: V2ToV3, T: Config>(
old_voter_bond: V::Balance,
old_candidacy_bond: V::Balance,
) -> Weight {
let storage_version = StorageVersion::get::<Pallet<T>>();
log::info!(
target: "runtime::elections-phragmen",
"Running migration for elections-phragmen with storage version {:?}",
storage_version,
);

if storage_version <= 2 {
migrate_voters_to_recorded_deposit::<T>(old_voter_bond);
migrate_candidates_to_recorded_deposit::<T>(old_candidacy_bond);
migrate_runners_up_to_recorded_deposit::<T>(old_candidacy_bond);
migrate_members_to_recorded_deposit::<T>(old_candidacy_bond);
migrate_voters_to_recorded_deposit::<V, T>(old_voter_bond);
migrate_candidates_to_recorded_deposit::<V, T>(old_candidacy_bond);
migrate_runners_up_to_recorded_deposit::<V, T>(old_candidacy_bond);
migrate_members_to_recorded_deposit::<V, T>(old_candidacy_bond);

StorageVersion::new(3).put::<T::Pallet>();
StorageVersion::new(3).put::<Pallet<T>>();

Weight::max_value()
} else {
Expand All @@ -114,21 +113,21 @@ pub fn apply<T: V2ToV3>(old_voter_bond: T::Balance, old_candidacy_bond: T::Balan
}

/// Migrate from the old legacy voting bond (fixed) to the new one (per-vote dynamic).
pub fn migrate_voters_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
<Voting<T>>::translate::<(T::Balance, Vec<T::AccountId>), _>(|_who, (stake, votes)| {
pub fn migrate_voters_to_recorded_deposit<V: V2ToV3, T: Config>(old_deposit: V::Balance) {
<Voting<V, T>>::translate::<(V::Balance, Vec<V::AccountId>), _>(|_who, (stake, votes)| {
Some(Voter { votes, stake, deposit: old_deposit })
});

log::info!(
target: "runtime::elections-phragmen",
"migrated {} voter accounts.",
<Voting<T>>::iter().count(),
<Voting<V, T>>::iter().count(),
);
}

/// Migrate all candidates to recorded deposit.
pub fn migrate_candidates_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
let _ = <Candidates<T>>::translate::<Vec<T::AccountId>, _>(|maybe_old_candidates| {
pub fn migrate_candidates_to_recorded_deposit<V: V2ToV3, T: Config>(old_deposit: V::Balance) {
let _ = <Candidates<V, T>>::translate::<Vec<V::AccountId>, _>(|maybe_old_candidates| {
maybe_old_candidates.map(|old_candidates| {
log::info!(
target: "runtime::elections-phragmen",
Expand All @@ -141,8 +140,8 @@ pub fn migrate_candidates_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance
}

/// Migrate all members to recorded deposit.
pub fn migrate_members_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
let _ = <Members<T>>::translate::<Vec<(T::AccountId, T::Balance)>, _>(|maybe_old_members| {
pub fn migrate_members_to_recorded_deposit<V: V2ToV3, T: Config>(old_deposit: V::Balance) {
let _ = <Members<V, T>>::translate::<Vec<(V::AccountId, V::Balance)>, _>(|maybe_old_members| {
maybe_old_members.map(|old_members| {
log::info!(
target: "runtime::elections-phragmen",
Expand All @@ -158,9 +157,9 @@ pub fn migrate_members_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
}

/// Migrate all runners-up to recorded deposit.
pub fn migrate_runners_up_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
let _ =
<RunnersUp<T>>::translate::<Vec<(T::AccountId, T::Balance)>, _>(|maybe_old_runners_up| {
pub fn migrate_runners_up_to_recorded_deposit<V: V2ToV3, T: Config>(old_deposit: V::Balance) {
let _ = <RunnersUp<V, T>>::translate::<Vec<(V::AccountId, V::Balance)>, _>(
|maybe_old_runners_up| {
maybe_old_runners_up.map(|old_runners_up| {
log::info!(
target: "runtime::elections-phragmen",
Expand All @@ -172,5 +171,6 @@ pub fn migrate_runners_up_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance
.map(|(who, stake)| SeatHolder { who, stake, deposit: old_deposit })
.collect::<Vec<_>>()
})
});
},
);
}
11 changes: 4 additions & 7 deletions frame/offences/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
// limitations under the License.

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

Expand All @@ -31,10 +29,9 @@ type DeferredOffenceOf<T> = (

// Deferred reports that have been rejected by the offence handler and need to be submitted
// at a later time.
generate_storage_alias!(
Offences,
DeferredOffences<T: Config> => Value<Vec<DeferredOffenceOf<T>>, ValueQuery>
);
#[storage_alias]
type DeferredOffences<T: Config> =
StorageValue<crate::Pallet<T>, Vec<DeferredOffenceOf<T>>, ValueQuery>;

pub fn remove_deferred_storage<T: Config>() -> Weight {
let mut weight = T::DbWeight::get().reads_writes(1, 1);
Expand Down
Loading