From 24495cdc5d75f938177e2135a5ccbac72dfbd8be Mon Sep 17 00:00:00 2001 From: Jennifer Date: Thu, 22 Aug 2024 21:58:25 +0100 Subject: [PATCH] Add tests for withdrawals provider (#10428) --- crates/blockchain-tree/src/block_buffer.rs | 2 +- crates/consensus/beacon/src/engine/mod.rs | 55 +++-- crates/net/downloaders/src/bodies/bodies.rs | 2 +- crates/net/downloaders/src/test_utils/mod.rs | 2 +- crates/prune/prune/src/segments/receipts.rs | 2 +- .../src/segments/static_file/transactions.rs | 2 +- .../src/segments/user/account_history.rs | 2 +- .../src/segments/user/receipts_by_logs.rs | 6 +- .../src/segments/user/sender_recovery.rs | 2 +- .../src/segments/user/storage_history.rs | 2 +- .../src/segments/user/transaction_lookup.rs | 2 +- crates/rpc/rpc-engine-api/src/engine_api.rs | 17 +- crates/rpc/rpc-engine-api/tests/it/payload.rs | 4 +- crates/stages/stages/benches/setup/mod.rs | 3 +- crates/stages/stages/src/stages/bodies.rs | 2 +- .../stages/src/stages/hashing_account.rs | 3 +- .../stages/src/stages/hashing_storage.rs | 3 +- .../src/stages/index_account_history.rs | 2 +- .../src/stages/index_storage_history.rs | 2 +- crates/stages/stages/src/stages/merkle.rs | 4 +- crates/stages/stages/src/stages/mod.rs | 5 +- crates/stages/stages/src/stages/prune.rs | 1 + .../stages/src/stages/sender_recovery.rs | 7 +- crates/stages/stages/src/stages/tx_lookup.rs | 14 +- .../static-file/src/static_file_producer.rs | 2 +- .../src/providers/blockchain_provider.rs | 220 ++++++++++++++---- .../provider/src/providers/database/mod.rs | 2 +- testing/testing-utils/src/generators.rs | 21 +- 28 files changed, 293 insertions(+), 98 deletions(-) diff --git a/crates/blockchain-tree/src/block_buffer.rs b/crates/blockchain-tree/src/block_buffer.rs index 4e673ffb323e..37eb3d2dc4de 100644 --- a/crates/blockchain-tree/src/block_buffer.rs +++ b/crates/blockchain-tree/src/block_buffer.rs @@ -191,7 +191,7 @@ mod tests { /// Create random block with specified number and parent hash. fn create_block(rng: &mut R, number: u64, parent: BlockHash) -> SealedBlockWithSenders { - let block = random_block(rng, number, Some(parent), None, None, None); + let block = random_block(rng, number, Some(parent), None, None, None, None); block.seal_with_senders().unwrap() } diff --git a/crates/consensus/beacon/src/engine/mod.rs b/crates/consensus/beacon/src/engine/mod.rs index 21605b042dae..79e8a77447a1 100644 --- a/crates/consensus/beacon/src/engine/mod.rs +++ b/crates/consensus/beacon/src/engine/mod.rs @@ -2230,8 +2230,8 @@ mod tests { })])) .build(); - let genesis = random_block(&mut rng, 0, None, None, Some(0), None); - let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None); + let genesis = random_block(&mut rng, 0, None, None, Some(0), None, None); + let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None, None); let (_static_dir, static_dir_path) = create_test_static_files_dir(); insert_blocks( @@ -2289,8 +2289,8 @@ mod tests { .disable_blockchain_tree_sync() .build(); - let genesis = random_block(&mut rng, 0, None, None, Some(0), None); - let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None); + let genesis = random_block(&mut rng, 0, None, None, Some(0), None, None); + let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None, None); let (_static_dir, static_dir_path) = create_test_static_files_dir(); @@ -2305,7 +2305,8 @@ mod tests { let mut engine_rx = spawn_consensus_engine(consensus_engine); - let next_head = random_block(&mut rng, 2, Some(block1.hash()), None, Some(0), None); + let next_head = + random_block(&mut rng, 2, Some(block1.hash()), None, Some(0), None, None); let next_forkchoice_state = ForkchoiceState { head_block_hash: next_head.hash(), finalized_block_hash: block1.hash(), @@ -2357,8 +2358,8 @@ mod tests { .disable_blockchain_tree_sync() .build(); - let genesis = random_block(&mut rng, 0, None, None, Some(0), None); - let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None); + let genesis = random_block(&mut rng, 0, None, None, Some(0), None, None); + let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None, None); let (_static_dir, static_dir_path) = create_test_static_files_dir(); @@ -2404,16 +2405,19 @@ mod tests { ])) .build(); - let genesis = random_block(&mut rng, 0, None, None, Some(0), None); - let mut block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None); + let genesis = random_block(&mut rng, 0, None, None, Some(0), None, None); + let mut block1 = + random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None, None); block1.header.set_difficulty(U256::from(1)); // a second pre-merge block - let mut block2 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None); + let mut block2 = + random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None, None); block2.header.set_difficulty(U256::from(1)); // a transition block - let mut block3 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None); + let mut block3 = + random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None, None); block3.header.set_difficulty(U256::from(1)); let (_static_dir, static_dir_path) = create_test_static_files_dir(); @@ -2461,8 +2465,8 @@ mod tests { ])) .build(); - let genesis = random_block(&mut rng, 0, None, None, Some(0), None); - let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None); + let genesis = random_block(&mut rng, 0, None, None, Some(0), None, None); + let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None, None); let (_temp_dir, temp_dir_path) = create_test_static_files_dir(); @@ -2525,7 +2529,7 @@ mod tests { // Send new payload let res = env .send_new_payload( - block_to_payload_v1(random_block(&mut rng, 0, None, None, Some(0), None)), + block_to_payload_v1(random_block(&mut rng, 0, None, None, Some(0), None, None)), None, ) .await; @@ -2536,7 +2540,7 @@ mod tests { // Send new payload let res = env .send_new_payload( - block_to_payload_v1(random_block(&mut rng, 1, None, None, Some(0), None)), + block_to_payload_v1(random_block(&mut rng, 1, None, None, Some(0), None, None)), None, ) .await; @@ -2565,9 +2569,9 @@ mod tests { })])) .build(); - let genesis = random_block(&mut rng, 0, None, None, Some(0), None); - let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None); - let block2 = random_block(&mut rng, 2, Some(block1.hash()), None, Some(0), None); + let genesis = random_block(&mut rng, 0, None, None, Some(0), None, None); + let block1 = random_block(&mut rng, 1, Some(genesis.hash()), None, Some(0), None, None); + let block2 = random_block(&mut rng, 2, Some(block1.hash()), None, Some(0), None, None); let (_static_dir, static_dir_path) = create_test_static_files_dir(); insert_blocks( @@ -2635,8 +2639,15 @@ mod tests { let genesis = SealedBlock { header: chain_spec.sealed_genesis_header(), ..Default::default() }; - let block1 = - random_block(&mut rng, 1, Some(chain_spec.genesis_hash()), None, Some(0), None); + let block1 = random_block( + &mut rng, + 1, + Some(chain_spec.genesis_hash()), + None, + Some(0), + None, + None, + ); // TODO: add transactions that transfer from the alloc accounts, generating the new // block tx and state root @@ -2686,7 +2697,7 @@ mod tests { })])) .build(); - let genesis = random_block(&mut rng, 0, None, None, Some(0), None); + let genesis = random_block(&mut rng, 0, None, None, Some(0), None, None); let (_static_dir, static_dir_path) = create_test_static_files_dir(); @@ -2715,7 +2726,7 @@ mod tests { // Send new payload let parent = rng.gen(); - let block = random_block(&mut rng, 2, Some(parent), None, Some(0), None); + let block = random_block(&mut rng, 2, Some(parent), None, Some(0), None, None); let res = env.send_new_payload(block_to_payload_v1(block), None).await; let expected_result = PayloadStatus::from_status(PayloadStatusEnum::Syncing); assert_matches!(res, Ok(result) => assert_eq!(result, expected_result)); diff --git a/crates/net/downloaders/src/bodies/bodies.rs b/crates/net/downloaders/src/bodies/bodies.rs index 05613a0370c8..b4583a6deb47 100644 --- a/crates/net/downloaders/src/bodies/bodies.rs +++ b/crates/net/downloaders/src/bodies/bodies.rs @@ -652,7 +652,7 @@ mod tests { // Generate some random blocks let db = create_test_rw_db(); let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, 0..=199, B256::ZERO, 1..2, None); + let blocks = random_block_range(&mut rng, 0..=199, B256::ZERO, 1..2, None, None); let headers = blocks.iter().map(|block| block.header.clone()).collect::>(); let bodies = blocks diff --git a/crates/net/downloaders/src/test_utils/mod.rs b/crates/net/downloaders/src/test_utils/mod.rs index 77ca8fc8a009..969881df30ab 100644 --- a/crates/net/downloaders/src/test_utils/mod.rs +++ b/crates/net/downloaders/src/test_utils/mod.rs @@ -21,7 +21,7 @@ pub(crate) fn generate_bodies( range: RangeInclusive, ) -> (Vec, HashMap) { let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, range, B256::ZERO, 0..2, None); + let blocks = random_block_range(&mut rng, range, B256::ZERO, 0..2, None, None); let headers = blocks.iter().map(|block| block.header.clone()).collect(); let bodies = blocks diff --git a/crates/prune/prune/src/segments/receipts.rs b/crates/prune/prune/src/segments/receipts.rs index 0585af7397be..dde26fae26eb 100644 --- a/crates/prune/prune/src/segments/receipts.rs +++ b/crates/prune/prune/src/segments/receipts.rs @@ -99,7 +99,7 @@ mod tests { let db = TestStageDB::default(); let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, 1..=10, B256::ZERO, 2..3, None); + let blocks = random_block_range(&mut rng, 1..=10, B256::ZERO, 2..3, None, None); db.insert_blocks(blocks.iter(), StorageKind::Database(None)).expect("insert blocks"); let mut receipts = Vec::new(); diff --git a/crates/prune/prune/src/segments/static_file/transactions.rs b/crates/prune/prune/src/segments/static_file/transactions.rs index fb41b5be2990..12cfa973f129 100644 --- a/crates/prune/prune/src/segments/static_file/transactions.rs +++ b/crates/prune/prune/src/segments/static_file/transactions.rs @@ -105,7 +105,7 @@ mod tests { let db = TestStageDB::default(); let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, 1..=100, B256::ZERO, 2..3, None); + let blocks = random_block_range(&mut rng, 1..=100, B256::ZERO, 2..3, None, None); db.insert_blocks(blocks.iter(), StorageKind::Database(None)).expect("insert blocks"); let transactions = blocks.iter().flat_map(|block| &block.body).collect::>(); diff --git a/crates/prune/prune/src/segments/user/account_history.rs b/crates/prune/prune/src/segments/user/account_history.rs index 56b64f232d10..86c3a0a622eb 100644 --- a/crates/prune/prune/src/segments/user/account_history.rs +++ b/crates/prune/prune/src/segments/user/account_history.rs @@ -151,7 +151,7 @@ mod tests { let db = TestStageDB::default(); let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, 1..=5000, B256::ZERO, 0..1, None); + let blocks = random_block_range(&mut rng, 1..=5000, B256::ZERO, 0..1, None, None); db.insert_blocks(blocks.iter(), StorageKind::Database(None)).expect("insert blocks"); let accounts = random_eoa_accounts(&mut rng, 2).into_iter().collect::>(); 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 e9fbcf7570eb..5ec2bb85f993 100644 --- a/crates/prune/prune/src/segments/user/receipts_by_logs.rs +++ b/crates/prune/prune/src/segments/user/receipts_by_logs.rs @@ -242,9 +242,9 @@ mod tests { let tip = 20000; let blocks = [ - random_block_range(&mut rng, 0..=100, B256::ZERO, 1..5, None), - random_block_range(&mut rng, (100 + 1)..=(tip - 100), B256::ZERO, 0..1, None), - random_block_range(&mut rng, (tip - 100 + 1)..=tip, B256::ZERO, 1..5, None), + random_block_range(&mut rng, 0..=100, B256::ZERO, 1..5, None, None), + random_block_range(&mut rng, (100 + 1)..=(tip - 100), B256::ZERO, 0..1, None, None), + random_block_range(&mut rng, (tip - 100 + 1)..=tip, B256::ZERO, 1..5, None, None), ] .concat(); db.insert_blocks(blocks.iter(), StorageKind::Database(None)).expect("insert blocks"); diff --git a/crates/prune/prune/src/segments/user/sender_recovery.rs b/crates/prune/prune/src/segments/user/sender_recovery.rs index 306d583fd9f9..649d90339ed6 100644 --- a/crates/prune/prune/src/segments/user/sender_recovery.rs +++ b/crates/prune/prune/src/segments/user/sender_recovery.rs @@ -101,7 +101,7 @@ mod tests { let db = TestStageDB::default(); let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, 1..=10, B256::ZERO, 2..3, None); + let blocks = random_block_range(&mut rng, 1..=10, B256::ZERO, 2..3, None, None); db.insert_blocks(blocks.iter(), StorageKind::Database(None)).expect("insert blocks"); let mut transaction_senders = Vec::new(); diff --git a/crates/prune/prune/src/segments/user/storage_history.rs b/crates/prune/prune/src/segments/user/storage_history.rs index af43ac4d6f3a..e3b203cb0876 100644 --- a/crates/prune/prune/src/segments/user/storage_history.rs +++ b/crates/prune/prune/src/segments/user/storage_history.rs @@ -157,7 +157,7 @@ mod tests { let db = TestStageDB::default(); let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, 0..=5000, B256::ZERO, 0..1, None); + let blocks = random_block_range(&mut rng, 0..=5000, B256::ZERO, 0..1, None, None); db.insert_blocks(blocks.iter(), StorageKind::Database(None)).expect("insert blocks"); let accounts = random_eoa_accounts(&mut rng, 2).into_iter().collect::>(); diff --git a/crates/prune/prune/src/segments/user/transaction_lookup.rs b/crates/prune/prune/src/segments/user/transaction_lookup.rs index d81f75de7006..1a28bcc8085e 100644 --- a/crates/prune/prune/src/segments/user/transaction_lookup.rs +++ b/crates/prune/prune/src/segments/user/transaction_lookup.rs @@ -130,7 +130,7 @@ mod tests { let db = TestStageDB::default(); let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, 1..=10, B256::ZERO, 2..3, None); + let blocks = random_block_range(&mut rng, 1..=10, B256::ZERO, 2..3, None, None); db.insert_blocks(blocks.iter(), StorageKind::Database(None)).expect("insert blocks"); let mut tx_hash_numbers = Vec::new(); diff --git a/crates/rpc/rpc-engine-api/src/engine_api.rs b/crates/rpc/rpc-engine-api/src/engine_api.rs index c24f1eaed2da..a62fd2f31e8e 100644 --- a/crates/rpc/rpc-engine-api/src/engine_api.rs +++ b/crates/rpc/rpc-engine-api/src/engine_api.rs @@ -1036,6 +1036,7 @@ mod tests { B256::default(), 0..2, None, + None, ); handle.provider.extend_blocks(blocks.iter().cloned().map(|b| (b.hash(), b.unseal()))); @@ -1061,6 +1062,7 @@ mod tests { B256::default(), 0..2, None, + None, ); // Insert only blocks in ranges 1-25 and 50-75 @@ -1153,9 +1155,9 @@ mod tests { let terminal_block_number = 1000; let consensus_terminal_block = - random_block(&mut rng, terminal_block_number, None, None, None, None); + random_block(&mut rng, terminal_block_number, None, None, None, None, None); let execution_terminal_block = - random_block(&mut rng, terminal_block_number, None, None, None, None); + random_block(&mut rng, terminal_block_number, None, None, None, None, None); let transition_config = TransitionConfiguration { terminal_total_difficulty: handle @@ -1196,8 +1198,15 @@ mod tests { let (handle, api) = setup_engine_api(); let terminal_block_number = 1000; - let terminal_block = - random_block(&mut generators::rng(), terminal_block_number, None, None, None, None); + let terminal_block = random_block( + &mut generators::rng(), + terminal_block_number, + None, + None, + None, + None, + None, + ); let transition_config = TransitionConfiguration { terminal_total_difficulty: handle diff --git a/crates/rpc/rpc-engine-api/tests/it/payload.rs b/crates/rpc/rpc-engine-api/tests/it/payload.rs index aa0211ac5c95..ba1e8c397f3b 100644 --- a/crates/rpc/rpc-engine-api/tests/it/payload.rs +++ b/crates/rpc/rpc-engine-api/tests/it/payload.rs @@ -32,7 +32,7 @@ fn transform_block Block>(src: SealedBlock, f: F) -> Executi #[test] fn payload_body_roundtrip() { let mut rng = generators::rng(); - for block in random_block_range(&mut rng, 0..=99, B256::default(), 0..2, None) { + for block in random_block_range(&mut rng, 0..=99, B256::default(), 0..2, None, None) { let unsealed = block.clone().unseal(); let payload_body: ExecutionPayloadBodyV1 = convert_to_payload_body_v1(unsealed); @@ -53,7 +53,7 @@ fn payload_body_roundtrip() { fn payload_validation() { let mut rng = generators::rng(); let parent = rng.gen(); - let block = random_block(&mut rng, 100, Some(parent), Some(3), Some(0), None); + let block = random_block(&mut rng, 100, Some(parent), Some(3), Some(0), None, None); // Valid extra data let block_with_valid_extra_data = transform_block(block.clone(), |mut b| { diff --git a/crates/stages/stages/benches/setup/mod.rs b/crates/stages/stages/benches/setup/mod.rs index 4173b8de1383..eda227a67b63 100644 --- a/crates/stages/stages/benches/setup/mod.rs +++ b/crates/stages/stages/benches/setup/mod.rs @@ -116,7 +116,8 @@ pub(crate) fn txs_testdata(num_blocks: u64) -> TestStageDB { .into_iter() .collect(); - let mut blocks = random_block_range(&mut rng, 0..=num_blocks, B256::ZERO, txs_range, None); + let mut blocks = + random_block_range(&mut rng, 0..=num_blocks, B256::ZERO, txs_range, None, None); let (transitions, start_state) = random_changeset_range( &mut rng, diff --git a/crates/stages/stages/src/stages/bodies.rs b/crates/stages/stages/src/stages/bodies.rs index b682f9de3d4d..e6e08b8069f0 100644 --- a/crates/stages/stages/src/stages/bodies.rs +++ b/crates/stages/stages/src/stages/bodies.rs @@ -719,7 +719,7 @@ mod tests { let mut rng = generators::rng(); // Static files do not support gaps in headers, so we need to generate 0 to end - let blocks = random_block_range(&mut rng, 0..=end, GENESIS_HASH, 0..2, None); + let blocks = random_block_range(&mut rng, 0..=end, GENESIS_HASH, 0..2, None, None); self.db.insert_headers_with_td(blocks.iter().map(|block| &block.header))?; if let Some(progress) = blocks.get(start as usize) { // Insert last progress data diff --git a/crates/stages/stages/src/stages/hashing_account.rs b/crates/stages/stages/src/stages/hashing_account.rs index 92a908afee3d..56c631bf09fd 100644 --- a/crates/stages/stages/src/stages/hashing_account.rs +++ b/crates/stages/stages/src/stages/hashing_account.rs @@ -72,7 +72,8 @@ impl AccountHashingStage { let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, opts.blocks.clone(), B256::ZERO, opts.txs, None); + let blocks = + random_block_range(&mut rng, opts.blocks.clone(), B256::ZERO, opts.txs, None, None); for block in blocks { provider.insert_historical_block(block.try_seal_with_senders().unwrap()).unwrap(); diff --git a/crates/stages/stages/src/stages/hashing_storage.rs b/crates/stages/stages/src/stages/hashing_storage.rs index 3799cb69f31f..73bd2b435aed 100644 --- a/crates/stages/stages/src/stages/hashing_storage.rs +++ b/crates/stages/stages/src/stages/hashing_storage.rs @@ -342,7 +342,8 @@ mod tests { let n_accounts = 31; let mut accounts = random_contract_account_range(&mut rng, &mut (0..n_accounts)); - let blocks = random_block_range(&mut rng, stage_progress..=end, B256::ZERO, 0..3, None); + let blocks = + random_block_range(&mut rng, stage_progress..=end, B256::ZERO, 0..3, None, None); self.db.insert_headers(blocks.iter().map(|block| &block.header))?; diff --git a/crates/stages/stages/src/stages/index_account_history.rs b/crates/stages/stages/src/stages/index_account_history.rs index 6dd3cb3f27dd..f3f888a567ea 100644 --- a/crates/stages/stages/src/stages/index_account_history.rs +++ b/crates/stages/stages/src/stages/index_account_history.rs @@ -538,7 +538,7 @@ mod tests { .into_iter() .collect::>(); - let blocks = random_block_range(&mut rng, start..=end, B256::ZERO, 0..3, None); + let blocks = random_block_range(&mut rng, start..=end, B256::ZERO, 0..3, None, None); let (changesets, _) = random_changeset_range( &mut rng, diff --git a/crates/stages/stages/src/stages/index_storage_history.rs b/crates/stages/stages/src/stages/index_storage_history.rs index 600660a6e7f9..5f2aa0f06da3 100644 --- a/crates/stages/stages/src/stages/index_storage_history.rs +++ b/crates/stages/stages/src/stages/index_storage_history.rs @@ -560,7 +560,7 @@ mod tests { .into_iter() .collect::>(); - let blocks = random_block_range(&mut rng, start..=end, B256::ZERO, 0..3, None); + let blocks = random_block_range(&mut rng, start..=end, B256::ZERO, 0..3, None, None); let (changesets, _) = random_changeset_range( &mut rng, diff --git a/crates/stages/stages/src/stages/merkle.rs b/crates/stages/stages/src/stages/merkle.rs index f16fb5a06564..d527cd7d3e23 100644 --- a/crates/stages/stages/src/stages/merkle.rs +++ b/crates/stages/stages/src/stages/merkle.rs @@ -502,6 +502,7 @@ mod tests { B256::ZERO, 0..1, None, + None, )); self.db.insert_blocks(preblocks.iter(), StorageKind::Static)?; } @@ -522,6 +523,7 @@ mod tests { Some(0), None, None, + None, ); let mut header = header.unseal(); @@ -536,7 +538,7 @@ mod tests { let head_hash = sealed_head.hash(); let mut blocks = vec![sealed_head]; - blocks.extend(random_block_range(&mut rng, start..=end, head_hash, 0..3, None)); + blocks.extend(random_block_range(&mut rng, start..=end, head_hash, 0..3, None, None)); let last_block = blocks.last().cloned().unwrap(); self.db.insert_blocks(blocks.iter(), StorageKind::Static)?; diff --git a/crates/stages/stages/src/stages/mod.rs b/crates/stages/stages/src/stages/mod.rs index bb64c1503e90..c3518ddb9ce2 100644 --- a/crates/stages/stages/src/stages/mod.rs +++ b/crates/stages/stages/src/stages/mod.rs @@ -94,7 +94,8 @@ mod tests { let mut head = block.hash(); let mut rng = generators::rng(); for block_number in 2..=tip { - let nblock = random_block(&mut rng, block_number, Some(head), Some(0), Some(0), None); + let nblock = + random_block(&mut rng, block_number, Some(head), Some(0), Some(0), None, None); head = nblock.hash(); provider_rw.insert_historical_block(nblock.try_seal_with_senders().unwrap()).unwrap(); } @@ -253,7 +254,7 @@ mod tests { let genesis_hash = B256::ZERO; let tip = (num_blocks - 1) as u64; - let blocks = random_block_range(&mut rng, 0..=tip, genesis_hash, 2..3, None); + let blocks = random_block_range(&mut rng, 0..=tip, genesis_hash, 2..3, None, None); db.insert_blocks(blocks.iter(), StorageKind::Static)?; let mut receipts = Vec::new(); diff --git a/crates/stages/stages/src/stages/prune.rs b/crates/stages/stages/src/stages/prune.rs index d7f7e0afe3b7..f1c6adcc013e 100644 --- a/crates/stages/stages/src/stages/prune.rs +++ b/crates/stages/stages/src/stages/prune.rs @@ -203,6 +203,7 @@ mod tests { B256::ZERO, 1..3, None, + None, ); self.db.insert_blocks(blocks.iter(), StorageKind::Static)?; self.db.insert_transaction_senders( diff --git a/crates/stages/stages/src/stages/sender_recovery.rs b/crates/stages/stages/src/stages/sender_recovery.rs index f122873d78a8..033ae02f2760 100644 --- a/crates/stages/stages/src/stages/sender_recovery.rs +++ b/crates/stages/stages/src/stages/sender_recovery.rs @@ -326,6 +326,7 @@ mod tests { Some((number == non_empty_block_number) as u8), None, None, + None, ) }) .collect::>(); @@ -370,6 +371,7 @@ mod tests { B256::ZERO, 0..4, None, + None, ); // set tx count range high enough to hit the threshold runner .db @@ -440,7 +442,7 @@ mod tests { let db = TestStageDB::default(); let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, 0..=100, B256::ZERO, 0..10, None); + let blocks = random_block_range(&mut rng, 0..=100, B256::ZERO, 0..10, None, None); db.insert_blocks(blocks.iter(), StorageKind::Static).expect("insert blocks"); let max_pruned_block = 30; @@ -553,7 +555,8 @@ mod tests { let stage_progress = input.checkpoint().block_number; let end = input.target(); - let blocks = random_block_range(&mut rng, stage_progress..=end, B256::ZERO, 0..2, None); + let blocks = + random_block_range(&mut rng, stage_progress..=end, B256::ZERO, 0..2, None, None); self.db.insert_blocks(blocks.iter(), StorageKind::Static)?; Ok(blocks) } diff --git a/crates/stages/stages/src/stages/tx_lookup.rs b/crates/stages/stages/src/stages/tx_lookup.rs index 72795a96d5c9..d32c2b8b70bb 100644 --- a/crates/stages/stages/src/stages/tx_lookup.rs +++ b/crates/stages/stages/src/stages/tx_lookup.rs @@ -277,6 +277,7 @@ mod tests { Some((number == non_empty_block_number) as u8), None, None, + None, ) }) .collect::>(); @@ -325,6 +326,7 @@ mod tests { B256::ZERO, 0..2, None, + None, ); runner .db @@ -359,7 +361,7 @@ mod tests { let db = TestStageDB::default(); let mut rng = generators::rng(); - let blocks = random_block_range(&mut rng, 0..=100, B256::ZERO, 0..10, None); + let blocks = random_block_range(&mut rng, 0..=100, B256::ZERO, 0..10, None, None); db.insert_blocks(blocks.iter(), StorageKind::Static).expect("insert blocks"); let max_pruned_block = 30; @@ -485,8 +487,14 @@ mod tests { let end = input.target(); let mut rng = generators::rng(); - let blocks = - random_block_range(&mut rng, stage_progress + 1..=end, B256::ZERO, 0..2, None); + let blocks = random_block_range( + &mut rng, + stage_progress + 1..=end, + B256::ZERO, + 0..2, + None, + None, + ); self.db.insert_blocks(blocks.iter(), StorageKind::Static)?; Ok(blocks) } diff --git a/crates/static-file/static-file/src/static_file_producer.rs b/crates/static-file/static-file/src/static_file_producer.rs index 6bf2628b5f2f..f1d41ed1f4fe 100644 --- a/crates/static-file/static-file/src/static_file_producer.rs +++ b/crates/static-file/static-file/src/static_file_producer.rs @@ -279,7 +279,7 @@ mod tests { let mut rng = generators::rng(); let db = TestStageDB::default(); - let blocks = random_block_range(&mut rng, 0..=3, B256::ZERO, 2..3, None); + let blocks = random_block_range(&mut rng, 0..=3, B256::ZERO, 2..3, None, None); db.insert_blocks(blocks.iter(), StorageKind::Database(None)).expect("insert blocks"); // Unwind headers from static_files and manually insert them into the database, so we're // able to check that static_file_producer works diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index 40a2e226fffc..9c3fb0fbf93c 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -1491,6 +1491,11 @@ where mod tests { use std::{ops::Range, sync::Arc, time::Instant}; + use crate::{ + providers::BlockchainProvider2, + test_utils::{create_test_provider_factory, create_test_provider_factory_with_chain_spec}, + BlockWriter, CanonChainTracker, StaticFileWriter, + }; use itertools::Itertools; use rand::Rng; use reth_chain_state::{ @@ -1504,11 +1509,12 @@ mod tests { use reth_execution_types::{Chain, ExecutionOutcome}; use reth_primitives::{ BlockHashOrNumber, BlockNumHash, BlockNumberOrTag, Receipt, SealedBlock, StaticFileSegment, - B256, + Withdrawals, B256, }; use reth_storage_api::{ BlockHashReader, BlockIdReader, BlockNumReader, BlockReader, BlockReaderIdExt, BlockSource, ChangeSetReader, HeaderProvider, ReceiptProviderIdExt, RequestsProvider, + WithdrawalsProvider, }; use reth_testing_utils::generators::{ self, random_block, random_block_range, random_changeset_range, random_eoa_accounts, @@ -1516,12 +1522,6 @@ mod tests { }; use revm::db::BundleState; - use crate::{ - providers::BlockchainProvider2, - test_utils::{create_test_provider_factory, create_test_provider_factory_with_chain_spec}, - BlockWriter, CanonChainTracker, StaticFileWriter, - }; - const TEST_BLOCKS_COUNT: usize = 5; fn random_blocks( @@ -1529,9 +1529,17 @@ mod tests { database_blocks: usize, in_memory_blocks: usize, requests_count: Option>, + withdrawals_count: Option>, ) -> (Vec, Vec) { let block_range = (database_blocks + in_memory_blocks - 1) as u64; - let blocks = random_block_range(rng, 0..=block_range, B256::ZERO, 0..1, requests_count); + let blocks = random_block_range( + rng, + 0..=block_range, + B256::ZERO, + 0..1, + requests_count, + withdrawals_count, + ); let (database_blocks, in_memory_blocks) = blocks.split_at(database_blocks); (database_blocks.to_vec(), in_memory_blocks.to_vec()) } @@ -1543,14 +1551,20 @@ mod tests { database_blocks: usize, in_memory_blocks: usize, requests_count: Option>, + withdrawals_count: Option>, ) -> eyre::Result<( BlockchainProvider2>>, Vec, Vec, Vec>, )> { - let (database_blocks, in_memory_blocks) = - random_blocks(rng, database_blocks, in_memory_blocks, requests_count); + let (database_blocks, in_memory_blocks) = random_blocks( + rng, + database_blocks, + in_memory_blocks, + requests_count, + withdrawals_count, + ); let receipts: Vec> = database_blocks .iter() .chain(in_memory_blocks.iter()) @@ -1622,6 +1636,7 @@ mod tests { database_blocks: usize, in_memory_blocks: usize, requests_count: Option>, + withdrawals_count: Option>, ) -> eyre::Result<( BlockchainProvider2>>, Vec, @@ -1634,6 +1649,7 @@ mod tests { database_blocks, in_memory_blocks, requests_count, + withdrawals_count, ) } @@ -1644,7 +1660,7 @@ mod tests { let factory = create_test_provider_factory(); // Generate 10 random blocks and split into database and in-memory blocks - let blocks = random_block_range(&mut rng, 0..=10, B256::ZERO, 0..1, None); + let blocks = random_block_range(&mut rng, 0..=10, B256::ZERO, 0..1, None, None); let (database_blocks, in_memory_blocks) = blocks.split_at(5); // Insert first 5 blocks into the database @@ -1738,7 +1754,7 @@ mod tests { let factory = create_test_provider_factory(); // Generate 10 random blocks and split into database and in-memory blocks - let blocks = random_block_range(&mut rng, 0..=10, B256::ZERO, 0..1, None); + let blocks = random_block_range(&mut rng, 0..=10, B256::ZERO, 0..1, None, None); let (database_blocks, in_memory_blocks) = blocks.split_at(5); // Insert first 5 blocks into the database @@ -1802,12 +1818,17 @@ mod tests { #[test] fn test_block_reader_pending_block() -> eyre::Result<()> { let mut rng = generators::rng(); - let (provider, _, _, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, _, _, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; // Generate a random block let mut rng = generators::rng(); - let block = random_block(&mut rng, 0, Some(B256::ZERO), None, None, None); + let block = random_block(&mut rng, 0, Some(B256::ZERO), None, None, None, None); // Set the block as pending provider.canonical_in_memory_state.set_pending_block(ExecutedBlock { @@ -1838,8 +1859,13 @@ mod tests { fn test_block_reader_ommers() -> eyre::Result<()> { // Create a new provider let mut rng = generators::rng(); - let (provider, _, in_memory_blocks, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, _, in_memory_blocks, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; let first_in_mem_block = in_memory_blocks.first().unwrap(); @@ -1870,8 +1896,13 @@ mod tests { #[test] fn test_block_hash_reader() -> eyre::Result<()> { let mut rng = generators::rng(); - let (provider, database_blocks, in_memory_blocks, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, database_blocks, in_memory_blocks, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; let database_block = database_blocks.first().unwrap().clone(); let in_memory_block = in_memory_blocks.last().unwrap().clone(); @@ -1894,8 +1925,13 @@ mod tests { #[test] fn test_header_provider() -> eyre::Result<()> { let mut rng = generators::rng(); - let (provider, database_blocks, in_memory_blocks, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, database_blocks, in_memory_blocks, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; let database_block = database_blocks.first().unwrap().clone(); let in_memory_block = in_memory_blocks.last().unwrap().clone(); @@ -2001,10 +2037,69 @@ mod tests { } #[test] - fn test_block_num_reader() -> eyre::Result<()> { + fn test_withdrawals_provider() -> eyre::Result<()> { let mut rng = generators::rng(); + let chain_spec = Arc::new(ChainSpecBuilder::mainnet().shanghai_activated().build()); let (provider, database_blocks, in_memory_blocks, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + provider_with_chain_spec_and_random_blocks( + &mut rng, + chain_spec.clone(), + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + Some(1..3), + )?; + let blocks = [database_blocks, in_memory_blocks].concat(); + + let shainghai_timestamp = + chain_spec.hardforks.fork(EthereumHardfork::Shanghai).as_timestamp().unwrap(); + + assert_eq!( + provider + .withdrawals_by_block( + reth_primitives::BlockHashOrNumber::Number(15), + shainghai_timestamp + ) + .expect("could not call withdrawals by block"), + Some(Withdrawals::new(vec![])), + "Expected withdrawals_by_block to return empty list if block does not exist" + ); + + for block in blocks.clone() { + assert_eq!( + provider + .withdrawals_by_block( + reth_primitives::BlockHashOrNumber::Number(block.number), + shainghai_timestamp + )? + .unwrap(), + block.withdrawals.unwrap(), + "Expected withdrawals_by_block to return correct withdrawals" + ); + } + + let canonical_block_num = provider.best_block_number().unwrap(); + let canonical_block = blocks.get(canonical_block_num as usize).unwrap(); + + assert_eq!( + Some(provider.latest_withdrawal()?.unwrap()), + canonical_block.withdrawals.clone().unwrap().pop(), + "Expected latest withdrawal to be equal to last withdrawal entry in canonical block" + ); + + Ok(()) + } + + #[test] + fn test_block_num_reader() -> eyre::Result<()> { + let mut rng = generators::rng(); + let (provider, database_blocks, in_memory_blocks, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; assert_eq!(provider.best_block_number()?, in_memory_blocks.last().unwrap().number); assert_eq!(provider.last_block_number()?, database_blocks.last().unwrap().number); @@ -2020,14 +2115,23 @@ mod tests { #[test] fn test_block_reader_id_ext_block_by_id() -> eyre::Result<()> { let mut rng = generators::rng(); - let (provider, database_blocks, in_memory_blocks, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, database_blocks, in_memory_blocks, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; let database_block = database_blocks.first().unwrap().clone(); let in_memory_block = in_memory_blocks.last().unwrap().clone(); let block_number = database_block.number; let block_hash = database_block.header.hash(); + let provider_block = + provider.block_by_id(block_number.into()).unwrap().unwrap().withdrawals; + println!("provider_block {:#?}", provider_block); + println!("db block withdrawals {:#?}", database_block.withdrawals); assert_eq!( provider.block_by_id(block_number.into()).unwrap(), Some(database_block.clone().unseal()) @@ -2051,8 +2155,13 @@ mod tests { #[test] fn test_block_reader_id_ext_header_by_number_or_tag() -> eyre::Result<()> { let mut rng = generators::rng(); - let (provider, database_blocks, in_memory_blocks, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, database_blocks, in_memory_blocks, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; let database_block = database_blocks.first().unwrap().clone(); @@ -2104,8 +2213,13 @@ mod tests { #[test] fn test_block_reader_id_ext_header_by_id() -> eyre::Result<()> { let mut rng = generators::rng(); - let (provider, database_blocks, in_memory_blocks, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, database_blocks, in_memory_blocks, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; let database_block = database_blocks.first().unwrap().clone(); let in_memory_block = in_memory_blocks.last().unwrap().clone(); @@ -2158,8 +2272,13 @@ mod tests { #[test] fn test_block_reader_id_ext_ommers_by_id() -> eyre::Result<()> { let mut rng = generators::rng(); - let (provider, database_blocks, in_memory_blocks, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, database_blocks, in_memory_blocks, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; let database_block = database_blocks.first().unwrap().clone(); let in_memory_block = in_memory_blocks.last().unwrap().clone(); @@ -2194,8 +2313,13 @@ mod tests { #[test] fn test_receipt_provider_id_ext_receipts_by_block_id() -> eyre::Result<()> { let mut rng = generators::rng(); - let (provider, database_blocks, in_memory_blocks, receipts) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, database_blocks, in_memory_blocks, receipts) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; let database_block = database_blocks.first().unwrap().clone(); let in_memory_block = in_memory_blocks.last().unwrap().clone(); @@ -2230,8 +2354,13 @@ mod tests { #[test] fn test_receipt_provider_id_ext_receipts_by_block_number_or_tag() -> eyre::Result<()> { let mut rng = generators::rng(); - let (provider, database_blocks, in_memory_blocks, receipts) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, database_blocks, in_memory_blocks, receipts) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; let database_block = database_blocks.first().unwrap().clone(); @@ -2265,7 +2394,7 @@ mod tests { let mut rng = generators::rng(); let (database_blocks, in_memory_blocks) = - random_blocks(&mut rng, TEST_BLOCKS_COUNT, 1, None); + random_blocks(&mut rng, TEST_BLOCKS_COUNT, 1, None, None); let first_database_block = database_blocks.first().map(|block| block.number).unwrap(); let last_database_block = database_blocks.last().map(|block| block.number).unwrap(); @@ -2387,6 +2516,7 @@ mod tests { TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, Some(1..2), + None, )?; let database_block = database_blocks.first().unwrap().clone(); @@ -2410,8 +2540,13 @@ mod tests { #[test] fn test_canon_state_tracker() -> eyre::Result<()> { let mut rng = generators::rng(); - let (provider, _, _, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, _, _, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; let before = Instant::now(); provider.on_forkchoice_update_received(&Default::default()); @@ -2436,8 +2571,13 @@ mod tests { fn test_block_id_reader() -> eyre::Result<()> { // Create a new provider let mut rng = generators::rng(); - let (provider, _, in_memory_blocks, _) = - provider_with_random_blocks(&mut rng, TEST_BLOCKS_COUNT, TEST_BLOCKS_COUNT, None)?; + let (provider, _, in_memory_blocks, _) = provider_with_random_blocks( + &mut rng, + TEST_BLOCKS_COUNT, + TEST_BLOCKS_COUNT, + None, + None, + )?; // Set the pending block in memory let pending_block = in_memory_blocks.last().unwrap(); diff --git a/crates/storage/provider/src/providers/database/mod.rs b/crates/storage/provider/src/providers/database/mod.rs index 70a516931f02..902836f5df4b 100644 --- a/crates/storage/provider/src/providers/database/mod.rs +++ b/crates/storage/provider/src/providers/database/mod.rs @@ -713,7 +713,7 @@ mod tests { let factory = create_test_provider_factory(); let mut rng = generators::rng(); - let block = random_block(&mut rng, 0, None, Some(3), None, None); + let block = random_block(&mut rng, 0, None, Some(3), None, None, None); let tx_ranges: Vec> = vec![0..=0, 1..=1, 2..=2, 0..=1, 1..=2]; for range in tx_ranges { diff --git a/testing/testing-utils/src/generators.rs b/testing/testing-utils/src/generators.rs index 759010af5a28..b62b4c2f0c04 100644 --- a/testing/testing-utils/src/generators.rs +++ b/testing/testing-utils/src/generators.rs @@ -10,7 +10,7 @@ use rand::{ use reth_primitives::{ proofs, sign_message, Account, Address, BlockNumber, Bytes, Header, Log, Receipt, Request, Requests, SealedBlock, SealedHeader, StorageEntry, Transaction, TransactionSigned, TxKind, - TxLegacy, B256, U256, + TxLegacy, Withdrawal, Withdrawals, B256, U256, }; use secp256k1::{Keypair, Secp256k1}; use std::{ @@ -138,6 +138,7 @@ pub fn random_block( tx_count: Option, ommers_count: Option, requests_count: Option, + withdrawals_count: Option, ) -> SealedBlock { // Generate transactions let tx_count = tx_count.unwrap_or_else(|| rng.gen::()); @@ -158,6 +159,18 @@ pub fn random_block( requests_count.map(|count| (0..count).map(|_| random_request(rng)).collect::>()); let requests_root = requests.as_ref().map(|requests| proofs::calculate_requests_root(requests)); + let withdrawals = withdrawals_count.map(|count| { + (0..count) + .map(|i| Withdrawal { + amount: rng.gen(), + index: i.into(), + validator_index: i.into(), + address: rng.gen(), + }) + .collect::>() + }); + let withdrawals_root = withdrawals.as_ref().map(|w| proofs::calculate_withdrawals_root(w)); + SealedBlock { header: Header { parent_hash: parent.unwrap_or_default(), @@ -168,12 +181,13 @@ pub fn random_block( ommers_hash, base_fee_per_gas: Some(rng.gen()), requests_root, + withdrawals_root, ..Default::default() } .seal_slow(), body: transactions, ommers, - withdrawals: None, + withdrawals: withdrawals.map(Withdrawals::new), requests: requests.map(Requests), } } @@ -190,12 +204,14 @@ pub fn random_block_range( head: B256, tx_count: Range, requests_count: Option>, + withdrawals_count: Option>, ) -> Vec { let mut blocks = Vec::with_capacity(block_numbers.end().saturating_sub(*block_numbers.start()) as usize); for idx in block_numbers { let tx_count = tx_count.clone().sample_single(rng); let requests_count = requests_count.clone().map(|r| r.sample_single(rng)); + let withdrawals_count = withdrawals_count.clone().map(|r| r.sample_single(rng)); blocks.push(random_block( rng, idx, @@ -203,6 +219,7 @@ pub fn random_block_range( Some(tx_count), None, requests_count, + withdrawals_count, )); } blocks