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

refactor : Simplify is_some in Header #5969

Merged
merged 2 commits into from
Jan 8, 2024
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
64 changes: 46 additions & 18 deletions crates/primitives/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,34 @@ impl Header {
self.extra_data.len() // extra data
}

/// Checks if `blob_gas_used` is present in the header.
///
/// Returns `true` if `blob_gas_used` is `Some`, otherwise `false`.
fn has_blob_gas_used(&self) -> bool {
self.blob_gas_used.is_some()
}

/// Checks if `excess_blob_gas` is present in the header.
///
/// Returns `true` if `excess_blob_gas` is `Some`, otherwise `false`.
fn has_excess_blob_gas(&self) -> bool {
self.excess_blob_gas.is_some()
}

// Checks if `withdrawals_root` is present in the header.
///
/// Returns `true` if `withdrawals_root` is `Some`, otherwise `false`.
fn has_withdrawals_root(&self) -> bool {
self.withdrawals_root.is_some()
}

/// Checks if `parent_beacon_block_root` is present in the header.
///
/// Returns `true` if `parent_beacon_block_root` is `Some`, otherwise `false`.
fn has_parent_beacon_block_root(&self) -> bool {
self.parent_beacon_block_root.is_some()
}

fn header_payload_length(&self) -> usize {
let mut length = 0;
length += self.parent_hash.length(); // Hash of the previous block.
Expand All @@ -303,10 +331,10 @@ impl Header {
if let Some(base_fee) = self.base_fee_per_gas {
// Adding base fee length if it exists.
length += U256::from(base_fee).length();
} else if self.withdrawals_root.is_some() ||
self.blob_gas_used.is_some() ||
self.excess_blob_gas.is_some() ||
self.parent_beacon_block_root.is_some()
} else if self.has_withdrawals_root() ||
self.has_blob_gas_used() ||
self.has_excess_blob_gas() ||
self.has_parent_beacon_block_root()
{
// Placeholder code for empty lists.
length += 1;
Expand All @@ -315,9 +343,9 @@ impl Header {
if let Some(root) = self.withdrawals_root {
// Adding withdrawals_root length if it exists.
length += root.length();
} else if self.blob_gas_used.is_some() ||
self.excess_blob_gas.is_some() ||
self.parent_beacon_block_root.is_some()
} else if self.has_blob_gas_used() ||
self.has_excess_blob_gas() ||
self.has_parent_beacon_block_root()
{
// Placeholder code for a missing string value.
length += 1;
Expand All @@ -326,15 +354,15 @@ impl Header {
if let Some(blob_gas_used) = self.blob_gas_used {
// Adding blob_gas_used length if it exists.
length += U256::from(blob_gas_used).length();
} else if self.excess_blob_gas.is_some() || self.parent_beacon_block_root.is_some() {
} else if self.has_excess_blob_gas() || self.has_parent_beacon_block_root() {
// Placeholder code for empty lists.
length += 1;
}

if let Some(excess_blob_gas) = self.excess_blob_gas {
// Adding excess_blob_gas length if it exists.
length += U256::from(excess_blob_gas).length();
} else if self.parent_beacon_block_root.is_some() {
} else if self.has_parent_beacon_block_root() {
// Placeholder code for empty lists.
length += 1;
}
Expand Down Expand Up @@ -383,10 +411,10 @@ impl Encodable for Header {
// but withdrawals root is present.
if let Some(ref base_fee) = self.base_fee_per_gas {
U256::from(*base_fee).encode(out);
} else if self.withdrawals_root.is_some() ||
self.blob_gas_used.is_some() ||
self.excess_blob_gas.is_some() ||
self.parent_beacon_block_root.is_some()
} else if self.has_withdrawals_root() ||
self.has_blob_gas_used() ||
self.has_excess_blob_gas() ||
self.has_parent_beacon_block_root()
{
out.put_u8(EMPTY_LIST_CODE);
}
Expand All @@ -395,9 +423,9 @@ impl Encodable for Header {
// but blob gas used is present.
if let Some(ref root) = self.withdrawals_root {
root.encode(out);
} else if self.blob_gas_used.is_some() ||
self.excess_blob_gas.is_some() ||
self.parent_beacon_block_root.is_some()
} else if self.has_blob_gas_used() ||
self.has_excess_blob_gas() ||
self.has_parent_beacon_block_root()
{
out.put_u8(EMPTY_STRING_CODE);
}
Expand All @@ -406,15 +434,15 @@ impl Encodable for Header {
// but excess blob gas is present.
if let Some(ref blob_gas_used) = self.blob_gas_used {
U256::from(*blob_gas_used).encode(out);
} else if self.excess_blob_gas.is_some() || self.parent_beacon_block_root.is_some() {
} else if self.has_excess_blob_gas() || self.has_parent_beacon_block_root() {
out.put_u8(EMPTY_LIST_CODE);
}

// Encode excess blob gas. Put empty list if excess blob gas is missing,
// but parent beacon block root is present.
if let Some(ref excess_blob_gas) = self.excess_blob_gas {
U256::from(*excess_blob_gas).encode(out);
} else if self.parent_beacon_block_root.is_some() {
} else if self.has_parent_beacon_block_root() {
out.put_u8(EMPTY_LIST_CODE);
}

Expand Down
Loading