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
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<B = BlockBody> {
    /// Full block response (with transactions or ommers)
    Full(SealedBlock<alloy_consensus::Header, B>),
    /// The empty block response
    Empty(SealedHeader),
}

impl<B> BlockResponse<B> {
    /// Return the reference to the response header
    pub const fn header(&self) -> &SealedHeader {
        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<B: InMemorySize> InMemorySize for BlockResponse<B> {
    #[inline]
    fn size(&self) -> usize {
        match self {
            Self::Full(block) => SealedBlock::size(block),
            Self::Empty(header) => SealedHeader::size(header),
        }
    }
}