Skip to content

Commit 1817ad7

Browse files
authored
chore: support functions for eip7918 (#2579)
* wip blob * chore: support functions for eip7918
1 parent 13de1d4 commit 1817ad7

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed

crates/context/interface/src/block.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//! [`Block`] trait is used to retrieve block information required for execution.
44
pub mod blob;
55

6-
pub use blob::{calc_blob_gasprice, calc_excess_blob_gas, BlobExcessGasAndPrice};
6+
pub use blob::{
7+
calc_blob_gasprice, calc_excess_blob_gas, calc_excess_blob_gas_osaka, BlobExcessGasAndPrice,
8+
};
79

810
use auto_impl::auto_impl;
911
use primitives::{Address, B256, U256};

crates/context/interface/src/block/blob.rs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
//! excess blob gas.
88
//!
99
//! [`BlobExcessGasAndPrice`] is used to store the blob gas price and excess blob gas.s
10-
use primitives::eip4844::MIN_BLOB_GASPRICE;
10+
use primitives::{
11+
eip4844::{GAS_PER_BLOB, MIN_BLOB_GASPRICE},
12+
eip7918,
13+
};
1114

1215
/// Structure holding block blob excess gas and it calculates blob fee
1316
///
@@ -27,6 +30,8 @@ pub struct BlobExcessGasAndPrice {
2730

2831
impl BlobExcessGasAndPrice {
2932
/// Creates a new instance by calculating the blob gas price with [`calc_blob_gasprice`].
33+
///
34+
/// `excess_blob_gas` is the excess blob gas of the block, it can be calculated with [`calc_excess_blob_gas`].
3035
pub fn new(excess_blob_gas: u64, blob_base_fee_update_fraction: u64) -> Self {
3136
let blob_gasprice = calc_blob_gasprice(excess_blob_gas, blob_base_fee_update_fraction);
3237
Self {
@@ -39,6 +44,9 @@ impl BlobExcessGasAndPrice {
3944
/// and the target blob gas per block.
4045
///
4146
/// This fields will be used to calculate `excess_blob_gas` with [`calc_excess_blob_gas`] func.
47+
#[deprecated(
48+
note = "Use `calc_excess_blob_gas` and `BlobExcessGasAndPrice::new` instead. Only works for forks before Osaka."
49+
)]
4250
pub fn from_parent_and_target(
4351
parent_excess_blob_gas: u64,
4452
parent_blob_gas_used: u64,
@@ -56,17 +64,69 @@ impl BlobExcessGasAndPrice {
5664
}
5765
}
5866

67+
/// Calculates the `excess_blob_gas` from the parent header's `blob_gas_used` and `excess_blob_gas`.
68+
/// uses [`calc_excess_blob_gas`] internally.
69+
#[inline]
70+
pub fn calc_excess_blob_gas(
71+
parent_excess_blob_gas: u64,
72+
parent_blob_gas_used: u64,
73+
parent_target_blob_gas_per_block: u64,
74+
) -> u64 {
75+
calc_excess_blob_gas_osaka(
76+
parent_excess_blob_gas,
77+
parent_blob_gas_used,
78+
parent_target_blob_gas_per_block,
79+
false,
80+
0,
81+
0,
82+
0,
83+
0,
84+
0,
85+
)
86+
}
87+
5988
/// Calculates the `excess_blob_gas` from the parent header's `blob_gas_used` and `excess_blob_gas`.
6089
///
6190
/// See also [the EIP-4844 helpers]<https://eips.ethereum.org/EIPS/eip-4844#helpers>
6291
/// (`calc_excess_blob_gas`).
92+
///
93+
/// [EIP-7918: Blob base fee bounded by execution cost](https://eips.ethereum.org/EIPS/eip-7918)
94+
///
95+
/// `blob_base_cost` is introduced in EIP-7918 in Osaka fork. All fields after is_osaka input are not needed before Osaka.
96+
#[allow(clippy::too_many_arguments)]
6397
#[inline]
64-
pub fn calc_excess_blob_gas(
98+
pub fn calc_excess_blob_gas_osaka(
6599
parent_excess_blob_gas: u64,
66100
parent_blob_gas_used: u64,
67101
parent_target_blob_gas_per_block: u64,
102+
is_osaka: bool,
103+
parent_base_fee_per_gas: u64,
104+
parent_blob_base_fee_per_gas: u64,
105+
parent_blob_base_fee_update_fraction: u64,
106+
max_blob_count: u64,
107+
target_blob_count: u64,
68108
) -> u64 {
69-
(parent_excess_blob_gas + parent_blob_gas_used).saturating_sub(parent_target_blob_gas_per_block)
109+
let excess_and_used = parent_excess_blob_gas.saturating_add(parent_blob_gas_used);
110+
111+
if is_osaka {
112+
if excess_and_used < parent_target_blob_gas_per_block {
113+
return 0;
114+
}
115+
116+
if (eip7918::BLOB_BASE_COST.saturating_mul(parent_base_fee_per_gas) as u128)
117+
> (GAS_PER_BLOB as u128).saturating_mul(get_base_fee_per_blob_gas(
118+
parent_blob_base_fee_per_gas,
119+
parent_blob_base_fee_update_fraction,
120+
))
121+
{
122+
return excess_and_used.saturating_add(
123+
parent_blob_gas_used.saturating_mul(max_blob_count - target_blob_count)
124+
/ max_blob_count,
125+
);
126+
}
127+
}
128+
129+
excess_and_used.saturating_sub(parent_target_blob_gas_per_block)
70130
}
71131

72132
/// Calculates the blob gas price from the header's excess blob gas field.
@@ -82,6 +142,12 @@ pub fn calc_blob_gasprice(excess_blob_gas: u64, blob_base_fee_update_fraction: u
82142
)
83143
}
84144

145+
/// Calculates the base fee per blob gas. Calls [`calc_blob_gasprice`] internally.
146+
/// Name of the function is aligned with EIP-4844 spec.
147+
pub fn get_base_fee_per_blob_gas(excess_blob_gas: u64, blob_base_fee_update_fraction: u64) -> u128 {
148+
calc_blob_gasprice(excess_blob_gas, blob_base_fee_update_fraction)
149+
}
150+
85151
/// Approximates `factor * e ** (numerator / denominator)` using Taylor expansion.
86152
///
87153
/// This is used to calculate the blob price.

crates/primitives/src/eip7918.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! Constants for EIP-7918: Blob base fee bounded by execution cost
2+
//!
3+
4+
/// Minimum base fee for blobs, if price of the blob is less than this value, this value will be used.
5+
pub const BLOB_BASE_COST: u64 = 2_u64.pow(14);

crates/primitives/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod eip4844;
1313
pub mod eip7702;
1414
pub mod eip7823;
1515
pub mod eip7825;
16+
pub mod eip7918;
1617
pub mod eof;
1718
pub mod hardfork;
1819

0 commit comments

Comments
 (0)