Skip to content

Commit

Permalink
Make the project capable of handling multiple runtimes (#721)
Browse files Browse the repository at this point in the history
* Migrate from modules to crates

Previously all runtimes were united in one crate and toggled by using flags. The client must be able to offer all runtimes, which was impossible with all runtimes being in one crate: Rust prohibits linking to the same crate with different features. Consequently, each runtime must have its own crate. This complicates things significantly and if the previously common runtime schema wants to be used, wrapper macros have to be introduced.

* Make client use required Executor and RuntimeApi

* Adjust subcommand interpreter to select proper runtime

* Add and implement abstract client

* Make launcher select correct RuntimeAPI and Executor

* Adjust SubstrateCli impl and make client work with chainspecs

* Add macro to generate generic genesis across different runtimes
  • Loading branch information
sea212 committed Aug 11, 2022
1 parent bee01bf commit a6ec392
Show file tree
Hide file tree
Showing 64 changed files with 4,107 additions and 2,395 deletions.
120 changes: 119 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
members = [
"node",
"primitives",
"runtime",
"runtime/common",
"runtime/battery-station",
"runtime/zeitgeist",
"zrml/authorized",
"zrml/court",
"zrml/liquidity-mining",
Expand Down
20 changes: 12 additions & 8 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sp-session = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/p
sp-storage = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/purestake/substrate" }
sp-timestamp = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/purestake/substrate" }
sp-transaction-pool = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/purestake/substrate" }
sp-trie = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/purestake/substrate" }
substrate-frame-rpc-system = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/purestake/substrate" }

# Try-Runtime
Expand Down Expand Up @@ -70,7 +71,6 @@ sc-chain-spec = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.co
sc-network = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/purestake/substrate", optional = true }
sc-tracing = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/purestake/substrate", optional = true }
serde = { features = ["derive"], optional = true, version = "1.0.136" }
sp-trie = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/purestake/substrate", optional = true }
sp-keystore = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/purestake/substrate", optional = true }
substrate-prometheus-endpoint = { branch = "moonbeam-polkadot-v0.9.19", git = "https://github.com/purestake/substrate", optional = true }

Expand Down Expand Up @@ -98,15 +98,17 @@ log = { optional = true, version = "0.4.14" }

# Zeitgeist

battery-station-runtime = { path = "../runtime/battery-station", optional = true }
zeitgeist-primitives = { path = "../primitives" }
zeitgeist-runtime = { path = "../runtime" }
zeitgeist-runtime = { path = "../runtime/zeitgeist", optional = true }
zrml-liquidity-mining = { path = "../zrml/liquidity-mining" }
zrml-swaps-rpc = { path = "../zrml/swaps/rpc" }

[features]
default = []
default = ["with-battery-station-runtime", "with-zeitgeist-runtime"]
parachain = [
"zeitgeist-runtime/parachain",
"battery-station-runtime?/parachain",
"zeitgeist-runtime?/parachain",

# Cumulus

Expand Down Expand Up @@ -136,7 +138,6 @@ parachain = [
"sc-tracing",
"serde",
"sp-keystore",
"sp-trie",
"substrate-prometheus-endpoint",

# Polkadot
Expand All @@ -148,15 +149,18 @@ parachain = [
"polkadot-test-service",
]
runtime-benchmarks = [
"battery-station-runtime?/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"polkadot-service?/runtime-benchmarks",
"zeitgeist-runtime/runtime-benchmarks"
"zeitgeist-runtime?/runtime-benchmarks",
]
try-runtime = [
"zeitgeist-runtime/try-runtime",
"battery-station-runtime?/try-runtime",
"zeitgeist-runtime?/try-runtime",
"try-runtime-cli",
]
without-sudo = []
with-battery-station-runtime = ["battery-station-runtime"]
with-zeitgeist-runtime = ["zeitgeist-runtime"]

[package]
authors = ["Zeitgeist PM <contact@zeitgeist.pm>"]
Expand Down
1 change: 0 additions & 1 deletion node/res/bs.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"tokenSymbol": "ZBP"
},
"consensusEngine": null,
"lightSyncState": null,
"genesis": {
"raw": {
"top": {
Expand Down
123 changes: 108 additions & 15 deletions node/src/chain_spec/battery_station.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,130 @@
#[cfg(not(feature = "without-sudo"))]
use crate::chain_spec::root_key_staging_testnet;
use crate::chain_spec::{
additional_chain_spec_staging_testnet, endowed_accounts_staging_testnet, generic_genesis,
telemetry_endpoints, token_properties, zeitgeist_wasm, ChainSpec,
};
#![cfg(feature = "with-battery-station-runtime")]

use super::{AdditionalChainSpec, EndowedAccountWithBalance};
use crate::chain_spec::{generate_generic_genesis_function, telemetry_endpoints, token_properties};
use battery_station_runtime::parameters::SS58Prefix;
use hex_literal::hex;
use sc_service::ChainType;
use sp_core::crypto::UncheckedInto;
use zeitgeist_primitives::{
constants::{
ztg::{LIQUIDITY_MINING, LIQUIDITY_MINING_PTD},
BASE,
},
types::AccountId,
};

#[cfg(feature = "parachain")]
use {
super::{Extensions, DEFAULT_COLLATOR_INFLATION_INFO},
battery_station_runtime::{CollatorDeposit, EligibilityValue, PolkadotXcmConfig},
};

cfg_if::cfg_if! {
if #[cfg(feature = "parachain")] {
pub(super) const DEFAULT_STAKING_AMOUNT_BATTERY_STATION: u128 = 2_000 * BASE;
const DEFAULT_COLLATOR_BALANCE_BATTERY_STATION: Option<u128> =
DEFAULT_STAKING_AMOUNT_BATTERY_STATION.checked_add(CollatorDeposit::get());
const DEFAULT_INITIAL_CROWDLOAN_FUNDS_BATTERY_STATION: u128 = 100 * BASE;
pub type BatteryStationChainSpec = sc_service::GenericChainSpec<battery_station_runtime::GenesisConfig, Extensions>;
} else {
pub type BatteryStationChainSpec = sc_service::GenericChainSpec<battery_station_runtime::GenesisConfig>;
}
}

const DEFAULT_INITIAL_BALANCE_BATTERY_STATION: u128 = 10_000 * BASE;

#[cfg(feature = "parachain")]
fn additional_chain_spec_staging_battery_station(
parachain_id: cumulus_primitives_core::ParaId,
) -> AdditionalChainSpec {
AdditionalChainSpec {
candidates: vec![(
hex!["302f6d7467ae2d7e3b9b962bfc3b9d929da9fae5f1e8c977a031ddf721b0790d"].into(),
hex!["e6ea0b63b2b5b7247a1e8280350a14c5f9e7745dec2fe3428b68aa4167d48e66"]
.unchecked_into(),
DEFAULT_STAKING_AMOUNT_BATTERY_STATION,
)],
crowdloan_fund_pot: DEFAULT_INITIAL_CROWDLOAN_FUNDS_BATTERY_STATION,
inflation_info: DEFAULT_COLLATOR_INFLATION_INFO,
nominations: vec![],
parachain_id,
}
}

#[cfg(not(feature = "parachain"))]
fn additional_chain_spec_staging_battery_station() -> AdditionalChainSpec {
AdditionalChainSpec {
initial_authorities: vec![(
// 5FCSJzvmeUW1hBo3ASnLzSxpUdn5QUDt1Eqobj1meiQB7mLu
hex!["8a9a54bdf73fb4a757f5ab81fabe2f173922fdb92bb8b6e8bedf8b17fa38f500"]
.unchecked_into(),
// 5HGProUwcyCDMJDxjBBKbv8u7ehr5uoTBS3bckYHPcZMTifW
hex!["e61786c6426b55a034f9c4b78dc57d4183927cef8e64b2e496225ed6fca41758"]
.unchecked_into(),
)],
}
}

fn endowed_accounts_staging_battery_station() -> Vec<EndowedAccountWithBalance> {
vec![
// 5D2L4ghyiYE8p2z7VNJo9JYwRuc8uzPWtMBqdVyvjRcsnw4P
EndowedAccountWithBalance(
hex!["2a6c61a907556e4c673880b5767dd4be08339ee7f2a58d5137d0c19ca9570a5c"].into(),
DEFAULT_INITIAL_BALANCE_BATTERY_STATION,
),
// 5EeeZVU4SiPG6ZRY7o8aDcav2p2mZMdu3ZLzbREWuHktYdhX
EndowedAccountWithBalance(
hex!["725bb6fd13d52b3d6830e5a9faed1f6499ca0f5e8aa285df09490646e71e831b"].into(),
DEFAULT_INITIAL_BALANCE_BATTERY_STATION,
),
// 5D9tF8w1FMSdz52bpiaQis1pCUZy5Gs6HcHS7gHxEzyq4XzU
#[cfg(feature = "parachain")]
EndowedAccountWithBalance(
hex!["302f6d7467ae2d7e3b9b962bfc3b9d929da9fae5f1e8c977a031ddf721b0790d"].into(),
DEFAULT_COLLATOR_BALANCE_BATTERY_STATION.unwrap(),
),
]
}

fn root_key_staging_battery_station() -> AccountId {
hex!["2a6c61a907556e4c673880b5767dd4be08339ee7f2a58d5137d0c19ca9570a5c"].into()
}

#[inline]
pub(super) fn get_wasm() -> Result<&'static [u8], String> {
battery_station_runtime::WASM_BINARY.ok_or_else(|| "WASM binary is not available".to_string())
}

generate_generic_genesis_function!(
battery_station_runtime,
sudo: battery_station_runtime::SudoConfig { key: Some(root_key_staging_battery_station()) },
);

pub fn battery_station_staging_config(
#[cfg(feature = "parachain")] parachain_id: cumulus_primitives_core::ParaId,
) -> Result<ChainSpec, String> {
let zeitgeist_wasm = zeitgeist_wasm()?;
) -> Result<BatteryStationChainSpec, String> {
let wasm = get_wasm()?;

Ok(ChainSpec::from_genesis(
Ok(BatteryStationChainSpec::from_genesis(
"Battery Station Staging",
"battery_station_staging",
ChainType::Live,
move || {
generic_genesis(
additional_chain_spec_staging_testnet(
additional_chain_spec_staging_battery_station(
#[cfg(feature = "parachain")]
parachain_id,
),
endowed_accounts_staging_testnet(),
#[cfg(not(feature = "without-sudo"))]
root_key_staging_testnet(),
zeitgeist_wasm,
endowed_accounts_staging_battery_station(),
wasm,
)
},
vec![],
telemetry_endpoints(),
Some("battery_station"),
None,
Some(token_properties("ZBS")),
Some(token_properties("ZBS", SS58Prefix::get())),
#[cfg(feature = "parachain")]
crate::chain_spec::Extensions {
relay_chain: "rococo".into(),
Expand Down
Loading

0 comments on commit a6ec392

Please sign in to comment.