Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions crates/client/flashblocks/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::sync::Arc;

use base_client_node::{BaseNodeExtension, FromExtensionConfig, OpBuilder};
use base_client_node::{BaseBuilder, BaseNodeExtension, FromExtensionConfig};
use reth_chain_state::CanonStateSubscriptions;
use tokio_stream::{StreamExt, wrappers::BroadcastStream};
use tracing::info;
Expand Down Expand Up @@ -50,7 +50,7 @@ impl FlashblocksExtension {

impl BaseNodeExtension for FlashblocksExtension {
/// Applies the extension to the supplied builder.
fn apply(self: Box<Self>, builder: OpBuilder) -> OpBuilder {
fn apply(self: Box<Self>, builder: BaseBuilder) -> BaseBuilder {
let Some(cfg) = self.config else {
info!(message = "flashblocks integration is disabled");
return builder;
Expand All @@ -64,7 +64,7 @@ impl BaseNodeExtension for FlashblocksExtension {
let state_for_start = state;

// Start state processor, subscriber, and canonical subscription after node is started
let builder = builder.on_node_started(move |ctx| {
let builder = builder.add_node_started_hook(move |ctx| {
info!(message = "Starting Flashblocks state processor");
state_for_start.start(ctx.provider().clone());
subscriber.start();
Expand All @@ -84,7 +84,7 @@ impl BaseNodeExtension for FlashblocksExtension {
});

// Extend with RPC modules
builder.extend_rpc_modules(move |ctx| {
builder.add_rpc_module(move |ctx| {
info!(message = "Starting Flashblocks RPC");

let api_ext = EthApiExt::new(
Expand Down
8 changes: 4 additions & 4 deletions crates/client/flashblocks/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
};

use base_client_node::{
BaseNodeExtension,
BaseBuilder, BaseNodeExtension,
test_utils::{
LocalNode, NODE_STARTUP_DELAY_MS, TestHarness, build_test_genesis, init_silenced_tracing,
},
Expand Down Expand Up @@ -107,7 +107,7 @@ impl FlashblocksTestExtension {
}

impl BaseNodeExtension for FlashblocksTestExtension {
fn apply(self: Box<Self>, builder: base_client_node::OpBuilder) -> base_client_node::OpBuilder {
fn apply(self: Box<Self>, builder: BaseBuilder) -> BaseBuilder {
let state = self.inner.state.clone();
let receiver = self.inner.receiver.clone();
let process_canonical = self.inner.process_canonical;
Expand All @@ -116,7 +116,7 @@ impl BaseNodeExtension for FlashblocksTestExtension {
let state_for_rpc = state.clone();

// Start state processor and subscriptions after node is started
let builder = builder.on_node_started(move |ctx| {
let builder = builder.add_node_started_hook(move |ctx| {
let provider = ctx.provider().clone();

// Start the state processor with the provider
Expand Down Expand Up @@ -152,7 +152,7 @@ impl BaseNodeExtension for FlashblocksTestExtension {
Ok(())
});

builder.extend_rpc_modules(move |ctx| {
builder.add_rpc_module(move |ctx| {
let fb = state_for_rpc;

let api_ext = EthApiExt::new(
Expand Down
6 changes: 3 additions & 3 deletions crates/client/metering/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::sync::Arc;

use base_client_node::{BaseNodeExtension, FromExtensionConfig, OpBuilder};
use base_client_node::{BaseBuilder, BaseNodeExtension, FromExtensionConfig};
use base_flashblocks::{FlashblocksConfig, FlashblocksState};
use tracing::info;

Expand All @@ -27,14 +27,14 @@ impl MeteringExtension {

impl BaseNodeExtension for MeteringExtension {
/// Applies the extension to the supplied builder.
fn apply(self: Box<Self>, builder: OpBuilder) -> OpBuilder {
fn apply(self: Box<Self>, builder: BaseBuilder) -> BaseBuilder {
if !self.enabled {
return builder;
}

let flashblocks_config = self.flashblocks_config;

builder.extend_rpc_modules(move |ctx| {
builder.add_rpc_module(move |ctx| {
info!(message = "Starting Metering RPC");

// Get flashblocks state from config, or create a default one if not configured
Expand Down
7 changes: 7 additions & 0 deletions crates/client/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ test-utils = [
"reth-provider/test-utils",
]

[dev-dependencies]
base-metering = { path = "../metering" }
base-txpool = { path = "../txpool" }
jsonrpsee = { workspace = true, features = ["http-client"] }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
Comment on lines +51 to +56
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to have client-node depend on base-metering (as it introduces a circular dependency).

This is a very useful test (and one that was missing!), but for now lets just add an issue for adding this. We can either add it to our system test framework or have a top level client client

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sidenote, I wonder where tests like these should live. They're not crossing top-level boundaries (i.e. need both builder + client) - but also are not limited to a single crate.

Do we introduce some tests within the bin/... crates to have tests that are constrained to only builder or client - but work across multiple sub-packages?

Or should they just go alongside the system tests when we do those even though they dont require the builder particularly


[dependencies]
# Project
base-primitives = { workspace = true, optional = true }
Expand Down
118 changes: 118 additions & 0 deletions crates/client/node/src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//! Wrapper around the OP node builder that accumulates hooks instead of replacing them.

use eyre::Result;
use reth_node_builder::{
NodeAdapter, NodeComponentsBuilder,
node::FullNode,
rpc::{RethRpcAddOns, RpcContext},
};
use std::fmt;

use crate::OpBuilder;
use crate::types::{OpAddOns, OpComponentsBuilder, OpNodeTypes};

/// Convenience alias for the OP node adapter type used by the reth builder.
pub(crate) type OpNodeAdapter = NodeAdapter<
OpNodeTypes,
<OpComponentsBuilder as NodeComponentsBuilder<OpNodeTypes>>::Components,
>;

/// Convenience alias for the OP Eth API type exposed by the reth RPC add-ons.
type OpEthApi = <OpAddOns as RethRpcAddOns<OpNodeAdapter>>::EthApi;

/// Convenience alias for the full OP node handle produced after launch.
type OpFullNode = FullNode<OpNodeAdapter, OpAddOns>;

/// Alias for the RPC context used by Base extensions.
pub type BaseRpcContext<'a> = RpcContext<'a, OpNodeAdapter, OpEthApi>;

/// Hook type for extending RPC modules.
type RpcModuleHook = Box<dyn FnMut(&mut BaseRpcContext<'_>) -> Result<()> + Send + 'static>;

/// Hook type for node-started callbacks.
type NodeStartedHook = Box<dyn FnMut(OpFullNode) -> Result<()> + Send + 'static>;

/// A thin wrapper over [`OpBuilder`] that accumulates RPC and node-start hooks.
pub struct BaseBuilder {
builder: OpBuilder,
rpc_hooks: Vec<RpcModuleHook>,
node_started_hooks: Vec<NodeStartedHook>,
}

impl BaseBuilder {
/// Create a new BaseBuilder wrapping the provided OP builder.
pub const fn new(builder: OpBuilder) -> Self {
Self { builder, rpc_hooks: Vec::new(), node_started_hooks: Vec::new() }
}

/// Consumes the wrapper and returns the inner builder after installing the accumulated hooks.
pub fn build(self) -> OpBuilder {
let BaseBuilder { mut builder, mut rpc_hooks, node_started_hooks } = self;

if !rpc_hooks.is_empty() {
builder = builder.extend_rpc_modules(move |mut ctx: BaseRpcContext<'_>| {
for hook in rpc_hooks.iter_mut() {
hook(&mut ctx)?;
}

Ok(())
});
}

if !node_started_hooks.is_empty() {
builder = builder.on_node_started(move |full_node: OpFullNode| {
let mut hooks = node_started_hooks;
for hook in hooks.iter_mut() {
hook(full_node.clone())?;
}
Ok(())
});
}

builder
}

/// Adds an RPC hook that will run when RPC modules are configured.
pub fn add_rpc_module<F>(mut self, hook: F) -> Self
where
F: FnOnce(&mut BaseRpcContext<'_>) -> Result<()> + Send + 'static,
{
let mut hook = Some(hook);
self.rpc_hooks.push(Box::new(move |ctx| {
if let Some(hook) = hook.take() {
hook(ctx)?;
}
Ok(())
}));
self
}

/// Adds a node-started hook that will run after the node has started.
pub fn add_node_started_hook<F>(mut self, hook: F) -> Self
where
F: FnOnce(OpFullNode) -> Result<()> + Send + 'static,
{
let mut hook = Some(hook);
self.node_started_hooks.push(Box::new(move |node| {
if let Some(hook) = hook.take() {
hook(node)?;
}
Ok(())
}));
self
}

/// Launches the node after applying accumulated hooks, delegating to the provided closure.
pub fn launch_with_fn<L, R>(self, launcher: L) -> R
where
L: FnOnce(OpBuilder) -> R,
{
launcher(self.build())
}
}

impl fmt::Debug for BaseBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BaseBuilder").finish_non_exhaustive()
}
}
4 changes: 2 additions & 2 deletions crates/client/node/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

use std::fmt::Debug;

use crate::OpBuilder;
use crate::BaseBuilder;

/// Customizes the node builder before launch.
///
/// Register extensions via [`BaseNodeRunner::install_ext`].
pub trait BaseNodeExtension: Send + Sync + Debug {
/// Applies the extension to the supplied builder.
fn apply(self: Box<Self>, builder: OpBuilder) -> OpBuilder;
fn apply(self: Box<Self>, builder: BaseBuilder) -> BaseBuilder;
}

/// An extension that can be built from a config.
Expand Down
3 changes: 3 additions & 0 deletions crates/client/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]

mod builder;
pub use builder::{BaseBuilder, BaseRpcContext};

mod extension;
pub use extension::{BaseNodeExtension, FromExtensionConfig};

Expand Down
7 changes: 4 additions & 3 deletions crates/client/node/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use reth_optimism_node::{OpNode, args::RollupArgs};
use reth_provider::providers::BlockchainProvider;
use tracing::info;

use crate::{BaseNodeBuilder, BaseNodeExtension, BaseNodeHandle, FromExtensionConfig};
use crate::{BaseBuilder, BaseNodeBuilder, BaseNodeExtension, BaseNodeHandle, FromExtensionConfig};

/// Wraps the Base node configuration and orchestrates builder wiring.
#[derive(Debug)]
Expand Down Expand Up @@ -50,8 +50,9 @@ impl BaseNodeRunner {
.with_add_ons(op_node.add_ons())
.on_component_initialized(move |_ctx| Ok(()));

let builder =
extensions.into_iter().fold(builder, |builder, extension| extension.apply(builder));
let builder = extensions
.into_iter()
.fold(BaseBuilder::new(builder), |builder, extension| extension.apply(builder));

builder
.launch_with_fn(|builder| {
Expand Down
12 changes: 9 additions & 3 deletions crates/client/node/src/test_utils/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use reth_optimism_node::{OpNode, args::RollupArgs};
use reth_provider::providers::BlockchainProvider;
use reth_tasks::TaskManager;

use crate::{BaseNodeExtension, OpProvider, test_utils::engine::EngineApi};
use crate::{BaseBuilder, BaseNodeExtension, OpProvider, test_utils::engine::EngineApi};

/// Convenience alias for the local blockchain provider type.
pub type LocalNodeProvider = OpProvider;
Expand Down Expand Up @@ -103,8 +103,9 @@ impl LocalNode {
.on_component_initialized(move |_ctx| Ok(()));

// Apply all extensions
let builder =
extensions.into_iter().fold(builder, |builder, extension| extension.apply(builder));
let builder = extensions
.into_iter()
.fold(BaseBuilder::new(builder), |builder, extension| extension.apply(builder));

// Launch with EngineNodeLauncher
let NodeHandle { node: node_handle, node_exit_future } = builder
Expand Down Expand Up @@ -166,6 +167,11 @@ impl LocalNode {
Ok(RootProvider::<Optimism>::new(client))
}

/// HTTP RPC address for the local node.
pub const fn http_addr(&self) -> SocketAddr {
self.http_api_addr
}

/// Build an Engine API client that talks to the node's IPC endpoint.
pub fn engine_api(&self) -> Result<EngineApi<crate::test_utils::engine::IpcEngine>> {
EngineApi::<crate::test_utils::engine::IpcEngine>::new(self.engine_ipc_path.clone())
Expand Down
9 changes: 6 additions & 3 deletions crates/client/node/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_node::OpNode;
use reth_provider::providers::BlockchainProvider;

type OpNodeTypes = FullNodeTypesAdapter<OpNode, Arc<DatabaseEnv>, OpProvider>;
type OpComponentsBuilder = <OpNode as Node<OpNodeTypes>>::ComponentsBuilder;
type OpAddOns = <OpNode as Node<OpNodeTypes>>::AddOns;
/// Internal alias for the OP node type adapter.
pub(crate) type OpNodeTypes = FullNodeTypesAdapter<OpNode, Arc<DatabaseEnv>, OpProvider>;
/// Internal alias for the OP node components builder.
pub(crate) type OpComponentsBuilder = <OpNode as Node<OpNodeTypes>>::ComponentsBuilder;
/// Internal alias for the OP node add-ons.
pub(crate) type OpAddOns = <OpNode as Node<OpNodeTypes>>::AddOns;

/// A [`BlockchainProvider`] instance.
pub type OpProvider = BlockchainProvider<NodeTypesWithDBAdapter<OpNode, Arc<DatabaseEnv>>>;
Expand Down
Loading
Loading