Skip to content

Commit

Permalink
refactor: use type erasure on types with lots of generics
Browse files Browse the repository at this point in the history
  • Loading branch information
dancoombs committed Oct 18, 2024
1 parent 65d9b50 commit 359c5b0
Show file tree
Hide file tree
Showing 14 changed files with 536 additions and 385 deletions.
14 changes: 2 additions & 12 deletions bin/rundler/src/cli/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use rundler_types::{chain::ChainSpec, EntryPointVersion};
use rundler_utils::emit::{self, WithEntryPoint, EVENT_CHANNEL_CAPACITY};
use tokio::sync::broadcast;

use super::{json::get_json_config, CommonArgs, RundlerProviders};
use super::{json::get_json_config, CommonArgs};

const REQUEST_CHANNEL_CAPACITY: usize = 1024;

Expand Down Expand Up @@ -459,22 +459,12 @@ pub async fn spawn_tasks<T: TaskSpawnerExt + 'static>(
)
.await?;

let RundlerProviders {
provider,
ep_v0_6,
ep_v0_7,
da_gas_oracle_sync,
} = super::construct_providers(&common_args, &chain_spec)?;

BuilderTask::new(
task_args,
event_sender,
LocalBuilderBuilder::new(REQUEST_CHANNEL_CAPACITY),
pool,
provider,
ep_v0_6,
ep_v0_7,
da_gas_oracle_sync,
super::construct_providers(&common_args, &chain_spec)?,
)
.spawn(task_spawner)
.await?;
Expand Down
44 changes: 34 additions & 10 deletions bin/rundler/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use reth_tasks::TaskManager;
use rpc::RpcCliArgs;
use rundler_provider::{
AlloyEntryPointV0_6, AlloyEntryPointV0_7, AlloyEvmProvider, DAGasOracleSync,
EntryPointProvider, EvmProvider,
EntryPointProvider, EvmProvider, Providers,
};
use rundler_rpc::{EthApiSettings, RundlerApiSettings};
use rundler_sim::{
Expand Down Expand Up @@ -541,23 +541,47 @@ pub struct Cli {
logs: LogsArgs,
}

pub struct RundlerProviders<P, EP06, EP07> {
#[derive(Clone)]
pub struct RundlerProviders<P, EP06, EP07, D> {
provider: P,
ep_v0_6: Option<EP06>,
ep_v0_7: Option<EP07>,
da_gas_oracle_sync: Option<Arc<dyn DAGasOracleSync>>,
da_gas_oracle_sync: Option<D>,
}

impl<P, EP06, EP07, D> Providers for RundlerProviders<P, EP06, EP07, D>
where
P: EvmProvider + Clone,
EP06: EntryPointProvider<UserOperationV0_6> + Clone,
EP07: EntryPointProvider<UserOperationV0_7> + Clone,
D: DAGasOracleSync + Clone,
{
type Evm = P;
type EntryPointV0_6 = EP06;
type EntryPointV0_7 = EP07;
type DAGasOracleSync = D;

fn evm(&self) -> &Self::Evm {
&self.provider
}

fn ep_v0_6(&self) -> &Option<Self::EntryPointV0_6> {
&self.ep_v0_6
}

fn ep_v0_7(&self) -> &Option<Self::EntryPointV0_7> {
&self.ep_v0_7
}

fn da_gas_oracle_sync(&self) -> &Option<Self::DAGasOracleSync> {
&self.da_gas_oracle_sync
}
}

pub fn construct_providers(
args: &CommonArgs,
chain_spec: &ChainSpec,
) -> anyhow::Result<
RundlerProviders<
impl EvmProvider + Clone + 'static,
impl EntryPointProvider<UserOperationV0_6> + Clone + 'static,
impl EntryPointProvider<UserOperationV0_7> + Clone + 'static,
>,
> {
) -> anyhow::Result<impl Providers> {
let provider = Arc::new(rundler_provider::new_alloy_provider(
args.node_http.as_ref().context("must provide node_http")?,
)?);
Expand Down
31 changes: 6 additions & 25 deletions bin/rundler/src/cli/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use rundler_utils::emit::{self, WithEntryPoint, EVENT_CHANNEL_CAPACITY};
use tokio::sync::broadcast;

use self::events::Event;
use super::RundlerProviders;
use crate::cli::{
builder::{self, BuilderArgs},
pool::PoolArgs,
Expand Down Expand Up @@ -110,21 +109,13 @@ pub async fn spawn_tasks<T: TaskSpawnerExt + 'static>(
let builder_builder = LocalBuilderBuilder::new(REQUEST_CHANNEL_CAPACITY);
let builder_handle = builder_builder.get_handle();

let RundlerProviders {
provider,
ep_v0_6,
ep_v0_7,
da_gas_oracle_sync,
} = super::construct_providers(&common_args, &chain_spec)?;
let providers = super::construct_providers(&common_args, &chain_spec)?;

PoolTask::new(
pool_task_args,
op_pool_event_sender,
pool_builder,
provider.clone(),
ep_v0_6.clone(),
ep_v0_7.clone(),
da_gas_oracle_sync.clone(),
providers.clone(),
)
.spawn(task_spawner.clone())
.await?;
Expand All @@ -134,24 +125,14 @@ pub async fn spawn_tasks<T: TaskSpawnerExt + 'static>(
builder_event_sender,
builder_builder,
pool_handle.clone(),
provider.clone(),
ep_v0_6.clone(),
ep_v0_7.clone(),
da_gas_oracle_sync,
providers.clone(),
)
.spawn(task_spawner.clone())
.await?;

RpcTask::new(
rpc_task_args,
pool_handle,
builder_handle,
provider,
ep_v0_6,
ep_v0_7,
)
.spawn(task_spawner)
.await?;
RpcTask::new(rpc_task_args, pool_handle, builder_handle, providers)
.spawn(task_spawner)
.await?;

Ok(())
}
14 changes: 2 additions & 12 deletions bin/rundler/src/cli/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use rundler_types::{chain::ChainSpec, EntryPointVersion};
use rundler_utils::emit::{self, EVENT_CHANNEL_CAPACITY};
use tokio::sync::broadcast;

use super::{CommonArgs, RundlerProviders};
use super::CommonArgs;
use crate::cli::json::get_json_config;

const REQUEST_CHANNEL_CAPACITY: usize = 1024;
Expand Down Expand Up @@ -301,21 +301,11 @@ pub async fn spawn_tasks<T: TaskSpawnerExt + 'static>(
Box::pin(emit::receive_and_log_events_with_filter(event_rx, |_| true)),
);

let RundlerProviders {
provider,
ep_v0_6,
ep_v0_7,
da_gas_oracle_sync,
} = super::construct_providers(&common_args, &chain_spec)?;

PoolTask::new(
task_args,
event_sender,
LocalPoolBuilder::new(REQUEST_CHANNEL_CAPACITY, BLOCK_CHANNEL_CAPACITY),
provider,
ep_v0_6,
ep_v0_7,
da_gas_oracle_sync,
super::construct_providers(&common_args, &chain_spec)?,
)
.spawn(task_spawner)
.await?;
Expand Down
21 changes: 9 additions & 12 deletions bin/rundler/src/cli/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rundler_sim::{EstimationSettings, PrecheckSettings};
use rundler_task::{server::connect_with_retries_shutdown, TaskSpawnerExt};
use rundler_types::chain::ChainSpec;

use super::{CommonArgs, RundlerProviders};
use super::CommonArgs;

/// CLI options for the RPC server
#[derive(Args, Debug)]
Expand Down Expand Up @@ -79,7 +79,6 @@ pub struct RpcArgs {
impl RpcArgs {
/// Convert the CLI arguments into the arguments for the RPC server combining
/// common and rpc specific arguments.
#[allow(clippy::too_many_arguments)]
pub fn to_args(
&self,
chain_spec: ChainSpec,
Expand Down Expand Up @@ -176,16 +175,14 @@ pub async fn spawn_tasks<T: TaskSpawnerExt + 'static>(
)
.await?;

let RundlerProviders {
provider,
ep_v0_6,
ep_v0_7,
..
} = super::construct_providers(&common_args, &chain_spec)?;

RpcTask::new(task_args, pool, builder, provider, ep_v0_6, ep_v0_7)
.spawn(task_spawner)
.await?;
RpcTask::new(
task_args,
pool,
builder,
super::construct_providers(&common_args, &chain_spec)?,
)
.spawn(task_spawner)
.await?;

Ok(())
}
Loading

0 comments on commit 359c5b0

Please sign in to comment.