construct_runtime: Fix generation of types behind features (#12229)

* construct_runtime: Fix generation of types behind features

With the recent addition of supporting features in `construct_runtime!` there was a bug overseen.
The `AllPalletsWithSystem` etc type declarations would be declared twice when a certain was enabled.
The problem was that in the macro we didn't feature gate the types that should be declared when
there is no feature enabled. This pull request now takes care of feature gating this type behind
`all(#( not(feature) ))`. So, these types will only be enabled if no of the configured features is enabled.

* Fix tests

* FMT
This commit is contained in:
Bastian Köcher
2022-09-10 20:50:59 +01:00
committed by GitHub
parent fc9848ce0d
commit 5647e71947
4 changed files with 133 additions and 92 deletions
+61 -17
View File
@@ -334,22 +334,22 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn conditional_value)]
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
pub type ConditionalValue<T> = StorageValue<_, u32>;
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
#[pallet::storage]
#[pallet::getter(fn conditional_map)]
pub type ConditionalMap<T> =
StorageMap<_, Twox64Concat, u16, u32, OptionQuery, GetDefault, ConstU32<12>>;
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
#[pallet::storage]
#[pallet::getter(fn conditional_double_map)]
pub type ConditionalDoubleMap<T> =
StorageDoubleMap<_, Blake2_128Concat, u8, Twox64Concat, u16, u32>;
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
#[pallet::storage]
#[pallet::getter(fn conditional_nmap)]
pub type ConditionalNMap<T> =
@@ -550,7 +550,9 @@ pub mod pallet2 {
#[frame_support::pallet]
pub mod pallet3 {
#[pallet::config]
pub trait Config: frame_system::Config<Origin = ()> {}
pub trait Config: frame_system::Config<Origin = <Self as Config>::Origin> {
type Origin;
}
#[pallet::pallet]
pub struct Pallet<T>(_);
@@ -612,6 +614,11 @@ impl pallet2::Config for Runtime {
impl pallet4::Config for Runtime {}
#[cfg(feature = "frame-feature-testing")]
impl pallet3::Config for Runtime {
type Origin = Origin;
}
pub type Header = sp_runtime::generic::Header<u32, sp_runtime::traits::BlakeTwo256>;
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, Call, (), ()>;
@@ -626,7 +633,7 @@ frame_support::construct_runtime!(
System: frame_system exclude_parts { Pallet, Storage },
Example: pallet,
Example2: pallet2 exclude_parts { Call },
#[cfg(feature = "example3")]
#[cfg(feature = "frame-feature-testing")]
Example3: pallet3,
Example4: pallet4 use_parts { Call },
}
@@ -1024,7 +1031,7 @@ fn storage_expand() {
Err(pallet::Error::<Runtime>::NonExistentStorageValue),
);
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
{
pallet::ConditionalValue::<Runtime>::put(1);
pallet::ConditionalMap::<Runtime>::insert(1, 2);
@@ -1164,8 +1171,10 @@ fn migrate_from_pallet_version_to_storage_version() {
AllPalletsWithSystem,
>(&db_weight);
// 4 pallets, 2 writes and every write costs 5 weight.
assert_eq!(Weight::from_ref_time(4 * 2 * 5), weight);
let pallet_num = if cfg!(feature = "frame-feature-testing") { 5 } else { 4 };
// `pallet_num` pallets, 2 writes and every write costs 5 weight.
assert_eq!(Weight::from_ref_time(pallet_num * 2 * 5), weight);
// All pallet versions should be removed
assert!(sp_io::storage::get(&pallet_version_key(Example::name())).is_none());
@@ -1332,7 +1341,7 @@ fn metadata() {
default: vec![1, 1],
docs: vec![],
},
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
StorageEntryMetadata {
name: "ConditionalValue",
modifier: StorageEntryModifier::Optional,
@@ -1340,7 +1349,7 @@ fn metadata() {
default: vec![0],
docs: vec![],
},
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
StorageEntryMetadata {
name: "ConditionalMap",
modifier: StorageEntryModifier::Optional,
@@ -1352,7 +1361,7 @@ fn metadata() {
default: vec![0],
docs: vec![],
},
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
StorageEntryMetadata {
name: "ConditionalDoubleMap",
modifier: StorageEntryModifier::Optional,
@@ -1367,7 +1376,7 @@ fn metadata() {
default: vec![0],
docs: vec![],
},
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
StorageEntryMetadata {
name: "ConditionalNMap",
modifier: StorageEntryModifier::Optional,
@@ -1489,6 +1498,16 @@ fn metadata() {
constants: vec![],
error: None,
},
#[cfg(feature = "frame-feature-testing")]
PalletMetadata {
index: 3,
name: "Example3",
storage: None,
calls: None,
event: None,
constants: vec![],
error: None,
},
];
let empty_doc = pallets[0].event.as_ref().unwrap().ty.type_info().docs().is_empty() &&
@@ -1633,7 +1652,7 @@ fn test_storage_info() {
max_values: None,
max_size: Some(16 + 1 + 8 + 2 + 16),
},
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
{
StorageInfo {
pallet_name: b"Example".to_vec(),
@@ -1643,7 +1662,7 @@ fn test_storage_info() {
max_size: Some(4),
}
},
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
{
StorageInfo {
pallet_name: b"Example".to_vec(),
@@ -1653,7 +1672,7 @@ fn test_storage_info() {
max_size: Some(8 + 2 + 4),
}
},
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
{
StorageInfo {
pallet_name: b"Example".to_vec(),
@@ -1663,7 +1682,7 @@ fn test_storage_info() {
max_size: Some(16 + 1 + 8 + 2 + 4),
}
},
#[cfg(feature = "conditional-storage")]
#[cfg(feature = "frame-feature-testing")]
{
StorageInfo {
pallet_name: b"Example".to_vec(),
@@ -1730,27 +1749,42 @@ fn assert_type_all_pallets_reversed_with_system_first_is_correct() {
// Just ensure the 2 types are same.
#[allow(deprecated)]
fn _a(_t: AllPalletsReversedWithSystemFirst) {}
#[cfg(not(feature = "frame-feature-testing"))]
fn _b(t: (System, Example4, Example2, Example)) {
_a(t)
}
#[cfg(feature = "frame-feature-testing")]
fn _b(t: (System, Example4, Example3, Example2, Example)) {
_a(t)
}
}
#[test]
fn assert_type_all_pallets_with_system_is_correct() {
// Just ensure the 2 types are same.
fn _a(_t: AllPalletsWithSystem) {}
#[cfg(not(feature = "frame-feature-testing"))]
fn _b(t: (System, Example, Example2, Example4)) {
_a(t)
}
#[cfg(feature = "frame-feature-testing")]
fn _b(t: (System, Example, Example2, Example3, Example4)) {
_a(t)
}
}
#[test]
fn assert_type_all_pallets_without_system_is_correct() {
// Just ensure the 2 types are same.
fn _a(_t: AllPalletsWithoutSystem) {}
#[cfg(not(feature = "frame-feature-testing"))]
fn _b(t: (Example, Example2, Example4)) {
_a(t)
}
#[cfg(feature = "frame-feature-testing")]
fn _b(t: (Example, Example2, Example3, Example4)) {
_a(t)
}
}
#[test]
@@ -1758,9 +1792,14 @@ fn assert_type_all_pallets_with_system_reversed_is_correct() {
// Just ensure the 2 types are same.
#[allow(deprecated)]
fn _a(_t: AllPalletsWithSystemReversed) {}
#[cfg(not(feature = "frame-feature-testing"))]
fn _b(t: (Example4, Example2, Example, System)) {
_a(t)
}
#[cfg(feature = "frame-feature-testing")]
fn _b(t: (Example4, Example3, Example2, Example, System)) {
_a(t)
}
}
#[test]
@@ -1768,9 +1807,14 @@ fn assert_type_all_pallets_without_system_reversed_is_correct() {
// Just ensure the 2 types are same.
#[allow(deprecated)]
fn _a(_t: AllPalletsWithoutSystemReversed) {}
#[cfg(not(feature = "frame-feature-testing"))]
fn _b(t: (Example4, Example2, Example)) {
_a(t)
}
#[cfg(feature = "frame-feature-testing")]
fn _b(t: (Example4, Example3, Example2, Example)) {
_a(t)
}
}
#[test]