Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ethereum-fork) : enhance Head struct with methods #5656

Merged
merged 8 commits into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions crates/ethereum-forks/src/head.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloy_primitives::{BlockNumber, B256, U256};
use serde::{Deserialize, Serialize};
use std::fmt;

/// Describes the current head block.
///
Expand All @@ -22,3 +23,46 @@ pub struct Head {
/// The timestamp of the head block.
pub timestamp: u64,
}
impl Head {
/// Creates a new `Head` instance.
pub fn new(
number: BlockNumber,
hash: B256,
difficulty: U256,
total_difficulty: U256,
timestamp: u64,
) -> Self {
Self { number, hash, difficulty, total_difficulty, timestamp }
}

/// Updates the head block with new information.
pub fn update(
&mut self,
number: BlockNumber,
hash: B256,
difficulty: U256,
total_difficulty: U256,
timestamp: u64,
) {
self.number = number;
self.hash = hash;
self.difficulty = difficulty;
self.total_difficulty = total_difficulty;
self.timestamp = timestamp;
}

/// Checks if the head block is an empty block (i.e., has default values).
pub fn is_empty(&self) -> bool {
*self == Self::default()
}
}

impl fmt::Display for Head {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Head Block:\n Number: {}\n Hash: {:?}\n Difficulty: {:?}\n Total Difficulty: {:?}\n Timestamp: {}",
self.number, self.hash, self.difficulty, self.total_difficulty, self.timestamp
)
}
}
Loading