|
| 1 | +//! Wrapper around the OP node builder that accumulates hooks instead of replacing them. |
| 2 | +
|
| 3 | +use eyre::Result; |
| 4 | +use reth_node_builder::{ |
| 5 | + NodeAdapter, NodeComponentsBuilder, |
| 6 | + node::FullNode, |
| 7 | + rpc::{RethRpcAddOns, RpcContext}, |
| 8 | +}; |
| 9 | +use std::fmt; |
| 10 | + |
| 11 | +use crate::OpBuilder; |
| 12 | +use crate::types::{OpAddOns, OpComponentsBuilder, OpNodeTypes}; |
| 13 | + |
| 14 | +/// Convenience alias for the OP node adapter type used by the reth builder. |
| 15 | +pub(crate) type OpNodeAdapter = NodeAdapter< |
| 16 | + OpNodeTypes, |
| 17 | + <OpComponentsBuilder as NodeComponentsBuilder<OpNodeTypes>>::Components, |
| 18 | +>; |
| 19 | + |
| 20 | +/// Convenience alias for the OP Eth API type exposed by the reth RPC add-ons. |
| 21 | +type OpEthApi = <OpAddOns as RethRpcAddOns<OpNodeAdapter>>::EthApi; |
| 22 | + |
| 23 | +/// Convenience alias for the full OP node handle produced after launch. |
| 24 | +type OpFullNode = FullNode<OpNodeAdapter, OpAddOns>; |
| 25 | + |
| 26 | +/// Alias for the RPC context used by Base extensions. |
| 27 | +pub type BaseRpcContext<'a> = RpcContext<'a, OpNodeAdapter, OpEthApi>; |
| 28 | + |
| 29 | +/// Hook type for extending RPC modules. |
| 30 | +type RpcModuleHook = Box<dyn FnMut(&mut BaseRpcContext<'_>) -> Result<()> + Send + 'static>; |
| 31 | + |
| 32 | +/// Hook type for node-started callbacks. |
| 33 | +type NodeStartedHook = Box<dyn FnMut(OpFullNode) -> Result<()> + Send + 'static>; |
| 34 | + |
| 35 | +/// A thin wrapper over [`OpBuilder`] that accumulates RPC and node-start hooks. |
| 36 | +pub struct BaseBuilder { |
| 37 | + builder: OpBuilder, |
| 38 | + rpc_hooks: Vec<RpcModuleHook>, |
| 39 | + node_started_hooks: Vec<NodeStartedHook>, |
| 40 | +} |
| 41 | + |
| 42 | +impl BaseBuilder { |
| 43 | + /// Create a new BaseBuilder wrapping the provided OP builder. |
| 44 | + pub const fn new(builder: OpBuilder) -> Self { |
| 45 | + Self { builder, rpc_hooks: Vec::new(), node_started_hooks: Vec::new() } |
| 46 | + } |
| 47 | + |
| 48 | + /// Consumes the wrapper and returns the inner builder after installing the accumulated hooks. |
| 49 | + pub fn build(self) -> OpBuilder { |
| 50 | + let BaseBuilder { mut builder, mut rpc_hooks, node_started_hooks } = self; |
| 51 | + |
| 52 | + if !rpc_hooks.is_empty() { |
| 53 | + builder = builder.extend_rpc_modules(move |mut ctx: BaseRpcContext<'_>| { |
| 54 | + for hook in rpc_hooks.iter_mut() { |
| 55 | + hook(&mut ctx)?; |
| 56 | + } |
| 57 | + |
| 58 | + Ok(()) |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + if !node_started_hooks.is_empty() { |
| 63 | + builder = builder.on_node_started(move |full_node: OpFullNode| { |
| 64 | + let mut hooks = node_started_hooks; |
| 65 | + for hook in hooks.iter_mut() { |
| 66 | + hook(full_node.clone())?; |
| 67 | + } |
| 68 | + Ok(()) |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + builder |
| 73 | + } |
| 74 | + |
| 75 | + /// Adds an RPC hook that will run when RPC modules are configured. |
| 76 | + pub fn add_rpc_module<F>(mut self, hook: F) -> Self |
| 77 | + where |
| 78 | + F: FnOnce(&mut BaseRpcContext<'_>) -> Result<()> + Send + 'static, |
| 79 | + { |
| 80 | + let mut hook = Some(hook); |
| 81 | + self.rpc_hooks.push(Box::new(move |ctx| { |
| 82 | + if let Some(hook) = hook.take() { |
| 83 | + hook(ctx)?; |
| 84 | + } |
| 85 | + Ok(()) |
| 86 | + })); |
| 87 | + self |
| 88 | + } |
| 89 | + |
| 90 | + /// Adds a node-started hook that will run after the node has started. |
| 91 | + pub fn add_node_started_hook<F>(mut self, hook: F) -> Self |
| 92 | + where |
| 93 | + F: FnOnce(OpFullNode) -> Result<()> + Send + 'static, |
| 94 | + { |
| 95 | + let mut hook = Some(hook); |
| 96 | + self.node_started_hooks.push(Box::new(move |node| { |
| 97 | + if let Some(hook) = hook.take() { |
| 98 | + hook(node)?; |
| 99 | + } |
| 100 | + Ok(()) |
| 101 | + })); |
| 102 | + self |
| 103 | + } |
| 104 | + |
| 105 | + /// Launches the node after applying accumulated hooks, delegating to the provided closure. |
| 106 | + pub fn launch_with_fn<L, R>(self, launcher: L) -> R |
| 107 | + where |
| 108 | + L: FnOnce(OpBuilder) -> R, |
| 109 | + { |
| 110 | + launcher(self.build()) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl fmt::Debug for BaseBuilder { |
| 115 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 116 | + f.debug_struct("BaseBuilder").finish_non_exhaustive() |
| 117 | + } |
| 118 | +} |
0 commit comments