From f04c85eba6cb0552e17c0146ad8a62b025843d63 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 29 Nov 2024 14:50:11 +0100 Subject: [PATCH] feat: add blob_gas_used to block body --- crates/consensus/common/src/validation.rs | 9 ++++++--- crates/primitives-traits/src/block/body.rs | 14 ++++++++++++-- crates/primitives/src/block.rs | 5 ----- crates/rpc/rpc-eth-types/src/fee_history.rs | 6 +++--- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/crates/consensus/common/src/validation.rs b/crates/consensus/common/src/validation.rs index 9e7f8d451fff..8035f8bf61c3 100644 --- a/crates/consensus/common/src/validation.rs +++ b/crates/consensus/common/src/validation.rs @@ -65,11 +65,14 @@ pub fn validate_shanghai_withdrawals Result<(), ConsensusError> { +pub fn validate_cancun_gas( + block: &SealedBlock, +) -> Result<(), ConsensusError> { // Check that the blob gas used in the header matches the sum of the blob gas used by each // blob tx - let header_blob_gas_used = block.blob_gas_used.ok_or(ConsensusError::BlobGasUsedMissing)?; - let total_blob_gas = block.blob_gas_used(); + let header_blob_gas_used = + block.header().blob_gas_used().ok_or(ConsensusError::BlobGasUsedMissing)?; + let total_blob_gas = block.body.blob_gas_used(); if total_blob_gas != header_blob_gas_used { return Err(ConsensusError::BlobGasUsedDiff(GotExpected { got: header_blob_gas_used, diff --git a/crates/primitives-traits/src/block/body.rs b/crates/primitives-traits/src/block/body.rs index 7491c75faf9e..1d9687dd2878 100644 --- a/crates/primitives-traits/src/block/body.rs +++ b/crates/primitives-traits/src/block/body.rs @@ -1,8 +1,8 @@ //! Block body abstraction. use alloc::{fmt, vec::Vec}; - -use alloy_eips::eip4895::Withdrawals; +use alloy_consensus::Transaction; +use alloy_eips::{eip4844::DATA_GAS_PER_BLOB, eip4895::Withdrawals}; use crate::{ FullSignedTx, InMemorySize, MaybeArbitrary, MaybeSerde, MaybeSerdeBincodeCompat, @@ -48,4 +48,14 @@ pub trait BlockBody: /// Returns block ommers if any. fn ommers(&self) -> Option<&[Self::OmmerHeader]>; + + /// Calculates the total blob gas used by _all_ EIP-4844 transactions in the block. + fn blob_gas_used(&self) -> u64 { + // TODO(mattss): simplify after + self.transactions() + .iter() + .filter_map(|tx| tx.blob_versioned_hashes()) + .map(|hashes| hashes.len() as u64 * DATA_GAS_PER_BLOB) + .sum() + } } diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index c4905458c75c..617b7d4162bb 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -273,11 +273,6 @@ impl SealedBlock { self.body.blob_transactions_iter() } - /// Calculates the total gas used by blob transactions in the sealed block. - pub fn blob_gas_used(&self) -> u64 { - self.blob_transactions().iter().filter_map(|tx| tx.blob_gas_used()).sum() - } - /// Returns whether or not the block contains any blob transactions. #[inline] pub fn has_blob_transactions(&self) -> bool { diff --git a/crates/rpc/rpc-eth-types/src/fee_history.rs b/crates/rpc/rpc-eth-types/src/fee_history.rs index 922c3f9d474a..35233e6c2192 100644 --- a/crates/rpc/rpc-eth-types/src/fee_history.rs +++ b/crates/rpc/rpc-eth-types/src/fee_history.rs @@ -17,13 +17,13 @@ use metrics::atomics::AtomicU64; use reth_chain_state::CanonStateNotification; use reth_chainspec::{ChainSpecProvider, EthChainSpec}; use reth_primitives::{NodePrimitives, Receipt, SealedBlock, TransactionSigned}; +use reth_primitives_traits::{Block, BlockBody}; +use reth_rpc_server_types::constants::gas_oracle::MAX_HEADER_HISTORY; use reth_storage_api::BlockReaderIdExt; use revm_primitives::{calc_blob_gasprice, calc_excess_blob_gas}; use serde::{Deserialize, Serialize}; use tracing::trace; -use reth_rpc_server_types::constants::gas_oracle::MAX_HEADER_HISTORY; - use super::{EthApiError, EthStateCache}; /// Contains cached fee history entries for blocks. @@ -366,7 +366,7 @@ impl FeeHistoryEntry { base_fee_per_gas: block.base_fee_per_gas.unwrap_or_default(), gas_used_ratio: block.gas_used as f64 / block.gas_limit as f64, base_fee_per_blob_gas: block.blob_fee(), - blob_gas_used_ratio: block.blob_gas_used() as f64 / + blob_gas_used_ratio: block.body().blob_gas_used() as f64 / alloy_eips::eip4844::MAX_DATA_GAS_PER_BLOCK as f64, excess_blob_gas: block.excess_blob_gas, blob_gas_used: block.blob_gas_used,