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

chore: make block field private #13628

Merged
merged 5 commits into from
Jan 3, 2025
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
2 changes: 1 addition & 1 deletion bin/reth-bench/src/bench/new_payload_fcu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Command {
let block_number = block.header.number;

let versioned_hashes: Vec<B256> =
block.body.blob_versioned_hashes_iter().copied().collect();
block.body().blob_versioned_hashes_iter().copied().collect();
let parent_beacon_block_root = block.parent_beacon_block_root;
let payload = block_to_payload(block).0;

Expand Down
2 changes: 1 addition & 1 deletion bin/reth-bench/src/bench/new_payload_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Command {
let gas_used = block.gas_used;

let versioned_hashes: Vec<B256> =
block.body.blob_versioned_hashes_iter().copied().collect();
block.body().blob_versioned_hashes_iter().copied().collect();
let parent_beacon_block_root = block.parent_beacon_block_root;
let payload = block_to_payload(block).0;

Expand Down
4 changes: 2 additions & 2 deletions book/sources/exex/tracking-state/src/bin/2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<Node: FullNodeComponents<Types: NodeTypes<Primitives = EthPrimitives>>> Fut
while let Some(notification) = ready!(this.ctx.notifications.try_next().poll_unpin(cx))? {
if let Some(reverted_chain) = notification.reverted_chain() {
this.transactions = this.transactions.saturating_sub(
reverted_chain.blocks_iter().map(|b| b.body.transactions.len() as u64).sum(),
reverted_chain.blocks_iter().map(|b| b.body().transactions.len() as u64).sum(),
);
}

Expand All @@ -45,7 +45,7 @@ impl<Node: FullNodeComponents<Types: NodeTypes<Primitives = EthPrimitives>>> Fut

this.transactions += committed_chain
.blocks_iter()
.map(|b| b.body.transactions.len() as u64)
.map(|b| b.body().transactions.len() as u64)
.sum::<u64>();

this.ctx
Expand Down
2 changes: 1 addition & 1 deletion crates/blockchain-tree-api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl<B: Block> std::fmt::Debug for InsertBlockErrorDataTwo<B> {
.field("hash", &self.block.hash())
.field("number", &self.block.number())
.field("parent_hash", &self.block.parent_hash())
.field("num_txs", &self.block.body.transactions().len())
.field("num_txs", &self.block.body().transactions().len())
.finish_non_exhaustive()
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/blockchain-tree/src/block_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,23 +572,23 @@ mod tests {

// Define blocks with their numbers and parent hashes.
let block_1 = SealedBlockWithSenders {
block: SealedBlock {
header: SealedHeader::new(
block: SealedBlock::new(
SealedHeader::new(
Header { parent_hash, number: 1, ..Default::default() },
block_hash_1,
),
..Default::default()
},
Default::default(),
),
..Default::default()
};
let block_2 = SealedBlockWithSenders {
block: SealedBlock {
header: SealedHeader::new(
block: SealedBlock::new(
SealedHeader::new(
Header { parent_hash: block_hash_1, number: 2, ..Default::default() },
block_hash_2,
),
..Default::default()
},
Default::default(),
),
..Default::default()
};

Expand Down
8 changes: 4 additions & 4 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,14 +1635,14 @@ mod tests {
};

SealedBlockWithSenders::new(
SealedBlock {
header: SealedHeader::seal(header),
body: BlockBody {
SealedBlock::new(
SealedHeader::seal(header),
BlockBody {
transactions: signed_body,
ommers: Vec::new(),
withdrawals: Some(Withdrawals::default()),
},
},
),
body.iter().map(|tx| tx.signer()).collect(),
)
.unwrap()
Expand Down
8 changes: 4 additions & 4 deletions crates/chain-state/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ impl<N: NodePrimitives> CanonicalInMemoryState<N> {
if let Some(tx) = block_state
.block_ref()
.block()
.body
.body()
.transactions()
.iter()
.find(|tx| tx.trie_hash() == hash)
Expand All @@ -573,7 +573,7 @@ impl<N: NodePrimitives> CanonicalInMemoryState<N> {
if let Some((index, tx)) = block_state
.block_ref()
.block()
.body
.body()
.transactions()
.iter()
.enumerate()
Expand Down Expand Up @@ -758,7 +758,7 @@ impl<N: NodePrimitives> BlockState<N> {
block_state
.block_ref()
.block()
.body
.body()
.transactions()
.iter()
.find(|tx| tx.trie_hash() == hash)
Expand All @@ -778,7 +778,7 @@ impl<N: NodePrimitives> BlockState<N> {
block_state
.block_ref()
.block()
.body
.body()
.transactions()
.iter()
.enumerate()
Expand Down
29 changes: 21 additions & 8 deletions crates/chain-state/src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,10 @@ impl<T: Clone + Sync + Send + 'static> Stream for ForkChoiceStream<T> {
#[cfg(test)]
mod tests {
use super::*;
use alloy_consensus::BlockBody;
use alloy_primitives::{b256, B256};
use reth_execution_types::ExecutionOutcome;
use reth_primitives::{Receipt, Receipts, TransactionSigned, TxType};
use reth_primitives::{Receipt, Receipts, SealedBlock, TransactionSigned, TxType};

#[test]
fn test_commit_notification() {
Expand Down Expand Up @@ -295,21 +296,25 @@ mod tests {
#[test]
fn test_block_receipts_commit() {
// Create a default block instance for use in block definitions.
let block: SealedBlockWithSenders = Default::default();
let mut body = BlockBody::<TransactionSigned>::default();

// Define unique hashes for two blocks to differentiate them in the chain.
let block1_hash = B256::new([0x01; 32]);
let block2_hash = B256::new([0x02; 32]);

// Create a default transaction to include in block1's transactions.
let tx = TransactionSigned::default();
body.transactions.push(tx);

let block: SealedBlockWithSenders =
SealedBlock::new(SealedHeader::seal(alloy_consensus::Header::default()), body)
.seal_with_senders()
.unwrap();

// Create a clone of the default block and customize it to act as block1.
let mut block1 = block.clone();
block1.set_block_number(1);
block1.set_hash(block1_hash);
// Add the transaction to block1's transactions.
block1.block.body.transactions.push(tx);

// Clone the default block and customize it to act as block2.
let mut block2 = block;
Expand Down Expand Up @@ -365,10 +370,14 @@ mod tests {
#[test]
fn test_block_receipts_reorg() {
// Define block1 for the old chain segment, which will be reverted.
let mut old_block1: SealedBlockWithSenders = Default::default();
let mut body = BlockBody::<TransactionSigned>::default();
body.transactions.push(TransactionSigned::default());
let mut old_block1: SealedBlockWithSenders =
SealedBlock::new(SealedHeader::seal(alloy_consensus::Header::default()), body)
.seal_with_senders()
.unwrap();
old_block1.set_block_number(1);
old_block1.set_hash(B256::new([0x01; 32]));
old_block1.block.body.transactions.push(TransactionSigned::default());

// Create a receipt for a transaction in the reverted block.
#[allow(clippy::needless_update)]
Expand All @@ -389,10 +398,14 @@ mod tests {
Arc::new(Chain::new(vec![old_block1.clone()], old_execution_outcome, None));

// Define block2 for the new chain segment, which will be committed.
let mut new_block1: SealedBlockWithSenders = Default::default();
let mut body = BlockBody::<TransactionSigned>::default();
body.transactions.push(TransactionSigned::default());
let mut new_block1: SealedBlockWithSenders =
SealedBlock::new(SealedHeader::seal(alloy_consensus::Header::default()), body)
.seal_with_senders()
.unwrap();
new_block1.set_block_number(2);
new_block1.set_hash(B256::new([0x02; 32]));
new_block1.block.body.transactions.push(TransactionSigned::default());

// Create a receipt for a transaction in the new committed block.
#[allow(clippy::needless_update)]
Expand Down
12 changes: 6 additions & 6 deletions crates/chain-state/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ impl<N: NodePrimitives> TestBlockBuilder<N> {
..Default::default()
};

let block = SealedBlock {
header: SealedHeader::seal(header),
body: BlockBody {
let block = SealedBlock::new(
SealedHeader::seal(header),
BlockBody {
transactions: transactions.into_iter().map(|tx| tx.into_signed()).collect(),
ommers: Vec::new(),
withdrawals: Some(vec![].into()),
},
};
);

SealedBlockWithSenders::new(block, vec![self.signer; num_txs as usize]).unwrap()
}
Expand Down Expand Up @@ -259,7 +259,7 @@ impl<N: NodePrimitives> TestBlockBuilder<N> {
/// updated.
pub fn get_execution_outcome(&mut self, block: SealedBlockWithSenders) -> ExecutionOutcome {
let receipts = block
.body
.body()
.transactions
.iter()
.enumerate()
Expand All @@ -273,7 +273,7 @@ impl<N: NodePrimitives> TestBlockBuilder<N> {

let mut bundle_state_builder = BundleState::builder(block.number..=block.number);

for tx in &block.body.transactions {
for tx in &block.body().transactions {
self.signer_execute_account_info.balance -= Self::single_tx_cost();
bundle_state_builder = bundle_state_builder.state_present_account_info(
self.signer,
Expand Down
3 changes: 1 addition & 2 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2774,8 +2774,7 @@ mod tests {
.with_real_consensus()
.build();

let genesis =
SealedBlock { header: chain_spec.sealed_genesis_header(), ..Default::default() };
let genesis = SealedBlock::new(chain_spec.sealed_genesis_header(), Default::default());
let block1 = random_block(
&mut rng,
1,
Expand Down
22 changes: 11 additions & 11 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn validate_header_base_fee<H: BlockHeader, ChainSpec: EthereumHardforks>(
pub fn validate_shanghai_withdrawals<H: BlockHeader, B: BlockBody>(
block: &SealedBlock<H, B>,
) -> Result<(), ConsensusError> {
let withdrawals = block.body.withdrawals().ok_or(ConsensusError::BodyWithdrawalsMissing)?;
let withdrawals = block.body().withdrawals().ok_or(ConsensusError::BodyWithdrawalsMissing)?;
let withdrawals_root = alloy_consensus::proofs::calculate_withdrawals_root(withdrawals);
let header_withdrawals_root =
block.withdrawals_root().ok_or(ConsensusError::WithdrawalsRootMissing)?;
Expand All @@ -67,7 +67,7 @@ pub fn validate_cancun_gas<H: BlockHeader, B: BlockBody>(
// blob tx
let header_blob_gas_used =
block.header().blob_gas_used().ok_or(ConsensusError::BlobGasUsedMissing)?;
let total_blob_gas = block.body.blob_gas_used();
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 Expand Up @@ -139,7 +139,7 @@ where
ChainSpec: EthereumHardforks,
{
// Check ommers hash
let ommers_hash = block.body.calculate_ommers_root();
let ommers_hash = block.body().calculate_ommers_root();
if Some(block.header.ommers_hash()) != ommers_hash {
return Err(ConsensusError::BodyOmmersHashDiff(
GotExpected {
Expand Down Expand Up @@ -514,10 +514,10 @@ mod tests {
let transactions = Vec::new();

(
SealedBlock {
header: SealedHeader::seal(header),
body: BlockBody { transactions, ommers, withdrawals: None },
},
SealedBlock::new(
SealedHeader::seal(header),
BlockBody { transactions, ommers, withdrawals: None },
),
parent,
)
}
Expand All @@ -539,10 +539,10 @@ mod tests {
..Default::default()
};

SealedBlock {
header: SealedHeader::seal(header),
body: BlockBody { withdrawals: Some(withdrawals), ..Default::default() },
}
SealedBlock::new(
SealedHeader::seal(header),
BlockBody { withdrawals: Some(withdrawals), ..Default::default() },
)
};

// Single withdrawal
Expand Down
2 changes: 1 addition & 1 deletion crates/e2e-test-utils/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<T: PayloadTypes> PayloadTestContext<T> {
pub async fn wait_for_built_payload(&self, payload_id: PayloadId) {
loop {
let payload = self.payload_builder.best_payload(payload_id).await.unwrap().unwrap();
if payload.block().body.transactions().is_empty() {
if payload.block().body().transactions().is_empty() {
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
continue
}
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ where

// Fetch reorg target block depending on its depth and its parent.
let mut previous_hash = next_block.parent_hash;
let mut candidate_transactions = next_block.body.transactions;
let mut candidate_transactions = next_block.into_body().transactions;
let reorg_target = 'target: {
loop {
let reorg_target = provider
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/execution-types/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl<N: NodePrimitives> Chain<N> {
self.blocks().iter().zip(self.execution_outcome.receipts().iter())
{
let mut tx_receipts = Vec::with_capacity(receipts.len());
for (tx, receipt) in block.body.transactions().iter().zip(receipts.iter()) {
for (tx, receipt) in block.body().transactions().iter().zip(receipts.iter()) {
tx_receipts.push((
tx.trie_hash(),
receipt.as_ref().expect("receipts have not been pruned").clone(),
Expand Down Expand Up @@ -437,7 +437,7 @@ impl<B: Block<Body: BlockBody<Transaction: SignedTransaction>>> ChainBlocks<'_,
/// Returns an iterator over all transactions in the chain.
#[inline]
pub fn transactions(&self) -> impl Iterator<Item = &<B::Body as BlockBody>::Transaction> + '_ {
self.blocks.values().flat_map(|block| block.body.transactions().iter())
self.blocks.values().flat_map(|block| block.body().transactions().iter())
}

/// Returns an iterator over all transactions and their senders.
Expand Down
7 changes: 4 additions & 3 deletions crates/exex/exex/src/backfill/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,16 @@ where
cumulative_gas += block.gas_used();

// Configure the executor to use the current state.
trace!(target: "exex::backfill", number = block_number, txs = block.body.transactions().len(), "Executing block");
trace!(target: "exex::backfill", number = block_number, txs = block.body().transactions().len(), "Executing block");

// Execute the block
let execute_start = Instant::now();

// Unseal the block for execution
let (block, senders) = block.into_components();
let (unsealed_header, hash) = block.header.split();
let block = P::Block::new(unsealed_header, block.body).with_senders_unchecked(senders);
let (header, body) = block.split_header_body();
let (unsealed_header, hash) = header.split();
let block = P::Block::new(unsealed_header, body).with_senders_unchecked(senders);

executor.execute_and_verify_one(&block)?;
execution_duration += execute_start.elapsed();
Expand Down
6 changes: 4 additions & 2 deletions crates/net/downloaders/src/bodies/bodies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,10 @@ mod tests {
);

let headers = blocks.iter().map(|block| block.header.clone()).collect::<Vec<_>>();
let bodies =
blocks.into_iter().map(|block| (block.hash(), block.body)).collect::<HashMap<_, _>>();
let bodies = blocks
.into_iter()
.map(|block| (block.hash(), block.into_body()))
.collect::<HashMap<_, _>>();

insert_headers(db.db(), &headers);

Expand Down
2 changes: 1 addition & 1 deletion crates/net/downloaders/src/bodies/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn zip_blocks<'a, H: Clone + BlockHeader + 'a, B>(
if header.is_empty() {
BlockResponse::Empty(header.clone())
} else {
BlockResponse::Full(SealedBlock { header: header.clone(), body })
BlockResponse::Full(SealedBlock::new(header.clone(), body))
}
})
.collect()
Expand Down
2 changes: 1 addition & 1 deletion crates/net/downloaders/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) fn generate_bodies(
);

let headers = blocks.iter().map(|block| block.header.clone()).collect();
let bodies = blocks.into_iter().map(|block| (block.hash(), block.body)).collect();
let bodies = blocks.into_iter().map(|block| (block.hash(), block.into_body())).collect();

(headers, bodies)
}
Expand Down
Loading
Loading