reth_engine_tree/
test_utils.rs

1use alloy_primitives::B256;
2use reth_chainspec::ChainSpec;
3use reth_ethereum_primitives::BlockBody;
4use reth_network_p2p::test_utils::TestFullBlockClient;
5use reth_primitives_traits::SealedHeader;
6use reth_provider::{
7    test_utils::{create_test_provider_factory_with_chain_spec, MockNodeTypesWithDB},
8    ExecutionOutcome,
9};
10use reth_prune_types::PruneModes;
11use reth_stages::{test_utils::TestStages, ExecOutput, StageError};
12use reth_stages_api::Pipeline;
13use reth_static_file::StaticFileProducer;
14use std::{collections::VecDeque, ops::Range, sync::Arc};
15use tokio::sync::watch;
16
17/// Test pipeline builder.
18#[derive(Default, Debug)]
19pub struct TestPipelineBuilder {
20    pipeline_exec_outputs: VecDeque<Result<ExecOutput, StageError>>,
21    executor_results: Vec<ExecutionOutcome>,
22}
23
24impl TestPipelineBuilder {
25    /// Create a new [`TestPipelineBuilder`].
26    pub const fn new() -> Self {
27        Self { pipeline_exec_outputs: VecDeque::new(), executor_results: Vec::new() }
28    }
29
30    /// Set the pipeline execution outputs to use for the test consensus engine.
31    pub fn with_pipeline_exec_outputs(
32        mut self,
33        pipeline_exec_outputs: VecDeque<Result<ExecOutput, StageError>>,
34    ) -> Self {
35        self.pipeline_exec_outputs = pipeline_exec_outputs;
36        self
37    }
38
39    /// Set the executor results to use for the test consensus engine.
40    pub fn with_executor_results(mut self, executor_results: Vec<ExecutionOutcome>) -> Self {
41        self.executor_results = executor_results;
42        self
43    }
44
45    /// Builds the pipeline.
46    pub fn build(self, chain_spec: Arc<ChainSpec>) -> Pipeline<MockNodeTypesWithDB> {
47        reth_tracing::init_test_tracing();
48
49        // Setup pipeline
50        let (tip_tx, _tip_rx) = watch::channel(B256::default());
51        let pipeline = Pipeline::<MockNodeTypesWithDB>::builder()
52            .add_stages(TestStages::new(self.pipeline_exec_outputs, Default::default()))
53            .with_tip_sender(tip_tx);
54
55        let provider_factory = create_test_provider_factory_with_chain_spec(chain_spec);
56
57        let static_file_producer =
58            StaticFileProducer::new(provider_factory.clone(), PruneModes::default());
59
60        pipeline.build(provider_factory, static_file_producer)
61    }
62}
63
64/// Starting from the given genesis header, inserts headers from the given
65/// range in the given test full block client.
66pub fn insert_headers_into_client(
67    client: &TestFullBlockClient,
68    genesis_header: SealedHeader,
69    range: Range<usize>,
70) {
71    let mut sealed_header = genesis_header;
72    let body = BlockBody::default();
73    for _ in range {
74        let (mut header, hash) = sealed_header.split();
75        // update to the next header
76        header.parent_hash = hash;
77        header.number += 1;
78        header.timestamp += 1;
79        sealed_header = SealedHeader::seal_slow(header);
80        client.insert(sealed_header.clone(), body.clone());
81    }
82}