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

add ensure_transaction_root_valid method for SealedBlock #6163

Merged
merged 2 commits into from
Jan 22, 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
8 changes: 2 additions & 6 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,8 @@ pub fn validate_block_standalone(
}

// Check transaction root
// TODO(onbjerg): This should probably be accessible directly on [Block]
let transaction_root = reth_primitives::proofs::calculate_transaction_root(&block.body);
if block.header.transactions_root != transaction_root {
return Err(ConsensusError::BodyTransactionRootDiff(
GotExpected { got: transaction_root, expected: block.header.transactions_root }.into(),
))
if let Err(error) = block.ensure_transaction_root_valid() {
return Err(ConsensusError::BodyTransactionRootDiff(error.into()));
}

// EIP-4895: Beacon chain push withdrawals as operations
Expand Down
29 changes: 27 additions & 2 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
Address, Header, SealedHeader, TransactionSigned, TransactionSignedEcRecovered, Withdrawal,
B256,
Address, GotExpected, Header, SealedHeader, TransactionSigned, TransactionSignedEcRecovered,
Withdrawal, B256,
};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use reth_codecs::derive_arbitrary;
Expand Down Expand Up @@ -323,6 +323,31 @@ impl SealedBlock {
pub fn blob_gas_used(&self) -> u64 {
self.blob_transactions().iter().filter_map(|tx| tx.blob_gas_used()).sum()
}

/// Ensures that the transaction root in the block header is valid.
///
/// The transaction root is the Keccak 256-bit hash of the root node of the trie structure
/// populated with each transaction in the transactions list portion of the block.
///
/// # Returns
///
/// Returns `Ok(())` if the calculated transaction root matches the one stored in the header,
/// indicating that the transactions in the block are correctly represented in the trie.
///
/// Returns `Err(error)` if the transaction root validation fails, providing a `GotExpected`
/// error containing the calculated and expected roots.
pub fn ensure_transaction_root_valid(&self) -> Result<(), GotExpected<B256>> {
let calculated_root = crate::proofs::calculate_transaction_root(&self.body);

if self.header.transactions_root != calculated_root {
return Err(GotExpected {
got: calculated_root,
expected: self.header.transactions_root,
})
}

Ok(())
}
}

impl From<SealedBlock> for Block {
Expand Down
Loading