Skip to content

Commit 352cc43

Browse files
refcellhaardikk21
authored andcommitted
feat(runner): Generic Extensions (#268)
* chore(runner): node handle * feat(runner): make extensions generic
1 parent de41f5f commit 352cc43

File tree

14 files changed

+193
-64
lines changed

14 files changed

+193
-64
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ reth-cli-util.workspace = true
2424

2525
# misc
2626
clap.workspace = true
27-
alloy-rlp = "0.3.12"
27+
once_cell.workspace = true
2828

2929
[features]
3030
default = []

bin/node/src/cli.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Contains the CLI arguments
22
3-
use base_reth_runner::{BaseNodeConfig, FlashblocksConfig, TracingConfig};
3+
use std::sync::Arc;
4+
5+
use base_reth_runner::{BaseNodeConfig, FlashblocksCell, FlashblocksConfig, TracingConfig};
6+
use once_cell::sync::OnceCell;
47
use reth_optimism_node::args::RollupArgs;
58

69
/// CLI Arguments
@@ -49,6 +52,7 @@ impl Args {
4952

5053
impl From<Args> for BaseNodeConfig {
5154
fn from(args: Args) -> Self {
55+
let flashblocks_cell: FlashblocksCell = Arc::new(OnceCell::new());
5256
let flashblocks = args.websocket_url.map(|websocket_url| FlashblocksConfig {
5357
websocket_url,
5458
max_pending_blocks_depth: args.max_pending_blocks_depth,
@@ -62,6 +66,7 @@ impl From<Args> for BaseNodeConfig {
6266
logs_enabled: args.enable_transaction_tracing_logs,
6367
},
6468
metering_enabled: args.enable_metering,
69+
flashblocks_cell,
6570
}
6671
}
6772
}

bin/node/src/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
pub mod cli;
77

8-
use base_reth_runner::BaseNodeRunner;
8+
use base_reth_runner::{
9+
BaseNodeRunner, BaseRpcExtension, FlashblocksCanonExtension, TransactionTracingExtension,
10+
};
911

1012
#[global_allocator]
1113
static ALLOC: reth_cli_util::allocator::Allocator = reth_cli_util::allocator::new_allocator();
@@ -21,9 +23,12 @@ fn main() {
2123

2224
// Step 3: Hand the parsed CLI to the node runner so it can build and launch the Base node.
2325
cli.run(|builder, args| async move {
24-
let runner = BaseNodeRunner::new(args);
25-
let handle = runner.build(builder).await?;
26-
runner.run(handle).await
26+
let mut runner = BaseNodeRunner::new(args);
27+
runner.install_ext::<FlashblocksCanonExtension>()?;
28+
runner.install_ext::<TransactionTracingExtension>()?;
29+
runner.install_ext::<BaseRpcExtension>()?;
30+
let handle = runner.run(builder);
31+
handle.await
2732
})
2833
.unwrap();
2934
}

crates/runner/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ futures-util.workspace = true
3030
once_cell.workspace = true
3131
tracing.workspace = true
3232
url.workspace = true
33+
derive_more = { workspace = true, features = ["debug"] }

crates/runner/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
use reth_optimism_node::args::RollupArgs;
44

5+
use crate::extensions::FlashblocksCell;
6+
57
/// Captures the pieces of CLI configuration that the node logic cares about.
68
#[derive(Debug, Clone)]
79
pub struct BaseNodeConfig {
@@ -13,6 +15,8 @@ pub struct BaseNodeConfig {
1315
pub tracing: TracingConfig,
1416
/// Indicates whether the metering RPC surface should be installed.
1517
pub metering_enabled: bool,
18+
/// Shared Flashblocks state cache.
19+
pub flashblocks_cell: FlashblocksCell,
1620
}
1721

1822
impl BaseNodeConfig {

crates/runner/src/extensions/canon.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use futures_util::TryStreamExt;
88
use reth_exex::ExExEvent;
99

1010
use crate::{
11-
FlashblocksConfig,
12-
extensions::{FlashblocksCell, OpBuilder},
11+
BaseNodeConfig, FlashblocksConfig,
12+
extensions::{BaseNodeExtension, ConfigurableBaseNodeExtension, FlashblocksCell, OpBuilder},
1313
};
1414

1515
/// Helper struct that wires the Flashblocks canon ExEx into the node builder.
@@ -23,12 +23,14 @@ pub struct FlashblocksCanonExtension {
2323

2424
impl FlashblocksCanonExtension {
2525
/// Create a new Flashblocks canon extension helper.
26-
pub const fn new(cell: FlashblocksCell, config: Option<FlashblocksConfig>) -> Self {
27-
Self { cell, config }
26+
pub fn new(config: &BaseNodeConfig) -> Self {
27+
Self { cell: config.flashblocks_cell.clone(), config: config.flashblocks.clone() }
2828
}
29+
}
2930

31+
impl BaseNodeExtension for FlashblocksCanonExtension {
3032
/// Applies the extension to the supplied builder.
31-
pub fn apply(&self, builder: OpBuilder) -> OpBuilder {
33+
fn apply(&self, builder: OpBuilder) -> OpBuilder {
3234
let flashblocks = self.config.clone();
3335
let flashblocks_enabled = flashblocks.is_some();
3436
let flashblocks_cell = self.cell.clone();
@@ -64,3 +66,9 @@ impl FlashblocksCanonExtension {
6466
})
6567
}
6668
}
69+
70+
impl ConfigurableBaseNodeExtension for FlashblocksCanonExtension {
71+
fn build(config: &BaseNodeConfig) -> eyre::Result<Self> {
72+
Ok(Self::new(config))
73+
}
74+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Traits describing configurable node builder extensions.
2+
3+
use std::fmt::Debug;
4+
5+
use eyre::Result;
6+
7+
use crate::{BaseNodeConfig, extensions::OpBuilder};
8+
9+
/// A node builder extension that can apply additional wiring to the builder.
10+
pub trait BaseNodeExtension: Send + Sync + Debug {
11+
/// Applies the extension to the supplied builder.
12+
fn apply(&self, builder: OpBuilder) -> OpBuilder;
13+
}
14+
15+
/// An extension that can be constructed from [`BaseNodeConfig`].
16+
pub trait ConfigurableBaseNodeExtension: BaseNodeExtension + Sized + 'static {
17+
/// Builds the extension from the node config.
18+
fn build(config: &BaseNodeConfig) -> Result<Self>;
19+
}

crates/runner/src/extensions/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
//! Builder extensions for the node nicely modularizes parts
44
//! of the node building process.
55
6+
mod extension;
7+
pub use extension::{BaseNodeExtension, ConfigurableBaseNodeExtension};
8+
69
mod canon;
710
pub use canon::FlashblocksCanonExtension;
811

crates/runner/src/extensions/rpc.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use tracing::info;
1111
use url::Url;
1212

1313
use crate::{
14-
FlashblocksConfig,
15-
extensions::{FlashblocksCell, OpBuilder},
14+
BaseNodeConfig, FlashblocksConfig,
15+
extensions::{BaseNodeExtension, ConfigurableBaseNodeExtension, FlashblocksCell, OpBuilder},
1616
};
1717

1818
/// Helper struct that wires the custom RPC modules into the node builder.
@@ -30,17 +30,19 @@ pub struct BaseRpcExtension {
3030

3131
impl BaseRpcExtension {
3232
/// Creates a new RPC extension helper.
33-
pub const fn new(
34-
flashblocks_cell: FlashblocksCell,
35-
flashblocks: Option<FlashblocksConfig>,
36-
metering_enabled: bool,
37-
sequencer_rpc: Option<String>,
38-
) -> Self {
39-
Self { flashblocks_cell, flashblocks, metering_enabled, sequencer_rpc }
33+
pub fn new(config: &BaseNodeConfig) -> Self {
34+
Self {
35+
flashblocks_cell: config.flashblocks_cell.clone(),
36+
flashblocks: config.flashblocks.clone(),
37+
metering_enabled: config.metering_enabled,
38+
sequencer_rpc: config.rollup_args.sequencer.clone(),
39+
}
4040
}
41+
}
4142

43+
impl BaseNodeExtension for BaseRpcExtension {
4244
/// Applies the extension to the supplied builder.
43-
pub fn apply(&self, builder: OpBuilder) -> OpBuilder {
45+
fn apply(&self, builder: OpBuilder) -> OpBuilder {
4446
let flashblocks_cell = self.flashblocks_cell.clone();
4547
let flashblocks = self.flashblocks.clone();
4648
let metering_enabled = self.metering_enabled;
@@ -93,3 +95,9 @@ impl BaseRpcExtension {
9395
})
9496
}
9597
}
98+
99+
impl ConfigurableBaseNodeExtension for BaseRpcExtension {
100+
fn build(config: &BaseNodeConfig) -> eyre::Result<Self> {
101+
Ok(Self::new(config))
102+
}
103+
}

0 commit comments

Comments
 (0)