1
// This file is part of Substrate.
2

            
3
// Copyright (C) Parity Technologies (UK) Ltd.
4
// SPDX-License-Identifier: Apache-2.0
5

            
6
// Licensed under the Apache License, Version 2.0 (the "License");
7
// you may not use this file except in compliance with the License.
8
// You may obtain a copy of the License at
9
//
10
// 	http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software
13
// distributed under the License is distributed on an "AS IS" BASIS,
14
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
// See the License for the specific language governing permissions and
16
// limitations under the License.
17

            
18
#![cfg_attr(not(feature = "std"), no_std)]
19

            
20
//! # Executive Module
21
//!
22
//! The Executive module acts as the orchestration layer for the runtime. It dispatches incoming
23
//! extrinsic calls to the respective modules in the runtime.
24
//!
25
//! ## Overview
26
//!
27
//! The executive module is not a typical pallet providing functionality around a specific feature.
28
//! It is a cross-cutting framework component for the FRAME. It works in conjunction with the
29
//! [FRAME System module](../frame_system/index.html) to perform these cross-cutting functions.
30
//!
31
//! The Executive module provides functions to:
32
//!
33
//! - Check transaction validity.
34
//! - Initialize a block.
35
//! - Apply extrinsics.
36
//! - Execute a block.
37
//! - Finalize a block.
38
//! - Start an off-chain worker.
39
//!
40
//! The flow of their application in a block is explained in the [block flowchart](block_flowchart).
41
//!
42
//! ### Implementations
43
//!
44
//! The Executive module provides the following implementations:
45
//!
46
//! - `ExecuteBlock`: Trait that can be used to execute a block.
47
//! - `Executive`: Type that can be used to make the FRAME available from the runtime.
48
//!
49
//! ## Usage
50
//!
51
//! The default Substrate node template declares the [`Executive`](./struct.Executive.html) type in
52
//! its library.
53
//!
54
//! ### Example
55
//!
56
//! `Executive` type declaration from the node template.
57
//!
58
//! ```
59
//! # use sp_runtime::generic;
60
//! # use frame_executive as executive;
61
//! # pub struct UncheckedExtrinsic {};
62
//! # pub struct Header {};
63
//! # type Context = frame_system::ChainContext<Runtime>;
64
//! # pub type Block = generic::Block<Header, UncheckedExtrinsic>;
65
//! # pub type Balances = u64;
66
//! # pub type AllPalletsWithSystem = u64;
67
//! # pub enum Runtime {};
68
//! # use sp_runtime::transaction_validity::{
69
//! #    TransactionValidity, UnknownTransaction, TransactionSource,
70
//! # };
71
//! # use sp_runtime::traits::ValidateUnsigned;
72
//! # impl ValidateUnsigned for Runtime {
73
//! #     type Call = ();
74
//! #
75
//! #     fn validate_unsigned(_source: TransactionSource, _call: &Self::Call) -> TransactionValidity {
76
//! #         UnknownTransaction::NoUnsignedValidator.into()
77
//! #     }
78
//! # }
79
//! /// Executive: handles dispatch to the various modules.
80
//! pub type Executive = executive::Executive<Runtime, Block, Context, Runtime, AllPalletsWithSystem>;
81
//! ```
82
//!
83
//! ### Custom `OnRuntimeUpgrade` logic
84
//!
85
//! You can add custom logic that should be called in your runtime on a runtime upgrade. This is
86
//! done by setting an optional generic parameter. The custom logic will be called before
87
//! the on runtime upgrade logic of all modules is called.
88
//!
89
//! ```
90
//! # use sp_runtime::generic;
91
//! # use frame_executive as executive;
92
//! # pub struct UncheckedExtrinsic {};
93
//! # pub struct Header {};
94
//! # type Context = frame_system::ChainContext<Runtime>;
95
//! # pub type Block = generic::Block<Header, UncheckedExtrinsic>;
96
//! # pub type Balances = u64;
97
//! # pub type AllPalletsWithSystem = u64;
98
//! # pub enum Runtime {};
99
//! # use sp_runtime::transaction_validity::{
100
//! #    TransactionValidity, UnknownTransaction, TransactionSource,
101
//! # };
102
//! # use sp_runtime::traits::ValidateUnsigned;
103
//! # impl ValidateUnsigned for Runtime {
104
//! #     type Call = ();
105
//! #
106
//! #     fn validate_unsigned(_source: TransactionSource, _call: &Self::Call) -> TransactionValidity {
107
//! #         UnknownTransaction::NoUnsignedValidator.into()
108
//! #     }
109
//! # }
110
//! struct CustomOnRuntimeUpgrade;
111
//! impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
112
//!     fn on_runtime_upgrade() -> frame_support::weights::Weight {
113
//!         // Do whatever you want.
114
//!         frame_support::weights::Weight::zero()
115
//!     }
116
//! }
117
//!
118
//! pub type Executive = executive::Executive<Runtime, Block, Context, Runtime, AllPalletsWithSystem, CustomOnRuntimeUpgrade>;
119
//! ```
120

            
121
#[cfg(doc)]
122
#[cfg_attr(doc, aquamarine::aquamarine)]
123
/// # Block Execution
124
///
125
/// These are the steps of block execution as done by [`Executive::execute_block`]. A block is
126
/// invalid if any of them fail.
127
///
128
/// ```mermaid
129
/// flowchart TD
130
///     Executive::execute_block --> on_runtime_upgrade
131
///     on_runtime_upgrade --> System::initialize
132
///     Executive::initialize_block --> System::initialize
133
///     System::initialize --> on_initialize
134
///     on_initialize --> PreInherents[System::PreInherents]
135
///     PreInherents --> Inherents[Apply Inherents]
136
///     Inherents --> PostInherents[System::PostInherents]
137
///     PostInherents --> Check{MBM ongoing?}
138
///     Check -->|No| poll
139
///     Check -->|Yes| post_transactions_2[System::PostTransaction]
140
///     post_transactions_2 --> Step[MBMs::step]
141
///     Step --> on_finalize
142
///     poll --> transactions[Apply Transactions]
143
///     transactions --> post_transactions_1[System::PostTransaction]
144
///     post_transactions_1 --> CheckIdle{Weight remaining?}
145
///     CheckIdle -->|Yes| on_idle
146
///     CheckIdle -->|No| on_finalize
147
///     on_idle --> on_finalize
148
/// ```
149
pub mod block_flowchart {}
150

            
151
#[cfg(test)]
152
mod tests;
153

            
154
extern crate alloc;
155

            
156
use codec::{Codec, Encode};
157
use core::marker::PhantomData;
158
use frame_support::{
159
	defensive_assert,
160
	dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, PostDispatchInfo},
161
	migrations::MultiStepMigrator,
162
	pallet_prelude::InvalidTransaction,
163
	traits::{
164
		BeforeAllRuntimeMigrations, EnsureInherentsAreFirst, ExecuteBlock, OffchainWorker,
165
		OnFinalize, OnIdle, OnInitialize, OnPoll, OnRuntimeUpgrade, PostInherents,
166
		PostTransactions, PreInherents,
167
	},
168
	weights::{Weight, WeightMeter},
169
};
170
use frame_system::pallet_prelude::BlockNumberFor;
171
use sp_runtime::{
172
	generic::Digest,
173
	traits::{
174
		self, Applyable, CheckEqual, Checkable, Dispatchable, Header, NumberFor, One,
175
		ValidateUnsigned, Zero,
176
	},
177
	transaction_validity::{TransactionSource, TransactionValidity},
178
	ApplyExtrinsicResult, ExtrinsicInclusionMode,
179
};
180

            
181
#[cfg(feature = "try-runtime")]
182
use ::{
183
	frame_support::{
184
		traits::{TryDecodeEntireStorage, TryDecodeEntireStorageError, TryState},
185
		StorageNoopGuard,
186
	},
187
	frame_try_runtime::{TryStateSelect, UpgradeCheckSelect},
188
	log,
189
	sp_runtime::TryRuntimeError,
190
};
191

            
192
#[allow(dead_code)]
193
const LOG_TARGET: &str = "runtime::executive";
194

            
195
pub type CheckedOf<E, C> = <E as Checkable<C>>::Checked;
196
pub type CallOf<E, C> = <CheckedOf<E, C> as Applyable>::Call;
197
pub type OriginOf<E, C> = <CallOf<E, C> as Dispatchable>::RuntimeOrigin;
198

            
199
/// Main entry point for certain runtime actions as e.g. `execute_block`.
200
///
201
/// Generic parameters:
202
/// - `System`: Something that implements `frame_system::Config`
203
/// - `Block`: The block type of the runtime
204
/// - `Context`: The context that is used when checking an extrinsic.
205
/// - `UnsignedValidator`: The unsigned transaction validator of the runtime.
206
/// - `AllPalletsWithSystem`: Tuple that contains all pallets including frame system pallet. Will be
207
///   used to call hooks e.g. `on_initialize`.
208
/// - `OnRuntimeUpgrade`: Custom logic that should be called after a runtime upgrade. Modules are
209
///   already called by `AllPalletsWithSystem`. It will be called before all modules will be called.
210
pub struct Executive<
211
	System,
212
	Block,
213
	Context,
214
	UnsignedValidator,
215
	AllPalletsWithSystem,
216
	OnRuntimeUpgrade = (),
217
>(
218
	PhantomData<(
219
		System,
220
		Block,
221
		Context,
222
		UnsignedValidator,
223
		AllPalletsWithSystem,
224
		OnRuntimeUpgrade,
225
	)>,
226
);
227

            
228
impl<
229
		System: frame_system::Config + EnsureInherentsAreFirst<Block>,
230
		Block: traits::Block<
231
			Header = frame_system::pallet_prelude::HeaderFor<System>,
232
			Hash = System::Hash,
233
		>,
234
		Context: Default,
235
		UnsignedValidator,
236
		AllPalletsWithSystem: OnRuntimeUpgrade
237
			+ BeforeAllRuntimeMigrations
238
			+ OnInitialize<BlockNumberFor<System>>
239
			+ OnIdle<BlockNumberFor<System>>
240
			+ OnFinalize<BlockNumberFor<System>>
241
			+ OffchainWorker<BlockNumberFor<System>>
242
			+ OnPoll<BlockNumberFor<System>>,
243
		COnRuntimeUpgrade: OnRuntimeUpgrade,
244
	> ExecuteBlock<Block>
245
	for Executive<System, Block, Context, UnsignedValidator, AllPalletsWithSystem, COnRuntimeUpgrade>
246
where
247
	Block::Extrinsic: Checkable<Context> + Codec,
248
	CheckedOf<Block::Extrinsic, Context>: Applyable + GetDispatchInfo,
249
	CallOf<Block::Extrinsic, Context>:
250
		Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
251
	OriginOf<Block::Extrinsic, Context>: From<Option<System::AccountId>>,
252
	UnsignedValidator: ValidateUnsigned<Call = CallOf<Block::Extrinsic, Context>>,
253
{
254
	fn execute_block(block: Block) {
255
		Executive::<
256
			System,
257
			Block,
258
			Context,
259
			UnsignedValidator,
260
			AllPalletsWithSystem,
261
			COnRuntimeUpgrade,
262
		>::execute_block(block);
263
	}
264
}
265

            
266
#[cfg(feature = "try-runtime")]
267
impl<
268
		System: frame_system::Config + EnsureInherentsAreFirst<Block>,
269
		Block: traits::Block<
270
			Header = frame_system::pallet_prelude::HeaderFor<System>,
271
			Hash = System::Hash,
272
		>,
273
		Context: Default,
274
		UnsignedValidator,
275
		AllPalletsWithSystem: OnRuntimeUpgrade
276
			+ BeforeAllRuntimeMigrations
277
			+ OnInitialize<BlockNumberFor<System>>
278
			+ OnIdle<BlockNumberFor<System>>
279
			+ OnFinalize<BlockNumberFor<System>>
280
			+ OffchainWorker<BlockNumberFor<System>>
281
			+ OnPoll<BlockNumberFor<System>>
282
			+ TryState<BlockNumberFor<System>>
283
			+ TryDecodeEntireStorage,
284
		COnRuntimeUpgrade: OnRuntimeUpgrade,
285
	> Executive<System, Block, Context, UnsignedValidator, AllPalletsWithSystem, COnRuntimeUpgrade>
286
where
287
	Block::Extrinsic: Checkable<Context> + Codec,
288
	CheckedOf<Block::Extrinsic, Context>: Applyable + GetDispatchInfo,
289
	CallOf<Block::Extrinsic, Context>:
290
		Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
291
	OriginOf<Block::Extrinsic, Context>: From<Option<System::AccountId>>,
292
	UnsignedValidator: ValidateUnsigned<Call = CallOf<Block::Extrinsic, Context>>,
293
{
294
	/// Execute given block, but don't as strict is the normal block execution.
295
	///
296
	/// Some checks can be disabled via:
297
	///
298
	/// - `state_root_check`
299
	/// - `signature_check`
300
	///
301
	/// Should only be used for testing ONLY.
302
	pub fn try_execute_block(
303
		block: Block,
304
		state_root_check: bool,
305
		signature_check: bool,
306
		select: frame_try_runtime::TryStateSelect,
307
	) -> Result<Weight, &'static str> {
308
		log::info!(
309
			target: LOG_TARGET,
310
			"try-runtime: executing block #{:?} / state root check: {:?} / signature check: {:?} / try-state-select: {:?}",
311
			block.header().number(),
312
			state_root_check,
313
			signature_check,
314
			select,
315
		);
316

            
317
		let mode = Self::initialize_block(block.header());
318
		let num_inherents = Self::initial_checks(&block) as usize;
319
		let (header, extrinsics) = block.deconstruct();
320

            
321
		// Check if there are any forbidden non-inherents in the block.
322
		if mode == ExtrinsicInclusionMode::OnlyInherents && extrinsics.len() > num_inherents {
323
			return Err("Only inherents allowed".into())
324
		}
325

            
326
		let try_apply_extrinsic = |uxt: Block::Extrinsic| -> ApplyExtrinsicResult {
327
			sp_io::init_tracing();
328
			let encoded = uxt.encode();
329
			let encoded_len = encoded.len();
330

            
331
			let is_inherent = System::is_inherent(&uxt);
332
			// skip signature verification.
333
			let xt = if signature_check {
334
				uxt.check(&Default::default())
335
			} else {
336
				uxt.unchecked_into_checked_i_know_what_i_am_doing(&Default::default())
337
			}?;
338

            
339
			let dispatch_info = xt.get_dispatch_info();
340
			if !is_inherent && !<frame_system::Pallet<System>>::inherents_applied() {
341
				Self::inherents_applied();
342
			}
343

            
344
			<frame_system::Pallet<System>>::note_extrinsic(encoded);
345
			let r = Applyable::apply::<UnsignedValidator>(xt, &dispatch_info, encoded_len)?;
346

            
347
			if r.is_err() && dispatch_info.class == DispatchClass::Mandatory {
348
				return Err(InvalidTransaction::BadMandatory.into())
349
			}
350

            
351
			<frame_system::Pallet<System>>::note_applied_extrinsic(&r, dispatch_info);
352

            
353
			Ok(r.map(|_| ()).map_err(|e| e.error))
354
		};
355

            
356
		// Apply extrinsics:
357
		for e in extrinsics.iter() {
358
			if let Err(err) = try_apply_extrinsic(e.clone()) {
359
				log::error!(
360
					target: LOG_TARGET, "transaction {:?} failed due to {:?}. Aborting the rest of the block execution.",
361
					e,
362
					err,
363
				);
364
				break
365
			}
366
		}
367

            
368
		// In this case there were no transactions to trigger this state transition:
369
		if !<frame_system::Pallet<System>>::inherents_applied() {
370
			Self::inherents_applied();
371
		}
372

            
373
		// post-extrinsics book-keeping
374
		<frame_system::Pallet<System>>::note_finished_extrinsics();
375
		<System as frame_system::Config>::PostTransactions::post_transactions();
376

            
377
		Self::on_idle_hook(*header.number());
378
		Self::on_finalize_hook(*header.number());
379

            
380
		// run the try-state checks of all pallets, ensuring they don't alter any state.
381
		let _guard = frame_support::StorageNoopGuard::default();
382
		<AllPalletsWithSystem as frame_support::traits::TryState<
383
			BlockNumberFor<System>,
384
		>>::try_state(*header.number(), select.clone())
385
		.map_err(|e| {
386
			log::error!(target: LOG_TARGET, "failure: {:?}", e);
387
			e
388
		})?;
389
		if select.any() {
390
			let res = AllPalletsWithSystem::try_decode_entire_state();
391
			Self::log_decode_result(res)?;
392
		}
393
		drop(_guard);
394

            
395
		// do some of the checks that would normally happen in `final_checks`, but perhaps skip
396
		// the state root check.
397
		{
398
			let new_header = <frame_system::Pallet<System>>::finalize();
399
			let items_zip = header.digest().logs().iter().zip(new_header.digest().logs().iter());
400
			for (header_item, computed_item) in items_zip {
401
				header_item.check_equal(computed_item);
402
				assert!(header_item == computed_item, "Digest item must match that calculated.");
403
			}
404

            
405
			if state_root_check {
406
				let storage_root = new_header.state_root();
407
				header.state_root().check_equal(storage_root);
408
				assert!(
409
					header.state_root() == storage_root,
410
					"Storage root must match that calculated."
411
				);
412
			}
413

            
414
			assert!(
415
				header.extrinsics_root() == new_header.extrinsics_root(),
416
				"Transaction trie root must be valid.",
417
			);
418
		}
419

            
420
		log::info!(
421
			target: LOG_TARGET,
422
			"try-runtime: Block #{:?} successfully executed",
423
			header.number(),
424
		);
425

            
426
		Ok(frame_system::Pallet::<System>::block_weight().total())
427
	}
428

            
429
	/// Execute all Migrations of this runtime.
430
	///
431
	/// The `checks` param determines whether to execute `pre/post_upgrade` and `try_state` hooks.
432
	///
433
	/// [`frame_system::LastRuntimeUpgrade`] is set to the current runtime version after
434
	/// migrations execute. This is important for idempotency checks, because some migrations use
435
	/// this value to determine whether or not they should execute.
436
	pub fn try_runtime_upgrade(checks: UpgradeCheckSelect) -> Result<Weight, TryRuntimeError> {
437
		let before_all_weight =
438
			<AllPalletsWithSystem as BeforeAllRuntimeMigrations>::before_all_runtime_migrations();
439
		let try_on_runtime_upgrade_weight =
440
			<(COnRuntimeUpgrade, AllPalletsWithSystem) as OnRuntimeUpgrade>::try_on_runtime_upgrade(
441
				checks.pre_and_post(),
442
			)?;
443

            
444
		frame_system::LastRuntimeUpgrade::<System>::put(
445
			frame_system::LastRuntimeUpgradeInfo::from(
446
				<System::Version as frame_support::traits::Get<_>>::get(),
447
			),
448
		);
449

            
450
		// Nothing should modify the state after the migrations ran:
451
		let _guard = StorageNoopGuard::default();
452

            
453
		// The state must be decodable:
454
		if checks.any() {
455
			let res = AllPalletsWithSystem::try_decode_entire_state();
456
			Self::log_decode_result(res)?;
457
		}
458

            
459
		// Check all storage invariants:
460
		if checks.try_state() {
461
			AllPalletsWithSystem::try_state(
462
				frame_system::Pallet::<System>::block_number(),
463
				TryStateSelect::All,
464
			)?;
465
		}
466

            
467
		Ok(before_all_weight.saturating_add(try_on_runtime_upgrade_weight))
468
	}
469

            
470
	/// Logs the result of trying to decode the entire state.
471
	fn log_decode_result(
472
		res: Result<usize, alloc::vec::Vec<TryDecodeEntireStorageError>>,
473
	) -> Result<(), TryRuntimeError> {
474
		match res {
475
			Ok(bytes) => {
476
				log::info!(
477
					target: LOG_TARGET,
478
					"✅ Entire runtime state decodes without error. {} bytes total.",
479
					bytes
480
				);
481

            
482
				Ok(())
483
			},
484
			Err(errors) => {
485
				log::error!(
486
					target: LOG_TARGET,
487
					"`try_decode_entire_state` failed with {} errors",
488
					errors.len(),
489
				);
490

            
491
				for (i, err) in errors.iter().enumerate() {
492
					// We log the short version to `error` and then the full debug info to `debug`:
493
					log::error!(target: LOG_TARGET, "- {i}. error: {err}");
494
					log::debug!(target: LOG_TARGET, "- {i}. error: {err:?}");
495
				}
496

            
497
				Err("`try_decode_entire_state` failed".into())
498
			},
499
		}
500
	}
501
}
502

            
503
impl<
504
		System: frame_system::Config + EnsureInherentsAreFirst<Block>,
505
		Block: traits::Block<
506
			Header = frame_system::pallet_prelude::HeaderFor<System>,
507
			Hash = System::Hash,
508
		>,
509
		Context: Default,
510
		UnsignedValidator,
511
		AllPalletsWithSystem: OnRuntimeUpgrade
512
			+ BeforeAllRuntimeMigrations
513
			+ OnInitialize<BlockNumberFor<System>>
514
			+ OnIdle<BlockNumberFor<System>>
515
			+ OnFinalize<BlockNumberFor<System>>
516
			+ OffchainWorker<BlockNumberFor<System>>
517
			+ OnPoll<BlockNumberFor<System>>,
518
		COnRuntimeUpgrade: OnRuntimeUpgrade,
519
	> Executive<System, Block, Context, UnsignedValidator, AllPalletsWithSystem, COnRuntimeUpgrade>
520
where
521
	Block::Extrinsic: Checkable<Context> + Codec,
522
	CheckedOf<Block::Extrinsic, Context>: Applyable + GetDispatchInfo,
523
	CallOf<Block::Extrinsic, Context>:
524
		Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
525
	OriginOf<Block::Extrinsic, Context>: From<Option<System::AccountId>>,
526
	UnsignedValidator: ValidateUnsigned<Call = CallOf<Block::Extrinsic, Context>>,
527
{
528
	/// Execute all `OnRuntimeUpgrade` of this runtime, and return the aggregate weight.
529
	pub fn execute_on_runtime_upgrade() -> Weight {
530
		let before_all_weight =
531
			<AllPalletsWithSystem as BeforeAllRuntimeMigrations>::before_all_runtime_migrations();
532

            
533
		let runtime_upgrade_weight = <(
534
			COnRuntimeUpgrade,
535
			<System as frame_system::Config>::SingleBlockMigrations,
536
			// We want to run the migrations before we call into the pallets as they may
537
			// access any state that would then not be migrated.
538
			AllPalletsWithSystem,
539
		) as OnRuntimeUpgrade>::on_runtime_upgrade();
540

            
541
		before_all_weight.saturating_add(runtime_upgrade_weight)
542
	}
543

            
544
	/// Start the execution of a particular block.
545
194763
	pub fn initialize_block(
546
194763
		header: &frame_system::pallet_prelude::HeaderFor<System>,
547
194763
	) -> ExtrinsicInclusionMode {
548
194763
		sp_io::init_tracing();
549
194763
		sp_tracing::enter_span!(sp_tracing::Level::TRACE, "init_block");
550
194763
		let digests = Self::extract_pre_digest(header);
551
194763
		Self::initialize_block_impl(header.number(), header.parent_hash(), &digests);
552
194763

            
553
194763
		Self::extrinsic_mode()
554
194763
	}
555

            
556
194763
	fn extrinsic_mode() -> ExtrinsicInclusionMode {
557
194763
		if <System as frame_system::Config>::MultiBlockMigrator::ongoing() {
558
			ExtrinsicInclusionMode::OnlyInherents
559
		} else {
560
194763
			ExtrinsicInclusionMode::AllExtrinsics
561
		}
562
194763
	}
563

            
564
194763
	fn extract_pre_digest(header: &frame_system::pallet_prelude::HeaderFor<System>) -> Digest {
565
194763
		let mut digest = <Digest>::default();
566
194763
		header.digest().logs().iter().for_each(|d| {
567
194763
			if d.as_pre_runtime().is_some() {
568
194763
				digest.push(d.clone())
569
			}
570
194763
		});
571
194763
		digest
572
194763
	}
573

            
574
194763
	fn initialize_block_impl(
575
194763
		block_number: &BlockNumberFor<System>,
576
194763
		parent_hash: &System::Hash,
577
194763
		digest: &Digest,
578
194763
	) {
579
194763
		// Reset events before apply runtime upgrade hook.
580
194763
		// This is required to preserve events from runtime upgrade hook.
581
194763
		// This means the format of all the event related storages must always be compatible.
582
194763
		<frame_system::Pallet<System>>::reset_events();
583
194763

            
584
194763
		let mut weight = Weight::zero();
585
194763
		if Self::runtime_upgraded() {
586
			weight = weight.saturating_add(Self::execute_on_runtime_upgrade());
587

            
588
			frame_system::LastRuntimeUpgrade::<System>::put(
589
				frame_system::LastRuntimeUpgradeInfo::from(
590
					<System::Version as frame_support::traits::Get<_>>::get(),
591
				),
592
			);
593
194763
		}
594
194763
		<frame_system::Pallet<System>>::initialize(block_number, parent_hash, digest);
595
194763
		weight = weight.saturating_add(<AllPalletsWithSystem as OnInitialize<
596
194763
			BlockNumberFor<System>,
597
194763
		>>::on_initialize(*block_number));
598
194763
		weight = weight.saturating_add(
599
194763
			<System::BlockWeights as frame_support::traits::Get<_>>::get().base_block,
600
194763
		);
601
194763
		<frame_system::Pallet<System>>::register_extra_weight_unchecked(
602
194763
			weight,
603
194763
			DispatchClass::Mandatory,
604
194763
		);
605
194763

            
606
194763
		frame_system::Pallet::<System>::note_finished_initialize();
607
194763
		<System as frame_system::Config>::PreInherents::pre_inherents();
608
194763
	}
609

            
610
	/// Returns if the runtime has been upgraded, based on [`frame_system::LastRuntimeUpgrade`].
611
194763
	fn runtime_upgraded() -> bool {
612
194763
		let last = frame_system::LastRuntimeUpgrade::<System>::get();
613
194763
		let current = <System::Version as frame_support::traits::Get<_>>::get();
614
194763

            
615
194763
		last.map(|v| v.was_upgraded(&current)).unwrap_or(true)
616
194763
	}
617

            
618
	/// Returns the number of inherents in the block.
619
	fn initial_checks(block: &Block) -> u32 {
620
		sp_tracing::enter_span!(sp_tracing::Level::TRACE, "initial_checks");
621
		let header = block.header();
622

            
623
		// Check that `parent_hash` is correct.
624
		let n = *header.number();
625
		assert!(
626
			n > BlockNumberFor::<System>::zero() &&
627
				<frame_system::Pallet<System>>::block_hash(n - BlockNumberFor::<System>::one()) ==
628
					*header.parent_hash(),
629
			"Parent hash should be valid.",
630
		);
631

            
632
		match System::ensure_inherents_are_first(block) {
633
			Ok(num) => num,
634
			Err(i) => panic!("Invalid inherent position for extrinsic at index {}", i),
635
		}
636
	}
637

            
638
	/// Actually execute all transitions for `block`.
639
	pub fn execute_block(block: Block) {
640
		sp_io::init_tracing();
641
		sp_tracing::within_span! {
642
			sp_tracing::info_span!("execute_block", ?block);
643
			// Execute `on_runtime_upgrade` and `on_initialize`.
644
			let mode = Self::initialize_block(block.header());
645
			let num_inherents = Self::initial_checks(&block) as usize;
646
			let (header, extrinsics) = block.deconstruct();
647
			let num_extrinsics = extrinsics.len();
648

            
649
			if mode == ExtrinsicInclusionMode::OnlyInherents && num_extrinsics > num_inherents {
650
				// Invalid block
651
				panic!("Only inherents are allowed in this block")
652
			}
653

            
654
			Self::apply_extrinsics(extrinsics.into_iter());
655

            
656
			// In this case there were no transactions to trigger this state transition:
657
			if !<frame_system::Pallet<System>>::inherents_applied() {
658
				defensive_assert!(num_inherents == num_extrinsics);
659
				Self::inherents_applied();
660
			}
661

            
662
			<frame_system::Pallet<System>>::note_finished_extrinsics();
663
			<System as frame_system::Config>::PostTransactions::post_transactions();
664

            
665
			Self::on_idle_hook(*header.number());
666
			Self::on_finalize_hook(*header.number());
667
			Self::final_checks(&header);
668
		}
669
	}
670

            
671
	/// Logic that runs directly after inherent application.
672
	///
673
	/// It advances the Multi-Block-Migrations or runs the `on_poll` hook.
674
194763
	pub fn inherents_applied() {
675
194763
		<frame_system::Pallet<System>>::note_inherents_applied();
676
194763
		<System as frame_system::Config>::PostInherents::post_inherents();
677
194763

            
678
194763
		if <System as frame_system::Config>::MultiBlockMigrator::ongoing() {
679
			let used_weight = <System as frame_system::Config>::MultiBlockMigrator::step();
680
			<frame_system::Pallet<System>>::register_extra_weight_unchecked(
681
				used_weight,
682
				DispatchClass::Mandatory,
683
			);
684
194763
		} else {
685
194763
			let block_number = <frame_system::Pallet<System>>::block_number();
686
194763
			Self::on_poll_hook(block_number);
687
194763
		}
688
194763
	}
689

            
690
	/// Execute given extrinsics.
691
	fn apply_extrinsics(extrinsics: impl Iterator<Item = Block::Extrinsic>) {
692
		extrinsics.into_iter().for_each(|e| {
693
			if let Err(e) = Self::apply_extrinsic(e) {
694
				let err: &'static str = e.into();
695
				panic!("{}", err)
696
			}
697
		});
698
	}
699

            
700
	/// Finalize the block - it is up the caller to ensure that all header fields are valid
701
	/// except state-root.
702
	// Note: Only used by the block builder - not Executive itself.
703
194763
	pub fn finalize_block() -> frame_system::pallet_prelude::HeaderFor<System> {
704
194763
		sp_io::init_tracing();
705
194763
		sp_tracing::enter_span!(sp_tracing::Level::TRACE, "finalize_block");
706

            
707
		// In this case there were no transactions to trigger this state transition:
708
194763
		if !<frame_system::Pallet<System>>::inherents_applied() {
709
194763
			Self::inherents_applied();
710
194763
		}
711

            
712
194763
		<frame_system::Pallet<System>>::note_finished_extrinsics();
713
194763
		<System as frame_system::Config>::PostTransactions::post_transactions();
714
194763
		let block_number = <frame_system::Pallet<System>>::block_number();
715
194763
		Self::on_idle_hook(block_number);
716
194763
		Self::on_finalize_hook(block_number);
717
194763
		<frame_system::Pallet<System>>::finalize()
718
194763
	}
719

            
720
	/// Run the `on_idle` hook of all pallet, but only if there is weight remaining and there are no
721
	/// ongoing MBMs.
722
194763
	fn on_idle_hook(block_number: NumberFor<Block>) {
723
194763
		if <System as frame_system::Config>::MultiBlockMigrator::ongoing() {
724
			return
725
194763
		}
726
194763

            
727
194763
		let weight = <frame_system::Pallet<System>>::block_weight();
728
194763
		let max_weight = <System::BlockWeights as frame_support::traits::Get<_>>::get().max_block;
729
194763
		let remaining_weight = max_weight.saturating_sub(weight.total());
730
194763

            
731
194763
		if remaining_weight.all_gt(Weight::zero()) {
732
59532
			let used_weight = <AllPalletsWithSystem as OnIdle<BlockNumberFor<System>>>::on_idle(
733
59532
				block_number,
734
59532
				remaining_weight,
735
59532
			);
736
59532
			<frame_system::Pallet<System>>::register_extra_weight_unchecked(
737
59532
				used_weight,
738
59532
				DispatchClass::Mandatory,
739
59532
			);
740
135231
		}
741
194763
	}
742

            
743
194763
	fn on_poll_hook(block_number: NumberFor<Block>) {
744
194763
		defensive_assert!(
745
194763
			!<System as frame_system::Config>::MultiBlockMigrator::ongoing(),
746
194763
			"on_poll should not be called during migrations"
747
194763
		);
748

            
749
194763
		let weight = <frame_system::Pallet<System>>::block_weight();
750
194763
		let max_weight = <System::BlockWeights as frame_support::traits::Get<_>>::get().max_block;
751
194763
		let remaining = max_weight.saturating_sub(weight.total());
752
194763

            
753
194763
		if remaining.all_gt(Weight::zero()) {
754
59532
			let mut meter = WeightMeter::with_limit(remaining);
755
59532
			<AllPalletsWithSystem as OnPoll<BlockNumberFor<System>>>::on_poll(
756
59532
				block_number,
757
59532
				&mut meter,
758
59532
			);
759
59532
			<frame_system::Pallet<System>>::register_extra_weight_unchecked(
760
59532
				meter.consumed(),
761
59532
				DispatchClass::Mandatory,
762
59532
			);
763
135231
		}
764
194763
	}
765

            
766
	/// Run the `on_finalize` hook of all pallet.
767
194763
	fn on_finalize_hook(block_number: NumberFor<Block>) {
768
194763
		<AllPalletsWithSystem as OnFinalize<BlockNumberFor<System>>>::on_finalize(block_number);
769
194763
	}
770

            
771
	/// Apply extrinsic outside of the block execution function.
772
	///
773
	/// This doesn't attempt to validate anything regarding the block, but it builds a list of uxt
774
	/// hashes.
775
194763
	pub fn apply_extrinsic(uxt: Block::Extrinsic) -> ApplyExtrinsicResult {
776
194763
		sp_io::init_tracing();
777
194763
		let encoded = uxt.encode();
778
194763
		let encoded_len = encoded.len();
779
194763
		sp_tracing::enter_span!(sp_tracing::info_span!("apply_extrinsic",
780
				ext=?sp_core::hexdisplay::HexDisplay::from(&encoded)));
781

            
782
		// We use the dedicated `is_inherent` check here, since just relying on `Mandatory` dispatch
783
		// class does not capture optional inherents.
784
194763
		let is_inherent = System::is_inherent(&uxt);
785

            
786
		// Verify that the signature is good.
787
194763
		let xt = uxt.check(&Default::default())?;
788
194763
		let dispatch_info = xt.get_dispatch_info();
789
194763

            
790
194763
		if !is_inherent && !<frame_system::Pallet<System>>::inherents_applied() {
791
			Self::inherents_applied();
792
194763
		}
793

            
794
		// We don't need to make sure to `note_extrinsic` only after we know it's going to be
795
		// executed to prevent it from leaking in storage since at this point, it will either
796
		// execute or panic (and revert storage changes).
797
194763
		<frame_system::Pallet<System>>::note_extrinsic(encoded);
798

            
799
		// AUDIT: Under no circumstances may this function panic from here onwards.
800

            
801
194763
		let r = Applyable::apply::<UnsignedValidator>(xt, &dispatch_info, encoded_len)?;
802

            
803
		// Mandatory(inherents) are not allowed to fail.
804
		//
805
		// The entire block should be discarded if an inherent fails to apply. Otherwise
806
		// it may open an attack vector.
807
194763
		if r.is_err() && dispatch_info.class == DispatchClass::Mandatory {
808
			return Err(InvalidTransaction::BadMandatory.into())
809
194763
		}
810
194763

            
811
194763
		<frame_system::Pallet<System>>::note_applied_extrinsic(&r, dispatch_info);
812
194763

            
813
194763
		Ok(r.map(|_| ()).map_err(|e| e.error))
814
194763
	}
815

            
816
	fn final_checks(header: &frame_system::pallet_prelude::HeaderFor<System>) {
817
		sp_tracing::enter_span!(sp_tracing::Level::TRACE, "final_checks");
818
		// remove temporaries
819
		let new_header = <frame_system::Pallet<System>>::finalize();
820

            
821
		// check digest
822
		assert_eq!(
823
			header.digest().logs().len(),
824
			new_header.digest().logs().len(),
825
			"Number of digest items must match that calculated."
826
		);
827
		let items_zip = header.digest().logs().iter().zip(new_header.digest().logs().iter());
828
		for (header_item, computed_item) in items_zip {
829
			header_item.check_equal(computed_item);
830
			assert!(header_item == computed_item, "Digest item must match that calculated.");
831
		}
832

            
833
		// check storage root.
834
		let storage_root = new_header.state_root();
835
		header.state_root().check_equal(storage_root);
836
		assert!(header.state_root() == storage_root, "Storage root must match that calculated.");
837

            
838
		assert!(
839
			header.extrinsics_root() == new_header.extrinsics_root(),
840
			"Transaction trie root must be valid.",
841
		);
842
	}
843

            
844
	/// Check a given signed transaction for validity. This doesn't execute any
845
	/// side-effects; it merely checks whether the transaction would panic if it were included or
846
	/// not.
847
	///
848
	/// Changes made to storage should be discarded.
849
	pub fn validate_transaction(
850
		source: TransactionSource,
851
		uxt: Block::Extrinsic,
852
		block_hash: Block::Hash,
853
	) -> TransactionValidity {
854
		sp_io::init_tracing();
855
		use sp_tracing::{enter_span, within_span};
856

            
857
		<frame_system::Pallet<System>>::initialize(
858
			&(frame_system::Pallet::<System>::block_number() + One::one()),
859
			&block_hash,
860
			&Default::default(),
861
		);
862

            
863
		enter_span! { sp_tracing::Level::TRACE, "validate_transaction" };
864

            
865
		let encoded_len = within_span! { sp_tracing::Level::TRACE, "using_encoded";
866
			uxt.using_encoded(|d| d.len())
867
		};
868

            
869
		let xt = within_span! { sp_tracing::Level::TRACE, "check";
870
			uxt.check(&Default::default())
871
		}?;
872

            
873
		let dispatch_info = within_span! { sp_tracing::Level::TRACE, "dispatch_info";
874
			xt.get_dispatch_info()
875
		};
876

            
877
		if dispatch_info.class == DispatchClass::Mandatory {
878
			return Err(InvalidTransaction::MandatoryValidation.into())
879
		}
880

            
881
		within_span! {
882
			sp_tracing::Level::TRACE, "validate";
883
			xt.validate::<UnsignedValidator>(source, &dispatch_info, encoded_len)
884
		}
885
	}
886

            
887
	/// Start an offchain worker and generate extrinsics.
888
	pub fn offchain_worker(header: &frame_system::pallet_prelude::HeaderFor<System>) {
889
		sp_io::init_tracing();
890
		// We need to keep events available for offchain workers,
891
		// hence we initialize the block manually.
892
		// OffchainWorker RuntimeApi should skip initialization.
893
		let digests = header.digest().clone();
894

            
895
		<frame_system::Pallet<System>>::initialize(header.number(), header.parent_hash(), &digests);
896

            
897
		// Frame system only inserts the parent hash into the block hashes as normally we don't know
898
		// the hash for the header before. However, here we are aware of the hash and we can add it
899
		// as well.
900
		frame_system::BlockHash::<System>::insert(header.number(), header.hash());
901

            
902
		<AllPalletsWithSystem as OffchainWorker<BlockNumberFor<System>>>::offchain_worker(
903
			*header.number(),
904
		)
905
	}
906
}