Skip to content

Commit

Permalink
refactor(provider): random_block and random_block_range functions (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jennifer authored Aug 27, 2024
1 parent 7df7bc2 commit 7ea748e
Show file tree
Hide file tree
Showing 28 changed files with 505 additions and 307 deletions.
8 changes: 3 additions & 5 deletions crates/blockchain-tree/src/block_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,13 @@ impl BlockBuffer {
mod tests {
use crate::BlockBuffer;
use reth_primitives::{BlockHash, BlockNumHash, SealedBlockWithSenders};
use reth_testing_utils::{
generators,
generators::{random_block, Rng},
};
use reth_testing_utils::generators::{self, random_block, BlockParams, Rng};
use std::collections::HashMap;

/// Create random block with specified number and parent hash.
fn create_block<R: Rng>(rng: &mut R, number: u64, parent: BlockHash) -> SealedBlockWithSenders {
let block = random_block(rng, number, Some(parent), None, None, None, None);
let block =
random_block(rng, number, BlockParams { parent: Some(parent), ..Default::default() });
block.seal_with_senders().unwrap()
}

Expand Down
174 changes: 143 additions & 31 deletions crates/consensus/beacon/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,7 @@ mod tests {

mod fork_choice_updated {
use super::*;
use generators::BlockParams;
use reth_db::{tables, test_utils::create_test_static_files_dir};
use reth_db_api::transaction::DbTxMut;
use reth_primitives::U256;
Expand Down Expand Up @@ -2230,8 +2231,20 @@ mod tests {
})]))
.build();

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 genesis = random_block(
&mut rng,
0,
BlockParams { ommers_count: Some(0), ..Default::default() },
);
let block1 = random_block(
&mut rng,
1,
BlockParams {
parent: Some(genesis.hash()),
ommers_count: Some(0),
..Default::default()
},
);
let (_static_dir, static_dir_path) = create_test_static_files_dir();

insert_blocks(
Expand Down Expand Up @@ -2289,8 +2302,16 @@ mod tests {
.disable_blockchain_tree_sync()
.build();

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 genesis = random_block(
&mut rng,
0,
BlockParams { ommers_count: Some(0), ..Default::default() },
);
let block1 = random_block(
&mut rng,
1,
BlockParams { parent: Some(genesis.hash()), ..Default::default() },
);

let (_static_dir, static_dir_path) = create_test_static_files_dir();

Expand All @@ -2304,9 +2325,15 @@ 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, None);
let next_head = random_block(
&mut rng,
2,
BlockParams {
parent: Some(block1.hash()),
ommers_count: Some(0),
..Default::default()
},
);
let next_forkchoice_state = ForkchoiceState {
head_block_hash: next_head.hash(),
finalized_block_hash: block1.hash(),
Expand Down Expand Up @@ -2358,8 +2385,20 @@ mod tests {
.disable_blockchain_tree_sync()
.build();

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 genesis = random_block(
&mut rng,
0,
BlockParams { ommers_count: Some(0), ..Default::default() },
);
let block1 = random_block(
&mut rng,
1,
BlockParams {
parent: Some(genesis.hash()),
ommers_count: Some(0),
..Default::default()
},
);

let (_static_dir, static_dir_path) = create_test_static_files_dir();

Expand Down Expand Up @@ -2405,19 +2444,44 @@ mod tests {
]))
.build();

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);
let genesis = random_block(
&mut rng,
0,
BlockParams { ommers_count: Some(0), ..Default::default() },
);
let mut block1 = random_block(
&mut rng,
1,
BlockParams {
parent: Some(genesis.hash()),
ommers_count: Some(0),
..Default::default()
},
);
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, None);
let mut block2 = random_block(
&mut rng,
1,
BlockParams {
parent: Some(genesis.hash()),
ommers_count: Some(0),
..Default::default()
},
);
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, None);
let mut block3 = random_block(
&mut rng,
1,
BlockParams {
parent: Some(genesis.hash()),
ommers_count: Some(0),
..Default::default()
},
);
block3.header.set_difficulty(U256::from(1));

let (_static_dir, static_dir_path) = create_test_static_files_dir();
Expand Down Expand Up @@ -2465,8 +2529,20 @@ mod tests {
]))
.build();

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 genesis = random_block(
&mut rng,
0,
BlockParams { ommers_count: Some(0), ..Default::default() },
);
let block1 = random_block(
&mut rng,
1,
BlockParams {
parent: Some(genesis.hash()),
ommers_count: Some(0),
..Default::default()
},
);

let (_temp_dir, temp_dir_path) = create_test_static_files_dir();

Expand Down Expand Up @@ -2500,6 +2576,7 @@ mod tests {
mod new_payload {
use super::*;
use alloy_genesis::Genesis;
use generators::BlockParams;
use reth_db::test_utils::create_test_static_files_dir;
use reth_primitives::{EthereumHardfork, U256};
use reth_provider::{
Expand Down Expand Up @@ -2529,7 +2606,11 @@ 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, None)),
block_to_payload_v1(random_block(
&mut rng,
0,
BlockParams { ommers_count: Some(0), ..Default::default() },
)),
None,
)
.await;
Expand All @@ -2540,7 +2621,11 @@ 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, None)),
block_to_payload_v1(random_block(
&mut rng,
1,
BlockParams { ommers_count: Some(0), ..Default::default() },
)),
None,
)
.await;
Expand Down Expand Up @@ -2569,9 +2654,29 @@ mod tests {
})]))
.build();

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 genesis = random_block(
&mut rng,
0,
BlockParams { ommers_count: Some(0), ..Default::default() },
);
let block1 = random_block(
&mut rng,
1,
BlockParams {
parent: Some(genesis.hash()),
ommers_count: Some(0),
..Default::default()
},
);
let block2 = random_block(
&mut rng,
2,
BlockParams {
parent: Some(block1.hash()),
ommers_count: Some(0),
..Default::default()
},
);

let (_static_dir, static_dir_path) = create_test_static_files_dir();
insert_blocks(
Expand Down Expand Up @@ -2642,11 +2747,11 @@ mod tests {
let block1 = random_block(
&mut rng,
1,
Some(chain_spec.genesis_hash()),
None,
Some(0),
None,
None,
BlockParams {
parent: Some(chain_spec.genesis_hash()),
ommers_count: Some(0),
..Default::default()
},
);

// TODO: add transactions that transfer from the alloc accounts, generating the new
Expand Down Expand Up @@ -2696,8 +2801,11 @@ mod tests {
done: true,
})]))
.build();

let genesis = random_block(&mut rng, 0, None, None, Some(0), None, None);
let genesis = random_block(
&mut rng,
0,
BlockParams { ommers_count: Some(0), ..Default::default() },
);

let (_static_dir, static_dir_path) = create_test_static_files_dir();

Expand Down Expand Up @@ -2726,7 +2834,11 @@ mod tests {

// Send new payload
let parent = rng.gen();
let block = random_block(&mut rng, 2, Some(parent), None, Some(0), None, None);
let block = random_block(
&mut rng,
2,
BlockParams { parent: Some(parent), ommers_count: Some(0), ..Default::default() },
);
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));
Expand Down
8 changes: 6 additions & 2 deletions crates/net/downloaders/src/bodies/bodies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ mod tests {
use reth_db::test_utils::{create_test_rw_db, create_test_static_files_dir};
use reth_primitives::{BlockBody, B256};
use reth_provider::{providers::StaticFileProvider, ProviderFactory};
use reth_testing_utils::{generators, generators::random_block_range};
use reth_testing_utils::generators::{self, random_block_range, BlockRangeParams};
use std::collections::HashMap;

// Check that the blocks are emitted in order of block number, not in order of
Expand Down Expand Up @@ -650,7 +650,11 @@ 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, None);
let blocks = random_block_range(
&mut rng,
0..=199,
BlockRangeParams { parent: Some(B256::ZERO), tx_count: 1..2, ..Default::default() },
);

let headers = blocks.iter().map(|block| block.header.clone()).collect::<Vec<_>>();
let bodies = blocks
Expand Down
8 changes: 6 additions & 2 deletions crates/net/downloaders/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::{bodies::test_utils::create_raw_bodies, file_codec::BlockFileCodec};
use futures::SinkExt;
use reth_primitives::{BlockBody, SealedHeader, B256};
use reth_testing_utils::{generators, generators::random_block_range};
use reth_testing_utils::generators::{self, random_block_range, BlockRangeParams};
use std::{collections::HashMap, io::SeekFrom, ops::RangeInclusive};
use tokio::{fs::File, io::AsyncSeekExt};
use tokio_util::codec::FramedWrite;
Expand All @@ -21,7 +21,11 @@ pub(crate) fn generate_bodies(
range: RangeInclusive<u64>,
) -> (Vec<SealedHeader>, HashMap<B256, BlockBody>) {
let mut rng = generators::rng();
let blocks = random_block_range(&mut rng, range, B256::ZERO, 0..2, None, None);
let blocks = random_block_range(
&mut rng,
range,
BlockRangeParams { parent: Some(B256::ZERO), tx_count: 0..2, ..Default::default() },
);

let headers = blocks.iter().map(|block| block.header.clone()).collect();
let bodies = blocks
Expand Down
11 changes: 7 additions & 4 deletions crates/prune/prune/src/segments/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ mod tests {
PruneCheckpoint, PruneInterruptReason, PruneLimiter, PruneMode, PruneProgress, PruneSegment,
};
use reth_stages::test_utils::{StorageKind, TestStageDB};
use reth_testing_utils::{
generators,
generators::{random_block_range, random_receipt},
use reth_testing_utils::generators::{
self, random_block_range, random_receipt, BlockRangeParams,
};
use std::ops::Sub;

Expand All @@ -99,7 +98,11 @@ 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, None);
let blocks = random_block_range(
&mut rng,
1..=10,
BlockRangeParams { parent: Some(B256::ZERO), tx_count: 2..3, ..Default::default() },
);
db.insert_blocks(blocks.iter(), StorageKind::Database(None)).expect("insert blocks");

let mut receipts = Vec::new();
Expand Down
8 changes: 6 additions & 2 deletions crates/prune/prune/src/segments/static_file/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,19 @@ mod tests {
PruneSegment, SegmentOutput,
};
use reth_stages::test_utils::{StorageKind, TestStageDB};
use reth_testing_utils::{generators, generators::random_block_range};
use reth_testing_utils::generators::{self, random_block_range, BlockRangeParams};
use std::ops::Sub;

#[test]
fn prune() {
let db = TestStageDB::default();
let mut rng = generators::rng();

let blocks = random_block_range(&mut rng, 1..=100, B256::ZERO, 2..3, None, None);
let blocks = random_block_range(
&mut rng,
1..=100,
BlockRangeParams { parent: Some(B256::ZERO), tx_count: 2..3, ..Default::default() },
);
db.insert_blocks(blocks.iter(), StorageKind::Database(None)).expect("insert blocks");

let transactions = blocks.iter().flat_map(|block| &block.body).collect::<Vec<_>>();
Expand Down
Loading

0 comments on commit 7ea748e

Please sign in to comment.