Skip to content

Commit

Permalink
feat: Implement "Add BlockBody::transaction_count helper function" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
frankudoags authored Jan 5, 2025
1 parent bfa97c1 commit f4ce10b
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 41 deletions.
4 changes: 4 additions & 0 deletions crates/primitives-traits/src/block/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub trait BlockBody:
self.transactions().iter().map(|tx| tx.tx_hash())
}

/// Returns the number of the transactions in the block.
fn transaction_count(&self) -> usize {
self.transactions().len()
}
/// Consume the block body and return a [`Vec`] of transactions.
fn into_transactions(self) -> Vec<Self::Transaction>;

Expand Down
21 changes: 21 additions & 0 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ where
}
}

impl<H, B> SealedBlock<H, B>
where
B: reth_primitives_traits::BlockBody,
{
/// Returns the number of transactions in the block.
#[inline]
pub fn transaction_count(&self) -> usize {
self.body.transaction_count()
}
}

impl<H, B> SealedBlock<H, B>
where
H: alloy_consensus::BlockHeader,
Expand Down Expand Up @@ -918,4 +929,14 @@ mod tests {
let decoded = BlockBody::decode(&mut buf.as_slice()).unwrap();
assert_eq!(body, decoded);
}

#[test]
fn test_transaction_count() {
let mut block = Block::default();
assert_eq!(block.body.transaction_count(), 0);
block.body.transactions.push(TransactionSigned::default());
assert_eq!(block.body.transaction_count(), 1);
block.body.transactions.push(TransactionSigned::default());
assert_eq!(block.body.transaction_count(), 2);
}
}
9 changes: 3 additions & 6 deletions crates/prune/prune/src/segments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ mod tests {
let range = input.get_next_tx_num_range(&provider).expect("Expected range").unwrap();

// Calculate the total number of transactions
let num_txs =
blocks.iter().map(|block| block.body().transactions.len() as u64).sum::<u64>();
let num_txs = blocks.iter().map(|block| block.transaction_count() as u64).sum::<u64>();

assert_eq!(range, 0..=num_txs - 1);
}
Expand Down Expand Up @@ -289,8 +288,7 @@ mod tests {
let range = input.get_next_tx_num_range(&provider).expect("Expected range").unwrap();

// Calculate the total number of transactions
let num_txs =
blocks.iter().map(|block| block.body().transactions.len() as u64).sum::<u64>();
let num_txs = blocks.iter().map(|block| block.transaction_count() as u64).sum::<u64>();

assert_eq!(range, 0..=num_txs - 1,);
}
Expand Down Expand Up @@ -324,8 +322,7 @@ mod tests {

// Get the last tx number
// Calculate the total number of transactions
let num_txs =
blocks.iter().map(|block| block.body().transactions.len() as u64).sum::<u64>();
let num_txs = blocks.iter().map(|block| block.transaction_count() as u64).sum::<u64>();
let max_range = num_txs - 1;

// Create a prune input with a previous checkpoint that is the last tx number
Expand Down
8 changes: 4 additions & 4 deletions crates/prune/prune/src/segments/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ mod tests {

let mut receipts = Vec::new();
for block in &blocks {
receipts.reserve_exact(block.body().transactions.len());
receipts.reserve_exact(block.transaction_count());
for transaction in &block.body().transactions {
receipts
.push((receipts.len() as u64, random_receipt(&mut rng, transaction, Some(0))));
Expand All @@ -124,7 +124,7 @@ mod tests {

assert_eq!(
db.table::<tables::Transactions>().unwrap().len(),
blocks.iter().map(|block| block.body().transactions.len()).sum::<usize>()
blocks.iter().map(|block| block.transaction_count()).sum::<usize>()
);
assert_eq!(
db.table::<tables::Transactions>().unwrap().len(),
Expand Down Expand Up @@ -158,7 +158,7 @@ mod tests {
let last_pruned_tx_number = blocks
.iter()
.take(to_block as usize)
.map(|block| block.body().transactions.len())
.map(|block| block.transaction_count())
.sum::<usize>()
.min(
next_tx_number_to_prune as usize +
Expand Down Expand Up @@ -186,7 +186,7 @@ mod tests {
let last_pruned_block_number = blocks
.iter()
.fold_while((0, 0), |(_, mut tx_count), block| {
tx_count += block.body().transactions.len();
tx_count += block.transaction_count();

if tx_count > last_pruned_tx_number {
Done((block.number, tx_count))
Expand Down
4 changes: 2 additions & 2 deletions crates/prune/prune/src/segments/static_file/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ mod tests {
let last_pruned_tx_number = blocks
.iter()
.take(to_block as usize)
.map(|block| block.body().transactions.len())
.map(|block| block.transaction_count())
.sum::<usize>()
.min(
next_tx_number_to_prune as usize +
Expand All @@ -185,7 +185,7 @@ mod tests {
let last_pruned_block_number = blocks
.iter()
.fold_while((0, 0), |(_, mut tx_count), block| {
tx_count += block.body().transactions.len();
tx_count += block.transaction_count();

if tx_count > last_pruned_tx_number {
Done((block.number, tx_count))
Expand Down
6 changes: 3 additions & 3 deletions crates/prune/prune/src/segments/user/receipts_by_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ mod tests {
let mut receipt = random_receipt(&mut rng, transaction, Some(1));
receipt.logs.push(random_log(
&mut rng,
(txi == (block.body().transactions.len() - 1)).then_some(deposit_contract_addr),
(txi == (block.transaction_count() - 1)).then_some(deposit_contract_addr),
Some(1),
));
receipts.push((receipts.len() as u64, receipt));
Expand All @@ -288,7 +288,7 @@ mod tests {

assert_eq!(
db.table::<tables::Transactions>().unwrap().len(),
blocks.iter().map(|block| block.body().transactions.len()).sum::<usize>()
blocks.iter().map(|block| block.transaction_count()).sum::<usize>()
);
assert_eq!(
db.table::<tables::Transactions>().unwrap().len(),
Expand Down Expand Up @@ -337,7 +337,7 @@ mod tests {

assert_eq!(
db.table::<tables::Receipts>().unwrap().len(),
blocks.iter().map(|block| block.body().transactions.len()).sum::<usize>() -
blocks.iter().map(|block| block.transaction_count()).sum::<usize>() -
((pruned_tx + 1) - unprunable) as usize
);

Expand Down
8 changes: 4 additions & 4 deletions crates/prune/prune/src/segments/user/sender_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ mod tests {

let mut transaction_senders = Vec::new();
for block in &blocks {
transaction_senders.reserve_exact(block.body().transactions.len());
transaction_senders.reserve_exact(block.transaction_count());
for transaction in &block.body().transactions {
transaction_senders.push((
transaction_senders.len() as u64,
Expand All @@ -124,7 +124,7 @@ mod tests {

assert_eq!(
db.table::<tables::Transactions>().unwrap().len(),
blocks.iter().map(|block| block.body().transactions.len()).sum::<usize>()
blocks.iter().map(|block| block.transaction_count()).sum::<usize>()
);
assert_eq!(
db.table::<tables::Transactions>().unwrap().len(),
Expand Down Expand Up @@ -159,7 +159,7 @@ mod tests {
let last_pruned_tx_number = blocks
.iter()
.take(to_block as usize)
.map(|block| block.body().transactions.len())
.map(|block| block.transaction_count())
.sum::<usize>()
.min(
next_tx_number_to_prune as usize +
Expand All @@ -170,7 +170,7 @@ mod tests {
let last_pruned_block_number = blocks
.iter()
.fold_while((0, 0), |(_, mut tx_count), block| {
tx_count += block.body().transactions.len();
tx_count += block.transaction_count();

if tx_count > last_pruned_tx_number {
Done((block.number, tx_count))
Expand Down
8 changes: 4 additions & 4 deletions crates/prune/prune/src/segments/user/transaction_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mod tests {

let mut tx_hash_numbers = Vec::new();
for block in &blocks {
tx_hash_numbers.reserve_exact(block.body().transactions.len());
tx_hash_numbers.reserve_exact(block.transaction_count());
for transaction in &block.body().transactions {
tx_hash_numbers.push((transaction.hash(), tx_hash_numbers.len() as u64));
}
Expand All @@ -149,7 +149,7 @@ mod tests {

assert_eq!(
db.table::<tables::Transactions>().unwrap().len(),
blocks.iter().map(|block| block.body().transactions.len()).sum::<usize>()
blocks.iter().map(|block| block.transaction_count()).sum::<usize>()
);
assert_eq!(
db.table::<tables::Transactions>().unwrap().len(),
Expand Down Expand Up @@ -184,7 +184,7 @@ mod tests {
let last_pruned_tx_number = blocks
.iter()
.take(to_block as usize)
.map(|block| block.body().transactions.len())
.map(|block| block.transaction_count())
.sum::<usize>()
.min(
next_tx_number_to_prune as usize +
Expand All @@ -195,7 +195,7 @@ mod tests {
let last_pruned_block_number = blocks
.iter()
.fold_while((0, 0), |(_, mut tx_count), block| {
tx_count += block.body().transactions.len();
tx_count += block.transaction_count();

if tx_count > last_pruned_tx_number {
Done((block.number, tx_count))
Expand Down
2 changes: 1 addition & 1 deletion crates/stages/stages/src/stages/bodies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ mod tests {

let body = StoredBlockBodyIndices {
first_tx_num: 0,
tx_count: progress.body().transactions.len() as u64,
tx_count: progress.transaction_count() as u64,
};

static_file_producer.set_block_range(0..=progress.number);
Expand Down
2 changes: 1 addition & 1 deletion crates/stages/stages/src/stages/hashing_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ mod tests {

let body = StoredBlockBodyIndices {
first_tx_num,
tx_count: progress.body().transactions.len() as u64,
tx_count: progress.transaction_count() as u64,
};

first_tx_num = next_tx_num;
Expand Down
2 changes: 1 addition & 1 deletion crates/stages/stages/src/stages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ mod tests {
let mut receipts = Vec::with_capacity(blocks.len());
let mut tx_num = 0u64;
for block in &blocks {
let mut block_receipts = Vec::with_capacity(block.body().transactions.len());
let mut block_receipts = Vec::with_capacity(block.transaction_count());
for transaction in &block.body().transactions {
block_receipts.push((tx_num, random_receipt(&mut rng, transaction, Some(0))));
tx_num += 1;
Expand Down
8 changes: 4 additions & 4 deletions crates/stages/stages/src/stages/sender_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ mod tests {
let expected_progress = seed
.iter()
.find(|x| {
tx_count += x.body().transactions.len();
tx_count += x.transaction_count();
tx_count as u64 > threshold
})
.map(|x| x.number)
Expand Down Expand Up @@ -555,7 +555,7 @@ mod tests {
tx_number: Some(
blocks[..=max_pruned_block as usize]
.iter()
.map(|block| block.body().transactions.len() as u64)
.map(|block| block.transaction_count() as u64)
.sum(),
),
prune_mode: PruneMode::Full,
Expand All @@ -570,9 +570,9 @@ mod tests {
EntitiesCheckpoint {
processed: blocks[..=max_processed_block]
.iter()
.map(|block| block.body().transactions.len() as u64)
.map(|block| block.transaction_count() as u64)
.sum(),
total: blocks.iter().map(|block| block.body().transactions.len() as u64).sum()
total: blocks.iter().map(|block| block.transaction_count() as u64).sum()
}
);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/stages/stages/src/stages/tx_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ mod tests {
tx_number: Some(
blocks[..=max_pruned_block as usize]
.iter()
.map(|block| block.body().transactions.len() as u64)
.map(|block| block.transaction_count() as u64)
.sum::<u64>()
.sub(1), // `TxNumber` is 0-indexed
),
Expand All @@ -419,9 +419,9 @@ mod tests {
EntitiesCheckpoint {
processed: blocks[..=max_processed_block]
.iter()
.map(|block| block.body().transactions.len() as u64)
.map(|block| block.transaction_count() as u64)
.sum(),
total: blocks.iter().map(|block| block.body().transactions.len() as u64).sum()
total: blocks.iter().map(|block| block.transaction_count() as u64).sum()
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/stages/stages/src/test_utils/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl TestStageDB {
// Insert into body tables.
let block_body_indices = StoredBlockBodyIndices {
first_tx_num: next_tx_num,
tx_count: block.body().transactions.len() as u64,
tx_count: block.transaction_count() as u64,
};

if !block.body().transactions.is_empty() {
Expand Down Expand Up @@ -489,7 +489,7 @@ impl StorageKind {

fn tx_offset(&self) -> u64 {
if let Self::Database(offset) = self {
return offset.unwrap_or_default()
return offset.unwrap_or_default();
}
0
}
Expand Down
12 changes: 6 additions & 6 deletions crates/storage/provider/src/providers/blockchain_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2167,9 +2167,9 @@ mod tests {
$(
// Since data moves for each tried method, need to recalculate everything
let db_tx_count =
database_blocks.iter().map(|b| b.body().transactions.len()).sum::<usize>() as u64;
database_blocks.iter().map(|b| b.transaction_count()).sum::<usize>() as u64;
let in_mem_tx_count =
in_memory_blocks.iter().map(|b| b.body().transactions.len()).sum::<usize>() as u64;
in_memory_blocks.iter().map(|b| b.transaction_count()).sum::<usize>() as u64;

let db_range = 0..=(db_tx_count - 1);
let in_mem_range = db_tx_count..=(in_mem_tx_count + db_range.end());
Expand Down Expand Up @@ -2410,7 +2410,7 @@ mod tests {
.iter()
.chain(in_memory_blocks.iter())
.take_while(|b| b.number < block.number)
.map(|b| b.body().transactions.len())
.map(|b| b.transaction_count())
.sum::<usize>() as u64
};

Expand All @@ -2431,7 +2431,7 @@ mod tests {
.iter()
.chain(in_memory_blocks.iter())
.take_while(|b| b.number < block.number)
.map(|b| b.body().transactions.len())
.map(|b| b.transaction_count())
.sum::<usize>() as u64
};

Expand Down Expand Up @@ -2527,7 +2527,7 @@ mod tests {
block.number,
Some(StoredBlockBodyIndices {
first_tx_num: tx_num,
tx_count: block.body().transactions.len() as u64
tx_count: block.transaction_count() as u64
})
),
u64::MAX
Expand Down Expand Up @@ -2725,7 +2725,7 @@ mod tests {
canonical_in_memory_state: CanonicalInMemoryState,
_factory: ProviderFactory<MockNodeTypesWithDB>| {
if let Some(tx) = canonical_in_memory_state.transaction_by_hash(hash) {
return Ok::<_, ProviderError>(Some(tx))
return Ok::<_, ProviderError>(Some(tx));
}
panic!("should not be in database");
// _factory.transaction_by_hash(hash)
Expand Down

0 comments on commit f4ce10b

Please sign in to comment.