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
use fuel_core_storage::{
    transactional::StorageTransaction,
    Result as StorageResult,
};
use fuel_core_types::{
    blockchain::{
        block::Block,
        consensus::Consensus,
        primitives::{
            BlockHeight,
            BlockId,
        },
    },
    services::executor::{
        Result as ExecutorResult,
        UncommittedResult,
    },
};

#[cfg_attr(test, mockall::automock(type Database = crate::importer::test::MockDatabase;))]
/// The executors port.
pub trait Executor: Send + Sync {
    /// The database used by the executor.
    type Database: ExecutorDatabase;

    /// Executes the block and returns the result of execution with uncommitted database
    /// transaction.
    fn execute_without_commit(
        &self,
        block: Block,
    ) -> ExecutorResult<UncommittedResult<StorageTransaction<Self::Database>>>;
}

/// The database port used by the block importer.
pub trait ImporterDatabase {
    /// Returns the latest block height.
    fn latest_block_height(&self) -> StorageResult<BlockHeight>;
}

/// The port for returned database from the executor.
pub trait ExecutorDatabase: ImporterDatabase {
    /// Assigns the `Consensus` data to the block under the `block_id`.
    /// Return the previous value at the `height`, if any.
    fn seal_block(
        &mut self,
        block_id: &BlockId,
        consensus: &Consensus,
    ) -> StorageResult<Option<Consensus>>;
}

#[cfg_attr(test, mockall::automock)]
/// The verifier of the block.
pub trait BlockVerifier {
    /// Verifies the consistency of the block fields for the block's height.
    /// It includes the verification of **all** fields, it includes the consensus rules for
    /// the corresponding height.
    ///
    /// Return an error if the verification failed, otherwise `Ok(())`.
    fn verify_block_fields(
        &self,
        consensus: &Consensus,
        block: &Block,
    ) -> anyhow::Result<()>;
}