From 60f66579884f94f841f009d2be7521a8ce698783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien?= <3535019+leruaa@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:06:47 +0200 Subject: [PATCH] feat: move core rpc server types to standalone crate (#8515) --- Cargo.lock | 10 +++++++ Cargo.toml | 1 + crates/node-core/Cargo.toml | 1 + crates/node-core/src/args/gas_price_oracle.rs | 2 +- crates/node-core/src/args/rpc_server.rs | 6 ++-- crates/rpc/rpc-builder/Cargo.toml | 1 + crates/rpc/rpc-builder/src/auth.rs | 6 ++-- crates/rpc/rpc-builder/src/eth.rs | 6 ++-- crates/rpc/rpc-builder/src/lib.rs | 25 ++++++++-------- crates/rpc/rpc-server-types/Cargo.toml | 16 ++++++++++ .../src/constants.rs | 29 +++++++++++++++---- crates/rpc/rpc-server-types/src/lib.rs | 12 ++++++++ crates/rpc/rpc/Cargo.toml | 1 + crates/rpc/rpc/src/eth/api/fee_history.rs | 3 +- crates/rpc/rpc/src/eth/gas_oracle.rs | 20 +------------ 15 files changed, 93 insertions(+), 46 deletions(-) create mode 100644 crates/rpc/rpc-server-types/Cargo.toml rename crates/rpc/{rpc-builder => rpc-server-types}/src/constants.rs (62%) create mode 100644 crates/rpc/rpc-server-types/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 7c36f044a..950934cfc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7210,6 +7210,7 @@ dependencies = [ "reth-rpc", "reth-rpc-api", "reth-rpc-builder", + "reth-rpc-server-types", "reth-rpc-types", "reth-rpc-types-compat", "reth-storage-errors", @@ -7544,6 +7545,7 @@ dependencies = [ "reth-revm", "reth-rpc-api", "reth-rpc-engine-api", + "reth-rpc-server-types", "reth-rpc-types", "reth-rpc-types-compat", "reth-tasks", @@ -7615,6 +7617,7 @@ dependencies = [ "reth-rpc-api", "reth-rpc-engine-api", "reth-rpc-layer", + "reth-rpc-server-types", "reth-rpc-types", "reth-rpc-types-compat", "reth-tasks", @@ -7679,6 +7682,13 @@ dependencies = [ "tracing", ] +[[package]] +name = "reth-rpc-server-types" +version = "0.2.0-beta.8" +dependencies = [ + "alloy-primitives", +] + [[package]] name = "reth-rpc-types" version = "0.2.0-beta.8" diff --git a/Cargo.toml b/Cargo.toml index 6e1b5073b..b325c6807 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -296,6 +296,7 @@ reth-rpc-api-testing-util = { path = "crates/rpc/rpc-testing-util" } reth-rpc-builder = { path = "crates/rpc/rpc-builder" } reth-rpc-engine-api = { path = "crates/rpc/rpc-engine-api" } reth-rpc-layer = { path = "crates/rpc/rpc-layer" } +reth-rpc-server-types = { path = "crates/rpc/rpc-server-types" } reth-rpc-types = { path = "crates/rpc/rpc-types" } reth-rpc-types-compat = { path = "crates/rpc/rpc-types-compat" } reth-stages = { path = "crates/stages/stages" } diff --git a/crates/node-core/Cargo.toml b/crates/node-core/Cargo.toml index a520f662f..a21e641f1 100644 --- a/crates/node-core/Cargo.toml +++ b/crates/node-core/Cargo.toml @@ -21,6 +21,7 @@ reth-network = { workspace = true, features = ["serde"] } reth-network-p2p.workspace = true reth-rpc-builder.workspace = true reth-rpc.workspace = true +reth-rpc-server-types.workspace = true reth-rpc-types.workspace = true reth-rpc-types-compat.workspace = true reth-rpc-api = { workspace = true, features = ["client"] } diff --git a/crates/node-core/src/args/gas_price_oracle.rs b/crates/node-core/src/args/gas_price_oracle.rs index edb3217c7..5148fdca3 100644 --- a/crates/node-core/src/args/gas_price_oracle.rs +++ b/crates/node-core/src/args/gas_price_oracle.rs @@ -1,7 +1,7 @@ use crate::primitives::U256; use clap::Args; use reth_rpc::eth::gas_oracle::GasPriceOracleConfig; -use reth_rpc_builder::constants::{ +use reth_rpc_server_types::constants::gas_oracle::{ DEFAULT_GAS_PRICE_BLOCKS, DEFAULT_GAS_PRICE_PERCENTILE, DEFAULT_IGNORE_GAS_PRICE, DEFAULT_MAX_GAS_PRICE, }; diff --git a/crates/node-core/src/args/rpc_server.rs b/crates/node-core/src/args/rpc_server.rs index 033c127e7..ec69e13bf 100644 --- a/crates/node-core/src/args/rpc_server.rs +++ b/crates/node-core/src/args/rpc_server.rs @@ -18,10 +18,10 @@ use reth_rpc::eth::{ cache::EthStateCacheConfig, gas_oracle::GasPriceOracleConfig, RPC_DEFAULT_GAS_CAP, }; use reth_rpc_builder::{ - auth::AuthServerConfig, constants, error::RpcError, EthConfig, Identity, IpcServerBuilder, - RethRpcModule, RpcModuleConfig, RpcModuleSelection, RpcServerConfig, ServerBuilder, - TransportRpcModuleConfig, + auth::AuthServerConfig, error::RpcError, EthConfig, Identity, IpcServerBuilder, RethRpcModule, + RpcModuleConfig, RpcModuleSelection, RpcServerConfig, ServerBuilder, TransportRpcModuleConfig, }; +use reth_rpc_server_types::constants; use std::{ ffi::OsStr, net::{IpAddr, Ipv4Addr, SocketAddr}, diff --git a/crates/rpc/rpc-builder/Cargo.toml b/crates/rpc/rpc-builder/Cargo.toml index 378df7f3b..50aef5224 100644 --- a/crates/rpc/rpc-builder/Cargo.toml +++ b/crates/rpc/rpc-builder/Cargo.toml @@ -19,6 +19,7 @@ reth-provider.workspace = true reth-rpc.workspace = true reth-rpc-api.workspace = true reth-rpc-layer.workspace = true +reth-rpc-server-types.workspace = true reth-tasks = { workspace = true, features = ["rayon"] } reth-transaction-pool.workspace = true reth-evm.workspace = true diff --git a/crates/rpc/rpc-builder/src/auth.rs b/crates/rpc/rpc-builder/src/auth.rs index 5c2ad2600..ddb916aec 100644 --- a/crates/rpc/rpc-builder/src/auth.rs +++ b/crates/rpc/rpc-builder/src/auth.rs @@ -1,6 +1,4 @@ use crate::{ - constants, - constants::{DEFAULT_MAX_BLOCKS_PER_FILTER, DEFAULT_MAX_LOGS_PER_RESPONSE}, error::{RpcError, ServerKind}, EthConfig, }; @@ -35,6 +33,10 @@ use reth_rpc_layer::{ secret_to_bearer_header, AuthClientLayer, AuthClientService, AuthLayer, JwtAuthValidator, JwtSecret, }; +use reth_rpc_server_types::{ + constants, + constants::{DEFAULT_MAX_BLOCKS_PER_FILTER, DEFAULT_MAX_LOGS_PER_RESPONSE}, +}; use reth_tasks::{pool::BlockingTaskPool, TaskSpawner}; use reth_transaction_pool::TransactionPool; use std::net::{IpAddr, Ipv4Addr, SocketAddr}; diff --git a/crates/rpc/rpc-builder/src/eth.rs b/crates/rpc/rpc-builder/src/eth.rs index afa0b9184..5d0d06424 100644 --- a/crates/rpc/rpc-builder/src/eth.rs +++ b/crates/rpc/rpc-builder/src/eth.rs @@ -1,6 +1,3 @@ -use crate::constants::{ - default_max_tracing_requests, DEFAULT_MAX_BLOCKS_PER_FILTER, DEFAULT_MAX_LOGS_PER_RESPONSE, -}; use reth_rpc::{ eth::{ cache::{EthStateCache, EthStateCacheConfig}, @@ -9,6 +6,9 @@ use reth_rpc::{ }, EthApi, EthFilter, EthPubSub, }; +use reth_rpc_server_types::constants::{ + default_max_tracing_requests, DEFAULT_MAX_BLOCKS_PER_FILTER, DEFAULT_MAX_LOGS_PER_RESPONSE, +}; use reth_tasks::pool::BlockingTaskPool; use serde::{Deserialize, Serialize}; diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index 3e83e9941..eae23b6b6 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -159,7 +159,6 @@ use crate::{ auth::AuthRpcModule, cors::CorsDomainError, error::WsHttpSamePortError, metrics::RpcRequestMetrics, RpcModuleSelection::Selection, }; -use constants::*; use error::{ConflictingModules, RpcError, ServerKind}; use hyper::{header::AUTHORIZATION, HeaderMap}; pub use jsonrpsee::server::ServerBuilder; @@ -192,6 +191,7 @@ use reth_rpc::{ }; use reth_rpc_api::servers::*; use reth_rpc_layer::{AuthLayer, Claims, JwtAuthValidator, JwtSecret}; +pub use reth_rpc_server_types::constants; use reth_tasks::{ pool::{BlockingTaskGuard, BlockingTaskPool}, TaskSpawner, TokioTaskExecutor, @@ -226,9 +226,6 @@ pub mod error; /// Eth utils mod eth; -/// Common RPC constants. -pub mod constants; - // Rpc server metrics mod metrics; @@ -1576,7 +1573,8 @@ impl RpcServerConfig { /// Configures the [`SocketAddr`] of the http server /// - /// Default is [`Ipv4Addr::LOCALHOST`] and [`DEFAULT_HTTP_RPC_PORT`] + /// Default is [`Ipv4Addr::LOCALHOST`] and + /// [`reth_rpc_server_types::constants::DEFAULT_HTTP_RPC_PORT`] pub const fn with_http_address(mut self, addr: SocketAddr) -> Self { self.http_addr = Some(addr); self @@ -1584,7 +1582,8 @@ impl RpcServerConfig { /// Configures the [`SocketAddr`] of the ws server /// - /// Default is [`Ipv4Addr::LOCALHOST`] and [`DEFAULT_WS_RPC_PORT`] + /// Default is [`Ipv4Addr::LOCALHOST`] and + /// [`reth_rpc_server_types::constants::DEFAULT_WS_RPC_PORT`] pub const fn with_ws_address(mut self, addr: SocketAddr) -> Self { self.ws_addr = Some(addr); self @@ -1621,7 +1620,7 @@ impl RpcServerConfig { /// Configures the endpoint of the ipc server /// - /// Default is [`DEFAULT_IPC_ENDPOINT`] + /// Default is [`reth_rpc_server_types::constants::DEFAULT_IPC_ENDPOINT`] pub fn with_ipc_endpoint(mut self, path: impl Into) -> Self { self.ipc_endpoint = Some(path.into()); self @@ -1681,12 +1680,13 @@ impl RpcServerConfig { ) -> Result { let http_socket_addr = self.http_addr.unwrap_or(SocketAddr::V4(SocketAddrV4::new( Ipv4Addr::LOCALHOST, - DEFAULT_HTTP_RPC_PORT, + constants::DEFAULT_HTTP_RPC_PORT, ))); - let ws_socket_addr = self - .ws_addr - .unwrap_or(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, DEFAULT_WS_RPC_PORT))); + let ws_socket_addr = self.ws_addr.unwrap_or(SocketAddr::V4(SocketAddrV4::new( + Ipv4Addr::LOCALHOST, + constants::DEFAULT_WS_RPC_PORT, + ))); // If both are configured on the same port, we combine them into one server. if self.http_addr == self.ws_addr && @@ -1815,7 +1815,8 @@ impl RpcServerConfig { if let Some(builder) = self.ipc_server_config { let metrics = modules.ipc.as_ref().map(RpcRequestMetrics::ipc).unwrap_or_default(); - let ipc_path = self.ipc_endpoint.unwrap_or_else(|| DEFAULT_IPC_ENDPOINT.into()); + let ipc_path = + self.ipc_endpoint.unwrap_or_else(|| constants::DEFAULT_IPC_ENDPOINT.into()); let ipc = builder .set_rpc_middleware(IpcRpcServiceBuilder::new().layer(metrics)) .build(ipc_path); diff --git a/crates/rpc/rpc-server-types/Cargo.toml b/crates/rpc/rpc-server-types/Cargo.toml new file mode 100644 index 000000000..93efbb133 --- /dev/null +++ b/crates/rpc/rpc-server-types/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "reth-rpc-server-types" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true +description = "RPC server types and constants" + +[lints] +workspace = true + +[dependencies] +# ethereum +alloy-primitives.workspace = true diff --git a/crates/rpc/rpc-builder/src/constants.rs b/crates/rpc/rpc-server-types/src/constants.rs similarity index 62% rename from crates/rpc/rpc-builder/src/constants.rs rename to crates/rpc/rpc-server-types/src/constants.rs index 6b2b54cdb..2df03fe08 100644 --- a/crates/rpc/rpc-builder/src/constants.rs +++ b/crates/rpc/rpc-server-types/src/constants.rs @@ -1,8 +1,3 @@ -/// GPO reexports -pub use reth_rpc::eth::gas_oracle::{ - DEFAULT_GAS_PRICE_BLOCKS, DEFAULT_GAS_PRICE_PERCENTILE, DEFAULT_IGNORE_GAS_PRICE, - DEFAULT_MAX_GAS_PRICE, -}; use std::cmp::max; /// The default port for the http server @@ -46,3 +41,27 @@ pub const DEFAULT_ENGINE_API_IPC_ENDPOINT: &str = r"\\.\pipe\reth_engine_api.ipc /// The `engine_api` IPC endpoint #[cfg(not(windows))] pub const DEFAULT_ENGINE_API_IPC_ENDPOINT: &str = "/tmp/reth_engine_api.ipc"; + +/// GPO specific constants +pub mod gas_oracle { + use alloy_primitives::U256; + + /// The number of transactions sampled in a block + pub const SAMPLE_NUMBER: usize = 3_usize; + + /// The default maximum number of blocks to use for the gas price oracle. + pub const MAX_HEADER_HISTORY: u64 = 1024; + + /// Number of recent blocks to check for gas price + pub const DEFAULT_GAS_PRICE_BLOCKS: u32 = 20; + + /// The percentile of gas prices to use for the estimate + pub const DEFAULT_GAS_PRICE_PERCENTILE: u32 = 60; + + /// Maximum transaction priority fee (or gas price before London Fork) to be recommended by the + /// gas price oracle + pub const DEFAULT_MAX_GAS_PRICE: U256 = U256::from_limbs([500_000_000_000u64, 0, 0, 0]); + + /// The default minimum gas price, under which the sample will be ignored + pub const DEFAULT_IGNORE_GAS_PRICE: U256 = U256::from_limbs([2u64, 0, 0, 0]); +} diff --git a/crates/rpc/rpc-server-types/src/lib.rs b/crates/rpc/rpc-server-types/src/lib.rs new file mode 100644 index 000000000..93e6a8170 --- /dev/null +++ b/crates/rpc/rpc-server-types/src/lib.rs @@ -0,0 +1,12 @@ +//! Reth RPC server types. + +#![doc( + html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png", + html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256", + issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/" +)] +#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +#![cfg_attr(not(test), warn(unused_crate_dependencies))] + +/// Common RPC constants. +pub mod constants; diff --git a/crates/rpc/rpc/Cargo.toml b/crates/rpc/rpc/Cargo.toml index 53225f2b5..2214a23d2 100644 --- a/crates/rpc/rpc/Cargo.toml +++ b/crates/rpc/rpc/Cargo.toml @@ -15,6 +15,7 @@ workspace = true # reth reth-primitives.workspace = true reth-rpc-api.workspace = true +reth-rpc-server-types.workspace = true reth-rpc-types.workspace = true reth-errors.workspace = true reth-provider = { workspace = true, features = ["test-utils"] } diff --git a/crates/rpc/rpc/src/eth/api/fee_history.rs b/crates/rpc/rpc/src/eth/api/fee_history.rs index 5c1085d51..da43be551 100644 --- a/crates/rpc/rpc/src/eth/api/fee_history.rs +++ b/crates/rpc/rpc/src/eth/api/fee_history.rs @@ -1,6 +1,6 @@ //! Consist of types adjacent to the fee history cache and its configs -use crate::eth::{cache::EthStateCache, error::EthApiError, gas_oracle::MAX_HEADER_HISTORY}; +use crate::eth::{cache::EthStateCache, error::EthApiError}; use futures::{ future::{Fuse, FusedFuture}, FutureExt, Stream, StreamExt, @@ -12,6 +12,7 @@ use reth_primitives::{ ChainSpec, Receipt, SealedBlock, TransactionSigned, B256, }; use reth_provider::{BlockReaderIdExt, CanonStateNotification, ChainSpecProvider}; +use reth_rpc_server_types::constants::gas_oracle::MAX_HEADER_HISTORY; use reth_rpc_types::TxGasAndReward; use serde::{Deserialize, Serialize}; use std::{ diff --git a/crates/rpc/rpc/src/eth/gas_oracle.rs b/crates/rpc/rpc/src/eth/gas_oracle.rs index 4b0a76a3a..bb44af67b 100644 --- a/crates/rpc/rpc/src/eth/gas_oracle.rs +++ b/crates/rpc/rpc/src/eth/gas_oracle.rs @@ -8,31 +8,13 @@ use crate::eth::{ use derive_more::{Deref, DerefMut}; use reth_primitives::{constants::GWEI_TO_WEI, BlockNumberOrTag, B256, U256}; use reth_provider::BlockReaderIdExt; +use reth_rpc_server_types::constants::gas_oracle::*; use schnellru::{ByLength, LruMap}; use serde::{Deserialize, Serialize}; use std::fmt::{self, Debug, Formatter}; use tokio::sync::Mutex; use tracing::warn; -/// The number of transactions sampled in a block -pub const SAMPLE_NUMBER: usize = 3_usize; - -/// The default maximum number of blocks to use for the gas price oracle. -pub const MAX_HEADER_HISTORY: u64 = 1024; - -/// Number of recent blocks to check for gas price -pub const DEFAULT_GAS_PRICE_BLOCKS: u32 = 20; - -/// The percentile of gas prices to use for the estimate -pub const DEFAULT_GAS_PRICE_PERCENTILE: u32 = 60; - -/// Maximum transaction priority fee (or gas price before London Fork) to be recommended by the gas -/// price oracle -pub const DEFAULT_MAX_GAS_PRICE: U256 = U256::from_limbs([500_000_000_000u64, 0, 0, 0]); - -/// The default minimum gas price, under which the sample will be ignored -pub const DEFAULT_IGNORE_GAS_PRICE: U256 = U256::from_limbs([2u64, 0, 0, 0]); - /// Settings for the [`GasPriceOracle`] #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")]