Skip to content

Commit

Permalink
Added extension support for auth rpc module (paradigmxyz#6060)
Browse files Browse the repository at this point in the history
Co-authored-by: root <root@Arindam>
  • Loading branch information
Arindam2407 and root authored Jan 15, 2024
1 parent 38559a9 commit 1cc3576
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
16 changes: 12 additions & 4 deletions bin/reth/src/args/rpc_server_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,19 @@ impl RpcServerArgs {
let module_config = self.transport_rpc_module_config();
debug!(target: "reth::cli", http=?module_config.http(), ws=?module_config.ws(), "Using RPC module config");

let (mut modules, auth_module, mut registry) = RpcModuleBuilder::default()
let (mut modules, mut auth_module, mut registry) = RpcModuleBuilder::default()
.with_provider(components.provider())
.with_pool(components.pool())
.with_network(components.network())
.with_events(components.events())
.with_executor(components.task_executor())
.build_with_auth_server(module_config, engine_api);

let rpc_components = RethRpcComponents { registry: &mut registry, modules: &mut modules };
let rpc_components = RethRpcComponents {
registry: &mut registry,
modules: &mut modules,
auth_module: &mut auth_module,
};
// apply configured customization
conf.extend_rpc_modules(self, components, rpc_components)?;

Expand All @@ -268,7 +272,7 @@ impl RpcServerArgs {
handle
});

let launch_auth = auth_module.start_server(auth_config).map_ok(|handle| {
let launch_auth = auth_module.clone().start_server(auth_config).map_ok(|handle| {
let addr = handle.local_addr();
info!(target: "reth::cli", url=%addr, "RPC auth server started");
handle
Expand All @@ -279,7 +283,11 @@ impl RpcServerArgs {
let handles = RethRpcServerHandles { rpc, auth };

// call hook
let rpc_components = RethRpcComponents { registry: &mut registry, modules: &mut modules };
let rpc_components = RethRpcComponents {
registry: &mut registry,
modules: &mut modules,
auth_module: &mut auth_module,
};
conf.on_rpc_server_started(self, components, rpc_components, handles.clone())?;

Ok(handles)
Expand Down
11 changes: 8 additions & 3 deletions bin/reth/src/cli/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use reth_provider::{
EvmEnvProvider, StateProviderFactory,
};
use reth_rpc_builder::{
auth::AuthServerHandle, RethModuleRegistry, RpcServerHandle, TransportRpcModules,
auth::{AuthRpcModule, AuthServerHandle},
RethModuleRegistry, RpcServerHandle, TransportRpcModules,
};
use reth_tasks::TaskSpawner;
use reth_transaction_pool::TransactionPool;
Expand Down Expand Up @@ -75,11 +76,11 @@ pub trait RethNodeComponents: Clone + Send + Sync + 'static {
}
}

/// Helper container to encapsulate [RethModuleRegistry] and [TransportRpcModules].
/// Helper container to encapsulate [RethModuleRegistry],[TransportRpcModules] and [AuthRpcModule].
///
/// This can be used to access installed modules, or create commonly used handlers like
/// [reth_rpc::EthApi], and ultimately merge additional rpc handler into the configured transport
/// modules [TransportRpcModules].
/// modules [TransportRpcModules] as well as configured authenticated methods [AuthRpcModule].
#[derive(Debug)]
#[allow(clippy::type_complexity)]
pub struct RethRpcComponents<'a, Reth: RethNodeComponents> {
Expand All @@ -98,6 +99,10 @@ pub struct RethRpcComponents<'a, Reth: RethNodeComponents> {
/// This can be used to merge additional modules into the configured transports (http, ipc,
/// ws). See [TransportRpcModules::merge_configured]
pub modules: &'a mut TransportRpcModules,
/// Holds jwt authenticated rpc module.
///
/// This can be used to merge additional modules into the configured authenticated methods
pub auth_module: &'a mut AuthRpcModule,
}

/// A Generic implementation of the RethNodeComponents trait.
Expand Down
15 changes: 13 additions & 2 deletions crates/rpc/rpc-builder/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use jsonrpsee::server::ServerBuilder;
use jsonrpsee::{
http_client::HeaderMap,
server::{RpcModule, ServerHandle},
Methods,
};
use reth_network_api::{NetworkInfo, Peers};
use reth_node_api::EngineTypes;
Expand Down Expand Up @@ -247,12 +248,12 @@ impl AuthServerConfigBuilder {
}

/// Holds installed modules for the auth server.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct AuthRpcModule {
pub(crate) inner: RpcModule<()>,
}

// === impl TransportRpcModules ===
// === impl AuthRpcModule ===

impl AuthRpcModule {
/// Create a new `AuthRpcModule` with the given `engine_api`.
Expand All @@ -271,6 +272,16 @@ impl AuthRpcModule {
&mut self.inner
}

/// Merge the given [Methods] in the configured authenticated methods.
///
/// Fails if any of the methods in other is present already.
pub fn merge_auth_methods(
&mut self,
other: impl Into<Methods>,
) -> Result<bool, jsonrpsee::core::error::Error> {
self.module_mut().merge(other.into()).map(|_| true)
}

/// Convenience function for starting a server
pub async fn start_server(
self,
Expand Down
2 changes: 1 addition & 1 deletion examples/additional-rpc-namespace-in-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! cargo run -p additional-rpc-namespace-in-cli -- node --http --ws --enable-ext
//! ```
//!
//! This installs an additional RPC method `txpoolExt_transactionCount` that can queried via [cast](https://github.com/foundry-rs/foundry)
//! This installs an additional RPC method `txpoolExt_transactionCount` that can be queried via [cast](https://github.com/foundry-rs/foundry)
//!
//! ```sh
//! cast rpc txpoolExt_transactionCount
Expand Down

0 comments on commit 1cc3576

Please sign in to comment.