Skip to content

Commit

Permalink
naive macro removed
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkucharczyk committed Oct 21, 2024
1 parent 6db4b41 commit d868b85
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
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;
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(
Expand All @@ -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
Expand All @@ -56,11 +54,9 @@ fn asset_hub_rococo_genesis(
)
})
.collect(),
..Default::default()
},
polkadot_xcm: PolkadotXcmConfig {
safe_xcm_version: Some(SAFE_XCM_VERSION),
..Default::default()
},
})
}
Expand Down
79 changes: 0 additions & 79 deletions substrate/frame/support/src/genesis_builder_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 comments on commit d868b85

Please sign in to comment.