Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Nov 6, 2024
1 parent b283c74 commit 9a23291
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 55 deletions.
10 changes: 5 additions & 5 deletions crates/consensus/src/block/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub struct AnyHeader {
skip_serializing_if = "Option::is_none"
)
)]
pub target_blob_count: Option<u64>,
pub target_blobs_per_block: Option<u64>,
}

impl BlockHeader for AnyHeader {
Expand Down Expand Up @@ -189,8 +189,8 @@ impl BlockHeader for AnyHeader {
self.requests_hash
}

fn target_blob_count(&self) -> Option<u64> {
self.target_blob_count
fn target_blobs_per_block(&self) -> Option<u64> {
self.target_blobs_per_block
}

fn extra_data(&self) -> &Bytes {
Expand Down Expand Up @@ -222,7 +222,7 @@ impl From<super::Header> for AnyHeader {
excess_blob_gas,
parent_beacon_block_root,
requests_hash,
target_blob_count,
target_blobs_per_block,
} = value;

Self {
Expand All @@ -247,7 +247,7 @@ impl From<super::Header> for AnyHeader {
excess_blob_gas,
parent_beacon_block_root,
requests_hash,
target_blob_count,
target_blobs_per_block,
}
}
}
77 changes: 44 additions & 33 deletions crates/consensus/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub struct Header {
skip_serializing_if = "Option::is_none"
)
)]
pub target_blob_count: Option<u64>,
pub target_blobs_per_block: Option<u64>,
}

impl AsRef<Self> for Header {
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Default for Header {
excess_blob_gas: None,
parent_beacon_block_root: None,
requests_hash: None,
target_blob_count: None,
target_blobs_per_block: None,
}
}
}
Expand Down Expand Up @@ -219,16 +219,19 @@ impl Header {
///
/// Returns `None` if `excess_blob_gas` is None.
///
/// If `next_block_target_blob_count` is [`Some`], uses EIP-7742 formula for calculating the
/// blob gas price, otherwise uses EIP-4844 formula.
/// If `next_block_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, next_block_target_blob_count: Option<u64>) -> Option<u128> {
pub fn next_block_blob_fee(
&self,
next_block_target_blobs_per_block: Option<u64>,
) -> Option<u128> {
let next_block_excess_blob_gas = self.next_block_excess_blob_gas()?;
Some(next_block_target_blob_count.map_or_else(
Some(next_block_target_blobs_per_block.map_or_else(
|| eip4844::calc_blob_gasprice(next_block_excess_blob_gas),
|target_blob_count| {
eip7742::calc_blob_gasprice(next_block_excess_blob_gas, target_blob_count)
|target_blobs_per_block| {
eip7742::calc_blob_gasprice(next_block_excess_blob_gas, target_blobs_per_block)
},
))
}
Expand All @@ -253,10 +256,14 @@ impl Header {
let excess_blob_gas = self.excess_blob_gas?;
let blob_gas_used = self.blob_gas_used?;

Some(self.target_blob_count.map_or_else(
Some(self.target_blobs_per_block.map_or_else(
|| eip4844::calc_excess_blob_gas(excess_blob_gas, blob_gas_used),
|target_blob_count| {
eip7742::calc_excess_blob_gas(excess_blob_gas, blob_gas_used, target_blob_count)
|target_blobs_per_block| {
eip7742::calc_excess_blob_gas(
excess_blob_gas,
blob_gas_used,
target_blobs_per_block,
)
},
))
}
Expand Down Expand Up @@ -333,8 +340,8 @@ impl Header {
length += requests_hash.length();
}

if let Some(target_blob_count) = self.target_blob_count {
length += target_blob_count.length();
if let Some(target_blobs_per_block) = self.target_blobs_per_block {
length += target_blobs_per_block.length();
}

length
Expand Down Expand Up @@ -431,8 +438,8 @@ impl Encodable for Header {
requests_hash.encode(out);
}

if let Some(ref target_blob_count) = self.target_blob_count {
target_blob_count.encode(out);
if let Some(ref target_blobs_per_block) = self.target_blobs_per_block {
target_blobs_per_block.encode(out);
}
}

Expand Down Expand Up @@ -473,7 +480,7 @@ impl Decodable for Header {
excess_blob_gas: None,
parent_beacon_block_root: None,
requests_hash: None,
target_blob_count: None,
target_blobs_per_block: None,
};
if started_len - buf.len() < rlp_head.payload_length {
this.base_fee_per_gas = Some(u64::decode(buf)?);
Expand Down Expand Up @@ -505,7 +512,7 @@ impl Decodable for Header {

// Decode target blob count.
if started_len - buf.len() < rlp_head.payload_length {
this.target_blob_count = Some(u64::decode(buf)?);
this.target_blobs_per_block = Some(u64::decode(buf)?);
}

let consumed = started_len - buf.len();
Expand Down Expand Up @@ -584,7 +591,7 @@ impl<'a> arbitrary::Arbitrary<'a> for Header {
parent_beacon_block_root: u.arbitrary()?,
requests_hash: u.arbitrary()?,
withdrawals_root: u.arbitrary()?,
target_blob_count: u.arbitrary()?,
target_blobs_per_block: u.arbitrary()?,
};

Ok(generate_valid_header(
Expand Down Expand Up @@ -660,7 +667,7 @@ pub trait BlockHeader {
fn requests_hash(&self) -> Option<B256>;

/// Retrieves the target blob count of the block, if available
fn target_blob_count(&self) -> Option<u64>;
fn target_blobs_per_block(&self) -> Option<u64>;

/// Retrieves the block's extra data field
fn extra_data(&self) -> &Bytes;
Expand All @@ -673,10 +680,14 @@ pub trait BlockHeader {
let excess_blob_gas = self.excess_blob_gas()?;
let blob_gas_used = self.blob_gas_used()?;

Some(self.target_blob_count().map_or_else(
Some(self.target_blobs_per_block().map_or_else(
|| eip4844::calc_excess_blob_gas(excess_blob_gas, blob_gas_used),
|target_blob_count| {
eip7742::calc_excess_blob_gas(excess_blob_gas, blob_gas_used, target_blob_count)
|target_blobs_per_block| {
eip7742::calc_excess_blob_gas(
excess_blob_gas,
blob_gas_used,
target_blobs_per_block,
)
},
))
}
Expand All @@ -685,16 +696,16 @@ pub trait BlockHeader {
///
/// Returns `None` if `excess_blob_gas` is None.
///
/// If `next_block_target_blob_count` is [`Some`], uses EIP-7742 formula for calculating the
/// blob gas price, otherwise uses EIP-4844 formula.
/// If `next_block_target_blobs_per_block` is [`Some`], 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, next_block_target_blob_count: Option<u64>) -> Option<u128> {
fn next_block_blob_fee(&self, next_block_target_blobs_per_block: Option<u64>) -> Option<u128> {
let next_block_excess_blob_gas = self.next_block_excess_blob_gas()?;
Some(next_block_target_blob_count.map_or_else(
Some(next_block_target_blobs_per_block.map_or_else(
|| eip4844::calc_blob_gasprice(next_block_excess_blob_gas),
|target_blob_count| {
eip7742::calc_blob_gasprice(next_block_excess_blob_gas, target_blob_count)
|target_blobs_per_block| {
eip7742::calc_blob_gasprice(next_block_excess_blob_gas, target_blobs_per_block)
},
))
}
Expand Down Expand Up @@ -781,8 +792,8 @@ impl BlockHeader for Header {
self.requests_hash
}

fn target_blob_count(&self) -> Option<u64> {
self.target_blob_count
fn target_blobs_per_block(&self) -> Option<u64> {
self.target_blobs_per_block
}

fn extra_data(&self) -> &Bytes {
Expand Down Expand Up @@ -842,7 +853,7 @@ pub(crate) mod serde_bincode_compat {
#[serde(default)]
requests_hash: Option<B256>,
#[serde(default)]
target_blob_count: Option<u64>,
target_blobs_per_block: Option<u64>,
extra_data: Cow<'a, Bytes>,
}

Expand Down Expand Up @@ -870,7 +881,7 @@ pub(crate) mod serde_bincode_compat {
parent_beacon_block_root: value.parent_beacon_block_root,
requests_hash: value.requests_hash,
extra_data: Cow::Borrowed(&value.extra_data),
target_blob_count: value.target_blob_count,
target_blobs_per_block: value.target_blobs_per_block,
}
}
}
Expand Down Expand Up @@ -899,7 +910,7 @@ pub(crate) mod serde_bincode_compat {
parent_beacon_block_root: value.parent_beacon_block_root,
requests_hash: value.requests_hash,
extra_data: value.extra_data.into_owned(),
target_blob_count: value.target_blob_count,
target_blobs_per_block: value.target_blobs_per_block,
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/eips/src/eip7742.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@

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

/// Controls the update rate of the blob base fee based on `target_blob_count`.
/// 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: u64 = 1112825;

/// Calculates the `excess_blob_gas` from the parent header's `blob_gas_used`, `excess_blob_gas` and
/// `target_blob_count`.
/// `target_blobs_per_block`.
///
/// Similar to [crate::eip4844::calc_excess_blob_gas], but derives the target blob gas from
/// `parent_target_blob_count`.
/// `parent_target_blobs_per_block`.
#[inline]
pub const fn calc_excess_blob_gas(
parent_excess_blob_gas: u64,
parent_blob_gas_used: u64,
parent_target_blob_count: u64,
parent_target_blobs_per_block: u64,
) -> u64 {
(parent_excess_blob_gas + parent_blob_gas_used)
.saturating_sub(DATA_GAS_PER_BLOB * parent_target_blob_count)
.saturating_sub(DATA_GAS_PER_BLOB * parent_target_blobs_per_block)
}

/// 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_blob_count`.
/// `target_blobs_per_block`.
#[inline]
pub fn calc_blob_gasprice(excess_blob_gas: u64, target_blob_count: u64) -> u128 {
let update_fraction = BLOB_BASE_FEE_UPDATE_FRACTION_PER_TARGET_BLOB * target_blob_count;
pub fn calc_blob_gasprice(excess_blob_gas: u64, target_blobs_per_block: u64) -> u128 {
let update_fraction = BLOB_BASE_FEE_UPDATE_FRACTION_PER_TARGET_BLOB * target_blobs_per_block;
fake_exponential(BLOB_TX_MIN_BLOB_GASPRICE, excess_blob_gas as u128, update_fraction as u128)
}
2 changes: 1 addition & 1 deletion crates/provider/src/fillers/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ where
//
// Blob target increases are expected to be rare, thus this should be correct most of
// the time
.next_block_blob_fee(latest_header.target_blob_count())
.next_block_blob_fee(latest_header.target_blobs_per_block())
.map(Into::into)
.ok_or(RpcError::UnsupportedFeature("eip4844"))
}
Expand Down
19 changes: 11 additions & 8 deletions crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ impl<H: BlockHeader> Header<H> {
/// Returns `None` if `excess_blob_gas` is None.
///
/// See also [Self::next_block_excess_blob_gas]
pub fn next_block_blob_fee(&self, next_block_target_blob_count: Option<u64>) -> Option<u128> {
self.inner.next_block_blob_fee(next_block_target_blob_count)
pub fn next_block_blob_fee(
&self,
next_block_target_blobs_per_block: Option<u64>,
) -> Option<u128> {
self.inner.next_block_blob_fee(next_block_target_blobs_per_block)
}

/// Calculate excess blob gas for the next block according to the EIP-4844
Expand Down Expand Up @@ -344,7 +347,7 @@ mod tests {
excess_blob_gas: None,
parent_beacon_block_root: None,
requests_hash: None,
target_blob_count: None,
target_blobs_per_block: None,
},
total_difficulty: Some(U256::from(100000)),
size: None,
Expand Down Expand Up @@ -392,7 +395,7 @@ mod tests {
excess_blob_gas: None,
parent_beacon_block_root: None,
requests_hash: None,
target_blob_count: None,
target_blobs_per_block: None,
},
size: None,
total_difficulty: Some(U256::from(100000)),
Expand Down Expand Up @@ -438,7 +441,7 @@ mod tests {
excess_blob_gas: None,
parent_beacon_block_root: None,
requests_hash: None,
target_blob_count: None,
target_blobs_per_block: None,
},
total_difficulty: Some(U256::from(100000)),
size: None,
Expand Down Expand Up @@ -710,7 +713,7 @@ mod tests {
excess_blob_gas: None,
parent_beacon_block_root: None,
requests_hash: None,
target_blob_count: None,
target_blobs_per_block: None,
},
size: None,
total_difficulty: None,
Expand Down Expand Up @@ -757,7 +760,7 @@ mod tests {
excess_blob_gas: None,
parent_beacon_block_root: None,
requests_hash: None,
target_blob_count: None,
target_blobs_per_block: None,
},
total_difficulty: None,
size: Some(U256::from(505)),
Expand Down Expand Up @@ -816,7 +819,7 @@ mod tests {
excess_blob_gas: None,
parent_beacon_block_root: None,
requests_hash: None,
target_blob_count: None,
target_blobs_per_block: None,
},
total_difficulty: Some(U256::from(100000)),
size: Some(U256::from(19)),
Expand Down

0 comments on commit 9a23291

Please sign in to comment.