seedling_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//! # Seedling Runtime
18//!
19//! Seedling is a parachain meant to help parachain auction winners migrate a blockchain from
20//! another consensus system into the consensus system of a given Relay Chain.
21
22#![cfg_attr(not(feature = "std"), no_std)]
23// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
24#![recursion_limit = "256"]
25
26// Make the WASM binary available.
27#[cfg(feature = "std")]
28include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
29
30extern crate alloc;
31
32use alloc::{vec, vec::Vec};
33use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
34use sp_api::impl_runtime_apis;
35pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
36use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
37use sp_runtime::{
38	create_runtime_str, generic, impl_opaque_keys,
39	traits::{AccountIdLookup, BlakeTwo256, Block as BlockT},
40	transaction_validity::{TransactionSource, TransactionValidity},
41	ApplyExtrinsicResult,
42};
43#[cfg(feature = "std")]
44use sp_version::NativeVersion;
45use sp_version::RuntimeVersion;
46
47// A few exports that help ease life for downstream crates.
48pub use frame_support::{
49	construct_runtime, derive_impl,
50	dispatch::DispatchClass,
51	genesis_builder_helper::{build_state, get_preset},
52	parameter_types,
53	traits::{ConstBool, ConstU32, ConstU64, ConstU8, EitherOfDiverse, IsInVec, Randomness},
54	weights::{
55		constants::{
56			BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND,
57		},
58		IdentityFee, Weight,
59	},
60	StorageValue,
61};
62use frame_system::limits::{BlockLength, BlockWeights};
63use parachains_common::{AccountId, Signature};
64#[cfg(any(feature = "std", test))]
65pub use sp_runtime::BuildStorage;
66pub use sp_runtime::{Perbill, Permill};
67
68impl_opaque_keys! {
69	pub struct SessionKeys {
70		pub aura: Aura,
71	}
72}
73
74/// This runtime version.
75#[sp_version::runtime_version]
76pub const VERSION: RuntimeVersion = RuntimeVersion {
77	spec_name: create_runtime_str!("seedling"),
78	impl_name: create_runtime_str!("seedling"),
79	authoring_version: 1,
80	spec_version: 1,
81	impl_version: 0,
82	apis: RUNTIME_API_VERSIONS,
83	transaction_version: 2,
84	state_version: 0,
85};
86
87/// The version information used to identify this runtime when compiled natively.
88#[cfg(feature = "std")]
89pub fn native_version() -> NativeVersion {
90	NativeVersion { runtime_version: VERSION, can_author_with: Default::default() }
91}
92
93/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included
94/// into the relay chain.
95const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1;
96/// How many parachain blocks are processed by the relay chain per parent. Limits the
97/// number of blocks authored per slot.
98const BLOCK_PROCESSING_VELOCITY: u32 = 1;
99/// Relay chain slot duration, in milliseconds.
100const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000;
101
102/// We assume that ~10% of the block weight is consumed by `on_initialize` handlers.
103/// This is used to limit the maximal weight of a single extrinsic.
104const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
105/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used
106/// by  Operational  extrinsics.
107const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
108/// We allow for .5 seconds of compute with a 12 second average block time.
109const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(
110	WEIGHT_REF_TIME_PER_SECOND.saturating_div(2),
111	cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64,
112);
113
114parameter_types! {
115	pub const BlockHashCount: BlockNumber = 250;
116	pub const Version: RuntimeVersion = VERSION;
117	pub RuntimeBlockLength: BlockLength =
118		BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
119	pub RuntimeBlockWeights: BlockWeights = BlockWeights::builder()
120		.base_block(BlockExecutionWeight::get())
121		.for_class(DispatchClass::all(), |weights| {
122			weights.base_extrinsic = ExtrinsicBaseWeight::get();
123		})
124		.for_class(DispatchClass::Normal, |weights| {
125			weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
126		})
127		.for_class(DispatchClass::Operational, |weights| {
128			weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
129			// Operational transactions have some extra reserved space, so that they
130			// are included even if block reached `MAXIMUM_BLOCK_WEIGHT`.
131			weights.reserved = Some(
132				MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT
133			);
134		})
135		.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
136		.build_or_panic();
137	pub const SS58Prefix: u8 = 42;
138}
139
140#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
141impl frame_system::Config for Runtime {
142	/// The identifier used to distinguish between accounts.
143	type AccountId = AccountId;
144	/// The aggregated dispatch type that is available for extrinsics.
145	type RuntimeCall = RuntimeCall;
146	/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
147	type Lookup = AccountIdLookup<AccountId, ()>;
148	/// The index type for storing how many extrinsics an account has signed.
149	type Nonce = Nonce;
150	/// The type for hashing blocks and tries.
151	type Hash = Hash;
152	/// The hashing algorithm used.
153	type Hashing = BlakeTwo256;
154	/// The block type.
155	type Block = Block;
156	/// The ubiquitous event type.
157	type RuntimeEvent = RuntimeEvent;
158	/// The ubiquitous origin type.
159	type RuntimeOrigin = RuntimeOrigin;
160	/// Maximum number of block number to block hash mappings to keep (oldest pruned first).
161	type BlockHashCount = BlockHashCount;
162	/// Runtime version.
163	type Version = Version;
164	/// Converts a module to an index of this module in the runtime.
165	type PalletInfo = PalletInfo;
166	type AccountData = pallet_balances::AccountData<u128>;
167	type OnNewAccount = ();
168	type OnKilledAccount = ();
169	type DbWeight = ();
170	type BaseCallFilter = frame_support::traits::Everything;
171	type SystemWeightInfo = ();
172	type BlockWeights = RuntimeBlockWeights;
173	type BlockLength = RuntimeBlockLength;
174	type SS58Prefix = SS58Prefix;
175	type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
176	type MaxConsumers = frame_support::traits::ConstU32<16>;
177}
178
179impl pallet_sudo::Config for Runtime {
180	type RuntimeCall = RuntimeCall;
181	type RuntimeEvent = RuntimeEvent;
182	type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
183}
184
185impl cumulus_pallet_solo_to_para::Config for Runtime {
186	type RuntimeEvent = RuntimeEvent;
187}
188
189impl cumulus_pallet_parachain_system::Config for Runtime {
190	type WeightInfo = ();
191	type RuntimeEvent = RuntimeEvent;
192	type OnSystemEvent = cumulus_pallet_solo_to_para::Pallet<Runtime>;
193	type SelfParaId = parachain_info::Pallet<Runtime>;
194	type OutboundXcmpMessageSource = ();
195	// Ignore all DMP messages by enqueueing them into `()`:
196	type DmpQueue = frame_support::traits::EnqueueWithOrigin<(), sp_core::ConstU8<0>>;
197	type ReservedDmpWeight = ();
198	type XcmpMessageHandler = ();
199	type ReservedXcmpWeight = ();
200	type CheckAssociatedRelayNumber = RelayNumberMonotonicallyIncreases;
201	type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
202		Runtime,
203		RELAY_CHAIN_SLOT_DURATION_MILLIS,
204		BLOCK_PROCESSING_VELOCITY,
205		UNINCLUDED_SEGMENT_CAPACITY,
206	>;
207}
208
209impl parachain_info::Config for Runtime {}
210
211impl cumulus_pallet_aura_ext::Config for Runtime {}
212
213impl pallet_aura::Config for Runtime {
214	type AuthorityId = AuraId;
215	type DisabledValidators = ();
216	type MaxAuthorities = ConstU32<100_000>;
217	type AllowMultipleBlocksPerSlot = ConstBool<false>;
218	type SlotDuration = pallet_aura::MinimumPeriodTimesTwo<Self>;
219}
220
221impl pallet_timestamp::Config for Runtime {
222	type Moment = u64;
223	type OnTimestampSet = Aura;
224	type MinimumPeriod = ConstU64<0>;
225	type WeightInfo = ();
226}
227
228construct_runtime! {
229	pub enum Runtime
230	{
231		System: frame_system,
232		Sudo: pallet_sudo,
233		Timestamp: pallet_timestamp,
234
235		ParachainSystem: cumulus_pallet_parachain_system,
236		ParachainInfo: parachain_info,
237		SoloToPara: cumulus_pallet_solo_to_para,
238		Aura: pallet_aura,
239		AuraExt: cumulus_pallet_aura_ext,
240	}
241}
242
243/// Index of a transaction in the chain.
244pub type Nonce = u32;
245/// A hash of some data used by the chain.
246pub type Hash = sp_core::H256;
247/// An index to a block.
248pub type BlockNumber = u32;
249/// The address format for describing accounts.
250pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
251/// Block header type as expected by this runtime.
252pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
253/// Block type as expected by this runtime.
254pub type Block = generic::Block<Header, UncheckedExtrinsic>;
255/// A Block signed with a Justification
256pub type SignedBlock = generic::SignedBlock<Block>;
257/// BlockId type as expected by this runtime.
258pub type BlockId = generic::BlockId<Block>;
259/// The SignedExtension to the basic transaction logic.
260pub type SignedExtra = (
261	frame_system::CheckSpecVersion<Runtime>,
262	frame_system::CheckTxVersion<Runtime>,
263	frame_system::CheckGenesis<Runtime>,
264	frame_system::CheckEra<Runtime>,
265	frame_system::CheckNonce<Runtime>,
266	pallet_sudo::CheckOnlySudoAccount<Runtime>,
267);
268/// Unchecked extrinsic type as expected by this runtime.
269pub type UncheckedExtrinsic =
270	generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
271
272/// Executive: handles dispatch to the various modules.
273pub type Executive = frame_executive::Executive<
274	Runtime,
275	Block,
276	frame_system::ChainContext<Runtime>,
277	Runtime,
278	AllPalletsWithSystem,
279>;
280
281impl_runtime_apis! {
282	impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {
283		fn slot_duration() -> sp_consensus_aura::SlotDuration {
284			sp_consensus_aura::SlotDuration::from_millis(Aura::slot_duration())
285		}
286
287		fn authorities() -> Vec<AuraId> {
288			pallet_aura::Authorities::<Runtime>::get().into_inner()
289		}
290	}
291
292	impl sp_api::Core<Block> for Runtime {
293		fn version() -> RuntimeVersion {
294			VERSION
295		}
296
297		fn execute_block(block: Block) {
298			Executive::execute_block(block)
299		}
300
301		fn initialize_block(header: &<Block as BlockT>::Header) -> sp_runtime::ExtrinsicInclusionMode {
302			Executive::initialize_block(header)
303		}
304	}
305
306	impl sp_api::Metadata<Block> for Runtime {
307		fn metadata() -> OpaqueMetadata {
308			OpaqueMetadata::new(Runtime::metadata().into())
309		}
310
311		fn metadata_at_version(version: u32) -> Option<OpaqueMetadata> {
312			Runtime::metadata_at_version(version)
313		}
314
315		fn metadata_versions() -> alloc::vec::Vec<u32> {
316			Runtime::metadata_versions()
317		}
318	}
319
320	impl sp_block_builder::BlockBuilder<Block> for Runtime {
321		fn apply_extrinsic(
322			extrinsic: <Block as BlockT>::Extrinsic,
323		) -> ApplyExtrinsicResult {
324			Executive::apply_extrinsic(extrinsic)
325		}
326
327		fn finalize_block() -> <Block as BlockT>::Header {
328			Executive::finalize_block()
329		}
330
331		fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
332			data.create_extrinsics()
333		}
334
335		fn check_inherents(block: Block, data: sp_inherents::InherentData) -> sp_inherents::CheckInherentsResult {
336			data.check_extrinsics(&block)
337		}
338	}
339
340	impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {
341		fn validate_transaction(
342			source: TransactionSource,
343			tx: <Block as BlockT>::Extrinsic,
344			block_hash: <Block as BlockT>::Hash,
345		) -> TransactionValidity {
346			Executive::validate_transaction(source, tx, block_hash)
347		}
348	}
349
350	impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
351		fn offchain_worker(header: &<Block as BlockT>::Header) {
352			Executive::offchain_worker(header)
353		}
354	}
355
356	impl sp_session::SessionKeys<Block> for Runtime {
357		fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
358			SessionKeys::generate(seed)
359		}
360
361		fn decode_session_keys(
362			encoded: Vec<u8>,
363		) -> Option<Vec<(Vec<u8>, KeyTypeId)>> {
364			SessionKeys::decode_into_raw_public_keys(&encoded)
365		}
366	}
367
368	impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {
369		fn collect_collation_info(header: &<Block as BlockT>::Header) -> cumulus_primitives_core::CollationInfo {
370			ParachainSystem::collect_collation_info(header)
371		}
372	}
373
374	impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
375		fn build_state(config: Vec<u8>) -> sp_genesis_builder::Result {
376			build_state::<RuntimeGenesisConfig>(config)
377		}
378
379		fn get_preset(id: &Option<sp_genesis_builder::PresetId>) -> Option<Vec<u8>> {
380			get_preset::<RuntimeGenesisConfig>(id, |_| None)
381		}
382
383		fn preset_names() -> Vec<sp_genesis_builder::PresetId> {
384			vec![]
385		}
386	}
387}
388
389cumulus_pallet_parachain_system::register_validate_block! {
390	Runtime = Runtime,
391	BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
392}