Skip to content

Commit

Permalink
runtimes: presets are provided as config patches (#6349)
Browse files Browse the repository at this point in the history
This PR introduces usage of `build_struct_json_patch` macro in all
runtimes (also guides) within the code base.

It also fixes macro to support _field init shorthand_, and _Struct
Update_ syntax which were missing in original implementation.

Follow up of #5700 and #5813
  • Loading branch information
michalkucharczyk authored Nov 8, 2024
1 parent 0e09ad4 commit 8c8f339
Show file tree
Hide file tree
Showing 12 changed files with 572 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use crate::*;
use alloc::{vec, vec::Vec};
use cumulus_primitives_core::ParaId;
use frame_support::build_struct_json_patch;
use hex_literal::hex;
use parachains_common::{AccountId, AuraId};
use sp_core::crypto::UncheckedInto;
Expand All @@ -35,15 +36,14 @@ fn asset_hub_westend_genesis(
endowment: Balance,
id: ParaId,
) -> serde_json::Value {
let config = RuntimeGenesisConfig {
build_struct_json_patch!(RuntimeGenesisConfig {
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_WESTEND_ED * 16,
..Default::default()
},
session: SessionConfig {
keys: invulnerables
Expand All @@ -56,16 +56,9 @@ fn asset_hub_westend_genesis(
)
})
.collect(),
..Default::default()
},
polkadot_xcm: PolkadotXcmConfig {
safe_xcm_version: Some(SAFE_XCM_VERSION),
..Default::default()
},
..Default::default()
};

serde_json::to_value(config).expect("Could not build genesis config.")
polkadot_xcm: PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
})
}

/// Encapsulates names of predefined presets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use crate::*;
use alloc::{vec, vec::Vec};
use cumulus_primitives_core::ParaId;
use frame_support::build_struct_json_patch;
use parachains_common::{AccountId, AuraId};
use sp_genesis_builder::PresetId;
use sp_keyring::Sr25519Keyring;
Expand All @@ -34,19 +35,18 @@ fn bridge_hub_rococo_genesis(
asset_hub_para_id: ParaId,
opened_bridges: Vec<(Location, InteriorLocation, Option<bp_messages::LegacyLaneId>)>,
) -> serde_json::Value {
let config = RuntimeGenesisConfig {
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts
.iter()
.cloned()
.map(|k| (k, 1u128 << 60))
.collect::<Vec<_>>(),
},
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: BRIDGE_HUB_ROCOCO_ED * 16,
..Default::default()
},
session: SessionConfig {
keys: invulnerables
Expand All @@ -59,33 +59,15 @@ fn bridge_hub_rococo_genesis(
)
})
.collect(),
..Default::default()
},
polkadot_xcm: PolkadotXcmConfig {
safe_xcm_version: Some(SAFE_XCM_VERSION),
..Default::default()
},
bridge_westend_grandpa: BridgeWestendGrandpaConfig {
owner: bridges_pallet_owner.clone(),
..Default::default()
},
polkadot_xcm: PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
bridge_westend_grandpa: BridgeWestendGrandpaConfig { owner: bridges_pallet_owner.clone() },
bridge_westend_messages: BridgeWestendMessagesConfig {
owner: bridges_pallet_owner.clone(),
..Default::default()
},
xcm_over_bridge_hub_westend: XcmOverBridgeHubWestendConfig {
opened_bridges,
..Default::default()
},
ethereum_system: EthereumSystemConfig {
para_id: id,
asset_hub_para_id,
..Default::default()
},
..Default::default()
};

serde_json::to_value(config).expect("Could not build genesis config.")
xcm_over_bridge_hub_westend: XcmOverBridgeHubWestendConfig { opened_bridges },
ethereum_system: EthereumSystemConfig { para_id: id, asset_hub_para_id },
})
}

/// Provides the JSON representation of predefined genesis config for given `id`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use crate::*;
use alloc::{vec, vec::Vec};
use cumulus_primitives_core::ParaId;
use frame_support::build_struct_json_patch;
use parachains_common::{AccountId, AuraId};
use sp_genesis_builder::PresetId;
use sp_keyring::Sr25519Keyring;
Expand All @@ -34,19 +35,18 @@ fn bridge_hub_westend_genesis(
asset_hub_para_id: ParaId,
opened_bridges: Vec<(Location, InteriorLocation, Option<bp_messages::LegacyLaneId>)>,
) -> serde_json::Value {
let config = RuntimeGenesisConfig {
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts
.iter()
.cloned()
.map(|k| (k, 1u128 << 60))
.collect::<Vec<_>>(),
},
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: BRIDGE_HUB_WESTEND_ED * 16,
..Default::default()
},
session: SessionConfig {
keys: invulnerables
Expand All @@ -59,33 +59,13 @@ fn bridge_hub_westend_genesis(
)
})
.collect(),
..Default::default()
},
polkadot_xcm: PolkadotXcmConfig {
safe_xcm_version: Some(SAFE_XCM_VERSION),
..Default::default()
},
bridge_rococo_grandpa: BridgeRococoGrandpaConfig {
owner: bridges_pallet_owner.clone(),
..Default::default()
},
bridge_rococo_messages: BridgeRococoMessagesConfig {
owner: bridges_pallet_owner.clone(),
..Default::default()
},
xcm_over_bridge_hub_rococo: XcmOverBridgeHubRococoConfig {
opened_bridges,
..Default::default()
},
ethereum_system: EthereumSystemConfig {
para_id: id,
asset_hub_para_id,
..Default::default()
},
..Default::default()
};

serde_json::to_value(config).expect("Could not build genesis config.")
polkadot_xcm: PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
bridge_rococo_grandpa: BridgeRococoGrandpaConfig { owner: bridges_pallet_owner.clone() },
bridge_rococo_messages: BridgeRococoMessagesConfig { owner: bridges_pallet_owner.clone() },
xcm_over_bridge_hub_rococo: XcmOverBridgeHubRococoConfig { opened_bridges },
ethereum_system: EthereumSystemConfig { para_id: id, asset_hub_para_id },
})
}

/// Provides the JSON representation of predefined genesis config for given `id`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use crate::*;
use alloc::{vec, vec::Vec};
use cumulus_primitives_core::ParaId;
use frame_support::build_struct_json_patch;
use parachains_common::{AccountId, AuraId};
use sp_genesis_builder::PresetId;
use sp_keyring::Sr25519Keyring;
Expand All @@ -30,19 +31,18 @@ fn collectives_westend_genesis(
endowed_accounts: Vec<AccountId>,
id: ParaId,
) -> serde_json::Value {
let config = RuntimeGenesisConfig {
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts
.iter()
.cloned()
.map(|k| (k, COLLECTIVES_WESTEND_ED * 4096))
.collect::<Vec<_>>(),
},
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: COLLECTIVES_WESTEND_ED * 16,
..Default::default()
},
session: SessionConfig {
keys: invulnerables
Expand All @@ -55,16 +55,9 @@ fn collectives_westend_genesis(
)
})
.collect(),
..Default::default()
},
polkadot_xcm: PolkadotXcmConfig {
safe_xcm_version: Some(SAFE_XCM_VERSION),
..Default::default()
},
..Default::default()
};

serde_json::to_value(config).expect("Could not build genesis config.")
polkadot_xcm: PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
})
}

/// Provides the JSON representation of predefined genesis config for given `id`.
Expand Down
8 changes: 3 additions & 5 deletions docs/sdk/packages/guides/first-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,21 @@ pub mod genesis_config_presets {
interface::{Balance, MinimumBalance},
BalancesConfig, RuntimeGenesisConfig, SudoConfig,
};
use frame::deps::frame_support::build_struct_json_patch;
use serde_json::Value;

/// Returns a development genesis config preset.
#[docify::export]
pub fn development_config_genesis() -> Value {
let endowment = <MinimumBalance as Get<Balance>>::get().max(1) * 1000;
let config = RuntimeGenesisConfig {
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: AccountKeyring::iter()
.map(|a| (a.to_account_id(), endowment))
.collect::<Vec<_>>(),
},
sudo: SudoConfig { key: Some(AccountKeyring::Alice.to_account_id()) },
..Default::default()
};

serde_json::to_value(config).expect("Could not build genesis config.")
})
}

/// Get the set of the available genesis config presets.
Expand Down
31 changes: 9 additions & 22 deletions polkadot/runtime/rococo/src/genesis_config_presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
#[cfg(not(feature = "std"))]
use alloc::format;
use alloc::{vec, vec::Vec};
use frame_support::build_struct_json_patch;
use polkadot_primitives::{AccountId, AssignmentId, SchedulerParams, ValidatorId};
use rococo_runtime_constants::currency::UNITS as ROC;
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
Expand Down Expand Up @@ -163,7 +164,7 @@ fn rococo_testnet_genesis(

const ENDOWMENT: u128 = 1_000_000 * ROC;

let config = RuntimeGenesisConfig {
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts.iter().map(|k| (k.clone(), ENDOWMENT)).collect::<Vec<_>>(),
},
Expand All @@ -185,9 +186,8 @@ fn rococo_testnet_genesis(
)
})
.collect::<Vec<_>>(),
..Default::default()
},
babe: BabeConfig { epoch_config: BABE_GENESIS_EPOCH_CONFIG, ..Default::default() },
babe: BabeConfig { epoch_config: BABE_GENESIS_EPOCH_CONFIG },
sudo: SudoConfig { key: Some(root_key.clone()) },
configuration: ConfigurationConfig {
config: polkadot_runtime_parachains::configuration::HostConfiguration {
Expand All @@ -198,14 +198,8 @@ fn rococo_testnet_genesis(
..default_parachains_host_configuration()
},
},
registrar: RegistrarConfig {
next_free_para_id: polkadot_primitives::LOWEST_PUBLIC_ID,
..Default::default()
},
..Default::default()
};

serde_json::to_value(config).expect("Could not build genesis config.")
registrar: RegistrarConfig { next_free_para_id: polkadot_primitives::LOWEST_PUBLIC_ID },
})
}

// staging_testnet
Expand Down Expand Up @@ -427,7 +421,7 @@ fn rococo_staging_testnet_config_genesis() -> serde_json::Value {
const ENDOWMENT: u128 = 1_000_000 * ROC;
const STASH: u128 = 100 * ROC;

let config = RuntimeGenesisConfig {
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts
.iter()
Expand All @@ -440,19 +434,12 @@ fn rococo_staging_testnet_config_genesis() -> serde_json::Value {
.into_iter()
.map(|x| (x.0.clone(), x.0, rococo_session_keys(x.2, x.3, x.4, x.5, x.6, x.7)))
.collect::<Vec<_>>(),
..Default::default()
},
babe: BabeConfig { epoch_config: BABE_GENESIS_EPOCH_CONFIG, ..Default::default() },
babe: BabeConfig { epoch_config: BABE_GENESIS_EPOCH_CONFIG },
sudo: SudoConfig { key: Some(endowed_accounts[0].clone()) },
configuration: ConfigurationConfig { config: default_parachains_host_configuration() },
registrar: RegistrarConfig {
next_free_para_id: polkadot_primitives::LOWEST_PUBLIC_ID,
..Default::default()
},
..Default::default()
};

serde_json::to_value(config).expect("Could not build genesis config.")
registrar: RegistrarConfig { next_free_para_id: polkadot_primitives::LOWEST_PUBLIC_ID },
})
}

//development
Expand Down
18 changes: 5 additions & 13 deletions polkadot/runtime/westend/src/genesis_config_presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn westend_staging_testnet_config_genesis() -> serde_json::Value {
//
// SECRET_SEED="slow awkward present example safe bundle science ocean cradle word tennis earn"
// subkey inspect -n polkadot "$SECRET_SEED"
let endowed_accounts = vec![
let endowed_accounts: Vec<AccountId> = vec![
// 15S75FkhCWEowEGfxWwVfrW3LQuy8w8PNhVmrzfsVhCMjUh1
hex!["c416837e232d9603e83162ef4bda08e61580eeefe60fe92fc044aa508559ae42"].into(),
];
Expand Down Expand Up @@ -338,7 +338,7 @@ fn westend_staging_testnet_config_genesis() -> serde_json::Value {
const ENDOWMENT: u128 = 1_000_000 * WND;
const STASH: u128 = 100 * WND;

let config = RuntimeGenesisConfig {
build_struct_json_patch!(RuntimeGenesisConfig {
balances: BalancesConfig {
balances: endowed_accounts
.iter()
Expand All @@ -364,7 +364,6 @@ fn westend_staging_testnet_config_genesis() -> serde_json::Value {
)
})
.collect::<Vec<_>>(),
..Default::default()
},
staking: StakingConfig {
validator_count: 50,
Expand All @@ -376,19 +375,12 @@ fn westend_staging_testnet_config_genesis() -> serde_json::Value {
invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect::<Vec<_>>(),
force_era: Forcing::ForceNone,
slash_reward_fraction: Perbill::from_percent(10),
..Default::default()
},
babe: BabeConfig { epoch_config: BABE_GENESIS_EPOCH_CONFIG, ..Default::default() },
babe: BabeConfig { epoch_config: BABE_GENESIS_EPOCH_CONFIG },
sudo: SudoConfig { key: Some(endowed_accounts[0].clone()) },
configuration: ConfigurationConfig { config: default_parachains_host_configuration() },
registrar: RegistrarConfig {
next_free_para_id: polkadot_primitives::LOWEST_PUBLIC_ID,
..Default::default()
},
..Default::default()
};

serde_json::to_value(config).expect("Could not build genesis config.")
registrar: RegistrarConfig { next_free_para_id: polkadot_primitives::LOWEST_PUBLIC_ID },
})
}

//development
Expand Down
Loading

0 comments on commit 8c8f339

Please sign in to comment.