Support pallet::storage conditional compilation (#8324)

* Support pallet::storage conditional compilation.

* Add docs for cfg attributes.

* Keep strong types for get cfg attrs return.

* Update frame/support/procedural/src/pallet/parse/helper.rs

* Update frame/support/procedural/src/pallet/parse/storage.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Shaun Wang
2021-03-16 21:51:51 +13:00
committed by GitHub
parent 900bf2832a
commit 844e25522c
13 changed files with 117 additions and 12 deletions
@@ -217,6 +217,28 @@ pub mod pallet {
#[pallet::storage]
pub type DoubleMap2<T> = StorageDoubleMap<_, Twox64Concat, u16, Blake2_128Concat, u32, u64>;
#[pallet::storage]
#[pallet::getter(fn conditional_value)]
#[cfg(feature = "conditional-storage")]
pub type ConditionalValue<T> = StorageValue<_, u32>;
#[cfg(feature = "conditional-storage")]
#[pallet::storage]
#[pallet::getter(fn conditional_map)]
pub type ConditionalMap<T> = StorageMap<_, Twox64Concat, u16, u32>;
#[cfg(feature = "conditional-storage")]
#[pallet::storage]
#[pallet::getter(fn conditional_double_map)]
pub type ConditionalDoubleMap<T> = StorageDoubleMap<
_,
Blake2_128Concat,
u8,
Twox64Concat,
u16,
u32,
>;
#[pallet::genesis_config]
#[derive(Default)]
pub struct GenesisConfig {
@@ -522,6 +544,13 @@ fn storage_expand() {
k.extend(2u32.using_encoded(blake2_128_concat));
assert_eq!(unhashed::get::<u64>(&k), Some(3u64));
assert_eq!(&k[..32], &<pallet::DoubleMap2<Runtime>>::final_prefix());
#[cfg(feature = "conditional-storage")]
{
pallet::ConditionalValue::<Runtime>::put(1);
pallet::ConditionalMap::<Runtime>::insert(1, 2);
pallet::ConditionalDoubleMap::<Runtime>::insert(1, 2, 3);
}
})
}
@@ -646,6 +675,38 @@ fn metadata() {
default: DecodeDifferent::Decoded(vec![0]),
documentation: DecodeDifferent::Decoded(vec![]),
},
#[cfg(feature = "conditional-storage")] StorageEntryMetadata {
name: DecodeDifferent::Decoded("ConditionalValue".to_string()),
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Plain(DecodeDifferent::Decoded("u32".to_string())),
default: DecodeDifferent::Decoded(vec![0]),
documentation: DecodeDifferent::Decoded(vec![]),
},
#[cfg(feature = "conditional-storage")] StorageEntryMetadata {
name: DecodeDifferent::Decoded("ConditionalMap".to_string()),
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Map {
key: DecodeDifferent::Decoded("u16".to_string()),
value: DecodeDifferent::Decoded("u32".to_string()),
hasher: StorageHasher::Twox64Concat,
unused: false,
},
default: DecodeDifferent::Decoded(vec![0]),
documentation: DecodeDifferent::Decoded(vec![]),
},
#[cfg(feature = "conditional-storage")] StorageEntryMetadata {
name: DecodeDifferent::Decoded("ConditionalDoubleMap".to_string()),
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::DoubleMap {
value: DecodeDifferent::Decoded("u32".to_string()),
key1: DecodeDifferent::Decoded("u8".to_string()),
key2: DecodeDifferent::Decoded("u16".to_string()),
hasher: StorageHasher::Blake2_128Concat,
key2_hasher: StorageHasher::Twox64Concat,
},
default: DecodeDifferent::Decoded(vec![0]),
documentation: DecodeDifferent::Decoded(vec![]),
},
]),
})),
calls: Some(DecodeDifferent::Decoded(vec![