Skip to main content

nimiq_consensus/sync/live/block_queue/
mod.rs

1//! Block queue auxiliary structures.
2
3use futures::stream::BoxStream;
4use nimiq_block::Block;
5use nimiq_network_interface::network::{MsgAcceptance, Network, PubsubId};
6pub use proxy::BlockQueueProxy as BlockQueue;
7use tokio::sync::oneshot;
8
9use crate::{
10    consensus::ResolveBlockError,
11    messages::{BlockBodyTopic, BlockHeaderTopic},
12};
13
14mod assembler;
15pub mod block_request_component;
16pub mod live_sync;
17mod proxy;
18mod queue;
19
20pub type BlockStream<N> = BoxStream<'static, BlockAndSource<N>>;
21pub type GossipSubBlockStream<N> = BoxStream<'static, (Block, <N as Network>::PubsubId)>;
22
23pub type BlockAndSource<N> = (Block, BlockSource<N>);
24
25pub type ResolveBlockSender<N> = oneshot::Sender<Result<Block, ResolveBlockError<N>>>;
26
27/// Represents the block queue events.
28pub enum QueuedBlock<N: Network> {
29    Head(BlockAndSource<N>),
30    Buffered(Vec<BlockAndSource<N>>),
31    Missing(Vec<BlockAndSource<N>>),
32    TooFarAhead(N::PeerId),
33    TooFarBehind(N::PeerId),
34}
35
36/// Describes the origin of a block
37#[derive(Debug)]
38pub enum BlockSource<N: Network> {
39    Announced {
40        header_id: N::PubsubId,
41        body_id: Option<N::PubsubId>,
42    },
43    Requested {
44        id: N::PeerId,
45    },
46}
47
48impl<N: Network> BlockSource<N> {
49    /// Creates a `BlockSource` for an announced block
50    pub fn announced(header_id: N::PubsubId, body_id: Option<N::PubsubId>) -> Self {
51        Self::Announced { header_id, body_id }
52    }
53
54    /// From a specific peer
55    pub fn requested(id: N::PeerId) -> Self {
56        Self::Requested { id }
57    }
58
59    /// Returns the peer ID associated with the `BlockSource`
60    pub fn peer_id(&self) -> N::PeerId {
61        match self {
62            BlockSource::Announced { header_id, .. } => header_id.propagation_source(),
63            BlockSource::Requested { id } => *id,
64        }
65    }
66
67    /// Returns true if the block was received via announcement.
68    pub fn is_announced(&self) -> bool {
69        matches!(self, BlockSource::Announced { .. })
70    }
71
72    /// Returns true if the block was received as a direct response to a request.
73    pub fn is_requested(&self) -> bool {
74        matches!(self, BlockSource::Requested { .. })
75    }
76
77    /// Marks the block as accepted.
78    pub fn accept_block(&self, network: &N) {
79        self.validate_block(network, MsgAcceptance::Accept);
80    }
81
82    /// Marks the block as rejected
83    pub fn reject_block(&self, network: &N) {
84        self.validate_block(network, MsgAcceptance::Reject);
85    }
86
87    /// Marks the block as ignored.
88    pub fn ignore_block(&self, network: &N) {
89        self.validate_block(network, MsgAcceptance::Ignore);
90    }
91
92    /// Applies the given acceptance decision to the associated network messages
93    /// for blocks received via gossip.
94    pub fn validate_block(&self, network: &N, acceptance: MsgAcceptance) {
95        match self {
96            BlockSource::Announced { header_id, body_id } => {
97                network.validate_message::<BlockHeaderTopic>(header_id.clone(), acceptance);
98                if let Some(body_id) = body_id {
99                    network.validate_message::<BlockBodyTopic>(body_id.clone(), acceptance);
100                }
101            }
102            BlockSource::Requested { .. } => {}
103        }
104    }
105}
106
107impl<N: Network> Clone for BlockSource<N> {
108    fn clone(&self) -> Self {
109        match self {
110            BlockSource::Requested { id } => BlockSource::Requested { id: *id },
111            BlockSource::Announced { header_id, body_id } => BlockSource::Announced {
112                header_id: header_id.clone(),
113                body_id: body_id.clone(),
114            },
115        }
116    }
117}