Skip to main content

reth_exex/
context.rs

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