reth_ethereum_forks/
head.rs

1use alloy_primitives::{BlockNumber, B256, U256};
2use core::fmt;
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// Describes the current head block.
7///
8/// The head block is the highest fully synced block.
9///
10/// Note: This is a slimmed down version of Header, primarily for communicating the highest block
11/// with the P2P network and the RPC.
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
14pub struct Head {
15    /// The number of the head block.
16    pub number: BlockNumber,
17    /// The hash of the head block.
18    pub hash: B256,
19    /// The difficulty of the head block.
20    pub difficulty: U256,
21    /// The total difficulty at the head block.
22    pub total_difficulty: U256,
23    /// The timestamp of the head block.
24    pub timestamp: u64,
25}
26impl Head {
27    /// Creates a new `Head` instance.
28    pub const fn new(
29        number: BlockNumber,
30        hash: B256,
31        difficulty: U256,
32        total_difficulty: U256,
33        timestamp: u64,
34    ) -> Self {
35        Self { number, hash, difficulty, total_difficulty, timestamp }
36    }
37
38    /// Updates the head block with new information.
39    pub fn update(
40        &mut self,
41        number: BlockNumber,
42        hash: B256,
43        difficulty: U256,
44        total_difficulty: U256,
45        timestamp: u64,
46    ) {
47        *self = Self { number, hash, difficulty, total_difficulty, timestamp };
48    }
49
50    /// Checks if the head block is an empty block (i.e., has default values).
51    pub fn is_empty(&self) -> bool {
52        *self == Self::default()
53    }
54}
55
56impl fmt::Display for Head {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        write!(
59            f,
60            "Head Block:\n Number: {}\n Hash: {}\n Difficulty: {:?}\n Total Difficulty: {:?}\n Timestamp: {}",
61            self.number, self.hash, self.difficulty, self.total_difficulty, self.timestamp
62        )
63    }
64}