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
This commit is contained in:
Bastian Köcher
2022-05-18 00:45:56 +02:00
committed by GitHub
parent 27f08fec3a
commit 74428fa8ac
21 changed files with 884 additions and 379 deletions
+29 -35
View File
@@ -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::*};
@@ -117,15 +114,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;
@@ -142,7 +140,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())
})
@@ -202,20 +200,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;
@@ -259,15 +256,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)
}
}