From 1962014a7790406100b61666085b9b32bc0842a3 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Wed, 20 Sep 2023 23:34:07 +0200 Subject: [PATCH] feat(primitives): add `calculate_blob_gas_used` function in `SealedBlock` (#4692) Co-authored-by: Matthias Seitz --- crates/consensus/common/src/validation.rs | 3 +-- crates/primitives/src/block.rs | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/consensus/common/src/validation.rs b/crates/consensus/common/src/validation.rs index 6a3894e373..0fbb038b36 100644 --- a/crates/consensus/common/src/validation.rs +++ b/crates/consensus/common/src/validation.rs @@ -252,8 +252,7 @@ pub fn validate_block_standalone( // 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_transactions().iter().filter_map(|tx| tx.blob_gas_used()).sum(); + let total_blob_gas = block.blob_gas_used(); if total_blob_gas != header_blob_gas_used { return Err(ConsensusError::BlobGasUsedDiff { header_blob_gas_used, diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index a6696ccefa..199b088d06 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -203,6 +203,11 @@ impl SealedBlock { self.ommers.iter().map(Header::size).sum::() + self.ommers.capacity() * std::mem::size_of::
() + self.withdrawals.as_ref().map(|w| w.iter().map(Withdrawal::size).sum::() + w.capacity() * std::mem::size_of::()).unwrap_or(std::mem::size_of::>>()) } + + /// 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() + } } impl From for Block {