Skip to content

Commit

Permalink
feat: move core rpc server types to standalone crate (paradigmxyz#8515)
Browse files Browse the repository at this point in the history
  • Loading branch information
leruaa authored Jun 4, 2024
1 parent adb3605 commit 60f6657
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 46 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions crates/node-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/node-core/src/args/gas_price_oracle.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down
6 changes: 3 additions & 3 deletions crates/node-core/src/args/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
1 change: 1 addition & 0 deletions crates/rpc/rpc-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions crates/rpc/rpc-builder/src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{
constants,
constants::{DEFAULT_MAX_BLOCKS_PER_FILTER, DEFAULT_MAX_LOGS_PER_RESPONSE},
error::{RpcError, ServerKind},
EthConfig,
};
Expand Down Expand Up @@ -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};
Expand Down
6 changes: 3 additions & 3 deletions crates/rpc/rpc-builder/src/eth.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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};

Expand Down
25 changes: 13 additions & 12 deletions crates/rpc/rpc-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -226,9 +226,6 @@ pub mod error;
/// Eth utils
mod eth;

/// Common RPC constants.
pub mod constants;

// Rpc server metrics
mod metrics;

Expand Down Expand Up @@ -1576,15 +1573,17 @@ 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
}

/// 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
Expand Down Expand Up @@ -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<String>) -> Self {
self.ipc_endpoint = Some(path.into());
self
Expand Down Expand Up @@ -1681,12 +1680,13 @@ impl RpcServerConfig {
) -> Result<WsHttpServer, RpcError> {
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 &&
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions crates/rpc/rpc-server-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]);
}
12 changes: 12 additions & 0 deletions crates/rpc/rpc-server-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions crates/rpc/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
3 changes: 2 additions & 1 deletion crates/rpc/rpc/src/eth/api/fee_history.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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::{
Expand Down
20 changes: 1 addition & 19 deletions crates/rpc/rpc/src/eth/gas_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 60f6657

Please sign in to comment.