reth_engine_tree/tree/txpool_prewarm/
mod.rs1mod control;
4mod worker;
5
6use self::control::Control;
7use crate::tree::{StateProviderBuilder, TxPoolPrewarmCacheSnapshot};
8use alloy_consensus::transaction::Recovered;
9use alloy_primitives::{Address, B256};
10use reth_evm::{ConfigureEvm, EvmEnvFor};
11use reth_primitives_traits::{NodePrimitives, TxTy};
12use reth_provider::{BlockReader, StateProviderFactory, StateReader};
13use std::{fmt::Debug, sync::Arc};
14
15pub(crate) struct Handle<N, P, Evm>
17where
18 N: NodePrimitives,
19 Evm: ConfigureEvm<Primitives = N>,
20{
21 control: Arc<Control<Job<N, P, Evm>>>,
22}
23
24impl<N, P, Evm> Debug for Handle<N, P, Evm>
25where
26 N: NodePrimitives,
27 Evm: ConfigureEvm<Primitives = N>,
28{
29 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 f.debug_struct("Handle").field("control", &self.control).finish()
31 }
32}
33
34impl<N, P, Evm> Handle<N, P, Evm>
35where
36 N: NodePrimitives,
37 P: BlockReader + StateProviderFactory + StateReader + Clone + Send + Sync + 'static,
38 Evm: ConfigureEvm<Primitives = N> + 'static,
39{
40 pub(crate) fn spawn(
43 runtime: &reth_tasks::Runtime,
44 source: Arc<dyn Source<N>>,
45 evm_config: Evm,
46 ) -> Self {
47 let (control, commands) = Control::new();
48 let publication = control.publication();
49 runtime.spawn_critical_os_thread("txpool-prewarm", "txpool prewarm worker", async move {
50 worker::Worker::new(commands, publication, source, evm_config).run()
51 });
52 Self { control }
53 }
54
55 pub(crate) fn pause(&self) -> impl Drop + Send + 'static {
64 self.control.pause()
65 }
66
67 pub(crate) fn snapshot(&self, parent_hash: B256) -> Option<TxPoolPrewarmCacheSnapshot> {
70 self.control.snapshot(parent_hash)
71 }
72
73 pub(crate) fn start(
75 &self,
76 parent_hash: B256,
77 evm_env: EvmEnvFor<Evm>,
78 provider_builder: StateProviderBuilder<N, P>,
79 ) {
80 self.control.start(parent_hash, Job { evm_env, provider_builder });
81 }
82}
83
84pub type Transactions<N> = Box<dyn Iterator<Item = Transaction<N>> + Send>;
89
90#[derive(Debug, Clone)]
92pub struct Transaction<N: NodePrimitives> {
93 pub hash: B256,
95 pub sender: Address,
97 pub transaction: Recovered<TxTy<N>>,
99}
100
101pub trait Source<N: NodePrimitives>: Send + Sync + Debug {
103 fn best_transactions(&self, parent_hash: B256) -> Option<Transactions<N>>;
109}
110
111struct Job<N: NodePrimitives, P, Evm: ConfigureEvm<Primitives = N>> {
113 evm_env: EvmEnvFor<Evm>,
114 provider_builder: StateProviderBuilder<N, P>,
115}