reth_network_p2p/bodies/
response.rs

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
use alloy_consensus::BlockHeader;
use alloy_primitives::{BlockNumber, U256};
use reth_primitives::{BlockBody, SealedBlock, SealedHeader};
use reth_primitives_traits::InMemorySize;

/// The block response
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum BlockResponse<H, B = BlockBody> {
    /// Full block response (with transactions or ommers)
    Full(SealedBlock<H, B>),
    /// The empty block response
    Empty(SealedHeader<H>),
}

impl<H, B> BlockResponse<H, B>
where
    H: BlockHeader,
{
    /// Return the reference to the response header
    pub const fn header(&self) -> &SealedHeader<H> {
        match self {
            Self::Full(block) => &block.header,
            Self::Empty(header) => header,
        }
    }

    /// Return the block number
    pub fn block_number(&self) -> BlockNumber {
        self.header().number()
    }

    /// Return the reference to the response header
    pub fn difficulty(&self) -> U256 {
        match self {
            Self::Full(block) => block.difficulty(),
            Self::Empty(header) => header.difficulty(),
        }
    }

    /// Return the reference to the response body
    pub fn into_body(self) -> Option<B> {
        match self {
            Self::Full(block) => Some(block.body),
            Self::Empty(_) => None,
        }
    }
}

impl<H: InMemorySize, B: InMemorySize> InMemorySize for BlockResponse<H, B> {
    #[inline]
    fn size(&self) -> usize {
        match self {
            Self::Full(block) => SealedBlock::size(block),
            Self::Empty(header) => SealedHeader::size(header),
        }
    }
}