//! Helpers for testing. use crate::execute::{ BatchBlockExecutionOutput, BatchExecutor, BlockExecutionInput, BlockExecutionOutput, BlockExecutorProvider, Executor, }; use parking_lot::Mutex; use reth_execution_errors::BlockExecutionError; use reth_primitives::{BlockNumber, BlockWithSenders, PruneModes, Receipt}; use reth_storage_errors::provider::ProviderError; use revm_primitives::db::Database; use std::sync::Arc; /// A [BlockExecutorProvider] that returns mocked execution results. #[derive(Clone, Debug, Default)] pub struct MockExecutorProvider { exec_results: Arc>>, } impl MockExecutorProvider { /// Extend the mocked execution results pub fn extend(&self, results: impl IntoIterator>) { self.exec_results.lock().extend(results.into_iter().map(Into::into)); } } impl BlockExecutorProvider for MockExecutorProvider { type Executor> = Self; type BatchExecutor> = Self; fn executor(&self, _: DB) -> Self::Executor where DB: Database, { self.clone() } fn batch_executor(&self, _: DB, _: PruneModes) -> Self::BatchExecutor where DB: Database, { self.clone() } } impl Executor for MockExecutorProvider { type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>; type Output = BlockExecutionOutput; type Error = BlockExecutionError; fn execute(self, _: Self::Input<'_>) -> Result { let BatchBlockExecutionOutput { bundle, receipts, requests, first_block: _ } = self.exec_results.lock().pop().unwrap(); Ok(BlockExecutionOutput { state: bundle, receipts: receipts.into_iter().flatten().flatten().collect(), requests: requests.into_iter().flatten().collect(), gas_used: 0, }) } } impl BatchExecutor for MockExecutorProvider { type Input<'a> = BlockExecutionInput<'a, BlockWithSenders>; type Output = BatchBlockExecutionOutput; type Error = BlockExecutionError; fn execute_and_verify_one(&mut self, _: Self::Input<'_>) -> Result<(), Self::Error> { Ok(()) } fn finalize(self) -> Self::Output { self.exec_results.lock().pop().unwrap() } fn set_tip(&mut self, _: BlockNumber) {} fn size_hint(&self) -> Option { None } }