sc_consensus/
block_import.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Block import helpers.
20
21use serde::{Deserialize, Serialize};
22use sp_runtime::{
23	traits::{Block as BlockT, HashingFor, Header as HeaderT, NumberFor},
24	DigestItem, Justification, Justifications,
25};
26use std::{any::Any, borrow::Cow, collections::HashMap, sync::Arc};
27
28use sc_client_api::PrefetchedIndexedTransactions;
29use sp_consensus::{BlockOrigin, Error};
30
31/// Block import result.
32#[derive(Debug, PartialEq, Eq)]
33pub enum ImportResult {
34	/// Block imported.
35	Imported(ImportedAux),
36	/// Already in the blockchain.
37	AlreadyInChain,
38	/// Block or parent is known to be bad.
39	KnownBad,
40	/// Block parent is not in the chain.
41	UnknownParent,
42	/// Parent state is missing.
43	MissingState,
44}
45
46/// Auxiliary data associated with an imported block result.
47#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
48pub struct ImportedAux {
49	/// Only the header has been imported. Block body verification was skipped.
50	pub header_only: bool,
51	/// Clear all pending justification requests.
52	pub clear_justification_requests: bool,
53	/// Request a justification for the given block.
54	pub needs_justification: bool,
55	/// Received a bad justification.
56	pub bad_justification: bool,
57	/// Whether the block that was imported is the new best block.
58	pub is_new_best: bool,
59}
60
61impl ImportResult {
62	/// Returns default value for `ImportResult::Imported` with
63	/// `clear_justification_requests`, `needs_justification`,
64	/// `bad_justification` set to false.
65	pub fn imported(is_new_best: bool) -> ImportResult {
66		let aux = ImportedAux { is_new_best, ..Default::default() };
67
68		ImportResult::Imported(aux)
69	}
70
71	/// Handles any necessary request for justifications (or clearing of pending requests) based on
72	/// the outcome of this block import.
73	pub fn handle_justification<B>(
74		&self,
75		hash: &B::Hash,
76		number: NumberFor<B>,
77		justification_sync_link: &dyn JustificationSyncLink<B>,
78	) where
79		B: BlockT,
80	{
81		match self {
82			ImportResult::Imported(aux) => {
83				if aux.clear_justification_requests {
84					justification_sync_link.clear_justification_requests();
85				}
86
87				if aux.needs_justification {
88					justification_sync_link.request_justification(hash, number);
89				}
90			},
91			_ => {},
92		}
93	}
94}
95
96/// Fork choice strategy.
97#[derive(Debug, PartialEq, Eq, Clone, Copy)]
98pub enum ForkChoiceStrategy {
99	/// Longest chain fork choice.
100	LongestChain,
101	/// Custom fork choice rule, where true indicates the new block should be the best block.
102	Custom(bool),
103}
104
105/// Data required to check validity of a Block.
106#[derive(Debug, PartialEq, Eq, Clone)]
107pub struct BlockCheckParams<Block: BlockT> {
108	/// Hash of the block that we verify.
109	pub hash: Block::Hash,
110	/// Block number of the block that we verify.
111	pub number: NumberFor<Block>,
112	/// Parent hash of the block that we verify.
113	pub parent_hash: Block::Hash,
114	/// Allow importing the block skipping state verification if parent state is missing.
115	pub allow_missing_state: bool,
116	/// Allow importing the block if parent block is missing.
117	pub allow_missing_parent: bool,
118	/// Re-validate existing block.
119	pub import_existing: bool,
120}
121
122/// Precomputed storage.
123pub enum StorageChanges<Block: BlockT> {
124	/// Changes coming from block execution.
125	Changes(sp_state_machine::StorageChanges<HashingFor<Block>>),
126	/// Whole new state.
127	Import(ImportedState<Block>),
128}
129
130/// Imported state data. A vector of key-value pairs that should form a trie.
131#[derive(PartialEq, Eq, Clone)]
132pub struct ImportedState<B: BlockT> {
133	/// Target block hash.
134	pub block: B::Hash,
135	/// State keys and values.
136	pub state: sp_state_machine::KeyValueStates,
137}
138
139impl<B: BlockT> std::fmt::Debug for ImportedState<B> {
140	fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
141		fmt.debug_struct("ImportedState").field("block", &self.block).finish()
142	}
143}
144
145/// Defines how a new state is computed for a given imported block.
146pub enum StateAction<Block: BlockT> {
147	/// Apply precomputed changes coming from block execution or state sync.
148	ApplyChanges(StorageChanges<Block>),
149	/// Execute block body (required) and compute state.
150	Execute,
151	/// Execute block body if parent state is available and compute state.
152	ExecuteIfPossible,
153	/// Don't execute or import state.
154	Skip,
155}
156
157impl<Block: BlockT> std::fmt::Debug for StateAction<Block> {
158	fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
159		match self {
160			Self::ApplyChanges(_) => fmt.write_str("ApplyChanges(..)"),
161			Self::Execute => fmt.write_str("Execute"),
162			Self::ExecuteIfPossible => fmt.write_str("ExecuteIfPossible"),
163			Self::Skip => fmt.write_str("Skip"),
164		}
165	}
166}
167
168impl<Block: BlockT> StateAction<Block> {
169	/// Check if execution checks that require runtime calls should be skipped.
170	pub fn skip_execution_checks(&self) -> bool {
171		match self {
172			StateAction::ApplyChanges(_) |
173			StateAction::Execute |
174			StateAction::ExecuteIfPossible => false,
175			StateAction::Skip => true,
176		}
177	}
178
179	/// Returns as storage changes.
180	pub fn as_storage_changes(
181		&self,
182	) -> Option<&sp_state_machine::StorageChanges<HashingFor<Block>>> {
183		match self {
184			StateAction::ApplyChanges(StorageChanges::Changes(changes)) => Some(&changes),
185			_ => None,
186		}
187	}
188}
189
190impl<Block: BlockT> From<StorageChanges<Block>> for StateAction<Block> {
191	fn from(value: StorageChanges<Block>) -> Self {
192		Self::ApplyChanges(value)
193	}
194}
195
196impl<Block: BlockT> From<sp_state_machine::StorageChanges<HashingFor<Block>>>
197	for StateAction<Block>
198{
199	fn from(value: sp_state_machine::StorageChanges<HashingFor<Block>>) -> Self {
200		Self::ApplyChanges(StorageChanges::Changes(value))
201	}
202}
203
204/// Data required to import a Block.
205#[non_exhaustive]
206pub struct BlockImportParams<Block: BlockT> {
207	/// Origin of the Block
208	pub origin: BlockOrigin,
209	/// The header, without consensus post-digests applied. This should be in the same
210	/// state as it comes out of the runtime.
211	///
212	/// Consensus engines which alter the header (by adding post-runtime digests)
213	/// should strip those off in the initial verification process and pass them
214	/// via the `post_digests` field. During block authorship, they should
215	/// not be pushed to the header directly.
216	///
217	/// The reason for this distinction is so the header can be directly
218	/// re-executed in a runtime that checks digest equivalence -- the
219	/// post-runtime digests are pushed back on after.
220	pub header: Block::Header,
221	/// Justification(s) provided for this block from the outside.
222	pub justifications: Option<Justifications>,
223	/// Digest items that have been added after the runtime for external
224	/// work, like a consensus signature.
225	pub post_digests: Vec<DigestItem>,
226	/// The body of the block.
227	pub body: Option<Vec<Block::Extrinsic>>,
228	/// Indexed transaction body of the block.
229	pub indexed_body: Option<Vec<Vec<u8>>>,
230	/// Specify how the new state is computed.
231	pub state_action: StateAction<Block>,
232	/// Is this block finalized already?
233	/// `true` implies instant finality.
234	pub finalized: bool,
235	/// Intermediate values that are interpreted by block importers. Each block importer,
236	/// upon handling a value, removes it from the intermediate list. The final block importer
237	/// rejects block import if there are still intermediate values that remain unhandled.
238	pub intermediates: HashMap<Cow<'static, [u8]>, Box<dyn Any + Send>>,
239	/// Auxiliary consensus data produced by the block.
240	/// Contains a list of key-value pairs. If values are `None`, the keys will be deleted. These
241	/// changes will be applied to `AuxStore` database all as one batch, which is more efficient
242	/// than updating `AuxStore` directly.
243	pub auxiliary: Vec<(Vec<u8>, Option<Vec<u8>>)>,
244	/// Fork choice strategy of this import. This should only be set by a
245	/// synchronous import, otherwise it may race against other imports.
246	/// `None` indicates that the current verifier or importer cannot yet
247	/// determine the fork choice value, and it expects subsequent importer
248	/// to modify it. If `None` is passed all the way down to bottom block
249	/// importer, the import fails with an `IncompletePipeline` error.
250	pub fork_choice: Option<ForkChoiceStrategy>,
251	/// Re-validate existing block.
252	pub import_existing: bool,
253	/// Whether to create "block gap" in case this block doesn't have parent.
254	pub create_gap: bool,
255	/// Cached full header hash (with post-digests applied).
256	pub post_hash: Option<Block::Hash>,
257	/// Indexed-transaction data attached by upstream block-import wrappers.
258	/// See [`PrefetchedIndexedTransactions`].
259	pub prefetched_indexed_transactions: PrefetchedIndexedTransactions,
260}
261
262impl<Block: BlockT> BlockImportParams<Block> {
263	/// Create a new block import params.
264	pub fn new(origin: BlockOrigin, header: Block::Header) -> Self {
265		Self {
266			origin,
267			header,
268			justifications: None,
269			post_digests: Vec::new(),
270			body: None,
271			indexed_body: None,
272			// Warp sync blocks are already verified, skip execution.
273			state_action: if origin == BlockOrigin::WarpSync {
274				StateAction::Skip
275			} else {
276				StateAction::Execute
277			},
278			finalized: false,
279			intermediates: HashMap::new(),
280			auxiliary: Vec::new(),
281			fork_choice: None,
282			import_existing: false,
283			// Never create gaps for warp sync imported blocks.
284			// Warp sync downloads only session blocks. Gap sync to work needs one gap even if
285			// between gap start and gap end some blocks are existing. If each warp sync block
286			// created a gap, every new block import would override the previous gap, losing the
287			// real gap start. In case of warp sync a gap is created separately when the target
288			// block with state is imported.
289			create_gap: origin != BlockOrigin::WarpSync,
290			post_hash: None,
291			prefetched_indexed_transactions: PrefetchedIndexedTransactions::default(),
292		}
293	}
294
295	/// Get the full header hash (with post-digests applied).
296	pub fn post_hash(&self) -> Block::Hash {
297		if let Some(hash) = self.post_hash {
298			hash
299		} else {
300			self.post_header().hash()
301		}
302	}
303
304	/// Get the post header.
305	pub fn post_header(&self) -> Block::Header {
306		if self.post_digests.is_empty() {
307			self.header.clone()
308		} else {
309			let mut hdr = self.header.clone();
310			for digest_item in &self.post_digests {
311				hdr.digest_mut().push(digest_item.clone());
312			}
313
314			hdr
315		}
316	}
317
318	/// Insert intermediate by given key.
319	pub fn insert_intermediate<T: 'static + Send>(&mut self, key: &'static [u8], value: T) {
320		self.intermediates.insert(Cow::from(key), Box::new(value));
321	}
322
323	/// Remove and return intermediate by given key.
324	pub fn remove_intermediate<T: 'static>(&mut self, key: &[u8]) -> Result<T, Error> {
325		let (k, v) = self.intermediates.remove_entry(key).ok_or(Error::NoIntermediate)?;
326
327		v.downcast::<T>().map(|v| *v).map_err(|v| {
328			self.intermediates.insert(k, v);
329			Error::InvalidIntermediate
330		})
331	}
332
333	/// Get a reference to a given intermediate.
334	pub fn get_intermediate<T: 'static>(&self, key: &[u8]) -> Result<&T, Error> {
335		self.intermediates
336			.get(key)
337			.ok_or(Error::NoIntermediate)?
338			.downcast_ref::<T>()
339			.ok_or(Error::InvalidIntermediate)
340	}
341
342	/// Get a mutable reference to a given intermediate.
343	pub fn get_intermediate_mut<T: 'static>(&mut self, key: &[u8]) -> Result<&mut T, Error> {
344		self.intermediates
345			.get_mut(key)
346			.ok_or(Error::NoIntermediate)?
347			.downcast_mut::<T>()
348			.ok_or(Error::InvalidIntermediate)
349	}
350
351	/// Check if this block contains state import action
352	pub fn with_state(&self) -> bool {
353		matches!(self.state_action, StateAction::ApplyChanges(StorageChanges::Import(_)))
354	}
355}
356
357/// Block import trait.
358#[async_trait::async_trait]
359pub trait BlockImport<B: BlockT> {
360	/// The error type.
361	type Error: std::error::Error + Send + 'static;
362
363	/// Check block preconditions.
364	async fn check_block(&self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error>;
365
366	/// Import a block.
367	async fn import_block(&self, block: BlockImportParams<B>) -> Result<ImportResult, Self::Error>;
368}
369
370#[async_trait::async_trait]
371impl<B: BlockT> BlockImport<B> for crate::import_queue::BoxBlockImport<B> {
372	type Error = sp_consensus::error::Error;
373
374	/// Check block preconditions.
375	async fn check_block(&self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error> {
376		(**self).check_block(block).await
377	}
378
379	/// Import a block.
380	async fn import_block(&self, block: BlockImportParams<B>) -> Result<ImportResult, Self::Error> {
381		(**self).import_block(block).await
382	}
383}
384
385#[async_trait::async_trait]
386impl<B: BlockT, T, E: std::error::Error + Send + 'static> BlockImport<B> for Arc<T>
387where
388	for<'r> &'r T: BlockImport<B, Error = E>,
389	T: Send + Sync,
390{
391	type Error = E;
392
393	async fn check_block(&self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error> {
394		(&**self).check_block(block).await
395	}
396
397	async fn import_block(&self, block: BlockImportParams<B>) -> Result<ImportResult, Self::Error> {
398		(&**self).import_block(block).await
399	}
400}
401
402/// Justification import trait
403#[async_trait::async_trait]
404pub trait JustificationImport<B: BlockT> {
405	type Error: std::error::Error + Send + 'static;
406
407	/// Called by the import queue when it is started. Returns a list of justifications to request
408	/// from the network.
409	async fn on_start(&mut self) -> Vec<(B::Hash, NumberFor<B>)>;
410
411	/// Import a Block justification and finalize the given block.
412	async fn import_justification(
413		&mut self,
414		hash: B::Hash,
415		number: NumberFor<B>,
416		justification: Justification,
417	) -> Result<(), Self::Error>;
418}
419
420/// Control the synchronization process of block justifications.
421///
422/// When importing blocks different consensus engines might require that
423/// additional finality data is provided (i.e. a justification for the block).
424/// This trait abstracts the required methods to issue those requests
425pub trait JustificationSyncLink<B: BlockT>: Send + Sync {
426	/// Request a justification for the given block.
427	fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>);
428
429	/// Clear all pending justification requests.
430	fn clear_justification_requests(&self);
431}
432
433impl<B: BlockT> JustificationSyncLink<B> for () {
434	fn request_justification(&self, _hash: &B::Hash, _number: NumberFor<B>) {}
435
436	fn clear_justification_requests(&self) {}
437}
438
439impl<B: BlockT, L: JustificationSyncLink<B>> JustificationSyncLink<B> for Arc<L> {
440	fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>) {
441		L::request_justification(self, hash, number);
442	}
443
444	fn clear_justification_requests(&self) {
445		L::clear_justification_requests(self);
446	}
447}