reth_exex/
context.rs

1use crate::{ExExContextDyn, ExExEvent, ExExNotifications, ExExNotificationsStream};
2use reth_exex_types::ExExHead;
3use reth_node_api::{FullNodeComponents, NodePrimitives, NodeTypes};
4use reth_node_core::node_config::NodeConfig;
5use reth_primitives::Head;
6use reth_provider::BlockReader;
7use reth_tasks::TaskExecutor;
8use std::fmt::Debug;
9use tokio::sync::mpsc::UnboundedSender;
10
11/// Captures the context that an `ExEx` has access to.
12pub struct ExExContext<Node: FullNodeComponents> {
13    /// The current head of the blockchain at launch.
14    pub head: Head,
15    /// The config of the node
16    pub config: NodeConfig<<Node::Types as NodeTypes>::ChainSpec>,
17    /// The loaded node config
18    pub reth_config: reth_config::Config,
19    /// Channel used to send [`ExExEvent`]s to the rest of the node.
20    ///
21    /// # Important
22    ///
23    /// The exex should emit a `FinishedHeight` whenever a processed block is safe to prune.
24    /// Additionally, the exex can pre-emptively emit a `FinishedHeight` event to specify what
25    /// blocks to receive notifications for.
26    pub events: UnboundedSender<ExExEvent>,
27    /// Channel to receive [`ExExNotification`](crate::ExExNotification)s.
28    ///
29    /// # Important
30    ///
31    /// Once an [`ExExNotification`](crate::ExExNotification) is sent over the channel, it is
32    /// considered delivered by the node.
33    pub notifications: ExExNotifications<Node::Provider, Node::Executor>,
34
35    /// Node components
36    pub components: Node,
37}
38
39impl<Node> Debug for ExExContext<Node>
40where
41    Node: FullNodeComponents,
42    Node::Provider: Debug,
43    Node::Executor: Debug,
44{
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        f.debug_struct("ExExContext")
47            .field("head", &self.head)
48            .field("config", &self.config)
49            .field("reth_config", &self.reth_config)
50            .field("events", &self.events)
51            .field("notifications", &self.notifications)
52            .field("components", &"...")
53            .finish()
54    }
55}
56
57impl<Node> ExExContext<Node>
58where
59    Node: FullNodeComponents,
60    Node::Provider: Debug + BlockReader,
61    Node::Executor: Debug,
62    Node::Types: NodeTypes<Primitives: NodePrimitives>,
63{
64    /// Returns dynamic version of the context
65    pub fn into_dyn(self) -> ExExContextDyn<<Node::Types as NodeTypes>::Primitives> {
66        ExExContextDyn::from(self)
67    }
68}
69
70impl<Node> ExExContext<Node>
71where
72    Node: FullNodeComponents,
73    Node::Types: NodeTypes<Primitives: NodePrimitives>,
74{
75    /// Returns the transaction pool of the node.
76    pub fn pool(&self) -> &Node::Pool {
77        self.components.pool()
78    }
79
80    /// Returns the node's evm config.
81    pub fn evm_config(&self) -> &Node::Evm {
82        self.components.evm_config()
83    }
84
85    /// Returns the node's executor type.
86    pub fn block_executor(&self) -> &Node::Executor {
87        self.components.block_executor()
88    }
89
90    /// Returns the provider of the node.
91    pub fn provider(&self) -> &Node::Provider {
92        self.components.provider()
93    }
94
95    /// Returns the handle to the network
96    pub fn network(&self) -> &Node::Network {
97        self.components.network()
98    }
99
100    /// Returns the handle to the payload builder service.
101    pub fn payload_builder(&self) -> &Node::PayloadBuilder {
102        self.components.payload_builder()
103    }
104
105    /// Returns the task executor.
106    pub fn task_executor(&self) -> &TaskExecutor {
107        self.components.task_executor()
108    }
109
110    /// Sets notifications stream to [`crate::ExExNotificationsWithoutHead`], a stream of
111    /// notifications without a head.
112    pub fn set_notifications_without_head(&mut self) {
113        self.notifications.set_without_head();
114    }
115
116    /// Sets notifications stream to [`crate::ExExNotificationsWithHead`], a stream of notifications
117    /// with the provided head.
118    pub fn set_notifications_with_head(&mut self, head: ExExHead) {
119        self.notifications.set_with_head(head);
120    }
121}
122
123#[cfg(test)]
124mod tests {
125    use reth_exex_types::ExExHead;
126    use reth_node_api::FullNodeComponents;
127    use reth_provider::BlockReader;
128
129    use crate::ExExContext;
130
131    /// <https://github.com/paradigmxyz/reth/issues/12054>
132    #[test]
133    const fn issue_12054() {
134        #[allow(dead_code)]
135        struct ExEx<Node: FullNodeComponents> {
136            ctx: ExExContext<Node>,
137        }
138
139        impl<Node: FullNodeComponents> ExEx<Node>
140        where
141            Node::Provider: BlockReader,
142        {
143            async fn _test_bounds(mut self) -> eyre::Result<()> {
144                self.ctx.pool();
145                self.ctx.block_executor();
146                self.ctx.provider();
147                self.ctx.network();
148                self.ctx.payload_builder();
149                self.ctx.task_executor();
150                self.ctx.set_notifications_without_head();
151                self.ctx.set_notifications_with_head(ExExHead { block: Default::default() });
152                Ok(())
153            }
154        }
155    }
156}