Refactor Block (#10913)

Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>
This commit is contained in:
Rohit Narurkar
2024-09-24 11:14:08 +01:00
committed by GitHub
parent b06bc1088a
commit fb0555ae7d
70 changed files with 739 additions and 749 deletions

View File

@ -172,7 +172,7 @@ where
let block_indices = StoredBlockBodyIndices {
first_tx_num: next_tx_num,
tx_count: match &response {
BlockResponse::Full(block) => block.body.len() as u64,
BlockResponse::Full(block) => block.body.transactions.len() as u64,
BlockResponse::Empty(_) => 0,
},
};
@ -195,12 +195,12 @@ where
match response {
BlockResponse::Full(block) => {
// write transaction block index
if !block.body.is_empty() {
if !block.body.transactions.is_empty() {
tx_block_cursor.append(block_indices.last_tx_num(), block.number)?;
}
// Write transactions
for transaction in block.body {
for transaction in block.body.transactions {
let appended_tx_number = static_file_producer
.append_transaction(next_tx_num, &transaction.into())?;
@ -219,13 +219,15 @@ where
}
// Write ommers if any
if !block.ommers.is_empty() {
ommers_cursor
.append(block_number, StoredBlockOmmers { ommers: block.ommers })?;
if !block.body.ommers.is_empty() {
ommers_cursor.append(
block_number,
StoredBlockOmmers { ommers: block.body.ommers },
)?;
}
// Write withdrawals if any
if let Some(withdrawals) = block.withdrawals {
if let Some(withdrawals) = block.body.withdrawals {
if !withdrawals.is_empty() {
withdrawals_cursor
.append(block_number, StoredBlockWithdrawals { withdrawals })?;
@ -233,7 +235,7 @@ where
}
// Write requests if any
if let Some(requests) = block.requests {
if let Some(requests) = block.body.requests {
if !requests.0.is_empty() {
requests_cursor.append(block_number, requests)?;
}
@ -657,15 +659,7 @@ mod tests {
/// A helper to create a collection of block bodies keyed by their hash.
pub(crate) fn body_by_hash(block: &SealedBlock) -> (B256, BlockBody) {
(
block.hash(),
BlockBody {
transactions: block.body.clone(),
ommers: block.ommers.clone(),
withdrawals: block.withdrawals.clone(),
requests: block.requests.clone(),
},
)
(block.hash(), block.body.clone())
}
/// A helper struct for running the [`BodyStage`].
@ -738,7 +732,7 @@ mod tests {
let body = StoredBlockBodyIndices {
first_tx_num: 0,
tx_count: progress.body.len() as u64,
tx_count: progress.body.transactions.len() as u64,
};
static_file_producer.set_block_range(0..=progress.number);
@ -762,7 +756,7 @@ mod tests {
if !progress.ommers_hash_is_empty() {
tx.put::<tables::BlockOmmers>(
progress.number,
StoredBlockOmmers { ommers: progress.ommers.clone() },
StoredBlockOmmers { ommers: progress.body.ommers.clone() },
)?;
}
@ -945,13 +939,7 @@ mod tests {
} else {
let body =
this.responses.remove(&header.hash()).expect("requested unknown body");
response.push(BlockResponse::Full(SealedBlock {
header,
body: body.transactions,
ommers: body.ommers,
withdrawals: body.withdrawals,
requests: body.requests,
}));
response.push(BlockResponse::Full(SealedBlock { header, body }));
}
if response.len() as u64 >= this.batch_size {

View File

@ -271,7 +271,7 @@ where
cumulative_gas += block.gas_used;
// Configure the executor to use the current state.
trace!(target: "sync::stages::execution", number = block_number, txs = block.body.len(), "Executing block");
trace!(target: "sync::stages::execution", number = block_number, txs = block.body.transactions.len(), "Executing block");
// Execute the block
let execute_start = Instant::now();

View File

@ -351,7 +351,7 @@ mod tests {
// Insert last progress data
let block_number = progress.number;
self.db.commit(|tx| {
progress.body.iter().try_for_each(
progress.body.transactions.iter().try_for_each(
|transaction| -> Result<(), reth_db::DatabaseError> {
tx.put::<tables::TransactionHashNumbers>(
transaction.hash(),
@ -399,7 +399,7 @@ mod tests {
let body = StoredBlockBodyIndices {
first_tx_num,
tx_count: progress.body.len() as u64,
tx_count: progress.body.transactions.len() as u64,
};
first_tx_num = next_tx_num;

View File

@ -523,7 +523,7 @@ mod tests {
accounts.iter().map(|(addr, acc)| (*addr, (*acc, std::iter::empty()))),
)?;
let SealedBlock { header, body, ommers, withdrawals, requests } = random_block(
let SealedBlock { header, body } = random_block(
&mut rng,
stage_progress,
BlockParams { parent: preblocks.last().map(|b| b.hash()), ..Default::default() },
@ -536,16 +536,9 @@ mod tests {
.into_iter()
.map(|(address, account)| (address, (account, std::iter::empty()))),
);
let sealed = header.seal_slow();
let (header, seal) = sealed.into_parts();
let sealed_head = SealedBlock {
header: SealedHeader::new(header, seal),
body,
ommers,
withdrawals,
requests,
};
let sealed_head = SealedBlock { header: SealedHeader::new(header, seal), body };
let head_hash = sealed_head.hash();
let mut blocks = vec![sealed_head];

View File

@ -268,8 +268,8 @@ mod tests {
let mut receipts = Vec::new();
let mut tx_num = 0u64;
for block in &blocks {
let mut block_receipts = Vec::with_capacity(block.body.len());
for transaction in &block.body {
let mut block_receipts = Vec::with_capacity(block.body.transactions.len());
for transaction in &block.body.transactions {
block_receipts.push((tx_num, random_receipt(&mut rng, transaction, Some(0))));
tx_num += 1;
}

View File

@ -213,9 +213,9 @@ mod tests {
);
self.db.insert_blocks(blocks.iter(), StorageKind::Static)?;
self.db.insert_transaction_senders(
blocks.iter().flat_map(|block| block.body.iter()).enumerate().map(|(i, tx)| {
(i as u64, tx.recover_signer().expect("failed to recover signer"))
}),
blocks.iter().flat_map(|block| block.body.transactions.iter()).enumerate().map(
|(i, tx)| (i as u64, tx.recover_signer().expect("failed to recover signer")),
),
)?;
Ok(blocks)
}

View File

@ -444,7 +444,7 @@ mod tests {
let expected_progress = seed
.iter()
.find(|x| {
tx_count += x.body.len();
tx_count += x.body.transactions.len();
tx_count as u64 > threshold
})
.map(|x| x.number)
@ -503,7 +503,7 @@ mod tests {
let mut tx_senders = Vec::new();
let mut tx_number = 0;
for block in &blocks[..=max_processed_block] {
for transaction in &block.body {
for transaction in &block.body.transactions {
if block.number > max_pruned_block {
tx_senders
.push((tx_number, transaction.recover_signer().expect("recover signer")));
@ -522,7 +522,7 @@ mod tests {
tx_number: Some(
blocks[..=max_pruned_block as usize]
.iter()
.map(|block| block.body.len() as u64)
.map(|block| block.body.transactions.len() as u64)
.sum::<u64>(),
),
prune_mode: PruneMode::Full,
@ -537,9 +537,9 @@ mod tests {
EntitiesCheckpoint {
processed: blocks[..=max_processed_block]
.iter()
.map(|block| block.body.len() as u64)
.map(|block| block.body.transactions.len() as u64)
.sum::<u64>(),
total: blocks.iter().map(|block| block.body.len() as u64).sum::<u64>()
total: blocks.iter().map(|block| block.body.transactions.len() as u64).sum::<u64>()
}
);
}

View File

@ -380,7 +380,7 @@ mod tests {
let mut tx_hash_numbers = Vec::new();
let mut tx_hash_number = 0;
for block in &blocks[..=max_processed_block] {
for transaction in &block.body {
for transaction in &block.body.transactions {
if block.number > max_pruned_block {
tx_hash_numbers.push((transaction.hash, tx_hash_number));
}
@ -398,7 +398,7 @@ mod tests {
tx_number: Some(
blocks[..=max_pruned_block as usize]
.iter()
.map(|block| block.body.len() as u64)
.map(|block| block.body.transactions.len() as u64)
.sum::<u64>()
.sub(1), // `TxNumber` is 0-indexed
),
@ -414,9 +414,9 @@ mod tests {
EntitiesCheckpoint {
processed: blocks[..=max_processed_block]
.iter()
.map(|block| block.body.len() as u64)
.map(|block| block.body.transactions.len() as u64)
.sum::<u64>(),
total: blocks.iter().map(|block| block.body.len() as u64).sum::<u64>()
total: blocks.iter().map(|block| block.body.transactions.len() as u64).sum::<u64>()
}
);
}

View File

@ -252,10 +252,10 @@ impl TestStageDB {
// Insert into body tables.
let block_body_indices = StoredBlockBodyIndices {
first_tx_num: next_tx_num,
tx_count: block.body.len() as u64,
tx_count: block.body.transactions.len() as u64,
};
if !block.body.is_empty() {
if !block.body.transactions.is_empty() {
tx.put::<tables::TransactionBlocks>(
block_body_indices.last_tx_num(),
block.number,
@ -263,7 +263,7 @@ impl TestStageDB {
}
tx.put::<tables::BlockBodyIndices>(block.number, block_body_indices)?;
let res = block.body.iter().try_for_each(|body_tx| {
let res = block.body.transactions.iter().try_for_each(|body_tx| {
if let Some(txs_writer) = &mut txs_writer {
txs_writer.append_transaction(next_tx_num, &body_tx.clone().into())?;
} else {