Skip to content

Commit

Permalink
chore: add OpEthApiBuilder and OpEthApiInner (#13009)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo authored Dec 2, 2024
1 parent 332cce1 commit 6789ff4
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 69 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

9 changes: 4 additions & 5 deletions crates/optimism/bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#![cfg(feature = "optimism")]

use clap::Parser;
use reth_node_builder::{engine_tree_config::TreeConfig, EngineNodeLauncher};
use reth_node_builder::{engine_tree_config::TreeConfig, EngineNodeLauncher, Node};
use reth_optimism_cli::{chainspec::OpChainSpecParser, Cli};
use reth_optimism_node::{args::RollupArgs, node::OpAddOns, OpNode};
use reth_optimism_node::{args::RollupArgs, OpNode};
use reth_provider::providers::BlockchainProvider2;

use tracing as _;
Expand All @@ -27,16 +27,15 @@ fn main() {
tracing::warn!(target: "reth::cli", "Experimental engine is default now, and the --engine.experimental flag is deprecated. To enable the legacy functionality, use --engine.legacy.");
}
let use_legacy_engine = rollup_args.legacy;
let sequencer_http_arg = rollup_args.sequencer_http.clone();
match use_legacy_engine {
false => {
let engine_tree_config = TreeConfig::default()
.with_persistence_threshold(rollup_args.persistence_threshold)
.with_memory_block_buffer_target(rollup_args.memory_block_buffer_target);
let handle = builder
.with_types_and_provider::<OpNode, BlockchainProvider2<_>>()
.with_components(OpNode::components(rollup_args))
.with_add_ons(OpAddOns::new(sequencer_http_arg))
.with_components(OpNode::components(rollup_args.clone()))
.with_add_ons(OpNode::new(rollup_args).add_ons())
.launch_with_fn(|builder| {
let launcher = EngineNodeLauncher::new(
builder.task_executor().clone(),
Expand Down
47 changes: 39 additions & 8 deletions crates/optimism/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use reth_optimism_payload_builder::builder::OpPayloadTransactions;
use reth_optimism_primitives::OpPrimitives;
use reth_optimism_rpc::{
witness::{DebugExecutionWitnessApiServer, OpDebugWitnessApi},
OpEthApi,
OpEthApi, SequencerClient,
};
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_primitives::BlockBody;
Expand Down Expand Up @@ -178,12 +178,11 @@ where
OpAddOns<NodeAdapter<N, <Self::ComponentsBuilder as NodeComponentsBuilder<N>>::Components>>;

fn components_builder(&self) -> Self::ComponentsBuilder {
let Self { args } = self;
Self::components(args.clone())
Self::components(self.args.clone())
}

fn add_ons(&self) -> Self::AddOns {
OpAddOns::new(self.args.sequencer_http.clone())
Self::AddOns::builder().with_sequencer(self.args.sequencer_http.clone()).build()
}
}

Expand All @@ -204,14 +203,14 @@ pub struct OpAddOns<N: FullNodeComponents>(pub RpcAddOns<N, OpEthApi<N>, OpEngin

impl<N: FullNodeComponents<Types: NodeTypes<Primitives = OpPrimitives>>> Default for OpAddOns<N> {
fn default() -> Self {
Self::new(None)
Self::builder().build()
}
}

impl<N: FullNodeComponents<Types: NodeTypes<Primitives = OpPrimitives>>> OpAddOns<N> {
/// Create a new instance with the given `sequencer_http` URL.
pub fn new(sequencer_http: Option<String>) -> Self {
Self(RpcAddOns::new(move |ctx| OpEthApi::new(ctx, sequencer_http), Default::default()))
/// Build a [`OpAddOns`] using [`OpAddOnsBuilder`].
pub fn builder() -> OpAddOnsBuilder {
OpAddOnsBuilder::default()
}
}

Expand Down Expand Up @@ -270,6 +269,38 @@ where
}
}

/// A regular optimism evm and executor builder.
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
pub struct OpAddOnsBuilder {
/// Sequencer client, configured to forward submitted transactions to sequencer of given OP
/// network.
sequencer_client: Option<SequencerClient>,
}

impl OpAddOnsBuilder {
/// With a [`SequencerClient`].
pub fn with_sequencer(mut self, sequencer_client: Option<String>) -> Self {
self.sequencer_client = sequencer_client.map(SequencerClient::new);
self
}
}

impl OpAddOnsBuilder {
/// Builds an instance of [`OpAddOns`].
pub fn build<N>(self) -> OpAddOns<N>
where
N: FullNodeComponents<Types: NodeTypes<Primitives = OpPrimitives>>,
{
let Self { sequencer_client, .. } = self;

OpAddOns(RpcAddOns::new(
move |ctx| OpEthApi::<N>::builder().with_sequencer(sequencer_client).build(ctx),
Default::default(),
))
}
}

/// A regular optimism evm and executor builder.
#[derive(Debug, Default, Clone, Copy)]
#[non_exhaustive]
Expand Down
9 changes: 5 additions & 4 deletions crates/optimism/node/tests/it/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
use reth_db::test_utils::create_test_rw_db;
use reth_node_api::FullNodeComponents;
use reth_node_builder::{NodeBuilder, NodeConfig};
use reth_node_builder::{Node, NodeBuilder, NodeConfig};
use reth_optimism_chainspec::BASE_MAINNET;
use reth_optimism_node::{node::OpAddOns, OpNode};
use reth_optimism_node::{args::RollupArgs, OpNode};

#[test]
fn test_basic_setup() {
// parse CLI -> config
let config = NodeConfig::new(BASE_MAINNET.clone());
let db = create_test_rw_db();
let args = RollupArgs::default();
let _builder = NodeBuilder::new(config)
.with_database(db)
.with_types::<OpNode>()
.with_components(OpNode::components(Default::default()))
.with_add_ons(OpAddOns::new(None))
.with_components(OpNode::components(args.clone()))
.with_add_ons(OpNode::new(args).add_ons())
.on_component_initialized(move |ctx| {
let _provider = ctx.provider();
Ok(())
Expand Down
1 change: 0 additions & 1 deletion crates/optimism/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ serde_json.workspace = true
# misc
thiserror.workspace = true
tracing.workspace = true
derive_more = { workspace = true, features = ["constructor", "deref"] }

[dev-dependencies]
reth-optimism-chainspec.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/rpc/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ where
};

Ok(OpReceiptBuilder::new(
&self.inner.provider().chain_spec(),
&self.inner.eth_api.provider().chain_spec(),
tx,
meta,
receipt,
Expand Down
4 changes: 2 additions & 2 deletions crates/optimism/rpc/src/eth/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ where
{
#[inline]
fn call_gas_limit(&self) -> u64 {
self.inner.gas_cap()
self.inner.eth_api.gas_cap()
}

#[inline]
fn max_simulate_blocks(&self) -> u64 {
self.inner.max_simulate_blocks()
self.inner.eth_api.max_simulate_blocks()
}

fn create_txn_env(
Expand Down
Loading

0 comments on commit 6789ff4

Please sign in to comment.