From fbccafad241978d37fb8ced7b38763c762ad9fc2 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Wed, 26 Jun 2024 12:25:15 +0200 Subject: [PATCH] Set `get_preset` for BridgeHubKusama --- Cargo.lock | 4 + Cargo.toml | 2 +- .../bridge-hubs/bridge-hub-kusama/Cargo.toml | 2 + .../src/genesis_config_presets.rs | 92 +++++++++++++++++++ .../bridge-hubs/bridge-hub-kusama/src/lib.rs | 8 +- system-parachains/constants/Cargo.toml | 6 ++ .../constants/src/genesis_presets.rs | 68 ++++++++++++++ system-parachains/constants/src/lib.rs | 1 + 8 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 system-parachains/bridge-hubs/bridge-hub-kusama/src/genesis_config_presets.rs create mode 100644 system-parachains/constants/src/genesis_presets.rs diff --git a/Cargo.lock b/Cargo.lock index be996e7cf0..af2bb2187b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1738,6 +1738,7 @@ dependencies = [ "polkadot-runtime-constants", "scale-info", "serde", + "serde_json", "snowbridge-beacon-primitives", "snowbridge-core", "snowbridge-outbound-queue-runtime-api", @@ -14525,7 +14526,10 @@ dependencies = [ "polkadot-primitives", "polkadot-runtime-constants", "smallvec", + "sp-core 34.0.0", "sp-runtime 38.0.0", + "sp-std", + "staging-xcm", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 066fe70f4d..1cbddedb06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -189,7 +189,7 @@ sc-chain-spec = { version = "34.0.0" } scale-info = { version = "2.10.0", default-features = false } separator = { version = "0.4.1" } serde = { version = "1.0.196" } -serde_json = { version = "1.0.113" } +serde_json = { version = "1.0.113", default-features = false } smallvec = { version = "1.13.1" } snowbridge-beacon-primitives = { version = "0.7.0", default-features = false } snowbridge-core = { version = "0.7.0", default-features = false } diff --git a/system-parachains/bridge-hubs/bridge-hub-kusama/Cargo.toml b/system-parachains/bridge-hubs/bridge-hub-kusama/Cargo.toml index 6eb8dea2a9..bdf8043fd2 100644 --- a/system-parachains/bridge-hubs/bridge-hub-kusama/Cargo.toml +++ b/system-parachains/bridge-hubs/bridge-hub-kusama/Cargo.toml @@ -17,6 +17,7 @@ hex-literal = { workspace = true } log = { workspace = true } scale-info = { features = ["derive"], workspace = true } serde = { optional = true, features = ["derive"], workspace = true } +serde_json = { features = ["alloc"], workspace = true } tuplex = { workspace = true } # Local @@ -191,6 +192,7 @@ std = [ "polkadot-runtime-constants/std", "scale-info/std", "serde", + "serde_json/std", "snowbridge-beacon-primitives/std", "snowbridge-core/std", "snowbridge-outbound-queue-runtime-api/std", diff --git a/system-parachains/bridge-hubs/bridge-hub-kusama/src/genesis_config_presets.rs b/system-parachains/bridge-hubs/bridge-hub-kusama/src/genesis_config_presets.rs new file mode 100644 index 0000000000..60711fe915 --- /dev/null +++ b/system-parachains/bridge-hubs/bridge-hub-kusama/src/genesis_config_presets.rs @@ -0,0 +1,92 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Genesis configs presets for the BridgeHubKusama runtime + +use crate::*; +use sp_std::vec::Vec; +use system_parachains_constants::genesis_presets::*; + +const BRIDGE_HUB_KUSAMA_ED: Balance = crate::ExistentialDeposit::get(); + +fn bridge_hub_kusama_genesis( + invulnerables: Vec<(AccountId, AuraId)>, + endowed_accounts: Vec, + id: ParaId, +) -> serde_json::Value { + serde_json::json!({ + "balances": BalancesConfig { + balances: endowed_accounts + .iter() + .cloned() + .map(|k| (k, BRIDGE_HUB_KUSAMA_ED * 4096 * 4096)) + .collect(), + }, + "parachainInfo": ParachainInfoConfig { + parachain_id: id, + ..Default::default() + }, + "collatorSelection": CollatorSelectionConfig { + invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(), + candidacy_bond: BRIDGE_HUB_KUSAMA_ED * 16, + ..Default::default() + }, + "session": SessionConfig { + keys: invulnerables + .into_iter() + .map(|(acc, aura)| { + ( + acc.clone(), // account id + acc, // validator id + SessionKeys { aura }, // session keys + ) + }) + .collect(), + }, + "polkadotXcm": { + "safeXcmVersion": Some(SAFE_XCM_VERSION), + }, + "ethereumSystem": EthereumSystemConfig { + para_id: id, + asset_hub_para_id: kusama_runtime_constants::system_parachain::ASSET_HUB_ID.into(), + ..Default::default() + }, + // no need to pass anything to aura, in fact it will panic if we do. Session will take care + // of this. `aura: Default::default()` + }) +} + +fn bridge_hub_kusama_local_testnet_genesis(para_id: ParaId) -> serde_json::Value { + bridge_hub_kusama_genesis(invulnerables(), testnet_accounts(), para_id) +} + +fn bridge_hub_kusama_development_genesis(para_id: ParaId) -> serde_json::Value { + bridge_hub_kusama_local_testnet_genesis(para_id) +} + +/// Provides the JSON representation of predefined genesis config for given `id`. +pub fn get_preset(id: &sp_genesis_builder::PresetId) -> Option> { + let patch = match id.try_into() { + Ok("development") => bridge_hub_kusama_development_genesis(1002.into()), + Ok("local_testnet") => bridge_hub_kusama_local_testnet_genesis(1002.into()), + _ => return None, + }; + Some( + serde_json::to_string(&patch) + .expect("serialization to json is expected to work. qed.") + .into_bytes(), + ) +} diff --git a/system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs b/system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs index 48fa7da454..758ce35515 100644 --- a/system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs +++ b/system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs @@ -24,6 +24,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); pub mod bridge_to_ethereum_config; pub mod bridge_to_polkadot_config; +mod genesis_config_presets; mod weights; pub mod xcm_config; @@ -751,11 +752,14 @@ impl_runtime_apis! { } fn get_preset(id: &Option) -> Option> { - get_preset::(id, |_| None) + get_preset::(id, &genesis_config_presets::get_preset) } fn preset_names() -> Vec { - vec![] + vec![ + sp_genesis_builder::PresetId::from("local_testnet"), + sp_genesis_builder::PresetId::from("development"), + ] } } diff --git a/system-parachains/constants/Cargo.toml b/system-parachains/constants/Cargo.toml index 27dd92e1f5..17582f7785 100644 --- a/system-parachains/constants/Cargo.toml +++ b/system-parachains/constants/Cargo.toml @@ -16,7 +16,10 @@ parachains-common = { workspace = true } polkadot-core-primitives = { workspace = true } polkadot-primitives = { workspace = true } polkadot-runtime-constants = { workspace = true } +sp-core = { workspace = true } sp-runtime = { workspace = true } +sp-std = { workspace = true } +xcm = { workspace = true } [features] default = ["std"] @@ -27,5 +30,8 @@ std = [ "polkadot-core-primitives/std", "polkadot-primitives/std", "polkadot-runtime-constants/std", + "sp-core/std", "sp-runtime/std", + "sp-std/std", + "xcm/std", ] diff --git a/system-parachains/constants/src/genesis_presets.rs b/system-parachains/constants/src/genesis_presets.rs new file mode 100644 index 0000000000..abb1f8b955 --- /dev/null +++ b/system-parachains/constants/src/genesis_presets.rs @@ -0,0 +1,68 @@ +// Copyright (C) Parity Technologies and the various Polkadot contributors, see Contributions.md +// for a list of specific contributors. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +use parachains_common::AuraId; +use polkadot_primitives::{AccountId, AccountPublic}; +use sp_core::{sr25519, Pair, Public}; +use sp_runtime::traits::IdentifyAccount; +#[cfg(not(feature = "std"))] +use sp_std::alloc::format; +use sp_std::vec::Vec; + +/// Invulnerable Collators +pub fn invulnerables() -> Vec<(parachains_common::AccountId, AuraId)> { + Vec::from([ + (get_account_id_from_seed::("Alice"), get_from_seed::("Alice")), + (get_account_id_from_seed::("Bob"), get_from_seed::("Bob")), + ]) +} + +/// Test accounts +pub fn testnet_accounts() -> Vec { + Vec::from([ + get_account_id_from_seed::("Alice"), + get_account_id_from_seed::("Bob"), + get_account_id_from_seed::("Charlie"), + get_account_id_from_seed::("Dave"), + get_account_id_from_seed::("Eve"), + get_account_id_from_seed::("Ferdie"), + get_account_id_from_seed::("Alice//stash"), + get_account_id_from_seed::("Bob//stash"), + get_account_id_from_seed::("Charlie//stash"), + get_account_id_from_seed::("Dave//stash"), + get_account_id_from_seed::("Eve//stash"), + get_account_id_from_seed::("Ferdie//stash"), + ]) +} + +/// Helper function to generate a crypto pair from seed +pub fn get_from_seed(seed: &str) -> ::Public { + TPublic::Pair::from_string(&format!("//{}", seed), None) + .expect("static values are valid; qed") + .public() +} + +/// Helper function to generate an account ID from seed +pub fn get_account_id_from_seed(seed: &str) -> AccountId +where + AccountPublic: From<::Public>, +{ + AccountPublic::from(get_from_seed::(seed)).into_account() +} + +/// The default XCM version to set in genesis config. +pub const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION; diff --git a/system-parachains/constants/src/lib.rs b/system-parachains/constants/src/lib.rs index d0d320d968..7f31f7e149 100644 --- a/system-parachains/constants/src/lib.rs +++ b/system-parachains/constants/src/lib.rs @@ -16,6 +16,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub mod genesis_presets; pub mod kusama; pub mod polkadot;