From 3658a7a1919a5ce6b765b16c7e04443e2c606505 Mon Sep 17 00:00:00 2001 From: frankudoags Date: Sun, 5 Jan 2025 11:53:32 +0100 Subject: [PATCH] refactor: replace all instances of direct transaction length access with transaction_count helper method --- crates/prune/prune/src/segments/mod.rs | 9 +++------ crates/prune/prune/src/segments/receipts.rs | 8 ++++---- .../prune/src/segments/static_file/transactions.rs | 4 ++-- .../prune/src/segments/user/receipts_by_logs.rs | 6 +++--- .../prune/prune/src/segments/user/sender_recovery.rs | 8 ++++---- .../prune/src/segments/user/transaction_lookup.rs | 8 ++++---- crates/stages/stages/src/stages/bodies.rs | 2 +- crates/stages/stages/src/stages/hashing_storage.rs | 2 +- crates/stages/stages/src/stages/mod.rs | 2 +- crates/stages/stages/src/stages/sender_recovery.rs | 8 ++++---- crates/stages/stages/src/stages/tx_lookup.rs | 6 +++--- crates/stages/stages/src/test_utils/test_db.rs | 4 ++-- .../provider/src/providers/blockchain_provider.rs | 12 ++++++------ 13 files changed, 38 insertions(+), 41 deletions(-) diff --git a/crates/prune/prune/src/segments/mod.rs b/crates/prune/prune/src/segments/mod.rs index 25f4b39cbb93..9f9e989dc06a 100644 --- a/crates/prune/prune/src/segments/mod.rs +++ b/crates/prune/prune/src/segments/mod.rs @@ -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::(); + let num_txs = blocks.iter().map(|block| block.transaction_count() as u64).sum::(); assert_eq!(range, 0..=num_txs - 1); } @@ -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::(); + let num_txs = blocks.iter().map(|block| block.transaction_count() as u64).sum::(); assert_eq!(range, 0..=num_txs - 1,); } @@ -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::(); + let num_txs = blocks.iter().map(|block| block.transaction_count() as u64).sum::(); let max_range = num_txs - 1; // Create a prune input with a previous checkpoint that is the last tx number diff --git a/crates/prune/prune/src/segments/receipts.rs b/crates/prune/prune/src/segments/receipts.rs index 922d11e83a64..50a21031f9ba 100644 --- a/crates/prune/prune/src/segments/receipts.rs +++ b/crates/prune/prune/src/segments/receipts.rs @@ -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)))); @@ -124,7 +124,7 @@ mod tests { assert_eq!( db.table::().unwrap().len(), - blocks.iter().map(|block| block.body().transactions.len()).sum::() + blocks.iter().map(|block| block.transaction_count()).sum::() ); assert_eq!( db.table::().unwrap().len(), @@ -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::() .min( next_tx_number_to_prune as usize + @@ -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)) diff --git a/crates/prune/prune/src/segments/static_file/transactions.rs b/crates/prune/prune/src/segments/static_file/transactions.rs index 19a6fd06d0e7..f3e5c3ffa972 100644 --- a/crates/prune/prune/src/segments/static_file/transactions.rs +++ b/crates/prune/prune/src/segments/static_file/transactions.rs @@ -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::() .min( next_tx_number_to_prune as usize + @@ -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)) diff --git a/crates/prune/prune/src/segments/user/receipts_by_logs.rs b/crates/prune/prune/src/segments/user/receipts_by_logs.rs index b82f38e5f1a8..4706b560bd31 100644 --- a/crates/prune/prune/src/segments/user/receipts_by_logs.rs +++ b/crates/prune/prune/src/segments/user/receipts_by_logs.rs @@ -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)); @@ -288,7 +288,7 @@ mod tests { assert_eq!( db.table::().unwrap().len(), - blocks.iter().map(|block| block.body().transactions.len()).sum::() + blocks.iter().map(|block| block.transaction_count()).sum::() ); assert_eq!( db.table::().unwrap().len(), @@ -337,7 +337,7 @@ mod tests { assert_eq!( db.table::().unwrap().len(), - blocks.iter().map(|block| block.body().transactions.len()).sum::() - + blocks.iter().map(|block| block.transaction_count()).sum::() - ((pruned_tx + 1) - unprunable) as usize ); diff --git a/crates/prune/prune/src/segments/user/sender_recovery.rs b/crates/prune/prune/src/segments/user/sender_recovery.rs index a119b8ca8fd1..be5a7842c58b 100644 --- a/crates/prune/prune/src/segments/user/sender_recovery.rs +++ b/crates/prune/prune/src/segments/user/sender_recovery.rs @@ -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, @@ -124,7 +124,7 @@ mod tests { assert_eq!( db.table::().unwrap().len(), - blocks.iter().map(|block| block.body().transactions.len()).sum::() + blocks.iter().map(|block| block.transaction_count()).sum::() ); assert_eq!( db.table::().unwrap().len(), @@ -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::() .min( next_tx_number_to_prune as usize + @@ -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)) diff --git a/crates/prune/prune/src/segments/user/transaction_lookup.rs b/crates/prune/prune/src/segments/user/transaction_lookup.rs index f1637123ad02..2629c217f0d0 100644 --- a/crates/prune/prune/src/segments/user/transaction_lookup.rs +++ b/crates/prune/prune/src/segments/user/transaction_lookup.rs @@ -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)); } @@ -149,7 +149,7 @@ mod tests { assert_eq!( db.table::().unwrap().len(), - blocks.iter().map(|block| block.body().transactions.len()).sum::() + blocks.iter().map(|block| block.transaction_count()).sum::() ); assert_eq!( db.table::().unwrap().len(), @@ -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::() .min( next_tx_number_to_prune as usize + @@ -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)) diff --git a/crates/stages/stages/src/stages/bodies.rs b/crates/stages/stages/src/stages/bodies.rs index 9bc1bcc45ca0..b17ad3562a09 100644 --- a/crates/stages/stages/src/stages/bodies.rs +++ b/crates/stages/stages/src/stages/bodies.rs @@ -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); diff --git a/crates/stages/stages/src/stages/hashing_storage.rs b/crates/stages/stages/src/stages/hashing_storage.rs index 6110d2101704..6075e62158fd 100644 --- a/crates/stages/stages/src/stages/hashing_storage.rs +++ b/crates/stages/stages/src/stages/hashing_storage.rs @@ -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; diff --git a/crates/stages/stages/src/stages/mod.rs b/crates/stages/stages/src/stages/mod.rs index 22ba6e139ca4..e5cf6d525c28 100644 --- a/crates/stages/stages/src/stages/mod.rs +++ b/crates/stages/stages/src/stages/mod.rs @@ -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; diff --git a/crates/stages/stages/src/stages/sender_recovery.rs b/crates/stages/stages/src/stages/sender_recovery.rs index f832df7ffeb7..8d768265465f 100644 --- a/crates/stages/stages/src/stages/sender_recovery.rs +++ b/crates/stages/stages/src/stages/sender_recovery.rs @@ -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) @@ -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, @@ -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() } ); } diff --git a/crates/stages/stages/src/stages/tx_lookup.rs b/crates/stages/stages/src/stages/tx_lookup.rs index 04542e1e08bf..dd15c4f43fca 100644 --- a/crates/stages/stages/src/stages/tx_lookup.rs +++ b/crates/stages/stages/src/stages/tx_lookup.rs @@ -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::() .sub(1), // `TxNumber` is 0-indexed ), @@ -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() } ); } diff --git a/crates/stages/stages/src/test_utils/test_db.rs b/crates/stages/stages/src/test_utils/test_db.rs index 18680c8ec78a..5e4c61b6fd36 100644 --- a/crates/stages/stages/src/test_utils/test_db.rs +++ b/crates/stages/stages/src/test_utils/test_db.rs @@ -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() { @@ -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 } diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index 73601316d8f0..1f66c7ab5d38 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -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::() as u64; + database_blocks.iter().map(|b| b.transaction_count()).sum::() as u64; let in_mem_tx_count = - in_memory_blocks.iter().map(|b| b.body().transactions.len()).sum::() as u64; + in_memory_blocks.iter().map(|b| b.transaction_count()).sum::() as u64; let db_range = 0..=(db_tx_count - 1); let in_mem_range = db_tx_count..=(in_mem_tx_count + db_range.end()); @@ -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::() as u64 }; @@ -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::() as u64 }; @@ -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 @@ -2725,7 +2725,7 @@ mod tests { canonical_in_memory_state: CanonicalInMemoryState, _factory: ProviderFactory| { 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)