use std::sync::Arc;
use std::collections::HashMap;
use std::marker::PhantomData;
use fp_consensus::{ensure_log, FindLogError};
use fp_rpc::EthereumRuntimeRPCApi;
use sc_client_api::{BlockOf, backend::AuxStore};
use sp_blockchain::{HeaderBackend, ProvideCache, well_known_cache_keys::Id as CacheKeyId};
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use sp_api::ProvideRuntimeApi;
use sp_consensus::{
BlockImportParams, Error as ConsensusError, BlockImport,
BlockCheckParams, ImportResult,
};
use sc_client_api;
#[derive(derive_more::Display, Debug)]
pub enum Error {
#[display(fmt = "Multiple runtime Ethereum blocks, rejecting!")]
MultipleRuntimeLogs,
#[display(fmt = "Runtime Ethereum block not found, rejecting!")]
NoRuntimeLog,
#[display(fmt = "Cannot access the runtime at genesis, rejecting!")]
RuntimeApiCallFailed,
}
impl From<Error> for String {
fn from(error: Error) -> String {
error.to_string()
}
}
impl From<FindLogError> for Error {
fn from(error: FindLogError) -> Error {
match error {
FindLogError::NotFound => Error::NoRuntimeLog,
FindLogError::MultipleLogs => Error::MultipleRuntimeLogs,
}
}
}
impl From<Error> for ConsensusError {
fn from(error: Error) -> ConsensusError {
ConsensusError::ClientImport(error.to_string())
}
}
pub struct FrontierBlockImport<B: BlockT, I, C> {
inner: I,
client: Arc<C>,
backend: Arc<fc_db::Backend<B>>,
_marker: PhantomData<B>,
}
impl<Block: BlockT, I: Clone + BlockImport<Block>, C> Clone for FrontierBlockImport<Block, I, C> {
fn clone(&self) -> Self {
FrontierBlockImport {
inner: self.inner.clone(),
client: self.client.clone(),
backend: self.backend.clone(),
_marker: PhantomData,
}
}
}
impl<B, I, C> FrontierBlockImport<B, I, C> where
B: BlockT,
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
I::Error: Into<ConsensusError>,
C: ProvideRuntimeApi<B> + Send + Sync + HeaderBackend<B> + AuxStore + ProvideCache<B> + BlockOf,
C::Api: EthereumRuntimeRPCApi<B>,
C::Api: BlockBuilderApi<B>,
{
pub fn new(
inner: I,
client: Arc<C>,
backend: Arc<fc_db::Backend::<B>>,
) -> Self {
Self {
inner,
client,
backend,
_marker: PhantomData,
}
}
}
impl<B, I, C> BlockImport<B> for FrontierBlockImport<B, I, C> where
B: BlockT,
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
I::Error: Into<ConsensusError>,
C: ProvideRuntimeApi<B> + Send + Sync + HeaderBackend<B> + AuxStore + ProvideCache<B> + BlockOf,
C::Api: EthereumRuntimeRPCApi<B>,
C::Api: BlockBuilderApi<B>,
{
type Error = ConsensusError;
type Transaction = sp_api::TransactionFor<C, B>;
fn check_block(
&mut self,
block: BlockCheckParams<B>,
) -> Result<ImportResult, Self::Error> {
self.inner.check_block(block).map_err(Into::into)
}
fn import_block(
&mut self,
block: BlockImportParams<B, Self::Transaction>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
ensure_log(&block.header.digest()).map_err(|e| Error::from(e))?;
self.inner.import_block(block, new_cache).map_err(Into::into)
}
}