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
use super::model::{
    BlockHeight,
    FuelBlock,
    SealedFuelBlock,
};
use crate::{
    common::fuel_tx::Transaction,
    model::ConsensusVote,
};
use async_trait::async_trait;
use std::{
    fmt::Debug,
    sync::Arc,
};
use tokio::sync::oneshot;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum TransactionBroadcast {
    NewTransaction(Transaction),
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ConsensusBroadcast {
    NewVote(ConsensusVote),
}

#[derive(Debug, Clone)]
pub enum BlockBroadcast {
    /// fuel block without consensus data
    NewBlock(FuelBlock),
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum GossipsubMessageAcceptance {
    Accept,
    Reject,
    Ignore,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GossipsubMessageInfo {
    pub message_id: Vec<u8>,
    pub peer_id: Vec<u8>,
}

impl<T> From<&GossipData<T>> for GossipsubMessageInfo {
    fn from(gossip_data: &GossipData<T>) -> Self {
        Self {
            message_id: gossip_data.message_id.clone(),
            peer_id: gossip_data.peer_id.clone(),
        }
    }
}

#[derive(Debug)]
pub enum P2pRequestEvent {
    RequestBlock {
        height: BlockHeight,
        response: oneshot::Sender<SealedFuelBlock>,
    },
    BroadcastNewTransaction {
        transaction: Arc<Transaction>,
    },
    BroadcastNewBlock {
        block: Arc<FuelBlock>,
    },
    BroadcastConsensusVote {
        vote: Arc<ConsensusVote>,
    },
    GossipsubMessageReport {
        message: GossipsubMessageInfo,
        acceptance: GossipsubMessageAcceptance,
    },
    Stop,
}

#[derive(Debug, Clone)]
pub struct GossipData<T> {
    pub data: Option<T>,
    pub peer_id: Vec<u8>,
    pub message_id: Vec<u8>,
}

pub type ConsensusGossipData = GossipData<ConsensusBroadcast>;
pub type TransactionGossipData = GossipData<TransactionBroadcast>;
pub type BlockGossipData = GossipData<BlockBroadcast>;

impl<T> GossipData<T> {
    pub fn new(
        data: T,
        peer_id: impl Into<Vec<u8>>,
        message_id: impl Into<Vec<u8>>,
    ) -> Self {
        Self {
            data: Some(data),
            peer_id: peer_id.into(),
            message_id: message_id.into(),
        }
    }
}

pub trait NetworkData<T>: Debug + Send {
    fn take_data(&mut self) -> Option<T>;
}

impl<T: Debug + Send + 'static> NetworkData<T> for GossipData<T> {
    fn take_data(&mut self) -> Option<T> {
        self.data.take()
    }
}

#[async_trait]
pub trait P2pDb: Send + Sync {
    async fn get_sealed_block(&self, height: BlockHeight)
        -> Option<Arc<SealedFuelBlock>>;
}