reth_node_builder/
engine_api_ext.rs

1//! `EngineApiBuilder` callback wrapper
2//!
3//! Wraps an `EngineApiBuilder` to provide access to the built Engine API instance.
4
5use crate::rpc::EngineApiBuilder;
6use eyre::Result;
7use reth_node_api::{AddOnsContext, FullNodeComponents};
8use reth_rpc_api::IntoEngineApiRpcModule;
9
10/// Provides access to an `EngineApi` instance with a callback
11#[derive(Debug)]
12pub struct EngineApiExt<B, F> {
13    /// The inner builder that constructs the actual `EngineApi`
14    inner: B,
15    /// Optional callback function to execute with the built API
16    callback: Option<F>,
17}
18
19impl<B, F> EngineApiExt<B, F> {
20    /// Creates a new wrapper that calls `callback` when the API is built.
21    pub const fn new(inner: B, callback: F) -> Self {
22        Self { inner, callback: Some(callback) }
23    }
24}
25
26impl<N, B, F> EngineApiBuilder<N> for EngineApiExt<B, F>
27where
28    B: EngineApiBuilder<N>,
29    N: FullNodeComponents,
30    B::EngineApi: IntoEngineApiRpcModule + Send + Sync + Clone + 'static,
31    F: FnOnce(B::EngineApi) + Send + Sync + 'static,
32{
33    type EngineApi = B::EngineApi;
34
35    /// Builds the `EngineApi` and executes the callback if present.
36    async fn build_engine_api(mut self, ctx: &AddOnsContext<'_, N>) -> Result<Self::EngineApi> {
37        let api = self.inner.build_engine_api(ctx).await?;
38
39        if let Some(callback) = self.callback.take() {
40            callback(api.clone());
41        }
42
43        Ok(api)
44    }
45}