From 8f7cf55591d1f7513d70d59c39616d3a2ddbd309 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Thu, 26 Sep 2024 14:13:25 +0200 Subject: [PATCH] consensus: make `Header` blob fees u64 (#1377) consensus: make Header blob fees u64 --- crates/consensus/src/header.rs | 22 +++++++++++----------- crates/eips/src/eip4844/mod.rs | 20 ++++++++++---------- crates/provider/src/fillers/gas.rs | 1 + crates/rpc-types-eth/src/block.rs | 6 +++--- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/crates/consensus/src/header.rs b/crates/consensus/src/header.rs index a11ddbc5c5b..2c06e169a43 100644 --- a/crates/consensus/src/header.rs +++ b/crates/consensus/src/header.rs @@ -100,7 +100,7 @@ pub struct Header { skip_serializing_if = "Option::is_none" ) )] - pub blob_gas_used: Option, + pub blob_gas_used: Option, /// 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. @@ -112,7 +112,7 @@ pub struct Header { skip_serializing_if = "Option::is_none" ) )] - pub excess_blob_gas: Option, + pub excess_blob_gas: Option, /// The hash of the parent beacon block's root is included in execution blocks, as proposed by /// EIP-4788. /// @@ -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 { + pub fn next_block_excess_blob_gas(&self) -> Option { Some(calc_excess_blob_gas(self.excess_blob_gas?, self.blob_gas_used?)) } @@ -529,7 +529,7 @@ 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::()); + this.blob_gas_used = Some(U256::decode(buf)?.to::()); } } @@ -537,7 +537,7 @@ impl Decodable for Header { 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::()); + this.excess_blob_gas = Some(U256::decode(buf)?.to::()); } } @@ -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 @@ -698,10 +698,10 @@ pub trait BlockHeader { fn base_fee_per_gas(&self) -> Option; /// Retrieves the blob gas used by the block, if available - fn blob_gas_used(&self) -> Option; + fn blob_gas_used(&self) -> Option; /// Retrieves the excess blob gas of the block, if available - fn excess_blob_gas(&self) -> Option; + fn excess_blob_gas(&self) -> Option; /// Retrieves the parent beacon block root of the block, if available fn parent_beacon_block_root(&self) -> Option; @@ -778,11 +778,11 @@ impl BlockHeader for Header { self.base_fee_per_gas } - fn blob_gas_used(&self) -> Option { + fn blob_gas_used(&self) -> Option { self.blob_gas_used } - fn excess_blob_gas(&self) -> Option { + fn excess_blob_gas(&self) -> Option { self.excess_blob_gas } diff --git a/crates/eips/src/eip4844/mod.rs b/crates/eips/src/eip4844/mod.rs index 91ee3f3d678..7718d36adc9 100644 --- a/crates/eips/src/eip4844/mod.rs +++ b/crates/eips/src/eip4844/mod.rs @@ -124,12 +124,8 @@ 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. @@ -137,8 +133,12 @@ pub const fn calc_excess_blob_gas( /// 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. @@ -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:?}"); } } diff --git a/crates/provider/src/fillers/gas.rs b/crates/provider/src/fillers/gas.rs index 6c484329762..2f1eecadb92 100644 --- a/crates/provider/src/fillers/gas.rs +++ b/crates/provider/src/fillers/gas.rs @@ -224,6 +224,7 @@ where .ok_or(RpcError::NullResp)? .header() .next_block_blob_fee() + .map(Into::into) .ok_or(RpcError::UnsupportedFeature("eip4844")) } diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index 676e0a7e2e0..02c1f6c75c2 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -130,7 +130,7 @@ pub struct Header { with = "alloy_serde::quantity::opt" ) )] - pub blob_gas_used: Option, + pub blob_gas_used: Option, /// Excess blob gas #[cfg_attr( feature = "serde", @@ -140,7 +140,7 @@ pub struct Header { with = "alloy_serde::quantity::opt" ) )] - pub excess_blob_gas: Option, + pub excess_blob_gas: Option, /// EIP-4788 parent beacon block root #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))] pub parent_beacon_block_root: Option, @@ -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 { + pub fn next_block_excess_blob_gas(&self) -> Option { Some(calc_excess_blob_gas(self.excess_blob_gas?, self.blob_gas_used?)) } }