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//!
25//! Given that all Cumulus-based runtimes use a simple Aura-based consensus mechanism, the following
26//! pallets also need to be added:
27//!
28//! - [`pallet_timestamp`]
29//! - [`pallet_aura`]
30//! - [`cumulus_pallet_aura_ext`]
31#![doc = docify::embed!("./src/polkadot_sdk/cumulus.rs", consensus_pallets)]
32//!
33//!
34//! Finally, a separate macro, similar to
35//! [`impl_runtime_api`](frame::runtime::prelude::impl_runtime_apis), which creates the default set
36//! of runtime APIs, will generate the parachain runtime's validation runtime API, also known as
37//! parachain validation function (PVF). Without this API, the relay chain is unable to validate
38//! blocks produced by our parachain.
39#![doc = docify::embed!("./src/polkadot_sdk/cumulus.rs", validate_block)]
40//!
41//! ---
42//!
43//! [FRAME]: crate::polkadot_sdk::frame_runtime
44
45#![deny(rustdoc::broken_intra_doc_links)]
46#![deny(rustdoc::private_intra_doc_links)]
47
48#[cfg(test)]
49mod tests {
50	mod runtime {
51		pub use frame::{
52			deps::sp_consensus_aura::sr25519::AuthorityId as AuraId, prelude::*,
53			runtime::prelude::*, testing_prelude::*,
54		};
55
56		#[docify::export(CR)]
57		construct_runtime!(
58			pub enum Runtime {
59				// system-level pallets.
60				System: frame_system,
61				Timestamp: pallet_timestamp,
62				ParachainSystem: cumulus_pallet_parachain_system,
63				ParachainInfo: parachain_info,
64
65				// parachain consensus support -- mandatory.
66				Aura: pallet_aura,
67				AuraExt: cumulus_pallet_aura_ext,
68			}
69		);
70
71		#[docify::export]
72		mod system_pallets {
73			use super::*;
74
75			#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
76			impl frame_system::Config for Runtime {
77				type Block = MockBlock<Self>;
78				type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
79			}
80
81			impl cumulus_pallet_parachain_system::Config for Runtime {
82				type RuntimeEvent = RuntimeEvent;
83				type OnSystemEvent = ();
84				type SelfParaId = parachain_info::Pallet<Runtime>;
85				type OutboundXcmpMessageSource = ();
86				type XcmpMessageHandler = ();
87				type ReservedDmpWeight = ();
88				type ReservedXcmpWeight = ();
89				type CheckAssociatedRelayNumber =
90					cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
91				type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
92					Runtime,
93					6000, // relay chain block time
94					1,
95					1,
96				>;
97				type WeightInfo = ();
98				type DmpQueue = frame::traits::EnqueueWithOrigin<(), sp_core::ConstU8<0>>;
99				type RelayParentOffset = ConstU32<0>;
100			}
101
102			impl parachain_info::Config for Runtime {}
103		}
104
105		#[docify::export]
106		mod consensus_pallets {
107			use super::*;
108
109			impl pallet_aura::Config for Runtime {
110				type AuthorityId = AuraId;
111				type DisabledValidators = ();
112				type MaxAuthorities = ConstU32<100_000>;
113				type AllowMultipleBlocksPerSlot = ConstBool<false>;
114				type SlotDuration = pallet_aura::MinimumPeriodTimesTwo<Self>;
115			}
116
117			#[docify::export(timestamp)]
118			#[derive_impl(pallet_timestamp::config_preludes::TestDefaultConfig)]
119			impl pallet_timestamp::Config for Runtime {}
120
121			impl cumulus_pallet_aura_ext::Config for Runtime {}
122		}
123
124		#[docify::export(validate_block)]
125		cumulus_pallet_parachain_system::register_validate_block! {
126			Runtime = Runtime,
127			BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
128		}
129	}
130}