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