mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
186 lines
6.0 KiB
Rust
186 lines
6.0 KiB
Rust
use std::marker::PhantomData;
|
|
|
|
use reth_evm::ConfigureEvm;
|
|
use reth_primitives::Header;
|
|
use reth_provider::{BlockReader, CanonStateSubscriptions, EvmEnvProvider, StateProviderFactory};
|
|
use reth_rpc::{EthFilter, EthPubSub};
|
|
use reth_rpc_eth_api::EthApiTypes;
|
|
use reth_rpc_eth_types::{
|
|
cache::cache_new_blocks_task, EthApiBuilderCtx, EthConfig, EthStateCache,
|
|
};
|
|
use reth_tasks::TaskSpawner;
|
|
|
|
/// Alias for `eth` namespace API builder.
|
|
pub type DynEthApiBuilder<Provider, Pool, EvmConfig, Network, Tasks, Events, EthApi> = Box<
|
|
dyn Fn(&EthApiBuilderCtx<Provider, Pool, EvmConfig, Network, Tasks, Events, EthApi>) -> EthApi,
|
|
>;
|
|
|
|
/// Handlers for core, filter and pubsub `eth` namespace APIs.
|
|
#[derive(Debug, Clone)]
|
|
pub struct EthHandlers<Provider, Pool, Network, Events, EthApi: EthApiTypes> {
|
|
/// Main `eth_` request handler
|
|
pub api: EthApi,
|
|
/// The async caching layer used by the eth handlers
|
|
pub cache: EthStateCache,
|
|
/// Polling based filter handler available on all transports
|
|
pub filter: EthFilter<Provider, Pool, EthApi>,
|
|
/// Handler for subscriptions only available for transports that support it (ws, ipc)
|
|
pub pubsub: EthPubSub<Provider, Pool, Events, Network, EthApi>,
|
|
}
|
|
|
|
impl<Provider, Pool, Network, Events, EthApi> EthHandlers<Provider, Pool, Network, Events, EthApi>
|
|
where
|
|
EthApi: EthApiTypes,
|
|
{
|
|
/// Returns a new [`EthHandlers`] builder.
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn builder<EvmConfig, Tasks>(
|
|
provider: Provider,
|
|
pool: Pool,
|
|
network: Network,
|
|
evm_config: EvmConfig,
|
|
config: EthConfig,
|
|
executor: Tasks,
|
|
events: Events,
|
|
eth_api_builder: DynEthApiBuilder<
|
|
Provider,
|
|
Pool,
|
|
EvmConfig,
|
|
Network,
|
|
Tasks,
|
|
Events,
|
|
EthApi,
|
|
>,
|
|
) -> EthHandlersBuilder<Provider, Pool, Network, Tasks, Events, EvmConfig, EthApi> {
|
|
EthHandlersBuilder {
|
|
provider,
|
|
pool,
|
|
network,
|
|
evm_config,
|
|
config,
|
|
executor,
|
|
events,
|
|
eth_api_builder,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Builds [`EthHandlers`] for core, filter, and pubsub `eth_` apis.
|
|
#[allow(missing_debug_implementations)]
|
|
pub struct EthHandlersBuilder<Provider, Pool, Network, Tasks, Events, EvmConfig, EthApi> {
|
|
provider: Provider,
|
|
pool: Pool,
|
|
network: Network,
|
|
evm_config: EvmConfig,
|
|
config: EthConfig,
|
|
executor: Tasks,
|
|
events: Events,
|
|
eth_api_builder: DynEthApiBuilder<Provider, Pool, EvmConfig, Network, Tasks, Events, EthApi>,
|
|
}
|
|
|
|
impl<Provider, Pool, Network, Tasks, Events, EvmConfig, EthApi>
|
|
EthHandlersBuilder<Provider, Pool, Network, Tasks, Events, EvmConfig, EthApi>
|
|
where
|
|
Provider: StateProviderFactory + BlockReader + EvmEnvProvider + Clone + Unpin + 'static,
|
|
Pool: Send + Sync + Clone + 'static,
|
|
EvmConfig: ConfigureEvm<Header = Header>,
|
|
Network: Clone + 'static,
|
|
Tasks: TaskSpawner + Clone + 'static,
|
|
Events: CanonStateSubscriptions + Clone + 'static,
|
|
EthApi: EthApiTypes + 'static,
|
|
{
|
|
/// Returns a new instance with handlers for `eth` namespace.
|
|
pub fn build(self) -> EthHandlers<Provider, Pool, Network, Events, EthApi> {
|
|
let Self { provider, pool, network, evm_config, config, executor, events, eth_api_builder } =
|
|
self;
|
|
|
|
let cache = EthStateCache::spawn_with(
|
|
provider.clone(),
|
|
config.cache,
|
|
executor.clone(),
|
|
evm_config.clone(),
|
|
);
|
|
|
|
let new_canonical_blocks = events.canonical_state_stream();
|
|
let c = cache.clone();
|
|
executor.spawn_critical(
|
|
"cache canonical blocks task",
|
|
Box::pin(async move {
|
|
cache_new_blocks_task(c, new_canonical_blocks).await;
|
|
}),
|
|
);
|
|
|
|
let ctx = EthApiBuilderCtx {
|
|
provider,
|
|
pool,
|
|
network,
|
|
evm_config,
|
|
config,
|
|
executor,
|
|
events,
|
|
cache,
|
|
_rpc_ty_builders: PhantomData,
|
|
};
|
|
|
|
let api = eth_api_builder(&ctx);
|
|
|
|
let filter = EthFilterApiBuilder::build(&ctx);
|
|
|
|
let pubsub = EthPubSubApiBuilder::build(&ctx);
|
|
|
|
EthHandlers { api, cache: ctx.cache, filter, pubsub }
|
|
}
|
|
}
|
|
|
|
/// Builds the `eth_` namespace API [`EthFilterApiServer`](reth_rpc_eth_api::EthFilterApiServer).
|
|
#[derive(Debug)]
|
|
pub struct EthFilterApiBuilder;
|
|
|
|
impl EthFilterApiBuilder {
|
|
/// Builds the [`EthFilterApiServer`](reth_rpc_eth_api::EthFilterApiServer), for given context.
|
|
pub fn build<Provider, Pool, EvmConfig, Network, Tasks, Events, Eth>(
|
|
ctx: &EthApiBuilderCtx<Provider, Pool, EvmConfig, Network, Tasks, Events, Eth>,
|
|
) -> EthFilter<Provider, Pool, Eth>
|
|
where
|
|
Provider: Send + Sync + Clone + 'static,
|
|
Pool: Send + Sync + Clone + 'static,
|
|
Tasks: TaskSpawner + Clone + 'static,
|
|
Eth: EthApiTypes + 'static,
|
|
{
|
|
EthFilter::new(
|
|
ctx.provider.clone(),
|
|
ctx.pool.clone(),
|
|
ctx.cache.clone(),
|
|
ctx.config.filter_config(),
|
|
Box::new(ctx.executor.clone()),
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Builds the `eth_` namespace API [`EthPubSubApiServer`](reth_rpc_eth_api::EthFilterApiServer).
|
|
#[derive(Debug)]
|
|
pub struct EthPubSubApiBuilder;
|
|
|
|
impl EthPubSubApiBuilder {
|
|
/// Builds the [`EthPubSubApiServer`](reth_rpc_eth_api::EthPubSubApiServer), for given context.
|
|
pub fn build<Provider, Pool, EvmConfig, Network, Tasks, Events, Eth>(
|
|
ctx: &EthApiBuilderCtx<Provider, Pool, EvmConfig, Network, Tasks, Events, Eth>,
|
|
) -> EthPubSub<Provider, Pool, Events, Network, Eth>
|
|
where
|
|
Provider: Clone,
|
|
Pool: Clone,
|
|
Events: Clone,
|
|
Network: Clone,
|
|
Tasks: TaskSpawner + Clone + 'static,
|
|
Eth: EthApiTypes + 'static,
|
|
{
|
|
EthPubSub::with_spawner(
|
|
ctx.provider.clone(),
|
|
ctx.pool.clone(),
|
|
ctx.events.clone(),
|
|
ctx.network.clone(),
|
|
Box::new(ctx.executor.clone()),
|
|
)
|
|
}
|
|
}
|