Crate reth_rpc_builder

Source
Expand description

Configure reth RPC.

This crate contains several builder and config types that allow to configure the selection of RethRpcModule specific to transports (ws, http, ipc).

The RpcModuleBuilder is the main entrypoint for configuring all reth modules. It takes instances of components required to start the servers, such as provider impls, network and transaction pool. RpcModuleBuilder::build returns a TransportRpcModules which contains the transport specific config (what APIs are available via this transport).

The RpcServerConfig is used to assemble and start the http server, ws server, ipc servers, it requires the TransportRpcModules so it can start the servers with the configured modules.

§Examples

Configure only an http server with a selection of RethRpcModules

use alloy_consensus::Header;
use reth_evm::{execute::BlockExecutorProvider, ConfigureEvm};
use reth_network_api::{NetworkInfo, Peers};
use reth_provider::{AccountReader, CanonStateSubscriptions, ChangeSetReader, FullRpcProvider};
use reth_rpc::EthApi;
use reth_rpc_builder::{
    RethRpcModule, RpcModuleBuilder, RpcServerConfig, ServerBuilder, TransportRpcModuleConfig,
};
use reth_tasks::TokioTaskExecutor;
use reth_transaction_pool::TransactionPool;

pub async fn launch<Provider, Pool, Network, Events, EvmConfig, BlockExecutor, Consensus>(
    provider: Provider,
    pool: Pool,
    network: Network,
    events: Events,
    evm_config: EvmConfig,
    block_executor: BlockExecutor,
    consensus: Consensus,
) where
    Provider: FullRpcProvider + AccountReader + ChangeSetReader,
    Pool: TransactionPool + Unpin + 'static,
    Network: NetworkInfo + Peers + Clone + 'static,
    Events: CanonStateSubscriptions + Clone + 'static,
    EvmConfig: ConfigureEvm<Header = Header>,
    BlockExecutor: BlockExecutorProvider,
    Consensus: reth_consensus::Consensus + Clone + 'static,
{
    // configure the rpc module per transport
    let transports = TransportRpcModuleConfig::default().with_http(vec![
        RethRpcModule::Admin,
        RethRpcModule::Debug,
        RethRpcModule::Eth,
        RethRpcModule::Web3,
    ]);
    let transport_modules = RpcModuleBuilder::new(
        provider,
        pool,
        network,
        TokioTaskExecutor::default(),
        events,
        evm_config,
        block_executor,
        consensus,
    )
    .build(transports, Box::new(EthApi::with_spawner));
    let handle = RpcServerConfig::default()
        .with_http(ServerBuilder::default())
        .start(&transport_modules)
        .await;
}

Configure a http and ws server with a separate auth server that handles the engine_ API

use alloy_consensus::Header;
use reth_engine_primitives::EngineTypes;
use reth_evm::{execute::BlockExecutorProvider, ConfigureEvm};
use reth_network_api::{NetworkInfo, Peers};
use reth_provider::{AccountReader, CanonStateSubscriptions, ChangeSetReader, FullRpcProvider};
use reth_rpc::EthApi;
use reth_rpc_api::EngineApiServer;
use reth_rpc_builder::{
    auth::AuthServerConfig, RethRpcModule, RpcModuleBuilder, RpcServerConfig,
    TransportRpcModuleConfig,
};
use reth_rpc_layer::JwtSecret;
use reth_tasks::TokioTaskExecutor;
use reth_transaction_pool::TransactionPool;
use tokio::try_join;

pub async fn launch<
    Provider,
    Pool,
    Network,
    Events,
    EngineApi,
    EngineT,
    EvmConfig,
    BlockExecutor,
    Consensus,
>(
    provider: Provider,
    pool: Pool,
    network: Network,
    events: Events,
    engine_api: EngineApi,
    evm_config: EvmConfig,
    block_executor: BlockExecutor,
    consensus: Consensus,
) where
    Provider: FullRpcProvider + AccountReader + ChangeSetReader,
    Pool: TransactionPool + Unpin + 'static,
    Network: NetworkInfo + Peers + Clone + 'static,
    Events: CanonStateSubscriptions + Clone + 'static,
    EngineApi: EngineApiServer<EngineT>,
    EngineT: EngineTypes,
    EvmConfig: ConfigureEvm<Header = Header>,
    BlockExecutor: BlockExecutorProvider,
    Consensus: reth_consensus::Consensus + Clone + 'static,
{
    // configure the rpc module per transport
    let transports = TransportRpcModuleConfig::default().with_http(vec![
        RethRpcModule::Admin,
        RethRpcModule::Debug,
        RethRpcModule::Eth,
        RethRpcModule::Web3,
    ]);
    let builder = RpcModuleBuilder::new(
        provider,
        pool,
        network,
        TokioTaskExecutor::default(),
        events,
        evm_config,
        block_executor,
        consensus,
    );

    // configure the server modules
    let (modules, auth_module, _registry) =
        builder.build_with_auth_server(transports, engine_api, Box::new(EthApi::with_spawner));

    // start the servers
    let auth_config = AuthServerConfig::builder(JwtSecret::random()).build();
    let config = RpcServerConfig::default();

    let (_rpc_handle, _auth_handle) =
        try_join!(config.start(&modules), auth_module.start_server(auth_config),).unwrap();
}

Re-exports§

Modules§

Structs§

Enums§

Functions§

  • Convenience function for starting a server in one step.