fuel_core_sync/
ports.rs

1//! Ports this services requires to function.
2
3use fuel_core_services::stream::BoxStream;
4use fuel_core_types::{
5    blockchain::{
6        primitives::DaBlockHeight,
7        SealedBlock,
8        SealedBlockHeader,
9    },
10    fuel_types::BlockHeight,
11    services::p2p::{
12        PeerId,
13        SourcePeer,
14        Transactions,
15    },
16};
17use std::ops::Range;
18
19/// Possible reasons to report a peer
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub enum PeerReportReason {
22    // Good
23    /// Successfully imported block
24    SuccessfulBlockImport,
25
26    // Bad
27    /// Did not receive advertised block headers
28    MissingBlockHeaders,
29    /// Report a peer for sending a bad block header
30    BadBlockHeader,
31    /// Did not receive advertised transactions
32    MissingTransactions,
33    /// Received invalid transactions
34    InvalidTransactions,
35}
36
37#[async_trait::async_trait]
38#[cfg_attr(any(test, feature = "benchmarking"), mockall::automock)]
39/// Port for communication with the network.
40pub trait PeerToPeerPort {
41    /// Stream of newly observed block heights.
42    fn height_stream(&self) -> BoxStream<BlockHeight>;
43
44    /// Request a range of sealed block headers from the network.
45    async fn get_sealed_block_headers(
46        &self,
47        block_height_range: Range<u32>,
48    ) -> anyhow::Result<SourcePeer<Option<Vec<SealedBlockHeader>>>>;
49
50    /// Request transactions from the network for the given block range
51    async fn get_transactions(
52        &self,
53        block_ids: Range<u32>,
54    ) -> anyhow::Result<SourcePeer<Option<Vec<Transactions>>>>;
55
56    /// Request transactions from the network for the given block range
57    /// and source peer.
58    async fn get_transactions_from_peer(
59        &self,
60        block_ids: SourcePeer<Range<u32>>,
61    ) -> anyhow::Result<Option<Vec<Transactions>>>;
62
63    /// Report a peer for some reason to modify their reputation.
64    fn report_peer(&self, peer: PeerId, report: PeerReportReason) -> anyhow::Result<()>;
65}
66
67#[cfg_attr(any(test, feature = "benchmarking"), mockall::automock)]
68/// Port for communication with the consensus service.
69pub trait ConsensusPort {
70    /// Check if the given sealed block header is valid.
71    fn check_sealed_header(&self, header: &SealedBlockHeader) -> anyhow::Result<bool>;
72    /// await for this DA height to be sync'd.
73    fn await_da_height(
74        &self,
75        da_height: &DaBlockHeight,
76    ) -> impl core::future::Future<Output = anyhow::Result<()>> + Send;
77}
78
79#[cfg_attr(any(test, feature = "benchmarking"), mockall::automock)]
80/// Port for communication with the block importer.
81pub trait BlockImporterPort {
82    /// Stream of newly committed block heights.
83    fn committed_height_stream(&self) -> BoxStream<BlockHeight>;
84
85    /// Execute the given sealed block
86    /// and commit it to the database.
87    fn execute_and_commit(
88        &self,
89        block: SealedBlock,
90    ) -> impl core::future::Future<Output = anyhow::Result<()>> + Send;
91}