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
2831impl 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.
0 commit comments