reth_network_p2p/bodies/
response.rs

1use alloy_consensus::BlockHeader;
2use alloy_primitives::{BlockNumber, U256};
3use reth_primitives::{BlockBody, SealedBlock, SealedHeader};
4use reth_primitives_traits::InMemorySize;
5
6/// The block response
7#[derive(PartialEq, Eq, Debug, Clone)]
8pub enum BlockResponse<H, B = BlockBody> {
9    /// Full block response (with transactions or ommers)
10    Full(SealedBlock<H, B>),
11    /// The empty block response
12    Empty(SealedHeader<H>),
13}
14
15impl<H, B> BlockResponse<H, B>
16where
17    H: BlockHeader,
18{
19    /// Return the reference to the response header
20    pub const fn header(&self) -> &SealedHeader<H> {
21        match self {
22            Self::Full(block) => &block.header,
23            Self::Empty(header) => header,
24        }
25    }
26
27    /// Return the block number
28    pub fn block_number(&self) -> BlockNumber {
29        self.header().number()
30    }
31
32    /// Return the reference to the response header
33    pub fn difficulty(&self) -> U256 {
34        match self {
35            Self::Full(block) => block.difficulty(),
36            Self::Empty(header) => header.difficulty(),
37        }
38    }
39
40    /// Return the reference to the response body
41    pub fn into_body(self) -> Option<B> {
42        match self {
43            Self::Full(block) => Some(block.body),
44            Self::Empty(_) => None,
45        }
46    }
47}
48
49impl<H: InMemorySize, B: InMemorySize> InMemorySize for BlockResponse<H, B> {
50    #[inline]
51    fn size(&self) -> usize {
52        match self {
53            Self::Full(block) => SealedBlock::size(block),
54            Self::Empty(header) => SealedHeader::size(header),
55        }
56    }
57}