shell_runtime/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3
4// Cumulus is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Cumulus is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Cumulus.  If not, see <http://www.gnu.org/licenses/>.
16
17//! # Shell Runtime
18//!
19//! The Shell runtime defines a minimal parachain. It can listen for a downward message authorizing
20//! an upgrade into another parachain.
21//!
22//! Generally (so far) only used as the first parachain on a Relay.
23
24#![cfg_attr(not(feature = "std"), no_std)]
25// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
26#![recursion_limit = "256"]
27
28// Make the WASM binary available.
29#[cfg(feature = "std")]
30include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
31
32pub mod xcm_config;
33
34extern crate alloc;
35
36use alloc::{vec, vec::Vec};
37use codec::{Decode, Encode};
38use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
39use cumulus_primitives_core::AggregateMessageOrigin;
40use frame_support::unsigned::TransactionValidityError;
41use scale_info::TypeInfo;
42use sp_api::impl_runtime_apis;
43pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
44use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
45use sp_runtime::{
46	create_runtime_str, generic, impl_opaque_keys,
47	traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, DispatchInfoOf},
48	transaction_validity::{TransactionSource, TransactionValidity},
49	ApplyExtrinsicResult,
50};
51#[cfg(feature = "std")]
52use sp_version::NativeVersion;
53use sp_version::RuntimeVersion;
54
55// A few exports that help ease life for downstream crates.
56pub use frame_support::{
57	construct_runtime, derive_impl,
58	dispatch::DispatchClass,
59	genesis_builder_helper::{build_state, get_preset},
60	parameter_types,
61	traits::{ConstBool, ConstU32, ConstU64, ConstU8, EitherOfDiverse, IsInVec, Randomness},
62	weights::{
63		constants::{
64			BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND,
65		},
66		IdentityFee, Weight,
67	},
68	StorageValue,
69};
70use frame_system::limits::{BlockLength, BlockWeights};
71use parachains_common::{AccountId, Signature};
72#[cfg(any(feature = "std", test))]
73pub use sp_runtime::BuildStorage;
74pub use sp_runtime::{Perbill, Permill};
75
76impl_opaque_keys! {
77	pub struct SessionKeys {
78		pub aura: Aura,
79	}
80}
81
82/// This runtime version.
83#[sp_version::runtime_version]
84pub const VERSION: RuntimeVersion = RuntimeVersion {
85	spec_name: create_runtime_str!("shell"),
86	impl_name: create_runtime_str!("shell"),
87	authoring_version: 1,
88	spec_version: 2,
89	impl_version: 0,
90	apis: RUNTIME_API_VERSIONS,
91	transaction_version: 1,
92	state_version: 0,
93};
94
95/// The version information used to identify this runtime when compiled natively.
96#[cfg(feature = "std")]
97pub fn native_version() -> NativeVersion {
98	NativeVersion { runtime_version: VERSION, can_author_with: Default::default() }
99}
100
101/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included
102/// into the relay chain.
103const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1;
104/// How many parachain blocks are processed by the relay chain per parent. Limits the
105/// number of blocks authored per slot.
106const BLOCK_PROCESSING_VELOCITY: u32 = 1;
107/// Relay chain slot duration, in milliseconds.
108const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000;
109
110/// We assume that ~10% of the block weight is consumed by `on_initialize` handlers.
111/// This is used to limit the maximal weight of a single extrinsic.
112const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
113/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used
114/// by  Operational  extrinsics.
115const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
116/// We allow for .5 seconds of compute with a 12 second average block time.
117const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(
118	WEIGHT_REF_TIME_PER_SECOND.saturating_div(2),
119	cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64,
120);
121
122parameter_types! {
123	pub const BlockHashCount: BlockNumber = 250;
124	pub const Version: RuntimeVersion = VERSION;
125	pub RuntimeBlockLength: BlockLength =
126		BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
127	pub RuntimeBlockWeights: BlockWeights = BlockWeights::builder()
128		.base_block(BlockExecutionWeight::get())
129		.for_class(DispatchClass::all(), |weights| {
130			weights.base_extrinsic = ExtrinsicBaseWeight::get();
131		})
132		.for_class(DispatchClass::Normal, |weights| {
133			weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
134		})
135		.for_class(DispatchClass::Operational, |weights| {
136			weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
137			// Operational transactions have some extra reserved space, so that they
138			// are included even if block reached `MAXIMUM_BLOCK_WEIGHT`.
139			weights.reserved = Some(
140				MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT
141			);
142		})
143		.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
144		.build_or_panic();
145	pub const SS58Prefix: u8 = 42;
146}
147
148#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
149impl frame_system::Config for Runtime {
150	/// The identifier used to distinguish between accounts.
151	type AccountId = AccountId;
152	/// The aggregated dispatch type that is available for extrinsics.
153	type RuntimeCall = RuntimeCall;
154	/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
155	type Lookup = AccountIdLookup<AccountId, ()>;
156	/// The index type for storing how many extrinsics an account has signed.
157	type Nonce = Nonce;
158	/// The type for hashing blocks and tries.
159	type Hash = Hash;
160	/// The hashing algorithm used.
161	type Hashing = BlakeTwo256;
162	/// The block type.
163	type Block = Block;
164	/// The ubiquitous event type.
165	type RuntimeEvent = RuntimeEvent;
166	/// The ubiquitous origin type.
167	type RuntimeOrigin = RuntimeOrigin;
168	/// Maximum number of block number to block hash mappings to keep (oldest pruned first).
169	type BlockHashCount = BlockHashCount;
170	/// Runtime version.
171	type Version = Version;
172	/// Converts a module to an index of this module in the runtime.
173	type PalletInfo = PalletInfo;
174	type AccountData = ();
175	type OnNewAccount = ();
176	type OnKilledAccount = ();
177	type DbWeight = ();
178	type BaseCallFilter = frame_support::traits::Everything;
179	type SystemWeightInfo = ();
180	type BlockWeights = RuntimeBlockWeights;
181	type BlockLength = RuntimeBlockLength;
182	type SS58Prefix = SS58Prefix;
183	type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
184	type MaxConsumers = frame_support::traits::ConstU32<16>;
185}
186
187parameter_types! {
188	pub const RelayOrigin: AggregateMessageOrigin = AggregateMessageOrigin::Parent;
189	pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
190}
191
192impl cumulus_pallet_parachain_system::Config for Runtime {
193	type WeightInfo = ();
194	type RuntimeEvent = RuntimeEvent;
195	type OnSystemEvent = ();
196	type SelfParaId = parachain_info::Pallet<Runtime>;
197	type OutboundXcmpMessageSource = ();
198	type DmpQueue = frame_support::traits::EnqueueWithOrigin<MessageQueue, RelayOrigin>;
199	type ReservedDmpWeight = ReservedDmpWeight;
200	type XcmpMessageHandler = ();
201	type ReservedXcmpWeight = ();
202	type CheckAssociatedRelayNumber = RelayNumberMonotonicallyIncreases;
203	type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
204		Runtime,
205		RELAY_CHAIN_SLOT_DURATION_MILLIS,
206		BLOCK_PROCESSING_VELOCITY,
207		UNINCLUDED_SEGMENT_CAPACITY,
208	>;
209}
210
211impl parachain_info::Config for Runtime {}
212
213parameter_types! {
214	pub MessageQueueServiceWeight: Weight = Perbill::from_percent(35) * RuntimeBlockWeights::get().max_block;
215}
216
217impl pallet_message_queue::Config for Runtime {
218	type RuntimeEvent = RuntimeEvent;
219	type WeightInfo = ();
220	#[cfg(feature = "runtime-benchmarks")]
221	type MessageProcessor = pallet_message_queue::mock_helpers::NoopMessageProcessor<
222		cumulus_primitives_core::AggregateMessageOrigin,
223	>;
224	#[cfg(not(feature = "runtime-benchmarks"))]
225	type MessageProcessor = xcm_builder::ProcessXcmMessage<
226		AggregateMessageOrigin,
227		xcm_executor::XcmExecutor<xcm_config::XcmConfig>,
228		RuntimeCall,
229	>;
230	type Size = u32;
231	// These need to be configured to the XCMP pallet - if it is deployed.
232	type QueueChangeHandler = ();
233	type QueuePausedQuery = ();
234	type HeapSize = sp_core::ConstU32<{ 103 * 1024 }>;
235	type MaxStale = sp_core::ConstU32<8>;
236	type ServiceWeight = MessageQueueServiceWeight;
237	type IdleMaxServiceWeight = MessageQueueServiceWeight;
238}
239
240impl cumulus_pallet_aura_ext::Config for Runtime {}
241
242impl pallet_aura::Config for Runtime {
243	type AuthorityId = AuraId;
244	type DisabledValidators = ();
245	type MaxAuthorities = ConstU32<100_000>;
246	type AllowMultipleBlocksPerSlot = ConstBool<false>;
247	type SlotDuration = pallet_aura::MinimumPeriodTimesTwo<Self>;
248}
249
250impl pallet_timestamp::Config for Runtime {
251	type Moment = u64;
252	type OnTimestampSet = Aura;
253	type MinimumPeriod = ConstU64<0>;
254	type WeightInfo = ();
255}
256
257construct_runtime! {
258	pub enum Runtime
259	{
260		System: frame_system,
261		Timestamp: pallet_timestamp,
262
263		ParachainSystem: cumulus_pallet_parachain_system,
264		ParachainInfo: parachain_info,
265
266		CumulusXcm: cumulus_pallet_xcm,
267		MessageQueue: pallet_message_queue,
268
269		Aura: pallet_aura,
270		AuraExt: cumulus_pallet_aura_ext,
271	}
272}
273
274/// Simple implementation which fails any transaction which is signed.
275#[derive(Eq, PartialEq, Clone, Default, sp_core::RuntimeDebug, Encode, Decode, TypeInfo)]
276pub struct DisallowSigned;
277impl sp_runtime::traits::SignedExtension for DisallowSigned {
278	const IDENTIFIER: &'static str = "DisallowSigned";
279	type AccountId = AccountId;
280	type Call = RuntimeCall;
281	type AdditionalSigned = ();
282	type Pre = ();
283	fn additional_signed(
284		&self,
285	) -> core::result::Result<(), sp_runtime::transaction_validity::TransactionValidityError> {
286		Ok(())
287	}
288	fn pre_dispatch(
289		self,
290		who: &Self::AccountId,
291		call: &Self::Call,
292		info: &DispatchInfoOf<Self::Call>,
293		len: usize,
294	) -> Result<Self::Pre, TransactionValidityError> {
295		self.validate(who, call, info, len).map(|_| ())
296	}
297	fn validate(
298		&self,
299		_who: &Self::AccountId,
300		_call: &Self::Call,
301		_info: &sp_runtime::traits::DispatchInfoOf<Self::Call>,
302		_len: usize,
303	) -> TransactionValidity {
304		let i = sp_runtime::transaction_validity::InvalidTransaction::BadProof;
305		Err(sp_runtime::transaction_validity::TransactionValidityError::Invalid(i))
306	}
307}
308
309/// Index of a transaction in the chain.
310pub type Nonce = u32;
311/// A hash of some data used by the chain.
312pub type Hash = sp_core::H256;
313/// An index to a block.
314pub type BlockNumber = u32;
315/// The address format for describing accounts.
316pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
317/// Block header type as expected by this runtime.
318pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
319/// Block type as expected by this runtime.
320pub type Block = generic::Block<Header, UncheckedExtrinsic>;
321/// A Block signed with a Justification
322pub type SignedBlock = generic::SignedBlock<Block>;
323/// BlockId type as expected by this runtime.
324pub type BlockId = generic::BlockId<Block>;
325/// The SignedExtension to the basic transaction logic.
326pub type SignedExtra = DisallowSigned;
327/// Unchecked extrinsic type as expected by this runtime.
328pub type UncheckedExtrinsic =
329	generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
330/// Executive: handles dispatch to the various modules.
331pub type Executive = frame_executive::Executive<
332	Runtime,
333	Block,
334	frame_system::ChainContext<Runtime>,
335	Runtime,
336	AllPalletsWithSystem,
337>;
338
339impl_runtime_apis! {
340	impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {
341		fn slot_duration() -> sp_consensus_aura::SlotDuration {
342			sp_consensus_aura::SlotDuration::from_millis(Aura::slot_duration())
343		}
344
345		fn authorities() -> Vec<AuraId> {
346			pallet_aura::Authorities::<Runtime>::get().into_inner()
347		}
348	}
349
350	impl sp_api::Core<Block> for Runtime {
351		fn version() -> RuntimeVersion {
352			VERSION
353		}
354
355		fn execute_block(block: Block) {
356			Executive::execute_block(block)
357		}
358
359		fn initialize_block(header: &<Block as BlockT>::Header) -> sp_runtime::ExtrinsicInclusionMode {
360			Executive::initialize_block(header)
361		}
362	}
363
364	impl sp_api::Metadata<Block> for Runtime {
365		fn metadata() -> OpaqueMetadata {
366			OpaqueMetadata::new(Runtime::metadata().into())
367		}
368
369		fn metadata_at_version(version: u32) -> Option<OpaqueMetadata> {
370			Runtime::metadata_at_version(version)
371		}
372
373		fn metadata_versions() -> alloc::vec::Vec<u32> {
374			Runtime::metadata_versions()
375		}
376	}
377
378	impl sp_block_builder::BlockBuilder<Block> for Runtime {
379		fn apply_extrinsic(
380			extrinsic: <Block as BlockT>::Extrinsic,
381		) -> ApplyExtrinsicResult {
382			Executive::apply_extrinsic(extrinsic)
383		}
384
385		fn finalize_block() -> <Block as BlockT>::Header {
386			Executive::finalize_block()
387		}
388
389		fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
390			data.create_extrinsics()
391		}
392
393		fn check_inherents(block: Block, data: sp_inherents::InherentData) -> sp_inherents::CheckInherentsResult {
394			data.check_extrinsics(&block)
395		}
396	}
397
398	impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {
399		fn validate_transaction(
400			source: TransactionSource,
401			tx: <Block as BlockT>::Extrinsic,
402			block_hash: <Block as BlockT>::Hash,
403		) -> TransactionValidity {
404			Executive::validate_transaction(source, tx, block_hash)
405		}
406	}
407
408	impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
409		fn offchain_worker(header: &<Block as BlockT>::Header) {
410			Executive::offchain_worker(header)
411		}
412	}
413
414	impl sp_session::SessionKeys<Block> for Runtime {
415		fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
416			SessionKeys::generate(seed)
417		}
418
419		fn decode_session_keys(
420			encoded: Vec<u8>,
421		) -> Option<Vec<(Vec<u8>, KeyTypeId)>> {
422			SessionKeys::decode_into_raw_public_keys(&encoded)
423		}
424	}
425
426	impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {
427		fn collect_collation_info(header: &<Block as BlockT>::Header) -> cumulus_primitives_core::CollationInfo {
428			ParachainSystem::collect_collation_info(header)
429		}
430	}
431
432	impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
433		fn build_state(config: Vec<u8>) -> sp_genesis_builder::Result {
434			build_state::<RuntimeGenesisConfig>(config)
435		}
436
437		fn get_preset(id: &Option<sp_genesis_builder::PresetId>) -> Option<Vec<u8>> {
438			get_preset::<RuntimeGenesisConfig>(id, |_| None)
439		}
440
441		fn preset_names() -> Vec<sp_genesis_builder::PresetId> {
442			vec![]
443		}
444	}
445}
446
447cumulus_pallet_parachain_system::register_validate_block! {
448	Runtime = Runtime,
449	BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
450}