1use 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#[derive(Debug, PartialEq, Eq)]
32pub enum ImportResult {
33 Imported(ImportedAux),
35 AlreadyInChain,
37 KnownBad,
39 UnknownParent,
41 MissingState,
43}
44
45#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
47pub struct ImportedAux {
48 pub header_only: bool,
50 pub clear_justification_requests: bool,
52 pub needs_justification: bool,
54 pub bad_justification: bool,
56 pub is_new_best: bool,
58}
59
60impl ImportResult {
61 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 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#[derive(Debug, PartialEq, Eq, Clone, Copy)]
97pub enum ForkChoiceStrategy {
98 LongestChain,
100 Custom(bool),
102}
103
104#[derive(Debug, PartialEq, Eq, Clone)]
106pub struct BlockCheckParams<Block: BlockT> {
107 pub hash: Block::Hash,
109 pub number: NumberFor<Block>,
111 pub parent_hash: Block::Hash,
113 pub allow_missing_state: bool,
115 pub allow_missing_parent: bool,
117 pub import_existing: bool,
119}
120
121pub enum StorageChanges<Block: BlockT> {
123 Changes(sp_state_machine::StorageChanges<HashingFor<Block>>),
125 Import(ImportedState<Block>),
127}
128
129#[derive(PartialEq, Eq, Clone)]
131pub struct ImportedState<B: BlockT> {
132 pub block: B::Hash,
134 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
144pub enum StateAction<Block: BlockT> {
146 ApplyChanges(StorageChanges<Block>),
148 Execute,
150 ExecuteIfPossible,
152 Skip,
154}
155
156impl<Block: BlockT> StateAction<Block> {
157 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
168impl<Block: BlockT> From<StorageChanges<Block>> for StateAction<Block> {
169 fn from(value: StorageChanges<Block>) -> Self {
170 Self::ApplyChanges(value)
171 }
172}
173
174impl<Block: BlockT> From<sp_state_machine::StorageChanges<HashingFor<Block>>>
175 for StateAction<Block>
176{
177 fn from(value: sp_state_machine::StorageChanges<HashingFor<Block>>) -> Self {
178 Self::ApplyChanges(StorageChanges::Changes(value))
179 }
180}
181
182#[non_exhaustive]
184pub struct BlockImportParams<Block: BlockT> {
185 pub origin: BlockOrigin,
187 pub header: Block::Header,
199 pub justifications: Option<Justifications>,
201 pub post_digests: Vec<DigestItem>,
204 pub body: Option<Vec<Block::Extrinsic>>,
206 pub indexed_body: Option<Vec<Vec<u8>>>,
208 pub state_action: StateAction<Block>,
210 pub finalized: bool,
213 pub intermediates: HashMap<Cow<'static, [u8]>, Box<dyn Any + Send>>,
217 pub auxiliary: Vec<(Vec<u8>, Option<Vec<u8>>)>,
222 pub fork_choice: Option<ForkChoiceStrategy>,
229 pub import_existing: bool,
231 pub create_gap: bool,
233 pub post_hash: Option<Block::Hash>,
235}
236
237impl<Block: BlockT> BlockImportParams<Block> {
238 pub fn new(origin: BlockOrigin, header: Block::Header) -> Self {
240 Self {
241 origin,
242 header,
243 justifications: None,
244 post_digests: Vec::new(),
245 body: None,
246 indexed_body: None,
247 state_action: if origin == BlockOrigin::WarpSync {
249 StateAction::Skip
250 } else {
251 StateAction::Execute
252 },
253 finalized: false,
254 intermediates: HashMap::new(),
255 auxiliary: Vec::new(),
256 fork_choice: None,
257 import_existing: false,
258 create_gap: origin != BlockOrigin::WarpSync,
265 post_hash: None,
266 }
267 }
268
269 pub fn post_hash(&self) -> Block::Hash {
271 if let Some(hash) = self.post_hash {
272 hash
273 } else {
274 self.post_header().hash()
275 }
276 }
277
278 pub fn post_header(&self) -> Block::Header {
280 if self.post_digests.is_empty() {
281 self.header.clone()
282 } else {
283 let mut hdr = self.header.clone();
284 for digest_item in &self.post_digests {
285 hdr.digest_mut().push(digest_item.clone());
286 }
287
288 hdr
289 }
290 }
291
292 pub fn insert_intermediate<T: 'static + Send>(&mut self, key: &'static [u8], value: T) {
294 self.intermediates.insert(Cow::from(key), Box::new(value));
295 }
296
297 pub fn remove_intermediate<T: 'static>(&mut self, key: &[u8]) -> Result<T, Error> {
299 let (k, v) = self.intermediates.remove_entry(key).ok_or(Error::NoIntermediate)?;
300
301 v.downcast::<T>().map(|v| *v).map_err(|v| {
302 self.intermediates.insert(k, v);
303 Error::InvalidIntermediate
304 })
305 }
306
307 pub fn get_intermediate<T: 'static>(&self, key: &[u8]) -> Result<&T, Error> {
309 self.intermediates
310 .get(key)
311 .ok_or(Error::NoIntermediate)?
312 .downcast_ref::<T>()
313 .ok_or(Error::InvalidIntermediate)
314 }
315
316 pub fn get_intermediate_mut<T: 'static>(&mut self, key: &[u8]) -> Result<&mut T, Error> {
318 self.intermediates
319 .get_mut(key)
320 .ok_or(Error::NoIntermediate)?
321 .downcast_mut::<T>()
322 .ok_or(Error::InvalidIntermediate)
323 }
324
325 pub fn with_state(&self) -> bool {
327 matches!(self.state_action, StateAction::ApplyChanges(StorageChanges::Import(_)))
328 }
329}
330
331#[async_trait::async_trait]
333pub trait BlockImport<B: BlockT> {
334 type Error: std::error::Error + Send + 'static;
336
337 async fn check_block(&self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error>;
339
340 async fn import_block(&self, block: BlockImportParams<B>) -> Result<ImportResult, Self::Error>;
342}
343
344#[async_trait::async_trait]
345impl<B: BlockT> BlockImport<B> for crate::import_queue::BoxBlockImport<B> {
346 type Error = sp_consensus::error::Error;
347
348 async fn check_block(&self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error> {
350 (**self).check_block(block).await
351 }
352
353 async fn import_block(&self, block: BlockImportParams<B>) -> Result<ImportResult, Self::Error> {
355 (**self).import_block(block).await
356 }
357}
358
359#[async_trait::async_trait]
360impl<B: BlockT, T, E: std::error::Error + Send + 'static> BlockImport<B> for Arc<T>
361where
362 for<'r> &'r T: BlockImport<B, Error = E>,
363 T: Send + Sync,
364{
365 type Error = E;
366
367 async fn check_block(&self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error> {
368 (&**self).check_block(block).await
369 }
370
371 async fn import_block(&self, block: BlockImportParams<B>) -> Result<ImportResult, Self::Error> {
372 (&**self).import_block(block).await
373 }
374}
375
376#[async_trait::async_trait]
378pub trait JustificationImport<B: BlockT> {
379 type Error: std::error::Error + Send + 'static;
380
381 async fn on_start(&mut self) -> Vec<(B::Hash, NumberFor<B>)>;
384
385 async fn import_justification(
387 &mut self,
388 hash: B::Hash,
389 number: NumberFor<B>,
390 justification: Justification,
391 ) -> Result<(), Self::Error>;
392}
393
394pub trait JustificationSyncLink<B: BlockT>: Send + Sync {
400 fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>);
402
403 fn clear_justification_requests(&self);
405}
406
407impl<B: BlockT> JustificationSyncLink<B> for () {
408 fn request_justification(&self, _hash: &B::Hash, _number: NumberFor<B>) {}
409
410 fn clear_justification_requests(&self) {}
411}
412
413impl<B: BlockT, L: JustificationSyncLink<B>> JustificationSyncLink<B> for Arc<L> {
414 fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>) {
415 L::request_justification(self, hash, number);
416 }
417
418 fn clear_justification_requests(&self) {
419 L::clear_justification_requests(self);
420 }
421}