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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions crates/context/interface/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
//! [`Block`] trait is used to retrieve block information required for execution.
pub mod blob;

pub use blob::{
calc_blob_gasprice, calc_excess_blob_gas, calc_excess_blob_gas_osaka, BlobExcessGasAndPrice,
};
pub use blob::{calc_blob_gasprice, BlobExcessGasAndPrice};

use auto_impl::auto_impl;
use primitives::{Address, B256, U256};
Expand Down Expand Up @@ -47,8 +45,6 @@ pub trait Block {
fn prevrandao(&self) -> Option<B256>;

/// Excess blob gas and blob gasprice.
/// See also [`calc_excess_blob_gas`]
/// and [`calc_blob_gasprice`].
///
/// Incorporated as part of the Cancun upgrade via [EIP-4844].
///
Expand Down
184 changes: 3 additions & 181 deletions crates/context/interface/src/block/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
//!
//! See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers).
//!
//! [`calc_blob_gasprice`] and [`calc_excess_blob_gas`] are used to calculate the blob gas price and
//! excess blob gas.
//!
//! [`BlobExcessGasAndPrice`] is used to store the blob gas price and excess blob gas.s
use primitives::{
eip4844::{GAS_PER_BLOB, MIN_BLOB_GASPRICE},
eip7918,
};
use primitives::eip4844::MIN_BLOB_GASPRICE;

/// Structure holding block blob excess gas and it calculates blob fee
///
Expand All @@ -31,102 +26,14 @@ pub struct BlobExcessGasAndPrice {
impl BlobExcessGasAndPrice {
/// Creates a new instance by calculating the blob gas price with [`calc_blob_gasprice`].
///
/// `excess_blob_gas` is the excess blob gas of the block, it can be calculated with [`calc_excess_blob_gas`].
/// `excess_blob_gas` is the excess blob gas of the block, it can be calculated with `calc_excess_blob_gas` function from alloy-eips.
pub fn new(excess_blob_gas: u64, blob_base_fee_update_fraction: u64) -> Self {
let blob_gasprice = calc_blob_gasprice(excess_blob_gas, blob_base_fee_update_fraction);
Self {
excess_blob_gas,
blob_gasprice,
}
}

/// Calculate this block excess gas and price from the parent excess gas and gas used
/// and the target blob gas per block.
///
/// These fields will be used to calculate `excess_blob_gas` with [`calc_excess_blob_gas`] func.
#[deprecated(
note = "Use `calc_excess_blob_gas` and `BlobExcessGasAndPrice::new` instead. Only works for forks before Osaka."
)]
pub fn from_parent_and_target(
parent_excess_blob_gas: u64,
parent_blob_gas_used: u64,
parent_target_blob_gas_per_block: u64,
blob_base_fee_update_fraction: u64,
) -> Self {
Self::new(
calc_excess_blob_gas(
parent_excess_blob_gas,
parent_blob_gas_used,
parent_target_blob_gas_per_block,
),
blob_base_fee_update_fraction,
)
}
}

/// Calculates the `excess_blob_gas` from the parent header's `blob_gas_used` and `excess_blob_gas`.
/// Uses [`calc_excess_blob_gas_osaka`] internally.
#[inline]
pub fn calc_excess_blob_gas(
parent_excess_blob_gas: u64,
parent_blob_gas_used: u64,
parent_target_blob_gas_per_block: u64,
) -> u64 {
calc_excess_blob_gas_osaka(
parent_excess_blob_gas,
parent_blob_gas_used,
parent_target_blob_gas_per_block,
false,
0,
0,
0,
0,
0,
)
}

/// Calculates the `excess_blob_gas` from the parent header's `blob_gas_used` and `excess_blob_gas`.
///
/// See also [the EIP-4844 helpers]<https://eips.ethereum.org/EIPS/eip-4844#helpers>
/// (`calc_excess_blob_gas`).
///
/// [EIP-7918: Blob base fee bounded by execution cost](https://eips.ethereum.org/EIPS/eip-7918)
///
/// `blob_base_cost` is introduced in EIP-7918 in Osaka fork. All fields after is_osaka input are not needed before Osaka.
#[allow(clippy::too_many_arguments)]
#[inline]
pub fn calc_excess_blob_gas_osaka(
parent_excess_blob_gas: u64,
parent_blob_gas_used: u64,
parent_target_blob_gas_per_block: u64,
is_osaka: bool,
parent_base_fee_per_gas: u64,
parent_blob_base_fee_per_gas: u64,
parent_blob_base_fee_update_fraction: u64,
max_blob_count: u64,
target_blob_count: u64,
) -> u64 {
let excess_and_used = parent_excess_blob_gas.saturating_add(parent_blob_gas_used);

if is_osaka {
if excess_and_used < parent_target_blob_gas_per_block {
return 0;
}

if (eip7918::BLOB_BASE_COST.saturating_mul(parent_base_fee_per_gas) as u128)
> (GAS_PER_BLOB as u128).saturating_mul(get_base_fee_per_blob_gas(
parent_blob_base_fee_per_gas,
parent_blob_base_fee_update_fraction,
))
{
return excess_and_used.saturating_add(
parent_blob_gas_used.saturating_mul(max_blob_count - target_blob_count)
/ max_blob_count,
);
}
}

excess_and_used.saturating_sub(parent_target_blob_gas_per_block)
}

/// Calculates the blob gas price from the header's excess blob gas field.
Expand Down Expand Up @@ -181,92 +88,7 @@ pub fn fake_exponential(factor: u64, numerator: u64, denominator: u64) -> u128 {
#[cfg(test)]
mod tests {
use super::*;
use primitives::eip4844::{
self, BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN, GAS_PER_BLOB,
TARGET_BLOB_GAS_PER_BLOCK_CANCUN as TARGET_BLOB_GAS_PER_BLOCK,
};

// https://github.com/ethereum/go-ethereum/blob/28857080d732857030eda80c69b9ba2c8926f221/consensus/misc/eip4844/eip4844_test.go#L27
#[test]
fn test_calc_excess_blob_gas() {
for t @ &(excess, blobs, expected) in &[
// The excess blob gas should not increase from zero if the used blob
// slots are below - or equal - to the target.
(0, 0, 0),
(0, 1, 0),
(0, TARGET_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB, 0),
// If the target blob gas is exceeded, the excessBlobGas should increase
// by however much it was overshot
(
0,
(TARGET_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB) + 1,
GAS_PER_BLOB,
),
(
1,
(TARGET_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB) + 1,
GAS_PER_BLOB + 1,
),
(
1,
(TARGET_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB) + 2,
2 * GAS_PER_BLOB + 1,
),
// The excess blob gas should decrease by however much the target was
// under-shot, capped at zero.
(
TARGET_BLOB_GAS_PER_BLOCK,
TARGET_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB,
TARGET_BLOB_GAS_PER_BLOCK,
),
(
TARGET_BLOB_GAS_PER_BLOCK,
(TARGET_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB) - 1,
TARGET_BLOB_GAS_PER_BLOCK - GAS_PER_BLOB,
),
(
TARGET_BLOB_GAS_PER_BLOCK,
(TARGET_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB) - 2,
TARGET_BLOB_GAS_PER_BLOCK - (2 * GAS_PER_BLOB),
),
(
GAS_PER_BLOB - 1,
(TARGET_BLOB_GAS_PER_BLOCK / GAS_PER_BLOB) - 1,
0,
),
] {
let actual = calc_excess_blob_gas(
excess,
blobs * GAS_PER_BLOB,
eip4844::TARGET_BLOB_GAS_PER_BLOCK_CANCUN,
);
assert_eq!(actual, expected, "test: {t:?}");
}
}

// https://github.com/ethereum/go-ethereum/blob/28857080d732857030eda80c69b9ba2c8926f221/consensus/misc/eip4844/eip4844_test.go#L60
#[test]
fn test_calc_blob_fee_cancun() {
let blob_fee_vectors = &[
(0, 1),
(2314057, 1),
(2314058, 2),
(10 * 1024 * 1024, 23),
// `calc_blob_gasprice` approximates `e ** (excess_blob_gas / BLOB_BASE_FEE_UPDATE_FRACTION)` using Taylor expansion
//
// to roughly find where boundaries will be hit:
// 2 ** bits = e ** (excess_blob_gas / BLOB_BASE_FEE_UPDATE_FRACTION)
// excess_blob_gas = ln(2 ** bits) * BLOB_BASE_FEE_UPDATE_FRACTION
(148099578, 18446739238971471609), // output is just below the overflow
(148099579, 18446744762204311910), // output is just after the overflow
(161087488, 902580055246494526580),
];

for &(excess, expected) in blob_fee_vectors {
let actual = calc_blob_gasprice(excess, BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN);
assert_eq!(actual, expected, "test: {excess}");
}
}
use primitives::eip4844::BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN;

// https://github.com/ethereum/go-ethereum/blob/28857080d732857030eda80c69b9ba2c8926f221/consensus/misc/eip4844/eip4844_test.go#L78
#[test]
Expand Down
2 changes: 0 additions & 2 deletions crates/context/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ pub struct BlockEnv {
pub prevrandao: Option<B256>,
/// Excess blob gas and blob gasprice
///
/// See also [`calc_excess_blob_gas`][context_interface::block::calc_excess_blob_gas]
/// and [`calc_blob_gasprice`][context_interface::block::blob::calc_blob_gasprice].
///
/// Incorporated as part of the Cancun upgrade via [EIP-4844].
///
Expand Down
6 changes: 0 additions & 6 deletions crates/primitives/src/eip7918.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub mod eip7702;
pub mod eip7823;
pub mod eip7825;
pub mod eip7907;
pub mod eip7918;
pub mod hardfork;
mod once_lock;

Expand Down
1 change: 1 addition & 0 deletions crates/statetest-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ serde = { workspace = true, features = ["derive", "rc"] }
serde_json = { workspace = true, features = ["preserve_order"] }
k256 = { workspace = true }
thiserror = { workspace = true }
alloy-eips = { workspace = true }
16 changes: 3 additions & 13 deletions crates/statetest-types/src/test_unit.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use alloy_eips::calc_excess_blob_gas;
use serde::Deserialize;
use std::collections::{BTreeMap, HashMap};

use crate::{AccountInfo, Env, SpecName, Test, TransactionParts};
use revm::{
context::{block::BlockEnv, cfg::CfgEnv},
context_interface::block::calc_excess_blob_gas,
database::CacheState,
primitives::{
eip4844::TARGET_BLOB_GAS_PER_BLOCK_CANCUN, hardfork::SpecId, keccak256, Address, Bytes,
B256,
},
primitives::{hardfork::SpecId, keccak256, Address, Bytes, B256},
state::Bytecode,
};

Expand Down Expand Up @@ -127,14 +124,7 @@ impl TestUnit {
self.env.parent_excess_blob_gas,
) {
block.set_blob_excess_gas_and_price(
calc_excess_blob_gas(
parent_blob_gas_used.to(),
parent_excess_blob_gas.to(),
self.env
.parent_target_blobs_per_block
.map(|i| i.to())
.unwrap_or(TARGET_BLOB_GAS_PER_BLOCK_CANCUN),
),
calc_excess_blob_gas(parent_blob_gas_used.to(), parent_excess_blob_gas.to()),
revm::primitives::eip4844::BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN,
);
}
Expand Down
Loading