From 7476120d8170d7269ec8525f173edac995e12385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Altu=C4=9F=20Bakan?= Date: Wed, 19 Jul 2023 21:26:16 +0200 Subject: [PATCH] feat: wrap gas limit into a new type (#3841) Co-authored-by: Matthias Seitz --- bin/reth/src/args/rpc_server_args.rs | 6 ++--- crates/rpc/rpc-builder/src/constants.rs | 7 ------ crates/rpc/rpc-builder/src/eth.rs | 4 +-- crates/rpc/rpc/src/eth/api/mod.rs | 33 +++++++++++++++++++++++-- crates/rpc/rpc/src/eth/mod.rs | 2 +- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/bin/reth/src/args/rpc_server_args.rs b/bin/reth/src/args/rpc_server_args.rs index 4dbde75cbd..452f6ba33e 100644 --- a/bin/reth/src/args/rpc_server_args.rs +++ b/bin/reth/src/args/rpc_server_args.rs @@ -17,13 +17,13 @@ use reth_rpc::{ DEFAULT_BLOCK_CACHE_MAX_LEN, DEFAULT_ENV_CACHE_MAX_LEN, DEFAULT_RECEIPT_CACHE_MAX_LEN, }, gas_oracle::GasPriceOracleConfig, + RPC_DEFAULT_GAS_CAP, }, JwtError, JwtSecret, }; use reth_rpc_builder::{ auth::{AuthServerConfig, AuthServerHandle}, constants, - constants::RPC_DEFAULT_GAS_CAP, error::RpcError, EthConfig, IpcServerBuilder, RethRpcModule, RpcModuleBuilder, RpcModuleConfig, RpcModuleSelection, RpcServerConfig, RpcServerHandle, ServerBuilder, TransportRpcModuleConfig, @@ -139,7 +139,7 @@ pub struct RpcServerArgs { alias = "rpc.gascap", value_name = "GAS_CAP", value_parser = RangedU64ValueParser::::new().range(1..), - default_value_t = RPC_DEFAULT_GAS_CAP + default_value_t = RPC_DEFAULT_GAS_CAP.into() )] pub rpc_gas_cap: u64, @@ -513,7 +513,7 @@ mod tests { fn test_rpc_gas_cap() { let args = CommandParser::::parse_from(["reth"]).args; let config = args.eth_config(); - assert_eq!(config.rpc_gas_cap, RPC_DEFAULT_GAS_CAP); + assert_eq!(config.rpc_gas_cap, Into::::into(RPC_DEFAULT_GAS_CAP)); let args = CommandParser::::parse_from(["reth", "--rpc.gascap", "1000"]).args; diff --git a/crates/rpc/rpc-builder/src/constants.rs b/crates/rpc/rpc-builder/src/constants.rs index 6768ca62a6..a1b2bc36a8 100644 --- a/crates/rpc/rpc-builder/src/constants.rs +++ b/crates/rpc/rpc-builder/src/constants.rs @@ -7,13 +7,6 @@ pub const DEFAULT_WS_RPC_PORT: u16 = 8546; /// The default port for the auth server. pub const DEFAULT_AUTH_PORT: u16 = 8551; -/// The default gas limit for eth_call and adjacent calls. -/// -/// This is different from the default to regular 30M block gas limit -/// [ETHEREUM_BLOCK_GAS_LIMIT](reth_primitives::constants::ETHEREUM_BLOCK_GAS_LIMIT) to allow for -/// more complex calls. -pub const RPC_DEFAULT_GAS_CAP: u64 = 50_000_000; - /// The default IPC endpoint #[cfg(windows)] pub const DEFAULT_IPC_ENDPOINT: &str = r"\\.\pipe\reth.ipc"; diff --git a/crates/rpc/rpc-builder/src/eth.rs b/crates/rpc/rpc-builder/src/eth.rs index 5b2d6780a5..34ec4989c6 100644 --- a/crates/rpc/rpc-builder/src/eth.rs +++ b/crates/rpc/rpc-builder/src/eth.rs @@ -1,8 +1,8 @@ -use crate::constants::RPC_DEFAULT_GAS_CAP; use reth_rpc::{ eth::{ cache::{EthStateCache, EthStateCacheConfig}, gas_oracle::GasPriceOracleConfig, + RPC_DEFAULT_GAS_CAP, }, EthApi, EthFilter, EthPubSub, }; @@ -51,7 +51,7 @@ impl Default for EthConfig { gas_oracle: GasPriceOracleConfig::default(), max_tracing_requests: DEFAULT_MAX_TRACING_REQUESTS, max_logs_per_response: DEFAULT_MAX_LOGS_PER_RESPONSE, - rpc_gas_cap: RPC_DEFAULT_GAS_CAP, + rpc_gas_cap: RPC_DEFAULT_GAS_CAP.into(), } } } diff --git a/crates/rpc/rpc/src/eth/api/mod.rs b/crates/rpc/rpc/src/eth/api/mod.rs index e68dd3686e..de5d12f8d1 100644 --- a/crates/rpc/rpc/src/eth/api/mod.rs +++ b/crates/rpc/rpc/src/eth/api/mod.rs @@ -87,7 +87,7 @@ where network: Network, eth_cache: EthStateCache, gas_oracle: GasPriceOracle, - gas_cap: u64, + gas_cap: impl Into, ) -> Self { Self::with_spawner( provider, @@ -95,7 +95,7 @@ where network, eth_cache, gas_oracle, - gas_cap, + gas_cap.into().into(), Box::::default(), ) } @@ -370,6 +370,35 @@ where } } +/// The default gas limit for eth_call and adjacent calls. +/// +/// This is different from the default to regular 30M block gas limit +/// [ETHEREUM_BLOCK_GAS_LIMIT](reth_primitives::constants::ETHEREUM_BLOCK_GAS_LIMIT) to allow for +/// more complex calls. +pub const RPC_DEFAULT_GAS_CAP: GasCap = GasCap(50_000_000); + +/// The wrapper type for gas limit +#[derive(Debug, Clone, Copy)] +pub struct GasCap(u64); + +impl Default for GasCap { + fn default() -> Self { + RPC_DEFAULT_GAS_CAP + } +} + +impl From for GasCap { + fn from(gas_cap: u64) -> Self { + Self(gas_cap) + } +} + +impl From for u64 { + fn from(gas_cap: GasCap) -> Self { + gas_cap.0 + } +} + /// Container type `EthApi` struct EthApiInner { /// The transaction pool. diff --git a/crates/rpc/rpc/src/eth/mod.rs b/crates/rpc/rpc/src/eth/mod.rs index bff4361e48..0a9efc5602 100644 --- a/crates/rpc/rpc/src/eth/mod.rs +++ b/crates/rpc/rpc/src/eth/mod.rs @@ -12,7 +12,7 @@ pub mod revm_utils; mod signer; pub(crate) mod utils; -pub use api::{EthApi, EthApiSpec, EthTransactions, TransactionSource}; +pub use api::{EthApi, EthApiSpec, EthTransactions, TransactionSource, RPC_DEFAULT_GAS_CAP}; pub use filter::EthFilter; pub use id_provider::EthSubscriptionIdProvider; pub use pubsub::EthPubSub;