mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: generic data primitives block builder test framework (#13522)
This commit is contained in:
@ -1389,8 +1389,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_canonical_in_memory_state_canonical_chain_single_block() {
|
||||
let block = TestBlockBuilder::<EthPrimitives>::default()
|
||||
.get_executed_block_with_number(1, B256::random());
|
||||
let block = TestBlockBuilder::eth().get_executed_block_with_number(1, B256::random());
|
||||
let hash = block.block().hash();
|
||||
let mut blocks = HashMap::default();
|
||||
blocks.insert(hash, Arc::new(BlockState::new(block)));
|
||||
@ -1408,7 +1407,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::eth();
|
||||
let state: CanonicalInMemoryState = CanonicalInMemoryState::empty();
|
||||
|
||||
for i in 1..=3 {
|
||||
@ -1430,7 +1429,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::<EthPrimitives>::eth();
|
||||
let state: CanonicalInMemoryState = CanonicalInMemoryState::empty();
|
||||
|
||||
for i in 1..=2 {
|
||||
|
||||
@ -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<N: NodePrimitives = reth_primitives::EthPrimitives> {
|
||||
pub struct TestBlockBuilder<N: NodePrimitives = EthPrimitives> {
|
||||
/// The account that signs all the block's transactions.
|
||||
pub signer: Address,
|
||||
/// Private key for signing.
|
||||
@ -66,7 +66,7 @@ impl<N: NodePrimitives> Default for TestBlockBuilder<N> {
|
||||
}
|
||||
}
|
||||
|
||||
impl TestBlockBuilder {
|
||||
impl<N: NodePrimitives> TestBlockBuilder<N> {
|
||||
/// Signer pk setter.
|
||||
pub fn with_signer_pk(mut self, signer_pk: PrivateKeySigner) -> Self {
|
||||
self.signer = signer_pk.address();
|
||||
@ -295,6 +295,13 @@ impl TestBlockBuilder {
|
||||
execution_outcome.with_receipts(Receipts::from(receipts))
|
||||
}
|
||||
}
|
||||
|
||||
impl TestBlockBuilder {
|
||||
/// Creates a `TestBlockBuilder` configured for Ethereum primitives.
|
||||
pub fn eth() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
/// A test `ChainEventSubscriptions`
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct TestCanonStateSubscriptions<N: NodePrimitives = reth_primitives::EthPrimitives> {
|
||||
|
||||
@ -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::eth();
|
||||
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::eth();
|
||||
let blocks = test_block_builder.get_executed_blocks(0..5).collect::<Vec<_>>();
|
||||
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::eth();
|
||||
for range in ranges {
|
||||
let blocks = test_block_builder.get_executed_blocks(range).collect::<Vec<_>>();
|
||||
let last_hash = blocks.last().unwrap().block().hash();
|
||||
|
||||
@ -3105,8 +3105,7 @@ mod tests {
|
||||
fn test_tree_persist_block_batch() {
|
||||
let tree_config = TreeConfig::default();
|
||||
let chain_spec = MAINNET.clone();
|
||||
let mut test_block_builder =
|
||||
TestBlockBuilder::default().with_chain_spec((*chain_spec).clone());
|
||||
let mut test_block_builder = TestBlockBuilder::eth().with_chain_spec((*chain_spec).clone());
|
||||
|
||||
// we need more than tree_config.persistence_threshold() +1 blocks to
|
||||
// trigger the persistence task.
|
||||
@ -3140,8 +3139,7 @@ mod tests {
|
||||
async fn test_tree_persist_blocks() {
|
||||
let tree_config = TreeConfig::default();
|
||||
let chain_spec = MAINNET.clone();
|
||||
let mut test_block_builder =
|
||||
TestBlockBuilder::default().with_chain_spec((*chain_spec).clone());
|
||||
let mut test_block_builder = TestBlockBuilder::eth().with_chain_spec((*chain_spec).clone());
|
||||
|
||||
// we need more than tree_config.persistence_threshold() +1 blocks to
|
||||
// trigger the persistence task.
|
||||
@ -3173,7 +3171,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::eth().get_executed_blocks(0..10).collect();
|
||||
let test_harness = TestHarness::new(MAINNET.clone()).with_blocks(blocks.clone());
|
||||
|
||||
for executed_block in blocks {
|
||||
@ -3200,7 +3198,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::eth()
|
||||
.get_executed_blocks(0..tree_config.persistence_threshold())
|
||||
.collect();
|
||||
let mut test_harness = TestHarness::new(MAINNET.clone())
|
||||
@ -3301,7 +3299,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::eth().get_executed_blocks(1..4).collect();
|
||||
|
||||
tree_state.insert_executed(blocks[0].clone());
|
||||
tree_state.insert_executed(blocks[1].clone());
|
||||
@ -3327,7 +3325,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::eth();
|
||||
let blocks: Vec<_> = test_block_builder.get_executed_blocks(1..6).collect();
|
||||
|
||||
for block in &blocks {
|
||||
@ -3367,7 +3365,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::eth().get_executed_blocks(1..6).collect();
|
||||
|
||||
for block in &blocks {
|
||||
tree_state.insert_executed(block.clone());
|
||||
@ -3417,7 +3415,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::eth().get_executed_blocks(1..6).collect();
|
||||
|
||||
for block in &blocks {
|
||||
tree_state.insert_executed(block.clone());
|
||||
@ -3467,7 +3465,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::eth().get_executed_blocks(1..6).collect();
|
||||
|
||||
for block in &blocks {
|
||||
tree_state.insert_executed(block.clone());
|
||||
@ -3517,7 +3515,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::eth();
|
||||
|
||||
let blocks: Vec<_> = test_block_builder.get_executed_blocks(1..6).collect();
|
||||
|
||||
@ -3569,7 +3567,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::eth();
|
||||
|
||||
let blocks: Vec<_> = test_block_builder.get_executed_blocks(0..5).collect();
|
||||
|
||||
@ -3636,7 +3634,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::eth();
|
||||
|
||||
let canonical_head_number = 9;
|
||||
let blocks: Vec<_> =
|
||||
@ -3691,8 +3689,7 @@ mod tests {
|
||||
let chain_spec = MAINNET.clone();
|
||||
let mut test_harness = TestHarness::new(chain_spec.clone());
|
||||
|
||||
let mut test_block_builder =
|
||||
TestBlockBuilder::default().with_chain_spec((*chain_spec).clone());
|
||||
let mut test_block_builder = TestBlockBuilder::eth().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);
|
||||
|
||||
@ -803,7 +803,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,
|
||||
@ -1395,7 +1395,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::eth();
|
||||
let block_1 = test_block_builder.generate_random_block(0, B256::ZERO);
|
||||
let block_hash_1 = block_1.hash();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user