Skip to content

Commit

Permalink
consensus: make Header blob fees u64 (alloy-rs#1377)
Browse files Browse the repository at this point in the history
consensus: make Header blob fees u64
  • Loading branch information
tcoratger authored Sep 26, 2024
1 parent 73f7ac4 commit 8f7cf55
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
22 changes: 11 additions & 11 deletions crates/consensus/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub struct Header {
skip_serializing_if = "Option::is_none"
)
)]
pub blob_gas_used: Option<u128>,
pub blob_gas_used: Option<u64>,
/// A running total of blob gas consumed in excess of the target, prior to the block. Blocks
/// with above-target blob gas consumption increase this value, blocks with below-target blob
/// gas consumption decrease it (bounded at 0). This was added in EIP-4844.
Expand All @@ -112,7 +112,7 @@ pub struct Header {
skip_serializing_if = "Option::is_none"
)
)]
pub excess_blob_gas: Option<u128>,
pub excess_blob_gas: Option<u64>,
/// The hash of the parent beacon block's root is included in execution blocks, as proposed by
/// EIP-4788.
///
Expand Down Expand Up @@ -244,7 +244,7 @@ impl Header {
/// spec.
///
/// Returns a `None` if no excess blob gas is set, no EIP-4844 support
pub fn next_block_excess_blob_gas(&self) -> Option<u128> {
pub fn next_block_excess_blob_gas(&self) -> Option<u64> {
Some(calc_excess_blob_gas(self.excess_blob_gas?, self.blob_gas_used?))
}

Expand Down Expand Up @@ -529,15 +529,15 @@ impl Decodable for Header {
if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() {
buf.advance(1)
} else {
this.blob_gas_used = Some(U256::decode(buf)?.to::<u128>());
this.blob_gas_used = Some(U256::decode(buf)?.to::<u64>());
}
}

if started_len - buf.len() < rlp_head.payload_length {
if buf.first().map(|b| *b == EMPTY_LIST_CODE).unwrap_or_default() {
buf.advance(1)
} else {
this.excess_blob_gas = Some(U256::decode(buf)?.to::<u128>());
this.excess_blob_gas = Some(U256::decode(buf)?.to::<u64>());
}
}

Expand Down Expand Up @@ -582,8 +582,8 @@ impl Decodable for Header {
pub(crate) const fn generate_valid_header(
mut header: Header,
eip_4844_active: bool,
blob_gas_used: u128,
excess_blob_gas: u128,
blob_gas_used: u64,
excess_blob_gas: u64,
parent_beacon_block_root: B256,
) -> Header {
// Clear all related fields if EIP-1559 is inactive
Expand Down Expand Up @@ -698,10 +698,10 @@ pub trait BlockHeader {
fn base_fee_per_gas(&self) -> Option<u64>;

/// Retrieves the blob gas used by the block, if available
fn blob_gas_used(&self) -> Option<u128>;
fn blob_gas_used(&self) -> Option<u64>;

/// Retrieves the excess blob gas of the block, if available
fn excess_blob_gas(&self) -> Option<u128>;
fn excess_blob_gas(&self) -> Option<u64>;

/// Retrieves the parent beacon block root of the block, if available
fn parent_beacon_block_root(&self) -> Option<B256>;
Expand Down Expand Up @@ -778,11 +778,11 @@ impl BlockHeader for Header {
self.base_fee_per_gas
}

fn blob_gas_used(&self) -> Option<u128> {
fn blob_gas_used(&self) -> Option<u64> {
self.blob_gas_used
}

fn excess_blob_gas(&self) -> Option<u128> {
fn excess_blob_gas(&self) -> Option<u64> {
self.excess_blob_gas
}

Expand Down
20 changes: 10 additions & 10 deletions crates/eips/src/eip4844/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,21 @@ pub fn kzg_to_versioned_hash(commitment: &[u8]) -> B256 {
/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers)
/// (`calc_excess_blob_gas`).
#[inline]
pub const fn calc_excess_blob_gas(
parent_excess_blob_gas: u128,
parent_blob_gas_used: u128,
) -> u128 {
(parent_excess_blob_gas + parent_blob_gas_used)
.saturating_sub(TARGET_DATA_GAS_PER_BLOCK as u128)
pub const fn calc_excess_blob_gas(parent_excess_blob_gas: u64, parent_blob_gas_used: u64) -> u64 {
(parent_excess_blob_gas + parent_blob_gas_used).saturating_sub(TARGET_DATA_GAS_PER_BLOCK)
}

/// Calculates the blob gas price from the header's excess blob gas field.
///
/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers)
/// (`get_blob_gasprice`).
#[inline]
pub fn calc_blob_gasprice(excess_blob_gas: u128) -> u128 {
fake_exponential(BLOB_TX_MIN_BLOB_GASPRICE, excess_blob_gas, BLOB_GASPRICE_UPDATE_FRACTION)
pub fn calc_blob_gasprice(excess_blob_gas: u64) -> u128 {
fake_exponential(
BLOB_TX_MIN_BLOB_GASPRICE,
excess_blob_gas as u128,
BLOB_GASPRICE_UPDATE_FRACTION,
)
}

/// Approximates `factor * e ** (numerator / denominator)` using Taylor expansion.
Expand Down Expand Up @@ -205,8 +205,8 @@ mod tests {
),
(DATA_GAS_PER_BLOB - 1, (TARGET_DATA_GAS_PER_BLOCK / DATA_GAS_PER_BLOB) - 1, 0),
] {
let actual = calc_excess_blob_gas(excess as u128, (blobs * DATA_GAS_PER_BLOB) as u128);
assert_eq!(actual, expected as u128, "test: {t:?}");
let actual = calc_excess_blob_gas(excess, blobs * DATA_GAS_PER_BLOB);
assert_eq!(actual, expected, "test: {t:?}");
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/provider/src/fillers/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ where
.ok_or(RpcError::NullResp)?
.header()
.next_block_blob_fee()
.map(Into::into)
.ok_or(RpcError::UnsupportedFeature("eip4844"))
}

Expand Down
6 changes: 3 additions & 3 deletions crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub struct Header {
with = "alloy_serde::quantity::opt"
)
)]
pub blob_gas_used: Option<u128>,
pub blob_gas_used: Option<u64>,
/// Excess blob gas
#[cfg_attr(
feature = "serde",
Expand All @@ -140,7 +140,7 @@ pub struct Header {
with = "alloy_serde::quantity::opt"
)
)]
pub excess_blob_gas: Option<u128>,
pub excess_blob_gas: Option<u64>,
/// EIP-4788 parent beacon block root
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
pub parent_beacon_block_root: Option<B256>,
Expand Down Expand Up @@ -170,7 +170,7 @@ impl Header {
/// spec.
///
/// Returns a `None` if no excess blob gas is set, no EIP-4844 support
pub fn next_block_excess_blob_gas(&self) -> Option<u128> {
pub fn next_block_excess_blob_gas(&self) -> Option<u64> {
Some(calc_excess_blob_gas(self.excess_blob_gas?, self.blob_gas_used?))
}
}
Expand Down

0 comments on commit 8f7cf55

Please sign in to comment.