1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This file is part of Frontier.
//
// Copyright (c) 2020-2022 Parity Technologies (UK) Ltd.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![deny(unused_crate_dependencies)]

use std::{marker::PhantomData, sync::Arc};

// Substrate
use sc_consensus::{BlockCheckParams, BlockImport, BlockImportParams, ImportResult};
use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_consensus::Error as ConsensusError;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
// Frontier
use fp_consensus::{ensure_log, FindLogError};
use fp_rpc::EthereumRuntimeRPCApi;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("Multiple runtime Ethereum blocks, rejecting!")]
    MultipleRuntimeLogs,
    #[error("Runtime Ethereum block not found, rejecting!")]
    NoRuntimeLog,
    #[error("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>,
    _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(),
            _marker: PhantomData,
        }
    }
}

impl<B, I, C> FrontierBlockImport<B, I, C>
where
    B: BlockT,
    I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>>,
    I::Error: Into<ConsensusError>,
    C: ProvideRuntimeApi<B>,
    C::Api: BlockBuilderApi<B> + EthereumRuntimeRPCApi<B>,
{
    pub fn new(inner: I, client: Arc<C>) -> Self {
        Self {
            inner,
            client,
            _marker: PhantomData,
        }
    }
}

#[async_trait::async_trait]
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,
    C::Api: BlockBuilderApi<B> + EthereumRuntimeRPCApi<B>,
{
    type Error = ConsensusError;
    type Transaction = sp_api::TransactionFor<C, B>;

    async fn check_block(
        &mut self,
        block: BlockCheckParams<B>,
    ) -> Result<ImportResult, Self::Error> {
        self.inner.check_block(block).await.map_err(Into::into)
    }

    async fn import_block(
        &mut self,
        block: BlockImportParams<B, Self::Transaction>,
    ) -> Result<ImportResult, Self::Error> {
        // We validate that there are only one frontier log. No other
        // actions are needed and mapping syncing is delegated to a separate
        // worker.
        ensure_log(block.header.digest()).map_err(Error::from)?;

        self.inner.import_block(block).await.map_err(Into::into)
    }
}