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