use serde::{Deserialize, Serialize};
use sp_runtime::{
traits::{Block as BlockT, HashingFor, Header as HeaderT, NumberFor},
DigestItem, Justification, Justifications,
};
use std::{any::Any, borrow::Cow, collections::HashMap, sync::Arc};
use sp_consensus::{BlockOrigin, Error};
#[derive(Debug, PartialEq, Eq)]
pub enum ImportResult {
Imported(ImportedAux),
AlreadyInChain,
KnownBad,
UnknownParent,
MissingState,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImportedAux {
pub header_only: bool,
pub clear_justification_requests: bool,
pub needs_justification: bool,
pub bad_justification: bool,
pub is_new_best: bool,
}
impl ImportResult {
pub fn imported(is_new_best: bool) -> ImportResult {
let aux = ImportedAux { is_new_best, ..Default::default() };
ImportResult::Imported(aux)
}
pub fn handle_justification<B>(
&self,
hash: &B::Hash,
number: NumberFor<B>,
justification_sync_link: &dyn JustificationSyncLink<B>,
) where
B: BlockT,
{
match self {
ImportResult::Imported(aux) => {
if aux.clear_justification_requests {
justification_sync_link.clear_justification_requests();
}
if aux.needs_justification {
justification_sync_link.request_justification(hash, number);
}
},
_ => {},
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ForkChoiceStrategy {
LongestChain,
Custom(bool),
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BlockCheckParams<Block: BlockT> {
pub hash: Block::Hash,
pub number: NumberFor<Block>,
pub parent_hash: Block::Hash,
pub allow_missing_state: bool,
pub allow_missing_parent: bool,
pub import_existing: bool,
}
pub enum StorageChanges<Block: BlockT> {
Changes(sp_state_machine::StorageChanges<HashingFor<Block>>),
Import(ImportedState<Block>),
}
#[derive(PartialEq, Eq, Clone)]
pub struct ImportedState<B: BlockT> {
pub block: B::Hash,
pub state: sp_state_machine::KeyValueStates,
}
impl<B: BlockT> std::fmt::Debug for ImportedState<B> {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.debug_struct("ImportedState").field("block", &self.block).finish()
}
}
pub enum StateAction<Block: BlockT> {
ApplyChanges(StorageChanges<Block>),
Execute,
ExecuteIfPossible,
Skip,
}
impl<Block: BlockT> StateAction<Block> {
pub fn skip_execution_checks(&self) -> bool {
match self {
StateAction::ApplyChanges(_) |
StateAction::Execute |
StateAction::ExecuteIfPossible => false,
StateAction::Skip => true,
}
}
}
impl<Block: BlockT> From<StorageChanges<Block>> for StateAction<Block> {
fn from(value: StorageChanges<Block>) -> Self {
Self::ApplyChanges(value)
}
}
impl<Block: BlockT> From<sp_state_machine::StorageChanges<HashingFor<Block>>>
for StateAction<Block>
{
fn from(value: sp_state_machine::StorageChanges<HashingFor<Block>>) -> Self {
Self::ApplyChanges(StorageChanges::Changes(value))
}
}
#[non_exhaustive]
pub struct BlockImportParams<Block: BlockT> {
pub origin: BlockOrigin,
pub header: Block::Header,
pub justifications: Option<Justifications>,
pub post_digests: Vec<DigestItem>,
pub body: Option<Vec<Block::Extrinsic>>,
pub indexed_body: Option<Vec<Vec<u8>>>,
pub state_action: StateAction<Block>,
pub finalized: bool,
pub intermediates: HashMap<Cow<'static, [u8]>, Box<dyn Any + Send>>,
pub auxiliary: Vec<(Vec<u8>, Option<Vec<u8>>)>,
pub fork_choice: Option<ForkChoiceStrategy>,
pub import_existing: bool,
pub create_gap: bool,
pub post_hash: Option<Block::Hash>,
}
impl<Block: BlockT> BlockImportParams<Block> {
pub fn new(origin: BlockOrigin, header: Block::Header) -> Self {
Self {
origin,
header,
justifications: None,
post_digests: Vec::new(),
body: None,
indexed_body: None,
state_action: if origin == BlockOrigin::WarpSync {
StateAction::Skip
} else {
StateAction::Execute
},
finalized: false,
intermediates: HashMap::new(),
auxiliary: Vec::new(),
fork_choice: None,
import_existing: false,
create_gap: origin != BlockOrigin::WarpSync,
post_hash: None,
}
}
pub fn post_hash(&self) -> Block::Hash {
if let Some(hash) = self.post_hash {
hash
} else {
self.post_header().hash()
}
}
pub fn post_header(&self) -> Block::Header {
if self.post_digests.is_empty() {
self.header.clone()
} else {
let mut hdr = self.header.clone();
for digest_item in &self.post_digests {
hdr.digest_mut().push(digest_item.clone());
}
hdr
}
}
pub fn insert_intermediate<T: 'static + Send>(&mut self, key: &'static [u8], value: T) {
self.intermediates.insert(Cow::from(key), Box::new(value));
}
pub fn remove_intermediate<T: 'static>(&mut self, key: &[u8]) -> Result<T, Error> {
let (k, v) = self.intermediates.remove_entry(key).ok_or(Error::NoIntermediate)?;
v.downcast::<T>().map(|v| *v).map_err(|v| {
self.intermediates.insert(k, v);
Error::InvalidIntermediate
})
}
pub fn get_intermediate<T: 'static>(&self, key: &[u8]) -> Result<&T, Error> {
self.intermediates
.get(key)
.ok_or(Error::NoIntermediate)?
.downcast_ref::<T>()
.ok_or(Error::InvalidIntermediate)
}
pub fn get_intermediate_mut<T: 'static>(&mut self, key: &[u8]) -> Result<&mut T, Error> {
self.intermediates
.get_mut(key)
.ok_or(Error::NoIntermediate)?
.downcast_mut::<T>()
.ok_or(Error::InvalidIntermediate)
}
pub fn with_state(&self) -> bool {
matches!(self.state_action, StateAction::ApplyChanges(StorageChanges::Import(_)))
}
}
#[async_trait::async_trait]
pub trait BlockImport<B: BlockT> {
type Error: std::error::Error + Send + 'static;
async fn check_block(&self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error>;
async fn import_block(&self, block: BlockImportParams<B>) -> Result<ImportResult, Self::Error>;
}
#[async_trait::async_trait]
impl<B: BlockT> BlockImport<B> for crate::import_queue::BoxBlockImport<B> {
type Error = sp_consensus::error::Error;
async fn check_block(&self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error> {
(**self).check_block(block).await
}
async fn import_block(&self, block: BlockImportParams<B>) -> Result<ImportResult, Self::Error> {
(**self).import_block(block).await
}
}
#[async_trait::async_trait]
impl<B: BlockT, T, E: std::error::Error + Send + 'static> BlockImport<B> for Arc<T>
where
for<'r> &'r T: BlockImport<B, Error = E>,
T: Send + Sync,
{
type Error = E;
async fn check_block(&self, block: BlockCheckParams<B>) -> Result<ImportResult, Self::Error> {
(&**self).check_block(block).await
}
async fn import_block(&self, block: BlockImportParams<B>) -> Result<ImportResult, Self::Error> {
(&**self).import_block(block).await
}
}
#[async_trait::async_trait]
pub trait JustificationImport<B: BlockT> {
type Error: std::error::Error + Send + 'static;
async fn on_start(&mut self) -> Vec<(B::Hash, NumberFor<B>)>;
async fn import_justification(
&mut self,
hash: B::Hash,
number: NumberFor<B>,
justification: Justification,
) -> Result<(), Self::Error>;
}
pub trait JustificationSyncLink<B: BlockT>: Send + Sync {
fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>);
fn clear_justification_requests(&self);
}
impl<B: BlockT> JustificationSyncLink<B> for () {
fn request_justification(&self, _hash: &B::Hash, _number: NumberFor<B>) {}
fn clear_justification_requests(&self) {}
}
impl<B: BlockT, L: JustificationSyncLink<B>> JustificationSyncLink<B> for Arc<L> {
fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>) {
L::request_justification(self, hash, number);
}
fn clear_justification_requests(&self) {
L::clear_justification_requests(self);
}
}