Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

chain-spec: support for json config/patch (and GenesisBuilder API) #7508

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d8fc2ca
ChainSpec: code moved from pallet_system to ChainSpec
michalkucharczyk Jul 17, 2023
9d6fc63
ChainSpec: tests agains legacy added
michalkucharczyk Jul 18, 2023
9f0581a
runtimes: genesis-builder implemented
michalkucharczyk Jul 18, 2023
f6a65f2
rococo-runtime: EpochDurationInBlock is controlled by feature
michalkucharczyk Jul 20, 2023
52b4d8c
polkadot_staging_testnet_config removed
michalkucharczyk Jul 20, 2023
cfc2dcb
chain-specs: RuntimeGenesisConfig remove, JSON patches added
michalkucharczyk Jul 20, 2023
ae9f732
polkadot_staging_testnet_config_genesis: removed
michalkucharczyk Jul 20, 2023
cf31128
Merge remote-tracking branch 'origin/master' into mku-chain-spec-supp…
michalkucharczyk Jul 20, 2023
39d2de3
polkadot_staging: removed
michalkucharczyk Jul 20, 2023
352bbb0
polkadot-test-service: RuntimeGenesisConfig removed, JSON patch added
michalkucharczyk Jul 20, 2023
b4f54b7
runtimes: disable-genesis-builder feature added
michalkucharczyk Jul 21, 2023
0229867
rococo-runtime: pre-built variants removed
michalkucharczyk Jul 24, 2023
3d7412e
warnings fixed
michalkucharczyk Jul 25, 2023
f5c1337
Merge remote-tracking branch 'origin/master' into mku-chain-spec-supp…
Jul 25, 2023
d417107
Merge remote-tracking branch 'origin/master' into mku-chain-spec-supp…
michalkucharczyk Jul 25, 2023
6dfe988
Merge remote-tracking branch 'origin/master' into mku-chain-spec-supp…
Aug 1, 2023
aa5760b
Merge remote-tracking branch 'origin/master' into mku-chain-spec-supp…
Aug 2, 2023
478b332
beefy::crypto -> ecdsa_crypto
michalkucharczyk Aug 3, 2023
9078fab
node/test: polkadot_test_runtime::RuntimeGenesisConfig removed in Cha…
michalkucharczyk Aug 3, 2023
18ee1ab
runtimes: build.rs env cleanup
michalkucharczyk Aug 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,109 changes: 494 additions & 615 deletions node/service/src/chain_spec.rs

Large diffs are not rendered by default.

1,907 changes: 1,907 additions & 0 deletions node/service/src/chain_spec/legacy_chain_spec.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion node/test/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct GenesisParameters;

impl substrate_test_client::GenesisInit for GenesisParameters {
fn genesis_storage(&self) -> Storage {
polkadot_test_service::chain_spec::polkadot_local_testnet_genesis()
polkadot_test_service::chain_spec::polkadot_local_testnet_config()
.build_storage()
.expect("Builds test runtime genesis storage")
}
Expand Down
2 changes: 1 addition & 1 deletion node/test/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" }
substrate-test-client = { git = "https://github.com/paritytech/substrate", branch = "master" }
serde_json = "1.0.96"

[dev-dependencies]
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
serde_json = "1.0.96"
substrate-test-utils = { git = "https://github.com/paritytech/substrate", branch = "master" }
tokio = { version = "1.24.2", features = ["macros"] }

Expand Down
111 changes: 47 additions & 64 deletions node/test/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,26 @@ use test_runtime_constants::currency::DOTS;
const DEFAULT_PROTOCOL_ID: &str = "dot";

/// The `ChainSpec` parameterized for polkadot test runtime.
pub type PolkadotChainSpec =
sc_service::GenericChainSpec<polkadot_test_runtime::RuntimeGenesisConfig, Extensions>;
pub type PolkadotChainSpec = sc_service::GenericChainSpec<(), Extensions>;

/// Local testnet config (multivalidator Alice + Bob)
pub fn polkadot_local_testnet_config() -> PolkadotChainSpec {
PolkadotChainSpec::from_genesis(
"Local Testnet",
"local_testnet",
ChainType::Local,
|| polkadot_local_testnet_genesis(),
vec![],
None,
Some(DEFAULT_PROTOCOL_ID),
None,
Some(polkadot_chain_spec_properties()),
Default::default(),
)
PolkadotChainSpec::builder()
.with_name("Local Testnet")
.with_id("local_testnet")
.with_chain_type(ChainType::Local)
.with_genesis_config_patch(polkadot_local_testnet_genesis())
.with_protocol_id(DEFAULT_PROTOCOL_ID)
.with_extensions(Default::default())
.with_properties(polkadot_chain_spec_properties())
.with_code(
polkadot_test_runtime::WASM_BINARY.expect("Wasm binary must be built for testing"),
)
.build()
}

/// Local testnet genesis config (multivalidator Alice + Bob)
pub fn polkadot_local_testnet_genesis() -> polkadot_test_runtime::RuntimeGenesisConfig {
pub fn polkadot_local_testnet_genesis() -> serde_json::Value {
polkadot_testnet_genesis(
vec![get_authority_keys_from_seed("Alice"), get_authority_keys_from_seed("Bob")],
get_account_id_from_seed::<sr25519::Public>("Alice"),
Expand Down Expand Up @@ -106,25 +105,20 @@ fn polkadot_testnet_genesis(
)>,
root_key: AccountId,
endowed_accounts: Option<Vec<AccountId>>,
) -> polkadot_test_runtime::RuntimeGenesisConfig {
) -> serde_json::Value {
use polkadot_test_runtime as runtime;

let endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(testnet_accounts);

const ENDOWMENT: u128 = 1_000_000 * DOTS;
const STASH: u128 = 100 * DOTS;

runtime::RuntimeGenesisConfig {
system: runtime::SystemConfig {
code: runtime::WASM_BINARY.expect("Wasm binary must be built for testing").to_vec(),
..Default::default()
},
indices: runtime::IndicesConfig { indices: vec![] },
balances: runtime::BalancesConfig {
balances: endowed_accounts.iter().map(|k| (k.clone(), ENDOWMENT)).collect(),
serde_json::json!({
"balances": {
"balances": endowed_accounts.iter().map(|k| (k.clone(), ENDOWMENT)).collect::<Vec<_>>(),
},
session: runtime::SessionConfig {
keys: initial_authorities
"session": {
"keys": initial_authorities
.iter()
.map(|x| {
(
Expand All @@ -141,48 +135,37 @@ fn polkadot_testnet_genesis(
})
.collect::<Vec<_>>(),
},
staking: runtime::StakingConfig {
minimum_validator_count: 1,
validator_count: 2,
stakers: initial_authorities
"staking": {
"minimumValidatorCount": 1,
"validatorCount": 2,
"stakers": initial_authorities
.iter()
.map(|x| (x.0.clone(), x.0.clone(), STASH, runtime::StakerStatus::Validator))
.collect(),
invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect(),
force_era: Forcing::NotForcing,
slash_reward_fraction: Perbill::from_percent(10),
..Default::default()
},
babe: runtime::BabeConfig {
authorities: vec![],
epoch_config: Some(BABE_GENESIS_EPOCH_CONFIG),
..Default::default()
.map(|x| (x.0.clone(), x.0.clone(), STASH, runtime::StakerStatus::<AccountId>::Validator))
.collect::<Vec<_>>(),
"invulnerables": initial_authorities.iter().map(|x| x.0.clone()).collect::<Vec<_>>(),
"forceEra": Forcing::NotForcing,
"slashRewardFraction": Perbill::from_percent(10),
},
grandpa: Default::default(),
authority_discovery: runtime::AuthorityDiscoveryConfig {
keys: vec![],
..Default::default()
"babe": {
"epochConfig": Some(BABE_GENESIS_EPOCH_CONFIG),
},
claims: runtime::ClaimsConfig { claims: vec![], vesting: vec![] },
vesting: runtime::VestingConfig { vesting: vec![] },
sudo: runtime::SudoConfig { key: Some(root_key) },
configuration: runtime::ConfigurationConfig {
config: polkadot_runtime_parachains::configuration::HostConfiguration {
validation_upgrade_cooldown: 10u32,
validation_upgrade_delay: 5,
code_retention_period: 1200,
max_code_size: MAX_CODE_SIZE,
max_pov_size: MAX_POV_SIZE,
max_head_data_size: 32 * 1024,
group_rotation_frequency: 20,
chain_availability_period: 4,
thread_availability_period: 4,
no_show_slots: 10,
minimum_validation_upgrade_delay: 5,
..Default::default()
"sudo": { "key": Some(root_key) },
"configuration": {
"config": {
"validationUpgradeCooldown": 10u32,
"validationUpgradeDelay": 5,
"codeRetentionPeriod": 1200,
"maxCodeSize": MAX_CODE_SIZE,
"maxPovSize": MAX_POV_SIZE,
"maxHeadDataSize": 32 * 1024,
"groupRotationFrequency": 20,
"chainAvailabilityPeriod": 4,
"threadAvailabilityPeriod": 4,
"noShowSlots": 10,
"minimumValidationUpgradeDelay": 5,
},
},
}
}
})
}

/// Can be called for a `Configuration` to check if it is a configuration for the `Test` network.
Expand Down
5 changes: 4 additions & 1 deletion runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ impl sp_runtime::traits::Convert<sp_core::U256, Balance> for U256ToBalance {
}

/// Macro to set a value (e.g. when using the `parameter_types` macro) to either a production value
/// or to an environment variable or testing value (in case the `fast-runtime` feature is selected).
/// or to an environment variable or testing value (in case the `fast-runtime` feature is selected)
/// or one of two testing values depending on feature.
/// Note that the environment variable is evaluated _at compile time_.
///
/// Usage:
Expand All @@ -255,6 +256,8 @@ impl sp_runtime::traits::Convert<sp_core::U256, Balance> for U256ToBalance {
/// // Note that the env variable version parameter cannot be const.
/// pub LaunchPeriod: BlockNumber = prod_or_fast!(7 * DAYS, 1, "KSM_LAUNCH_PERIOD");
/// pub const VotingPeriod: BlockNumber = prod_or_fast!(7 * DAYS, 1 * MINUTES);
/// pub const EpochDuration: BlockNumber =
/// prod_or_fast!(1 * HOURS, "fast-runtime", 1 * MINUTES, "fast-runtime-10m", 10 * MINUTES);
/// }
/// ```
#[macro_export]
Expand Down
6 changes: 6 additions & 0 deletions runtime/kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ inherents = { package = "sp-inherents", git = "https://github.com/paritytech/sub
offchain-primitives = { package = "sp-offchain", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { package = "sp-std", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-genesis-builder = { package = "sp-genesis-builder", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-mmr-primitives = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -315,9 +316,14 @@ disable-runtime-api = []
# to make it smaller like logging for example.
on-chain-release-build = [
"sp-api/disable-logging",
"disable-genesis-builder",
]

# Set timing constants (e.g. session period) to faster versions to speed up testing.
fast-runtime = []

# When enabled, the GenesisBuilder API will not be supprted, GenesisConfig shall be
# stripped from the final binary
disable-genesis-builder = []

runtime-metrics = ["runtime-parachains/runtime-metrics", "sp-io/with-tracing"]
15 changes: 14 additions & 1 deletion runtime/kusama/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,23 @@

use substrate_wasm_builder::WasmBuilder;

const NO_GENESIS_BUILDER_ENV: &str = "BUILD_NO_GENESIS_BUILDER_SUPPORT";

fn main() {
WasmBuilder::new()
.with_current_project()
.import_memory()
.export_heap_base()
.build()
.build();

if std::env::var(NO_GENESIS_BUILDER_ENV).is_ok() {
WasmBuilder::new()
.with_current_project()
.import_memory()
.export_heap_base()
.enable_feature("disable-genesis-builder")
.set_file_name("kusama_runtime_no_genesis_builder")
.build();
};
println!("cargo:rerun-if-env-changed={}", NO_GENESIS_BUILDER_ENV);
}
14 changes: 14 additions & 0 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ use beefy_primitives::ecdsa_crypto::{AuthorityId as BeefyId, Signature as BeefyS
use frame_election_provider_support::{
generate_solution_type, onchain, NposSolution, SequentialPhragmen,
};

#[cfg(not(feature = "disable-genesis-builder"))]
use frame_support::genesis_builder_helper::{build_config, create_default_config};
use frame_support::{
construct_runtime, parameter_types,
traits::{
Expand Down Expand Up @@ -2216,6 +2219,17 @@ sp_api::impl_runtime_apis! {
Ok(batches)
}
}

#[cfg(not(feature = "disable-genesis-builder"))]
impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
fn create_default_config() -> Vec<u8> {
create_default_config::<RuntimeGenesisConfig>()
}

fn build_config(config: Vec<u8>) -> sp_genesis_builder::Result {
build_config::<RuntimeGenesisConfig>(config)
}
}
}

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions runtime/polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tx-pool-api = { package = "sp-transaction-pool", git = "https://github.com/parit
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-genesis-builder = { package = "sp-genesis-builder", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-mmr-primitives = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -298,9 +299,14 @@ disable-runtime-api = []
# to make it smaller like logging for example.
on-chain-release-build = [
"sp-api/disable-logging",
"disable-genesis-builder",
]

# Set timing constants (e.g. session period) to faster versions to speed up testing.
fast-runtime = []

# When enabled, the GenesisBuilder API will not be supprted, GenesisConfig shall be
# stripped from the final binary
disable-genesis-builder = []

runtime-metrics = ["runtime-parachains/runtime-metrics", "sp-io/with-tracing"]
15 changes: 14 additions & 1 deletion runtime/polkadot/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,23 @@

use substrate_wasm_builder::WasmBuilder;

const NO_GENESIS_BUILDER_ENV: &str = "BUILD_NO_GENESIS_BUILDER_SUPPORT";

fn main() {
WasmBuilder::new()
.with_current_project()
.import_memory()
.export_heap_base()
.build()
.build();

if std::env::var(NO_GENESIS_BUILDER_ENV).is_ok() {
WasmBuilder::new()
.with_current_project()
.import_memory()
.export_heap_base()
.enable_feature("disable-genesis-builder")
.set_file_name("polkadot_runtime_no_genesis_builder")
.build();
};
println!("cargo:rerun-if-env-changed={}", NO_GENESIS_BUILDER_ENV);
}
14 changes: 14 additions & 0 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ use runtime_parachains::{
use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId;
use beefy_primitives::ecdsa_crypto::{AuthorityId as BeefyId, Signature as BeefySignature};
use frame_election_provider_support::{generate_solution_type, onchain, SequentialPhragmen};
#[cfg(not(feature = "disable-genesis-builder"))]
use frame_support::genesis_builder_helper::{build_config, create_default_config};
use frame_support::{
construct_runtime, parameter_types,
traits::{
Expand Down Expand Up @@ -2190,6 +2192,18 @@ sp_api::impl_runtime_apis! {
Ok(batches)
}
}

#[cfg(not(feature = "disable-genesis-builder"))]
impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
fn create_default_config() -> Vec<u8> {
create_default_config::<RuntimeGenesisConfig>()
}

fn build_config(config: Vec<u8>) -> sp_genesis_builder::Result {
build_config::<RuntimeGenesisConfig>(config)
}
}

}

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion runtime/rococo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", d
inherents = { package = "sp-inherents", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
offchain-primitives = { package = "sp-offchain", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { package = "sp-std", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-genesis-builder = { package = "sp-genesis-builder", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-mmr-primitives = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -273,6 +274,6 @@ try-runtime = [
disable-runtime-api = []

# Set timing constants (e.g. session period) to faster versions to speed up testing.
fast-runtime = []
fast-runtime = [ "rococo-runtime-constants/fast-runtime" ]

runtime-metrics = ["runtime-parachains/runtime-metrics", "sp-io/with-tracing"]
24 changes: 19 additions & 5 deletions runtime/rococo/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,26 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use std::env;
use substrate_wasm_builder::WasmBuilder;

// note: needs to be synced with rococo-runtime-constants::time hard-coded string literal in prod_or_fast macro.
const ROCOCO_EPOCH_DURATION_ENV: &str = "ROCOCO_EPOCH_DURATION";
const ROCOCO_FAST_RUNTIME_ENV: &str = "ROCOCO_FAST_RUNTIME";

fn main() {
WasmBuilder::new()
.with_current_project()
.import_memory()
.export_heap_base()
.build()
#[cfg(feature = "std")]
{
let mut builder =
WasmBuilder::new().with_current_project().import_memory().export_heap_base();

if env::var(ROCOCO_EPOCH_DURATION_ENV).is_ok() | env::var(ROCOCO_FAST_RUNTIME_ENV).is_ok() {
builder = builder.enable_feature("fast-runtime")
};

builder.build();

println!("cargo:rerun-if-env-changed={}", ROCOCO_EPOCH_DURATION_ENV);
println!("cargo:rerun-if-env-changed={}", ROCOCO_FAST_RUNTIME_ENV);
}
}
3 changes: 3 additions & 0 deletions runtime/rococo/constants/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ std = [
"sp-runtime/std",
"sp-weights/std"
]

# Set timing constants (e.g. session period) to faster versions to speed up testing.
fast-runtime = []
Loading