Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into bko-bridges-backpor…
Browse files Browse the repository at this point in the history
…ts-and-nits
  • Loading branch information
bkontur committed Jun 5, 2024
2 parents c0eeff6 + 3977f38 commit 87a52db
Show file tree
Hide file tree
Showing 29 changed files with 1,044 additions and 132 deletions.
34 changes: 32 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ members = [
"cumulus/test/service",
"cumulus/xcm/xcm-emulator",
"docs/sdk",
"docs/sdk/src/reference_docs/chain_spec_runtime",
"polkadot",
"polkadot/cli",
"polkadot/core-primitives",
Expand Down
28 changes: 15 additions & 13 deletions cumulus/parachains/chain-specs/asset-hub-rococo.json

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions cumulus/parachains/chain-specs/bridge-hub-rococo.json

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions cumulus/parachains/chain-specs/contracts-rococo.json

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions cumulus/parachains/chain-specs/coretime-rococo.json

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions cumulus/parachains/chain-specs/people-rococo.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions cumulus/test/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sp-version = { path = "../../../substrate/primitives/version", default-features

# Cumulus
cumulus-pallet-parachain-system = { path = "../../pallets/parachain-system", default-features = false }
parachain-info = { package = "staging-parachain-info", path = "../../parachains/pallets/parachain-info", default-features = false }
cumulus-primitives-aura = { path = "../../primitives/aura", default-features = false }
pallet-collator-selection = { path = "../../pallets/collator-selection", default-features = false }
cumulus-pallet-aura-ext = { path = "../../pallets/aura-ext", default-features = false }
Expand Down Expand Up @@ -74,6 +75,7 @@ std = [
"pallet-sudo/std",
"pallet-timestamp/std",
"pallet-transaction-payment/std",
"parachain-info/std",
"scale-info/std",
"sp-api/std",
"sp-block-builder/std",
Expand Down
10 changes: 4 additions & 6 deletions cumulus/test/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
>;
impl cumulus_pallet_parachain_system::Config for Runtime {
type WeightInfo = ();
type SelfParaId = ParachainId;
type SelfParaId = parachain_info::Pallet<Runtime>;
type RuntimeEvent = RuntimeEvent;
type OnSystemEvent = ();
type OutboundXcmpMessageSource = ();
Expand All @@ -294,6 +294,8 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type ConsensusHook = ConsensusHook;
}

impl parachain_info::Config for Runtime {}

impl pallet_aura::Config for Runtime {
type AuthorityId = AuraId;
type DisabledValidators = ();
Expand All @@ -302,11 +304,6 @@ impl pallet_aura::Config for Runtime {
type SlotDuration = ConstU64<SLOT_DURATION>;
}

parameter_types! {
// will be set by test_pallet during genesis init
pub storage ParachainId: cumulus_primitives_core::ParaId = PARACHAIN_ID.into();
}

impl test_pallet::Config for Runtime {}

construct_runtime! {
Expand All @@ -315,6 +312,7 @@ construct_runtime! {
System: frame_system,
ParachainSystem: cumulus_pallet_parachain_system,
Timestamp: pallet_timestamp,
ParachainInfo: parachain_info,
Balances: pallet_balances,
Sudo: pallet_sudo,
TransactionPayment: pallet_transaction_payment,
Expand Down
4 changes: 0 additions & 4 deletions cumulus/test/runtime/src/test_pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ pub mod pallet {
#[derive(frame_support::DefaultNoBound)]
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub self_para_id: Option<cumulus_primitives_core::ParaId>,
#[serde(skip)]
pub _config: sp_std::marker::PhantomData<T>,
}
Expand All @@ -87,9 +86,6 @@ pub mod pallet {
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
sp_io::storage::set(TEST_RUNTIME_UPGRADE_KEY, &[1, 2, 3, 4]);
self.self_para_id.map(|para_id| {
crate::ParachainId::set(&para_id);
});
}
}
}
4 changes: 3 additions & 1 deletion cumulus/test/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ pub fn testnet_genesis(
balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(),
},
"sudo": cumulus_test_runtime::SudoConfig { key: Some(root_key) },
"testPallet": cumulus_test_runtime::TestPalletConfig { self_para_id: Some(self_para_id), ..Default::default() },
"parachainInfo": {
"parachainId": self_para_id,
},
"aura": cumulus_test_runtime::AuraConfig { authorities: invulnerables }
})
}
5 changes: 5 additions & 0 deletions docs/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ sc-consensus-manual-seal = { path = "../../substrate/client/consensus/manual-sea
sc-consensus-pow = { path = "../../substrate/client/consensus/pow" }
sc-executor = { path = "../../substrate/client/executor" }
sc-service = { path = "../../substrate/client/service" }
sc-chain-spec = { path = "../../substrate/client/chain-spec" }

substrate-wasm-builder = { path = "../../substrate/utils/wasm-builder" }

Expand Down Expand Up @@ -90,6 +91,7 @@ sp-core = { path = "../../substrate/primitives/core" }
sp-keyring = { path = "../../substrate/primitives/keyring" }
sp-runtime = { path = "../../substrate/primitives/runtime" }
sp-arithmetic = { path = "../../substrate/primitives/arithmetic" }
sp-genesis-builder = { path = "../../substrate/primitives/genesis-builder" }

# Misc pallet dependencies
pallet-referenda = { path = "../../substrate/frame/referenda" }
Expand All @@ -102,3 +104,6 @@ sp-version = { path = "../../substrate/primitives/version" }
# XCM
xcm = { package = "staging-xcm", path = "../../polkadot/xcm" }
xcm-docs = { path = "../../polkadot/xcm/docs" }

# runtime guides
chain-spec-guide-runtime = { path = "./src/reference_docs/chain_spec_runtime" }
187 changes: 184 additions & 3 deletions docs/sdk/src/reference_docs/chain_spec_genesis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,185 @@
//! Chain spec and genesis build.
//! # What is chain-spec.
//!
//! What is chain-spec.
//! What is genesis state and how to build it.
//! A chain specification file defines the set of properties that are required to run the node as
//! part of the chain. The chain specification consists of two main parts:
//! - initial state of the runtime,
//! - network / logical properties of the chain, the most important property being the list of
//! bootnodes.
//!
//! This document describes how initial state is handled in pallets and runtime, and how to interact
//! with the runtime in order to build genesis state.
//!
//! For more information on chain specification and its properties, refer to
//! [`sc_chain_spec#from-initial-state-to-raw-genesis`].
//!
//! The initial genesis state can be provided in the following formats:
//! - full
//! - patch
//! - raw
//!
//! Each of the formats is explained in [_chain-spec-format_][`sc_chain_spec#chain-spec-formats`].
//!
//!
//! # `GenesisConfig` for `pallet`
//!
//! Every frame pallet may have its initial state which is defined by the `GenesisConfig` internal
//! struct. It is a regular rust struct, annotated with [`pallet::genesis_config`] attribute.
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", pallet_bar_GenesisConfig)]
//!
//! The struct shall be defined within the pallet `mod`, as in the following code:
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", pallet_bar)]
//!
//! The initial state conveyed in the `GenesisConfig` struct is transformed into state storage
//! items by means of the [`BuildGenesisConfig`] trait which shall be implemented for the pallet's
//! `GenesisConfig` struct. The [`pallet::genesis_build`] attribute shall be attached to the `impl`
//! block:
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", pallet_bar_build)]
//!
//! `GenesisConfig` may also contain more complicated types, including nested structs or enums, as
//! in the example for `pallet_foo`:
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", pallet_foo_GenesisConfig)]
//!
//! Note that [`serde`] attributes can be used to control how the data
//! structures are being stored into JSON. In the following example [`sp_core::bytes`] function is
//! used to serialize the `values` field.
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", SomeFooData2)]
//!
//! Please note that fields of `GenesisConfig` may not be directly mapped to storage items. In the
//! following example the initial struct fields are used to compute (sum) the value that will be
//! stored in state as `ProcessedEnumValue`:
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/pallets.rs", pallet_foo_build)]
//!
//! # `GenesisConfig` for `runtimes`
//!
//! The runtime genesis config struct consists of configs for every pallet. For [_demonstration
//! runtime_][`chain_spec_guide_runtime`] used in this guide, it consists of `SystemConfig`,
//! `BarConfig` and `FooConfig`. This structure was automatically generated by macro and it can be
//! sneak peaked here: [`RuntimeGenesisConfig`]. For further reading on generated runtime
//! types refer to [`frame_runtime_types`].
//!
//! The macro automatically adds an attribute that renames all the fields to [`camelCase`]. It is a
//! good practice to add it to nested structures too, to have the naming of the JSON keys consistent
//! across the chain-spec file.
//!
//! ## `Default` for `GenesisConfig`
//!
//! `GenesisConfig` of all pallets must implement `Defualt` trait. Those are aggregated into
//! runtime's `RuntimeGenesisConfig`'s `Default`.
//!
//! Default value of `RuntimeGenesisConfig` can be queried by [`GenesisBuilder::get_preset`]
//! function provided by runtime with `id:None`.
//!
//! A default value for RuntimeGenesisConfig usually is not operational. This is because for some
//! pallets it is not possible to define good defaults (e.g. an initial set of authorities).
//!
//! A default value is a base upon which a patch for `GenesisConfig` is applied. A good description
//! of how it exactly works is provided in [`get_storage_for_patch`] (and also in
//! [`GenesisBuilder::get_preset`]). A patch can be provided as a external file (manually created)
//! or as builtin runtime preset. More info on presets are in the material to follow.
//!
//! ## Implementing `GenesisBuilder` for runtime
//!
//! The runtime exposes a dedicated runtime API for interacting with its genesis config:
//! [`sp_genesis_builder::GenesisBuilder`]. The implementation shall be provided within
//! [`sp_api::impl_runtime_apis`] macro, typically making use of some helpers provided:
//! [`build_state`], [`get_preset`].
//! A typical implementation of [`sp_genesis_builder::GenesisBuilder`] looks as follows:
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/runtime.rs", runtime_impl)]
//!
//! Please note that two functions are customized: `preset_names` and `get_preset`. The first one
//! just provides a `Vec` of the names of supported presets, while the latter one delegates the call
//! to a function that maps the name to an actual preset:
//! [`chain_spec_guide_runtime::presets::get_builtin_preset`]
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", get_builtin_preset)]
//!
//! ## Genesis state presets for runtime
//!
//! The runtime may provide many flavors of initial genesis state. This may be useful for predefined
//! testing networks, local development, or CI integration tests. Predefined genesis state may
//! contain a list of pre-funded accounts, predefined authorities for consensus, sudo key and many
//! others useful for testing.
//!
//! Internally presets can be provided in a number of ways:
//! - JSON in string form:
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", preset_1)]
//! - JSON using runtime types to serialize values:
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", preset_2)]
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", preset_3)]
//! It is worth noting that preset does not have to be the full `RuntimeGenesisConfig`, in that
//! sense that it does not have to contain all the keys of the struct. The preset is actually a JSON
//! patch that will be merged with default value of `RuntimeGenesisConfig`. This approach should
//! simplify maintanance of builtin presets. Following example illustrates runtime genesis config
//! patch:
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", preset_4)]
//!
//! ## Note on the importance of testing presets
//!
//! It is recommended to always test presets by adding the tests that convert the preset into the
//! raw storage. Converting to raw storage involves the deserialization of the provided JSON blob,
//! what enforces the verification of the preset. The following code shows one of the approaches
//! that can be taken for testing:
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", check_presets)]
//!
//! ## Note on tne importance of using `deny_unknown_fields` attribute
//!
//! It is worth noting that it is easy to make a hard to spot mistake as in the following example
//! ([`FooStruct`] does not contain `fieldC`):
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", preset_invalid)]
//! Even though `preset_invalid` contains a key that does not exist, the deserialization of the JSON
//! blob does not fail. The mispelling is silently ignored due to lack of [`deny_unknown_fields`]
//! attribute on the [`FooStruct`] struct, which is internally used in `GenesisConfig`.
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/src/presets.rs", invalid_preset_works)]
//!
//! ## Runtime `GenesisConfig` raw format
//!
//! A raw format of genesis config cotains just state's keys and values as they are stored in the
//! storage. This format is used to directly initialize the genesis storage. This format is useful
//! for long-term running chains, where the `GenesisConfig` stucture for pallets may be evolving
//! over the time. The JSON representation created at some point in time may no longer be
//! deserializable in future, making a chain specification useless. The raw format is recommended
//! for the production chains.
//!
//! For detailed description on how raw format is built please refer to
//! [_chain-spec-raw-genesis_][`sc_chain_spec#from-initial-state-to-raw-genesis`]. A plain and
//! corresponding raw examples of chain-spec are given in
//! [_chain-spec-examples_][`sc_chain_spec#json-chain-specification-example`].
//! The [`chain_spec_builder`] util supports building the raw storage.
//!
//! # Interacting with the tool
//!
//! The [`chain_spec_builder`] util allows to interact with runtime in order to list or display
//! presets and build the chain specification file. It is possible to use the tool with the
//! [_demonstration runtime_][`chain_spec_guide_runtime`]. To build required packages just run the
//! following command:
//! ```ignore
//! cargo build -p staging-chain-spec-builder -p chain-spec-guide-runtime --release
//! ```
//! The `chain-spec-builder` util can also be installed with `cargo install`:
//! ```ignore
//! cargo install staging-chain-spec-builder
//! cargo build -p chain-spec-guide-runtime --release
//! ```
//! Here are some examples in the form of rust tests:
//! ## Listing available presets names:
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs", list_presets)]
//! ## Displaying preset with given name
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs", get_preset)]
//! ## Building chain-spec using given preset
#![doc = docify::embed!("./src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs", generate_chain_spec)]
//!
//! [`RuntimeGenesisConfig`]:
//! chain_spec_guide_runtime::runtime::RuntimeGenesisConfig
//! [`FooStruct`]:
//! chain_spec_guide_runtime::pallets::FooStruct
//! [`impl_runtime_apis`]: frame::runtime::prelude::impl_runtime_apis
//! [`build_state`]: frame_support::genesis_builder_helper::build_state
//! [`get_preset`]: frame_support::genesis_builder_helper::get_preset
//! [`pallet::genesis_build`]: frame_support::pallet_macros::genesis_build
//! [`pallet::genesis_config`]: frame_support::pallet_macros::genesis_config
//! [`BuildGenesisConfig`]: frame_support::traits::BuildGenesisConfig
//! [`chain_spec_builder`]: ../../../staging_chain_spec_builder/index.html
//! [`serde`]: https://serde.rs/field-attrs.html
//! [`get_storage_for_patch`]: sc_chain_spec::GenesisConfigBuilderRuntimeCaller::get_storage_for_patch
//! [`GenesisBuilder::get_preset`]: sp_genesis_builder::GenesisBuilder::get_preset
//! [`deny_unknown_fields`]: https://serde.rs/container-attrs.html#deny_unknown_fields
//! [`camelCase`]: https://serde.rs/container-attrs.html#rename_all
Loading

0 comments on commit 87a52db

Please sign in to comment.