ethrex_rpc/lib.rs
1//! # ethrex RPC
2//!
3//! This crate implements the Ethereum JSON-RPC API for the ethrex node.
4//!
5//! ## Overview
6//!
7//! The RPC server provides three interfaces:
8//! - **HTTP API**: Public JSON-RPC endpoint for client requests (`eth_*`, `debug_*`, `net_*`, etc.)
9//! - **WebSocket API**: Optional WebSocket endpoint for subscriptions and real-time updates
10//! - **Auth RPC API**: Authenticated endpoint for consensus client communication (`engine_*` methods)
11//!
12//! ## Supported Namespaces
13//!
14//! - `eth`: Standard Ethereum methods (blocks, transactions, accounts, gas estimation)
15//! - `engine`: Consensus layer methods for block building and fork choice
16//! - `debug`: Debugging methods (raw blocks, execution witnesses, tracing)
17//! - `net`: Network information methods
18//! - `admin`: Node administration methods
19//! - `web3`: Web3 utility methods
20//! - `txpool`: Transaction pool inspection methods
21//!
22//! ## Usage
23//!
24//! ```ignore
25//! use ethrex_rpc::{start_api, RpcApiContext};
26//!
27//! // Start the RPC server
28//! start_api(
29//! http_addr,
30//! ws_addr,
31//! authrpc_addr,
32//! storage,
33//! blockchain,
34//! jwt_secret,
35//! // ... other parameters
36//! ).await?;
37//! ```
38//!
39//! ## Implementing Custom RPC Handlers
40//!
41//! Implement the [`RpcHandler`] trait to create custom RPC endpoints:
42//!
43//! ```ignore
44//! use ethrex_rpc::{RpcHandler, RpcApiContext, RpcErr};
45//!
46//! struct MyHandler { /* fields */ }
47//!
48//! impl RpcHandler for MyHandler {
49//! fn parse(params: &Option<Vec<Value>>) -> Result<Self, RpcErr> {
50//! // Parse JSON-RPC parameters
51//! }
52//!
53//! async fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
54//! // Handle the request
55//! }
56//! }
57//! ```
58
59// This is added because otherwise some tests would fail due to reaching the recursion limit
60#![recursion_limit = "400"]
61
62mod admin;
63mod authentication;
64pub mod debug;
65pub mod engine;
66mod eth;
67mod mempool;
68mod net;
69pub mod rpc;
70pub mod subscription_manager;
71mod tracing;
72
73pub mod clients;
74pub mod types;
75pub mod utils;
76pub use clients::{EngineClient, EthClient};
77
78pub use rpc::{start_api, start_block_executor};
79
80#[cfg(any(test, feature = "test-utils"))]
81pub mod test_utils;
82
83// TODO: These exports are needed by ethrex-l2-rpc, but we do not want to
84// export them in the public API of this crate.
85pub use eth::{
86 filter::{ActiveFilters, clean_outdated_filters},
87 gas_price::GasPrice,
88 gas_tip_estimator::GasTipEstimator,
89 transaction::EstimateGasRequest,
90};
91pub use rpc::{
92 ClientVersion, NodeData, RpcApiContext, RpcHandler, RpcRequestWrapper, WebSocketConfig,
93 handle_eth_subscribe, handle_eth_unsubscribe, handle_websocket, map_debug_requests,
94 map_eth_requests, map_http_requests, rpc_response, shutdown_signal,
95};
96pub use subscription_manager::{SubscriptionManager, SubscriptionManagerProtocol};
97pub use utils::{RpcErr, RpcErrorMetadata, RpcNamespace};
98
99/// Default namespaces enabled on the public HTTP/WS RPC endpoint.
100///
101/// Operators who need `admin`, `debug` or `txpool` must enable them explicitly
102/// via `--http.api`.
103pub const DEFAULT_HTTP_API: &[RpcNamespace] =
104 &[RpcNamespace::Eth, RpcNamespace::Net, RpcNamespace::Web3];