Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add blob_gas_used to block body #13012

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ pub fn validate_shanghai_withdrawals<H: BlockHeader, B: reth_primitives_traits::
///
/// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
#[inline]
pub fn validate_cancun_gas(block: &SealedBlock) -> Result<(), ConsensusError> {
pub fn validate_cancun_gas<H: BlockHeader, B: reth_primitives_traits::BlockBody>(
block: &SealedBlock<H, B>,
) -> 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,
Expand Down
14 changes: 12 additions & 2 deletions crates/primitives-traits/src/block/body.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 <https://github.com/alloy-rs/alloy/pull/1704>
self.transactions()
.iter()
.filter_map(|tx| tx.blob_versioned_hashes())
.map(|hashes| hashes.len() as u64 * DATA_GAS_PER_BLOB)
.sum()
}
}
5 changes: 0 additions & 5 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions crates/rpc/rpc-eth-types/src/fee_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
Loading