Skip to content

Commit 67576f9

Browse files
authored
fix: adjust EIP-7742 to latest spec (#1713)
1 parent eb72def commit 67576f9

File tree

2 files changed

+7
-77
lines changed

2 files changed

+7
-77
lines changed

crates/consensus/src/block/header.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,9 @@ impl Header {
221221
///
222222
/// Returns `None` if `excess_blob_gas` is None.
223223
///
224-
/// If [`Self::target_blobs_per_block`] is [`Some`], uses EIP-7742 formula for calculating
225-
/// the blob gas price, otherwise uses EIP-4844 formula.
226-
///
227224
/// See also [Self::next_block_excess_blob_gas]
228225
pub fn next_block_blob_fee(&self) -> Option<u128> {
229-
let next_block_excess_blob_gas = self.next_block_excess_blob_gas()?;
230-
231-
if self.target_blobs_per_block().is_none() {
232-
Some(eip4844::calc_blob_gasprice(next_block_excess_blob_gas))
233-
} else {
234-
Some(eip7742::calc_blob_gasprice(next_block_excess_blob_gas))
235-
}
226+
Some(eip4844::calc_blob_gasprice(self.next_block_excess_blob_gas()?))
236227
}
237228

238229
/// Calculate base fee for next block according to the EIP-1559 spec.
@@ -253,9 +244,6 @@ impl Header {
253244
/// If [`Self::target_blobs_per_block`] is [`Some`], uses EIP-7742 formula for calculating
254245
/// the excess blob gas, otherwise uses EIP-4844 formula.
255246
///
256-
/// Note: this function will return incorrect (unnormalized, lower) value at EIP-7742 activation
257-
/// block. If this is undesired, consider using [`eip7742::calc_excess_blob_gas_at_transition`].
258-
///
259247
/// Returns a `None` if no excess blob gas is set, no EIP-4844 support
260248
pub fn next_block_excess_blob_gas(&self) -> Option<u64> {
261249
let excess_blob_gas = self.excess_blob_gas?;
@@ -691,9 +679,6 @@ pub trait BlockHeader {
691679
/// If [`BlockHeader::target_blobs_per_block`] is [`Some`], uses EIP-7742 formula for
692680
/// calculating the excess blob gas, otherwise uses EIP-4844 formula.
693681
///
694-
/// Note: this function will return incorrect (unnormalized, lower) value at EIP-7742 activation
695-
/// block. If this is undesired, consider using [`eip7742::calc_excess_blob_gas_at_transition`].
696-
///
697682
/// Returns a `None` if no excess blob gas is set, no EIP-4844 support
698683
fn next_block_excess_blob_gas(&self) -> Option<u64> {
699684
let excess_blob_gas = self.excess_blob_gas()?;
@@ -715,18 +700,9 @@ pub trait BlockHeader {
715700
///
716701
/// Returns `None` if `excess_blob_gas` is None.
717702
///
718-
/// If this header has `target_blobs_per_block` set, uses EIP-7742 formula for calculating
719-
/// the blob gas price, otherwise uses EIP-4844 formula.
720-
///
721703
/// See also [BlockHeader::next_block_excess_blob_gas]
722704
fn next_block_blob_fee(&self) -> Option<u128> {
723-
let next_block_excess_blob_gas = self.next_block_excess_blob_gas()?;
724-
725-
if self.target_blobs_per_block().is_none() {
726-
Some(eip4844::calc_blob_gasprice(next_block_excess_blob_gas))
727-
} else {
728-
Some(eip7742::calc_blob_gasprice(next_block_excess_blob_gas))
729-
}
705+
Some(eip4844::calc_blob_gasprice(self.next_block_excess_blob_gas()?))
730706
}
731707

732708
/// Calculate base fee for next block according to the EIP-1559 spec.

crates/eips/src/eip7742.rs

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,18 @@
11
//! Contains constants and utility functions for [EIP-7742](https://eips.ethereum.org/EIPS/eip-7742)
22
3-
use crate::eip4844::{self, fake_exponential, BLOB_TX_MIN_BLOB_GASPRICE, DATA_GAS_PER_BLOB};
4-
5-
/// Controls the update rate of the blob base fee based on `target_blobs_per_block`.
6-
pub const BLOB_BASE_FEE_UPDATE_FRACTION_PER_TARGET_BLOB: u128 = 1112825;
7-
8-
/// Controls the update rate of the blob base fee based on `target_blobs_per_block`.
9-
pub const EXCESS_BLOB_GAS_NORMALIZATION_TARGET: u64 = 128;
10-
11-
/// Same as [`eip4844::BLOB_GASPRICE_UPDATE_FRACTION`], but normalized for the target of 128
12-
/// blobs.
13-
pub const BLOB_BASE_FEE_UPDATE_FRACTION_NORMALIZED: u128 =
14-
BLOB_BASE_FEE_UPDATE_FRACTION_PER_TARGET_BLOB * EXCESS_BLOB_GAS_NORMALIZATION_TARGET as u128;
15-
16-
/// Calculates the `excess_blob_gas` for the header of the block enabling EIP-7742.
17-
///
18-
/// Normalizes the parent's excess blob gas as per EIP-7742.
19-
#[inline]
20-
pub const fn calc_excess_blob_gas_at_transition(
21-
parent_excess_blob_gas: u64,
22-
parent_blob_gas_used: u64,
23-
) -> u64 {
24-
let normalized_parent_excess_blob_gas = parent_excess_blob_gas
25-
* EXCESS_BLOB_GAS_NORMALIZATION_TARGET
26-
/ eip4844::TARGET_BLOBS_PER_BLOCK;
27-
28-
calc_excess_blob_gas(
29-
normalized_parent_excess_blob_gas,
30-
parent_blob_gas_used,
31-
eip4844::TARGET_BLOBS_PER_BLOCK,
32-
)
33-
}
3+
use crate::eip4844;
344

355
/// Calculates the `excess_blob_gas` from the parent header's `blob_gas_used`, `excess_blob_gas` and
366
/// `target_blobs_per_block`.
377
///
38-
/// Note: this function assumes that the parent block's excess blob gas is normalized as per
39-
/// EIP-7742.
8+
/// Similar to [`eip4844::calc_excess_blob_gas`], but uses the `target_blobs_per_block` to calculate
9+
/// the target gas usage.
4010
#[inline]
4111
pub const fn calc_excess_blob_gas(
4212
parent_excess_blob_gas: u64,
4313
parent_blob_gas_used: u64,
4414
parent_target_blobs_per_block: u64,
4515
) -> u64 {
46-
let normalized_blob_gas_used =
47-
parent_blob_gas_used * EXCESS_BLOB_GAS_NORMALIZATION_TARGET / parent_target_blobs_per_block;
48-
let normalized_target_blob_gas = DATA_GAS_PER_BLOB * EXCESS_BLOB_GAS_NORMALIZATION_TARGET;
49-
50-
(parent_excess_blob_gas + normalized_blob_gas_used).saturating_sub(normalized_target_blob_gas)
51-
}
52-
53-
/// Calculates the blob gas price from the header's excess blob gas field.
54-
///
55-
/// Similar to [crate::eip4844::calc_blob_gasprice], but adjusts the update rate based on
56-
/// `target_blobs_per_block`.
57-
#[inline]
58-
pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u128 {
59-
fake_exponential(
60-
BLOB_TX_MIN_BLOB_GASPRICE,
61-
excess_blob_gas as u128,
62-
BLOB_BASE_FEE_UPDATE_FRACTION_NORMALIZED,
63-
)
16+
(parent_excess_blob_gas + parent_blob_gas_used)
17+
.saturating_sub(parent_target_blobs_per_block * eip4844::DATA_GAS_PER_BLOB)
6418
}

0 commit comments

Comments
 (0)