fuel_core_interfaces/
p2p.rs

1use super::model::{
2    BlockHeight,
3    FuelBlock,
4    SealedFuelBlock,
5};
6use crate::{
7    common::fuel_tx::Transaction,
8    model::ConsensusVote,
9};
10use async_trait::async_trait;
11use std::{
12    fmt::Debug,
13    sync::Arc,
14};
15use tokio::sync::oneshot;
16
17#[derive(Debug, PartialEq, Eq, Clone)]
18pub enum TransactionBroadcast {
19    NewTransaction(Transaction),
20}
21
22#[derive(Debug, PartialEq, Eq, Clone)]
23pub enum ConsensusBroadcast {
24    NewVote(ConsensusVote),
25}
26
27#[derive(Debug, Clone)]
28pub enum BlockBroadcast {
29    /// fuel block without consensus data
30    NewBlock(FuelBlock),
31}
32
33#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
34pub enum GossipsubMessageAcceptance {
35    Accept,
36    Reject,
37    Ignore,
38}
39
40#[derive(Debug, Clone, Hash, PartialEq, Eq)]
41#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
42pub struct GossipsubMessageInfo {
43    pub message_id: Vec<u8>,
44    pub peer_id: Vec<u8>,
45}
46
47impl<T> From<&GossipData<T>> for GossipsubMessageInfo {
48    fn from(gossip_data: &GossipData<T>) -> Self {
49        Self {
50            message_id: gossip_data.message_id.clone(),
51            peer_id: gossip_data.peer_id.clone(),
52        }
53    }
54}
55
56#[derive(Debug)]
57pub enum P2pRequestEvent {
58    RequestBlock {
59        height: BlockHeight,
60        response: oneshot::Sender<SealedFuelBlock>,
61    },
62    BroadcastNewTransaction {
63        transaction: Arc<Transaction>,
64    },
65    BroadcastNewBlock {
66        block: Arc<FuelBlock>,
67    },
68    BroadcastConsensusVote {
69        vote: Arc<ConsensusVote>,
70    },
71    GossipsubMessageReport {
72        message: GossipsubMessageInfo,
73        acceptance: GossipsubMessageAcceptance,
74    },
75    Stop,
76}
77
78#[derive(Debug, Clone)]
79pub struct GossipData<T> {
80    pub data: Option<T>,
81    pub peer_id: Vec<u8>,
82    pub message_id: Vec<u8>,
83}
84
85pub type ConsensusGossipData = GossipData<ConsensusBroadcast>;
86pub type TransactionGossipData = GossipData<TransactionBroadcast>;
87pub type BlockGossipData = GossipData<BlockBroadcast>;
88
89impl<T> GossipData<T> {
90    pub fn new(
91        data: T,
92        peer_id: impl Into<Vec<u8>>,
93        message_id: impl Into<Vec<u8>>,
94    ) -> Self {
95        Self {
96            data: Some(data),
97            peer_id: peer_id.into(),
98            message_id: message_id.into(),
99        }
100    }
101}
102
103pub trait NetworkData<T>: Debug + Send {
104    fn take_data(&mut self) -> Option<T>;
105}
106
107impl<T: Debug + Send + 'static> NetworkData<T> for GossipData<T> {
108    fn take_data(&mut self) -> Option<T> {
109        self.data.take()
110    }
111}
112
113#[async_trait]
114pub trait P2pDb: Send + Sync {
115    async fn get_sealed_block(&self, height: BlockHeight)
116        -> Option<Arc<SealedFuelBlock>>;
117}