Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 2 additions & 26 deletions crates/consensus/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,9 @@ impl Header {
///
/// Returns `None` if `excess_blob_gas` is None.
///
/// If [`Self::target_blobs_per_block`] is [`Some`], uses EIP-7742 formula for calculating
/// the blob gas price, otherwise uses EIP-4844 formula.
///
/// See also [Self::next_block_excess_blob_gas]
pub fn next_block_blob_fee(&self) -> Option<u128> {
let next_block_excess_blob_gas = self.next_block_excess_blob_gas()?;

if self.target_blobs_per_block().is_none() {
Some(eip4844::calc_blob_gasprice(next_block_excess_blob_gas))
} else {
Some(eip7742::calc_blob_gasprice(next_block_excess_blob_gas))
}
Some(eip4844::calc_blob_gasprice(self.next_block_excess_blob_gas()?))
}

/// Calculate base fee for next block according to the EIP-1559 spec.
Expand All @@ -253,9 +244,6 @@ impl Header {
/// If [`Self::target_blobs_per_block`] is [`Some`], uses EIP-7742 formula for calculating
/// the excess blob gas, otherwise uses EIP-4844 formula.
///
/// Note: this function will return incorrect (unnormalized, lower) value at EIP-7742 activation
/// block. If this is undesired, consider using [`eip7742::calc_excess_blob_gas_at_transition`].
///
/// Returns a `None` if no excess blob gas is set, no EIP-4844 support
pub fn next_block_excess_blob_gas(&self) -> Option<u64> {
let excess_blob_gas = self.excess_blob_gas?;
Expand Down Expand Up @@ -691,9 +679,6 @@ pub trait BlockHeader {
/// If [`BlockHeader::target_blobs_per_block`] is [`Some`], uses EIP-7742 formula for
/// calculating the excess blob gas, otherwise uses EIP-4844 formula.
///
/// Note: this function will return incorrect (unnormalized, lower) value at EIP-7742 activation
/// block. If this is undesired, consider using [`eip7742::calc_excess_blob_gas_at_transition`].
///
/// Returns a `None` if no excess blob gas is set, no EIP-4844 support
fn next_block_excess_blob_gas(&self) -> Option<u64> {
let excess_blob_gas = self.excess_blob_gas()?;
Expand All @@ -715,18 +700,9 @@ pub trait BlockHeader {
///
/// Returns `None` if `excess_blob_gas` is None.
///
/// If this header has `target_blobs_per_block` set, uses EIP-7742 formula for calculating
/// the blob gas price, otherwise uses EIP-4844 formula.
///
/// See also [BlockHeader::next_block_excess_blob_gas]
fn next_block_blob_fee(&self) -> Option<u128> {
let next_block_excess_blob_gas = self.next_block_excess_blob_gas()?;

if self.target_blobs_per_block().is_none() {
Some(eip4844::calc_blob_gasprice(next_block_excess_blob_gas))
} else {
Some(eip7742::calc_blob_gasprice(next_block_excess_blob_gas))
}
Some(eip4844::calc_blob_gasprice(self.next_block_excess_blob_gas()?))
}

/// Calculate base fee for next block according to the EIP-1559 spec.
Expand Down
56 changes: 5 additions & 51 deletions crates/eips/src/eip7742.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,18 @@
//! Contains constants and utility functions for [EIP-7742](https://eips.ethereum.org/EIPS/eip-7742)

use crate::eip4844::{self, fake_exponential, BLOB_TX_MIN_BLOB_GASPRICE, DATA_GAS_PER_BLOB};

/// Controls the update rate of the blob base fee based on `target_blobs_per_block`.
pub const BLOB_BASE_FEE_UPDATE_FRACTION_PER_TARGET_BLOB: u128 = 1112825;

/// Controls the update rate of the blob base fee based on `target_blobs_per_block`.
pub const EXCESS_BLOB_GAS_NORMALIZATION_TARGET: u64 = 128;

/// Same as [`eip4844::BLOB_GASPRICE_UPDATE_FRACTION`], but normalized for the target of 128
/// blobs.
pub const BLOB_BASE_FEE_UPDATE_FRACTION_NORMALIZED: u128 =
BLOB_BASE_FEE_UPDATE_FRACTION_PER_TARGET_BLOB * EXCESS_BLOB_GAS_NORMALIZATION_TARGET as u128;

/// Calculates the `excess_blob_gas` for the header of the block enabling EIP-7742.
///
/// Normalizes the parent's excess blob gas as per EIP-7742.
#[inline]
pub const fn calc_excess_blob_gas_at_transition(
parent_excess_blob_gas: u64,
parent_blob_gas_used: u64,
) -> u64 {
let normalized_parent_excess_blob_gas = parent_excess_blob_gas
* EXCESS_BLOB_GAS_NORMALIZATION_TARGET
/ eip4844::TARGET_BLOBS_PER_BLOCK;

calc_excess_blob_gas(
normalized_parent_excess_blob_gas,
parent_blob_gas_used,
eip4844::TARGET_BLOBS_PER_BLOCK,
)
}
use crate::eip4844;

/// Calculates the `excess_blob_gas` from the parent header's `blob_gas_used`, `excess_blob_gas` and
/// `target_blobs_per_block`.
///
/// Note: this function assumes that the parent block's excess blob gas is normalized as per
/// EIP-7742.
/// Similar to [`eip4844::calc_excess_blob_gas`], but uses the `target_blobs_per_block` to calculate
/// the target gas usage.
#[inline]
pub const fn calc_excess_blob_gas(
parent_excess_blob_gas: u64,
parent_blob_gas_used: u64,
parent_target_blobs_per_block: u64,
) -> u64 {
let normalized_blob_gas_used =
parent_blob_gas_used * EXCESS_BLOB_GAS_NORMALIZATION_TARGET / parent_target_blobs_per_block;
let normalized_target_blob_gas = DATA_GAS_PER_BLOB * EXCESS_BLOB_GAS_NORMALIZATION_TARGET;

(parent_excess_blob_gas + normalized_blob_gas_used).saturating_sub(normalized_target_blob_gas)
}

/// Calculates the blob gas price from the header's excess blob gas field.
///
/// Similar to [crate::eip4844::calc_blob_gasprice], but adjusts the update rate based on
/// `target_blobs_per_block`.
#[inline]
pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u128 {
fake_exponential(
BLOB_TX_MIN_BLOB_GASPRICE,
excess_blob_gas as u128,
BLOB_BASE_FEE_UPDATE_FRACTION_NORMALIZED,
)
(parent_excess_blob_gas + parent_blob_gas_used)
.saturating_sub(parent_target_blobs_per_block * eip4844::DATA_GAS_PER_BLOB)
}