Skip to content
Snippets Groups Projects
Verified Commit d868b858 authored by Michal Kucharczyk's avatar Michal Kucharczyk
Browse files

naive macro removed

parent 6db4b418
No related merge requests found
Pipeline #502245 waiting for manual action with stages
in 7 minutes and 52 seconds
......@@ -18,6 +18,7 @@
use crate::*;
use alloc::{vec, vec::Vec};
use cumulus_primitives_core::ParaId;
use frame_support::generate_config;
use hex_literal::hex;
use parachains_common::{AccountId, AuraId};
use sp_core::crypto::UncheckedInto;
......@@ -25,8 +26,6 @@ use sp_genesis_builder::PresetId;
use sp_keyring::Sr25519Keyring;
use testnet_parachains_constants::rococo::{currency::UNITS as ROC, xcm_version::SAFE_XCM_VERSION};
use frame_support::runtime_genesis_config_json;
const ASSET_HUB_ROCOCO_ED: Balance = ExistentialDeposit::get();
fn asset_hub_rococo_genesis(
......@@ -35,15 +34,14 @@ fn asset_hub_rococo_genesis(
endowment: Balance,
id: ParaId,
) -> serde_json::Value {
runtime_genesis_config_json!({
generate_config!({
balances: BalancesConfig {
balances: endowed_accounts.iter().cloned().map(|k| (k, endowment)).collect(),
},
parachain_info: ParachainInfoConfig { parachain_id: id, ..Default::default() },
parachain_info: ParachainInfoConfig { parachain_id: id },
collator_selection: CollatorSelectionConfig {
invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
candidacy_bond: ASSET_HUB_ROCOCO_ED * 16,
..Default::default()
},
session: SessionConfig {
keys: invulnerables
......@@ -56,11 +54,9 @@ fn asset_hub_rococo_genesis(
)
})
.collect(),
..Default::default()
},
polkadot_xcm: PolkadotXcmConfig {
safe_xcm_version: Some(SAFE_XCM_VERSION),
..Default::default()
},
})
}
......
......@@ -55,82 +55,3 @@ where
preset_for_name,
)
}
/// Creates a `RuntimeGenesisConfig` JSON patch.
///
/// This macro creates a default `RuntimeGenesisConfig` initializing provided fields with given
/// values, serialize it to JSON blob, and retain only the specified fields.
///
/// This macro helps to prevent errors that could occur from manually creating JSON objects, such
/// as typos or discrepancies caused by future changes to the `RuntimeGenesisConfig` structure. By
/// using the actual struct, it ensures that the JSON generated is valid and up-to-date.
///
/// This macro assumes that `serde(rename_all = "camelCase")` attribute is used for
/// RuntimeGenesisConfig, what should be the case for frame-based runtimes.
///
/// # Example
///
/// ```rust
/// use frame_support::runtime_genesis_config_json;
/// #[derive(Default, serde::Serialize, serde::Deserialize)]
/// #[serde(rename_all = "camelCase")]
/// struct RuntimeGenesisConfig {
/// a_field: u32,
/// b_field: u32,
/// c_field: u32,
/// }
/// assert_eq!(
/// runtime_genesis_config_json! ({a_field:31, b_field:127}),
/// serde_json::json!({"aField":31, "bField":127})
/// );
/// assert_eq!(
/// runtime_genesis_config_json! ({a_field:31}),
/// serde_json::json!({"aField":31})
/// );
/// ```
#[macro_export]
macro_rules! runtime_genesis_config_json {
({ $( $key:ident : $value:expr ),* $(,)? }) => {{
let config = RuntimeGenesisConfig {
$( $key : $value, )*
..Default::default()
};
#[inline]
fn compare_keys(
mut snake_chars: core::str::Chars,
mut camel_chars: core::str::Chars,
) -> bool {
loop {
match (snake_chars.next(), camel_chars.next()) {
(None, None) => return true,
(None, Some(_)) | (Some(_), None) => return false,
(Some('_'), Some(c)) => {
if let Some(s) = snake_chars.next() {
if s.to_ascii_uppercase() != c {
return false;
};
};
}
(Some(s), Some(c)) => {
if c != s {
return false;
}
}
}
}
}
let mut json_value =
serde_json::to_value(config).expect("serialization to json should work. qed");
if let serde_json::Value::Object(ref mut map) = json_value {
let keys_to_keep : Vec<&'static str> = vec![ $( stringify!($key) ),* ];
map.retain(|json_key, _| {
keys_to_keep.iter().any(|&struct_key| {
compare_keys(struct_key.chars(), json_key.chars())
})
});
}
json_value
}};
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment