fuel_core_txpool/
ports.rs

1use std::sync::Arc;
2
3use fuel_core_services::stream::BoxStream;
4use fuel_core_storage::{
5    PredicateStorageRequirements,
6    Result as StorageResult,
7};
8use fuel_core_types::{
9    blockchain::header::ConsensusParametersVersion,
10    entities::{
11        coins::coin::CompressedCoin,
12        relayer::message::Message,
13    },
14    fuel_tx::{
15        BlobId,
16        Bytes32,
17        ConsensusParameters,
18        ContractId,
19        Transaction,
20        TxId,
21        UtxoId,
22    },
23    fuel_types::Nonce,
24    services::{
25        block_importer::SharedImportResult,
26        p2p::{
27            GossipsubMessageAcceptance,
28            GossipsubMessageInfo,
29            NetworkData,
30            PeerId,
31        },
32        transaction_status::{
33            PreConfirmationStatus,
34            TransactionStatus,
35        },
36    },
37};
38use tokio::sync::broadcast;
39
40use crate::GasPrice;
41
42pub use fuel_core_storage::transactional::AtomicView;
43use fuel_core_types::services::transaction_status::statuses;
44
45pub trait TxStatusManager: Send + Sync + 'static {
46    fn status_update(&self, tx_id: TxId, tx_status: TransactionStatus);
47
48    fn preconfirmations_update_listener(
49        &self,
50    ) -> broadcast::Receiver<(TxId, PreConfirmationStatus)>;
51
52    fn squeezed_out_txs(&self, statuses: Vec<(TxId, statuses::SqueezedOut)>);
53}
54
55pub trait BlockImporter {
56    /// Wait until the next block is available
57    fn block_events(&self) -> BoxStream<SharedImportResult>;
58}
59
60/// Trait for getting the latest chain state info.
61pub trait ChainStateInfoProvider: Send + Sync + 'static {
62    /// Get latest consensus parameters.
63    fn latest_consensus_parameters(
64        &self,
65    ) -> (ConsensusParametersVersion, Arc<ConsensusParameters>);
66}
67
68pub trait TxPoolPersistentStorage:
69    Clone + PredicateStorageRequirements + Send + Sync + 'static
70{
71    /// Returns `true` if the transaction already is inside the storage.
72    fn contains_tx(&self, tx_id: &TxId) -> StorageResult<bool>;
73
74    /// Get the UTXO by its ID.
75    fn utxo(&self, utxo_id: &UtxoId) -> StorageResult<Option<CompressedCoin>>;
76
77    /// Check if the contract with the given ID exists.
78    fn contract_exist(&self, contract_id: &ContractId) -> StorageResult<bool>;
79
80    /// Check if the blob with the given ID exists.
81    fn blob_exist(&self, blob_id: &BlobId) -> StorageResult<bool>;
82
83    /// Get the message by its ID.
84    fn message(&self, message_id: &Nonce) -> StorageResult<Option<Message>>;
85}
86
87/// Trait for getting gas price for the Tx Pool code to look up the gas price for a given block height
88pub trait GasPriceProvider: Send + Sync + 'static {
89    /// Calculate gas price for the next block.
90    fn next_gas_price(&self) -> GasPrice;
91}
92
93#[derive(Debug, Clone, Copy, PartialEq)]
94pub enum WasmValidityError {
95    /// Wasm support is not enabled.
96    NotEnabled,
97    /// The supposedly-uploaded wasm was not found.
98    NotFound,
99    /// The uploaded bytecode was found but it's is not valid wasm.
100    NotValid,
101}
102
103pub trait WasmChecker: Send + Sync + 'static {
104    fn validate_uploaded_wasm(
105        &self,
106        wasm_root: &Bytes32,
107    ) -> Result<(), WasmValidityError>;
108}
109
110pub trait P2PSubscriptions {
111    type GossipedTransaction: NetworkData<Transaction>;
112
113    /// Creates a stream that is filled with the peer_id when they subscribe to
114    /// our transactions gossip.
115    fn subscribe_new_peers(&self) -> BoxStream<PeerId>;
116
117    /// Creates a stream of next transactions gossiped from the network.
118    fn gossiped_transaction_events(&self) -> BoxStream<Self::GossipedTransaction>;
119}
120
121pub trait NotifyP2P {
122    /// Gossip broadcast a transaction inserted via API.
123    fn broadcast_transaction(&self, transaction: Arc<Transaction>) -> anyhow::Result<()>;
124
125    /// Report the validity of a transaction received from the network.
126    fn notify_gossip_transaction_validity(
127        &self,
128        message_info: GossipsubMessageInfo,
129        validity: GossipsubMessageAcceptance,
130    ) -> anyhow::Result<()>;
131}
132
133#[async_trait::async_trait]
134pub trait P2PRequests: NotifyP2P + Send + Sync + 'static {
135    /// Asks the network to gather all tx ids of a specific peer
136    async fn request_tx_ids(&self, peer_id: PeerId) -> anyhow::Result<Vec<TxId>>;
137
138    /// Asks the network to gather specific transactions from a specific peer
139    async fn request_txs(
140        &self,
141        peer_id: PeerId,
142        tx_ids: Vec<TxId>,
143    ) -> anyhow::Result<Vec<Option<Transaction>>>;
144}