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

Commit 19b44f0

Browse files
authored
generate_storage_alias: Rewrite as proc macro attribute (#11387)
* generate_storage_alias: Rewrite as proc macro attribute This rewrites the `generate_storage_alias!` declarative macro as proc-macro attribute. While doing this the name is changed to `storage_alias`. The prefix can now also be the name of a pallet. This makes storage aliases work in migrations for all kind of chains and not just for the ones that use predefined prefixes. * Fix compilation and FMT * Moare fixes * 🤦 * ...... * Rework the syntax and support instancing * FMT * Prefix variants with `Storage` * Make it compile * Fix where clause on rust stable
1 parent 2823dc4 commit 19b44f0

File tree

21 files changed

+886
-381
lines changed

21 files changed

+886
-381
lines changed

frame/bags-list/src/list/tests.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,10 @@ mod list {
368368
assert_eq!(crate::ListNodes::<Runtime>::count(), 4);
369369
// we do some wacky stuff here to get access to the counter, since it is (reasonably)
370370
// not exposed as mutable in any sense.
371-
frame_support::generate_storage_alias!(
372-
BagsList,
373-
CounterForListNodes
374-
=> Value<u32, frame_support::pallet_prelude::ValueQuery>
375-
);
376-
CounterForListNodes::mutate(|counter| *counter += 1);
371+
#[frame_support::storage_alias]
372+
type CounterForListNodes<T: Config> =
373+
StorageValue<crate::Pallet<T>, u32, frame_support::pallet_prelude::ValueQuery>;
374+
CounterForListNodes::<Runtime>::mutate(|counter| *counter += 1);
377375
assert_eq!(crate::ListNodes::<Runtime>::count(), 5);
378376

379377
assert_eq!(List::<Runtime>::sanity_check(), Err("iter_count != stored_count"));

frame/bags-list/src/migrations.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ impl<T: crate::Config> OnRuntimeUpgrade for CheckCounterPrefix<T> {
3030
fn pre_upgrade() -> Result<(), &'static str> {
3131
use frame_support::ensure;
3232
// The old explicit storage item.
33-
frame_support::generate_storage_alias!(BagsList, CounterForListNodes => Value<u32>);
33+
#[frame_support::storage_alias]
34+
type CounterForListNodes<T: crate::Config> = StorageValue<crate::Pallet<T>, u32>;
3435

3536
// ensure that a value exists in the counter struct.
3637
ensure!(
37-
crate::ListNodes::<T>::count() == CounterForListNodes::get().unwrap(),
38+
crate::ListNodes::<T>::count() == CounterForListNodes::<T>::get().unwrap(),
3839
"wrong list node counter"
3940
);
4041

frame/contracts/src/migration.rs

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
use crate::{BalanceOf, CodeHash, Config, Pallet, TrieId, Weight};
1919
use codec::{Decode, Encode};
2020
use frame_support::{
21-
codec, generate_storage_alias,
22-
pallet_prelude::*,
23-
storage::migration,
24-
traits::{Get, PalletInfoAccess},
25-
Identity, Twox64Concat,
21+
codec, pallet_prelude::*, storage::migration, storage_alias, traits::Get, Identity,
22+
Twox64Concat,
2623
};
2724
use sp_std::{marker::PhantomData, prelude::*};
2825

@@ -117,15 +114,16 @@ mod v5 {
117114
trie_id: TrieId,
118115
}
119116

120-
generate_storage_alias!(
121-
Contracts,
122-
ContractInfoOf<T: Config> => Map<(Twox64Concat, T::AccountId), ContractInfo<T>>
123-
);
117+
#[storage_alias]
118+
type ContractInfoOf<T: Config> = StorageMap<
119+
Pallet<T>,
120+
Twox64Concat,
121+
<T as frame_system::Config>::AccountId,
122+
ContractInfo<T>,
123+
>;
124124

125-
generate_storage_alias!(
126-
Contracts,
127-
DeletionQueue => Value<Vec<DeletedContract>>
128-
);
125+
#[storage_alias]
126+
type DeletionQueue<T: Config> = StorageValue<Pallet<T>, Vec<DeletedContract>>;
129127

130128
pub fn migrate<T: Config>() -> Weight {
131129
let mut weight: Weight = 0;
@@ -142,7 +140,7 @@ mod v5 {
142140
}
143141
});
144142

145-
DeletionQueue::translate(|old: Option<Vec<OldDeletedContract>>| {
143+
DeletionQueue::<T>::translate(|old: Option<Vec<OldDeletedContract>>| {
146144
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
147145
old.map(|old| old.into_iter().map(|o| DeletedContract { trie_id: o.trie_id }).collect())
148146
})
@@ -202,20 +200,19 @@ mod v6 {
202200

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

205-
generate_storage_alias!(
206-
Contracts,
207-
ContractInfoOf<T: Config> => Map<(Twox64Concat, T::AccountId), ContractInfo<T>>
208-
);
203+
#[storage_alias]
204+
type ContractInfoOf<T: Config> = StorageMap<
205+
Pallet<T>,
206+
Twox64Concat,
207+
<T as frame_system::Config>::AccountId,
208+
ContractInfo<T>,
209+
>;
209210

210-
generate_storage_alias!(
211-
Contracts,
212-
CodeStorage<T: Config> => Map<(Identity, CodeHash<T>), PrefabWasmModule>
213-
);
211+
#[storage_alias]
212+
type CodeStorage<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, PrefabWasmModule>;
214213

215-
generate_storage_alias!(
216-
Contracts,
217-
OwnerInfoOf<T: Config> => Map<(Identity, CodeHash<T>), OwnerInfo<T>>
218-
);
214+
#[storage_alias]
215+
type OwnerInfoOf<T: Config> = StorageMap<Pallet<T>, Identity, CodeHash<T>, OwnerInfo<T>>;
219216

220217
pub fn migrate<T: Config>() -> Weight {
221218
let mut weight: Weight = 0;
@@ -259,15 +256,12 @@ mod v7 {
259256
use super::*;
260257

261258
pub fn migrate<T: Config>() -> Weight {
262-
generate_storage_alias!(
263-
Contracts,
264-
AccountCounter => Value<u64, ValueQuery>
265-
);
266-
generate_storage_alias!(
267-
Contracts,
268-
Nonce => Value<u64, ValueQuery>
269-
);
270-
Nonce::set(AccountCounter::take());
259+
#[storage_alias]
260+
type AccountCounter<T: Config> = StorageValue<Pallet<T>, u64, ValueQuery>;
261+
#[storage_alias]
262+
type Nonce<T: Config> = StorageValue<Pallet<T>, u64, ValueQuery>;
263+
264+
Nonce::<T>::set(AccountCounter::<T>::take());
271265
T::DbWeight::get().reads_writes(1, 2)
272266
}
273267
}

frame/elections-phragmen/src/migrations/v3.rs

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
//! Migrations to version [`3.0.0`], as denoted by the changelog.
1919
20+
use crate::{Config, Pallet};
2021
use codec::{Decode, Encode, FullCodec};
2122
use frame_support::{
22-
pallet_prelude::ValueQuery,
23-
traits::{PalletInfoAccess, StorageVersion},
24-
weights::Weight,
25-
RuntimeDebug, Twox64Concat,
23+
pallet_prelude::ValueQuery, traits::StorageVersion, weights::Weight, RuntimeDebug, Twox64Concat,
2624
};
2725
use sp_std::prelude::*;
2826

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

4341
/// Trait to implement to give information about types used for migration
4442
pub trait V2ToV3 {
45-
/// The elections-phragmen pallet.
46-
type Pallet: 'static + PalletInfoAccess;
47-
4843
/// System config account id
4944
type AccountId: 'static + FullCodec;
5045

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

55-
frame_support::generate_storage_alias!(
56-
PhragmenElection, Candidates<T: V2ToV3> => Value<
57-
Vec<(T::AccountId, T::Balance)>,
58-
ValueQuery
59-
>
60-
);
61-
frame_support::generate_storage_alias!(
62-
PhragmenElection, Members<T: V2ToV3> => Value<
63-
Vec<SeatHolder<T::AccountId, T::Balance>>,
64-
ValueQuery
65-
>
66-
);
67-
frame_support::generate_storage_alias!(
68-
PhragmenElection, RunnersUp<T: V2ToV3> => Value<
69-
Vec<SeatHolder<T::AccountId, T::Balance>>,
70-
ValueQuery
71-
>
72-
);
73-
frame_support::generate_storage_alias!(
74-
PhragmenElection, Voting<T: V2ToV3> => Map<
75-
(Twox64Concat, T::AccountId),
76-
Voter<T::AccountId, T::Balance>
77-
>
78-
);
50+
#[frame_support::storage_alias]
51+
type Candidates<V, T: Config> =
52+
StorageValue<Pallet<T>, Vec<(<V as V2ToV3>::AccountId, <V as V2ToV3>::Balance)>, ValueQuery>;
53+
54+
#[frame_support::storage_alias]
55+
type Members<V, T: Config> = StorageValue<
56+
Pallet<T>,
57+
Vec<SeatHolder<<V as V2ToV3>::AccountId, <V as V2ToV3>::Balance>>,
58+
ValueQuery,
59+
>;
60+
61+
#[frame_support::storage_alias]
62+
type RunnersUp<V, T: Config> = StorageValue<
63+
Pallet<T>,
64+
Vec<SeatHolder<<V as V2ToV3>::AccountId, <V as V2ToV3>::Balance>>,
65+
ValueQuery,
66+
>;
67+
68+
#[frame_support::storage_alias]
69+
type Voting<V, T: Config> = StorageMap<
70+
Pallet<T>,
71+
Twox64Concat,
72+
<V as V2ToV3>::AccountId,
73+
Voter<<V as V2ToV3>::AccountId, <V as V2ToV3>::Balance>,
74+
>;
7975

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

9796
if storage_version <= 2 {
98-
migrate_voters_to_recorded_deposit::<T>(old_voter_bond);
99-
migrate_candidates_to_recorded_deposit::<T>(old_candidacy_bond);
100-
migrate_runners_up_to_recorded_deposit::<T>(old_candidacy_bond);
101-
migrate_members_to_recorded_deposit::<T>(old_candidacy_bond);
97+
migrate_voters_to_recorded_deposit::<V, T>(old_voter_bond);
98+
migrate_candidates_to_recorded_deposit::<V, T>(old_candidacy_bond);
99+
migrate_runners_up_to_recorded_deposit::<V, T>(old_candidacy_bond);
100+
migrate_members_to_recorded_deposit::<V, T>(old_candidacy_bond);
102101

103-
StorageVersion::new(3).put::<T::Pallet>();
102+
StorageVersion::new(3).put::<Pallet<T>>();
104103

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

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

122121
log::info!(
123122
target: "runtime::elections-phragmen",
124123
"migrated {} voter accounts.",
125-
<Voting<T>>::iter().count(),
124+
<Voting<V, T>>::iter().count(),
126125
);
127126
}
128127

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

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

160159
/// Migrate all runners-up to recorded deposit.
161-
pub fn migrate_runners_up_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
162-
let _ =
163-
<RunnersUp<T>>::translate::<Vec<(T::AccountId, T::Balance)>, _>(|maybe_old_runners_up| {
160+
pub fn migrate_runners_up_to_recorded_deposit<V: V2ToV3, T: Config>(old_deposit: V::Balance) {
161+
let _ = <RunnersUp<V, T>>::translate::<Vec<(V::AccountId, V::Balance)>, _>(
162+
|maybe_old_runners_up| {
164163
maybe_old_runners_up.map(|old_runners_up| {
165164
log::info!(
166165
target: "runtime::elections-phragmen",
@@ -172,5 +171,6 @@ pub fn migrate_runners_up_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance
172171
.map(|(who, stake)| SeatHolder { who, stake, deposit: old_deposit })
173172
.collect::<Vec<_>>()
174173
})
175-
});
174+
},
175+
);
176176
}

frame/offences/src/migration.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
// limitations under the License.
1717

1818
use super::{Config, OffenceDetails, Perbill, SessionIndex};
19-
use frame_support::{
20-
generate_storage_alias, pallet_prelude::ValueQuery, traits::Get, weights::Weight,
21-
};
19+
use frame_support::{pallet_prelude::ValueQuery, storage_alias, traits::Get, weights::Weight};
2220
use sp_staking::offence::{DisableStrategy, OnOffenceHandler};
2321
use sp_std::vec::Vec;
2422

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

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

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

0 commit comments

Comments
 (0)