referrerpolicy=no-referrer-when-downgrade

polkadot_sdk_docs/polkadot_sdk/
cumulus.rs

1//! # Cumulus
2//!
3//! Substrate provides a framework ([FRAME]) through which a blockchain node and runtime can easily
4//! be created. Cumulus aims to extend the same approach to creation of Polkadot parachains.
5//!
6//! > Cumulus clouds are shaped sort of like dots; together they form a system that is intricate,
7//! > beautiful and functional.
8//!
9//! ## Example: Runtime
10//!
11//! A Cumulus-based runtime is fairly similar to other [FRAME]-based runtimes. Most notably, the
12//! following changes are applied to a normal FRAME-based runtime to make it a Cumulus-based
13//! runtime:
14//!
15//! #### Cumulus Pallets
16//!
17//! A parachain runtime should use a number of pallets that are provided by Cumulus and Substrate.
18//! Notably:
19//!
20//! - [`frame-system`](frame::prelude::frame_system), like all FRAME-based runtimes.
21//! - [`cumulus_pallet_parachain_system`]
22//! - [`parachain_info`]
23#![doc = docify::embed!("./src/polkadot_sdk/cumulus.rs", system_pallets)]
24//! Given that all Cumulus-based runtimes use a simple Aura-based consensus mechanism, the following
25//! pallets also need to be added:
26//!
27//! - [`pallet_timestamp`]
28//! - [`pallet_aura`]
29//! - [`cumulus_pallet_aura_ext`]
30#![doc = docify::embed!("./src/polkadot_sdk/cumulus.rs", consensus_pallets)]
31//! Finally, a separate macro, similar to
32//! [`impl_runtime_api`](frame::runtime::prelude::impl_runtime_apis), which creates the default set
33//! of runtime APIs, will generate the parachain runtime's validation runtime API, also known as
34//! parachain validation function (PVF). Without this API, the relay chain is unable to validate
35//! blocks produced by our parachain.
36#![doc = docify::embed!("./src/polkadot_sdk/cumulus.rs", validate_block)]
37//! ---
38//!
39//! [FRAME]: crate::polkadot_sdk::frame_runtime
40
41#![deny(rustdoc::broken_intra_doc_links)]
42#![deny(rustdoc::private_intra_doc_links)]
43
44#[cfg(test)]
45mod tests {
46	mod runtime {
47		pub use frame::{
48			deps::sp_consensus_aura::sr25519::AuthorityId as AuraId, prelude::*,
49			runtime::prelude::*, testing_prelude::*,
50		};
51
52		#[docify::export(CR)]
53		construct_runtime!(
54			pub enum Runtime {
55				// system-level pallets.
56				System: frame_system,
57				Timestamp: pallet_timestamp,
58				ParachainSystem: cumulus_pallet_parachain_system,
59				ParachainInfo: parachain_info,
60
61				// parachain consensus support -- mandatory.
62				Aura: pallet_aura,
63				AuraExt: cumulus_pallet_aura_ext,
64			}
65		);
66
67		#[docify::export]
68		mod system_pallets {
69			use super::*;
70
71			#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
72			impl frame_system::Config for Runtime {
73				type Block = MockBlock<Self>;
74				type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
75			}
76
77			impl cumulus_pallet_parachain_system::Config for Runtime {
78				type RuntimeEvent = RuntimeEvent;
79				type OnSystemEvent = ();
80				type SelfParaId = parachain_info::Pallet<Runtime>;
81				type OutboundXcmpMessageSource = ();
82				type XcmpMessageHandler = ();
83				type ReservedDmpWeight = ();
84				type ReservedXcmpWeight = ();
85				type CheckAssociatedRelayNumber =
86					cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
87				type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
88					Runtime,
89					6000, // relay chain block time
90					1,
91					1,
92				>;
93				type WeightInfo = ();
94				type DmpQueue = frame::traits::EnqueueWithOrigin<(), sp_core::ConstU8<0>>;
95				type RelayParentOffset = ConstU32<0>;
96				type SchedulingSignatureVerifier = ();
97			}
98
99			impl parachain_info::Config for Runtime {}
100		}
101
102		#[docify::export]
103		mod consensus_pallets {
104			use super::*;
105
106			impl pallet_aura::Config for Runtime {
107				type AuthorityId = AuraId;
108				type DisabledValidators = ();
109				type MaxAuthorities = ConstU32<100_000>;
110				type AllowMultipleBlocksPerSlot = ConstBool<false>;
111				type SlotDuration = pallet_aura::MinimumPeriodTimesTwo<Self>;
112			}
113
114			#[docify::export(timestamp)]
115			#[derive_impl(pallet_timestamp::config_preludes::TestDefaultConfig)]
116			impl pallet_timestamp::Config for Runtime {}
117
118			impl cumulus_pallet_aura_ext::Config for Runtime {}
119		}
120
121		#[docify::export(validate_block)]
122		cumulus_pallet_parachain_system::register_validate_block! {
123			Runtime = Runtime,
124			BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
125		}
126	}
127}