From 033b9861c2fa04f2081375609d8ac696c6fd9541 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Mon, 23 Dec 2024 21:43:12 +0700 Subject: [PATCH 1/2] feat: generic data primitives block builder test framework --- crates/chain-state/src/in_memory.rs | 4 +-- crates/chain-state/src/test_utils.rs | 4 +-- crates/engine/tree/src/persistence.rs | 6 ++--- crates/engine/tree/src/tree/mod.rs | 26 +++++++++---------- .../src/providers/blockchain_provider.rs | 4 +-- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index 3f8fa8a5a88e..a1c05595bcc6 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -1408,7 +1408,7 @@ mod tests { #[test] fn test_canonical_in_memory_state_canonical_chain_multiple_blocks() { let mut parent_hash = B256::random(); - let mut block_builder = TestBlockBuilder::default(); + let mut block_builder = TestBlockBuilder::::default(); let state: CanonicalInMemoryState = CanonicalInMemoryState::empty(); for i in 1..=3 { @@ -1430,7 +1430,7 @@ mod tests { #[test] fn test_canonical_in_memory_state_canonical_chain_with_pending_block() { let mut parent_hash = B256::random(); - let mut block_builder = TestBlockBuilder::default(); + let mut block_builder = TestBlockBuilder::::default(); let state: CanonicalInMemoryState = CanonicalInMemoryState::empty(); for i in 1..=2 { diff --git a/crates/chain-state/src/test_utils.rs b/crates/chain-state/src/test_utils.rs index 0741e958db45..00ca61210c5d 100644 --- a/crates/chain-state/src/test_utils.rs +++ b/crates/chain-state/src/test_utils.rs @@ -34,7 +34,7 @@ use tokio::sync::broadcast::{self, Sender}; /// Functionality to build blocks for tests and help with assertions about /// their execution. #[derive(Debug)] -pub struct TestBlockBuilder { +pub struct TestBlockBuilder { /// The account that signs all the block's transactions. pub signer: Address, /// Private key for signing. @@ -66,7 +66,7 @@ impl Default for TestBlockBuilder { } } -impl TestBlockBuilder { +impl TestBlockBuilder { /// Signer pk setter. pub fn with_signer_pk(mut self, signer_pk: PrivateKeySigner) -> Self { self.signer = signer_pk.address(); diff --git a/crates/engine/tree/src/persistence.rs b/crates/engine/tree/src/persistence.rs index c7ad41100866..b7c4dbaeed1d 100644 --- a/crates/engine/tree/src/persistence.rs +++ b/crates/engine/tree/src/persistence.rs @@ -336,7 +336,7 @@ mod tests { reth_tracing::init_test_tracing(); let persistence_handle = default_persistence_handle(); let block_number = 0; - let mut test_block_builder = TestBlockBuilder::default(); + let mut test_block_builder = TestBlockBuilder::::default(); let executed = test_block_builder.get_executed_block_with_number(block_number, B256::random()); let block_hash = executed.block().hash(); @@ -361,7 +361,7 @@ mod tests { reth_tracing::init_test_tracing(); let persistence_handle = default_persistence_handle(); - let mut test_block_builder = TestBlockBuilder::default(); + let mut test_block_builder = TestBlockBuilder::::default(); let blocks = test_block_builder.get_executed_blocks(0..5).collect::>(); let last_hash = blocks.last().unwrap().block().hash(); let (tx, rx) = oneshot::channel(); @@ -377,7 +377,7 @@ mod tests { let persistence_handle = default_persistence_handle(); let ranges = [0..1, 1..2, 2..4, 4..5]; - let mut test_block_builder = TestBlockBuilder::default(); + let mut test_block_builder = TestBlockBuilder::::default(); for range in ranges { let blocks = test_block_builder.get_executed_blocks(range).collect::>(); let last_hash = blocks.last().unwrap().block().hash(); diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs index eb3798692e5e..9091166007ce 100644 --- a/crates/engine/tree/src/tree/mod.rs +++ b/crates/engine/tree/src/tree/mod.rs @@ -3106,7 +3106,7 @@ mod tests { let tree_config = TreeConfig::default(); let chain_spec = MAINNET.clone(); let mut test_block_builder = - TestBlockBuilder::default().with_chain_spec((*chain_spec).clone()); + TestBlockBuilder::::default().with_chain_spec((*chain_spec).clone()); // we need more than tree_config.persistence_threshold() +1 blocks to // trigger the persistence task. @@ -3141,7 +3141,7 @@ mod tests { let tree_config = TreeConfig::default(); let chain_spec = MAINNET.clone(); let mut test_block_builder = - TestBlockBuilder::default().with_chain_spec((*chain_spec).clone()); + TestBlockBuilder::::default().with_chain_spec((*chain_spec).clone()); // we need more than tree_config.persistence_threshold() +1 blocks to // trigger the persistence task. @@ -3173,7 +3173,7 @@ mod tests { #[tokio::test] async fn test_in_memory_state_trait_impl() { - let blocks: Vec<_> = TestBlockBuilder::default().get_executed_blocks(0..10).collect(); + let blocks: Vec<_> = TestBlockBuilder::::default().get_executed_blocks(0..10).collect(); let test_harness = TestHarness::new(MAINNET.clone()).with_blocks(blocks.clone()); for executed_block in blocks { @@ -3200,7 +3200,7 @@ mod tests { #[tokio::test] async fn test_engine_request_during_backfill() { let tree_config = TreeConfig::default(); - let blocks: Vec<_> = TestBlockBuilder::default() + let blocks: Vec<_> = TestBlockBuilder::::default() .get_executed_blocks(0..tree_config.persistence_threshold()) .collect(); let mut test_harness = TestHarness::new(MAINNET.clone()) @@ -3301,7 +3301,7 @@ mod tests { #[tokio::test] async fn test_tree_state_insert_executed() { let mut tree_state = TreeState::new(BlockNumHash::default()); - let blocks: Vec<_> = TestBlockBuilder::default().get_executed_blocks(1..4).collect(); + let blocks: Vec<_> = TestBlockBuilder::::default().get_executed_blocks(1..4).collect(); tree_state.insert_executed(blocks[0].clone()); tree_state.insert_executed(blocks[1].clone()); @@ -3327,7 +3327,7 @@ mod tests { #[tokio::test] async fn test_tree_state_insert_executed_with_reorg() { let mut tree_state = TreeState::new(BlockNumHash::default()); - let mut test_block_builder = TestBlockBuilder::default(); + let mut test_block_builder = TestBlockBuilder::::default(); let blocks: Vec<_> = test_block_builder.get_executed_blocks(1..6).collect(); for block in &blocks { @@ -3367,7 +3367,7 @@ mod tests { async fn test_tree_state_remove_before() { let start_num_hash = BlockNumHash::default(); let mut tree_state = TreeState::new(start_num_hash); - let blocks: Vec<_> = TestBlockBuilder::default().get_executed_blocks(1..6).collect(); + let blocks: Vec<_> = TestBlockBuilder::::default().get_executed_blocks(1..6).collect(); for block in &blocks { tree_state.insert_executed(block.clone()); @@ -3417,7 +3417,7 @@ mod tests { async fn test_tree_state_remove_before_finalized() { let start_num_hash = BlockNumHash::default(); let mut tree_state = TreeState::new(start_num_hash); - let blocks: Vec<_> = TestBlockBuilder::default().get_executed_blocks(1..6).collect(); + let blocks: Vec<_> = TestBlockBuilder::::default().get_executed_blocks(1..6).collect(); for block in &blocks { tree_state.insert_executed(block.clone()); @@ -3467,7 +3467,7 @@ mod tests { async fn test_tree_state_remove_before_lower_finalized() { let start_num_hash = BlockNumHash::default(); let mut tree_state = TreeState::new(start_num_hash); - let blocks: Vec<_> = TestBlockBuilder::default().get_executed_blocks(1..6).collect(); + let blocks: Vec<_> = TestBlockBuilder::::default().get_executed_blocks(1..6).collect(); for block in &blocks { tree_state.insert_executed(block.clone()); @@ -3517,7 +3517,7 @@ mod tests { async fn test_tree_state_on_new_head() { let chain_spec = MAINNET.clone(); let mut test_harness = TestHarness::new(chain_spec); - let mut test_block_builder = TestBlockBuilder::default(); + let mut test_block_builder = TestBlockBuilder::::default(); let blocks: Vec<_> = test_block_builder.get_executed_blocks(1..6).collect(); @@ -3569,7 +3569,7 @@ mod tests { let chain_spec = MAINNET.clone(); let mut test_harness = TestHarness::new(chain_spec); - let mut test_block_builder = TestBlockBuilder::default(); + let mut test_block_builder = TestBlockBuilder::::default(); let blocks: Vec<_> = test_block_builder.get_executed_blocks(0..5).collect(); @@ -3636,7 +3636,7 @@ mod tests { async fn test_get_canonical_blocks_to_persist() { let chain_spec = MAINNET.clone(); let mut test_harness = TestHarness::new(chain_spec); - let mut test_block_builder = TestBlockBuilder::default(); + let mut test_block_builder = TestBlockBuilder::::default(); let canonical_head_number = 9; let blocks: Vec<_> = @@ -3692,7 +3692,7 @@ mod tests { let mut test_harness = TestHarness::new(chain_spec.clone()); let mut test_block_builder = - TestBlockBuilder::default().with_chain_spec((*chain_spec).clone()); + TestBlockBuilder::::default().with_chain_spec((*chain_spec).clone()); let blocks: Vec<_> = test_block_builder.get_executed_blocks(0..5).collect(); test_harness = test_harness.with_blocks(blocks); diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index e621a56030c2..c24a8e9b3315 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -816,7 +816,7 @@ mod tests { use reth_db_api::{cursor::DbCursorRO, transaction::DbTx}; use reth_errors::ProviderError; use reth_execution_types::{Chain, ExecutionOutcome}; - use reth_primitives::{BlockExt, Receipt, SealedBlock, StaticFileSegment}; + use reth_primitives::{BlockExt, EthPrimitives, Receipt, SealedBlock, StaticFileSegment}; use reth_primitives_traits::{BlockBody as _, SignedTransaction}; use reth_storage_api::{ BlockBodyIndicesProvider, BlockHashReader, BlockIdReader, BlockNumReader, BlockReader, @@ -1408,7 +1408,7 @@ mod tests { let factory = create_test_provider_factory(); // Generate a random block to initialise the blockchain provider. - let mut test_block_builder = TestBlockBuilder::default(); + let mut test_block_builder = TestBlockBuilder::::default(); let block_1 = test_block_builder.generate_random_block(0, B256::ZERO); let block_hash_1 = block_1.hash(); From 300c03a5b408271400683eade0fb2ab0d0407eab Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Mon, 23 Dec 2024 21:53:28 +0700 Subject: [PATCH 2/2] bet --- crates/engine/tree/src/tree/mod.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs index 9091166007ce..ec30c5cbc7b3 100644 --- a/crates/engine/tree/src/tree/mod.rs +++ b/crates/engine/tree/src/tree/mod.rs @@ -3173,7 +3173,8 @@ mod tests { #[tokio::test] async fn test_in_memory_state_trait_impl() { - let blocks: Vec<_> = TestBlockBuilder::::default().get_executed_blocks(0..10).collect(); + let blocks: Vec<_> = + TestBlockBuilder::::default().get_executed_blocks(0..10).collect(); let test_harness = TestHarness::new(MAINNET.clone()).with_blocks(blocks.clone()); for executed_block in blocks { @@ -3301,7 +3302,8 @@ mod tests { #[tokio::test] async fn test_tree_state_insert_executed() { let mut tree_state = TreeState::new(BlockNumHash::default()); - let blocks: Vec<_> = TestBlockBuilder::::default().get_executed_blocks(1..4).collect(); + let blocks: Vec<_> = + TestBlockBuilder::::default().get_executed_blocks(1..4).collect(); tree_state.insert_executed(blocks[0].clone()); tree_state.insert_executed(blocks[1].clone()); @@ -3367,7 +3369,8 @@ mod tests { async fn test_tree_state_remove_before() { let start_num_hash = BlockNumHash::default(); let mut tree_state = TreeState::new(start_num_hash); - let blocks: Vec<_> = TestBlockBuilder::::default().get_executed_blocks(1..6).collect(); + let blocks: Vec<_> = + TestBlockBuilder::::default().get_executed_blocks(1..6).collect(); for block in &blocks { tree_state.insert_executed(block.clone()); @@ -3417,7 +3420,8 @@ mod tests { async fn test_tree_state_remove_before_finalized() { let start_num_hash = BlockNumHash::default(); let mut tree_state = TreeState::new(start_num_hash); - let blocks: Vec<_> = TestBlockBuilder::::default().get_executed_blocks(1..6).collect(); + let blocks: Vec<_> = + TestBlockBuilder::::default().get_executed_blocks(1..6).collect(); for block in &blocks { tree_state.insert_executed(block.clone()); @@ -3467,7 +3471,8 @@ mod tests { async fn test_tree_state_remove_before_lower_finalized() { let start_num_hash = BlockNumHash::default(); let mut tree_state = TreeState::new(start_num_hash); - let blocks: Vec<_> = TestBlockBuilder::::default().get_executed_blocks(1..6).collect(); + let blocks: Vec<_> = + TestBlockBuilder::::default().get_executed_blocks(1..6).collect(); for block in &blocks { tree_state.insert_executed(block.clone());