From 32bf97d7bc7a862e5440f3182428de427585bb0f Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Fri, 8 Dec 2023 03:22:42 -0500 Subject: [PATCH] chore: add default gpo constants (#5662) Co-authored-by: Dan Cline <6798349+Rjected@users.noreply.github.com> Co-authored-by: Matthias Seitz --- bin/reth/src/args/gas_price_oracle_args.rs | 55 +++++++++++++++------- bin/reth/src/args/rpc_server_args.rs | 7 +-- crates/rpc/rpc-builder/src/constants.rs | 6 +++ crates/rpc/rpc/src/eth/gas_oracle.rs | 47 +++++++----------- 4 files changed, 61 insertions(+), 54 deletions(-) diff --git a/bin/reth/src/args/gas_price_oracle_args.rs b/bin/reth/src/args/gas_price_oracle_args.rs index 001ba9017c97..54087693af7a 100644 --- a/bin/reth/src/args/gas_price_oracle_args.rs +++ b/bin/reth/src/args/gas_price_oracle_args.rs @@ -1,33 +1,53 @@ +use crate::primitives::U256; use clap::Args; +use reth_rpc::eth::gas_oracle::GasPriceOracleConfig; +use reth_rpc_builder::constants::{ + DEFAULT_GAS_PRICE_BLOCKS, DEFAULT_GAS_PRICE_PERCENTILE, DEFAULT_IGNORE_GAS_PRICE, + DEFAULT_MAX_GAS_PRICE, +}; /// Parameters to configure Gas Price Oracle -#[derive(Debug, Clone, Args, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, Args, PartialEq, Eq)] #[clap(next_help_heading = "Gas Price Oracle")] pub struct GasPriceOracleArgs { /// Number of recent blocks to check for gas price - #[arg(long = "gpo.blocks", default_value = "20")] - pub blocks: Option, + #[arg(long = "gpo.blocks", default_value_t = DEFAULT_GAS_PRICE_BLOCKS)] + pub blocks: u32, /// Gas Price below which gpo will ignore transactions - #[arg(long = "gpo.ignoreprice", default_value = "2")] - pub ignore_price: Option, + #[arg(long = "gpo.ignoreprice", default_value_t = DEFAULT_IGNORE_GAS_PRICE.to())] + pub ignore_price: u64, /// Maximum transaction priority fee(or gasprice before London Fork) to be recommended by gpo - #[arg(long = "gpo.maxprice", default_value = "500000000000")] - pub max_price: Option, + #[arg(long = "gpo.maxprice", default_value_t = DEFAULT_MAX_GAS_PRICE.to())] + pub max_price: u64, /// The percentile of gas prices to use for the estimate - #[arg(long = "gpo.percentile", default_value = "60")] - pub percentile: Option, + #[arg(long = "gpo.percentile", default_value_t = DEFAULT_GAS_PRICE_PERCENTILE)] + pub percentile: u32, +} + +impl GasPriceOracleArgs { + /// Returns a [GasPriceOracleConfig] from the arguments. + pub fn gas_price_oracle_config(&self) -> GasPriceOracleConfig { + let Self { blocks, ignore_price, max_price, percentile } = self; + GasPriceOracleConfig { + max_price: Some(U256::from(*max_price)), + ignore_price: Some(U256::from(*ignore_price)), + percentile: *percentile, + blocks: *blocks, + ..Default::default() + } + } } impl Default for GasPriceOracleArgs { fn default() -> Self { Self { - blocks: Some(20), - ignore_price: Some(2), - max_price: Some(500000000000), - percentile: Some(60), + blocks: DEFAULT_GAS_PRICE_BLOCKS, + ignore_price: DEFAULT_IGNORE_GAS_PRICE.to(), + max_price: DEFAULT_MAX_GAS_PRICE.to(), + percentile: DEFAULT_GAS_PRICE_PERCENTILE, } } } @@ -36,7 +56,6 @@ impl Default for GasPriceOracleArgs { mod tests { use super::*; use clap::Parser; - /// A helper type to parse Args more easily #[derive(Parser)] struct CommandParser { @@ -50,10 +69,10 @@ mod tests { assert_eq!( args, GasPriceOracleArgs { - blocks: Some(20), - ignore_price: Some(2), - max_price: Some(500000000000), - percentile: Some(60), + blocks: DEFAULT_GAS_PRICE_BLOCKS, + ignore_price: DEFAULT_IGNORE_GAS_PRICE.to(), + max_price: DEFAULT_MAX_GAS_PRICE.to(), + percentile: DEFAULT_GAS_PRICE_PERCENTILE, } ); } diff --git a/bin/reth/src/args/rpc_server_args.rs b/bin/reth/src/args/rpc_server_args.rs index e72807db2f0a..54f0f040fda0 100644 --- a/bin/reth/src/args/rpc_server_args.rs +++ b/bin/reth/src/args/rpc_server_args.rs @@ -358,12 +358,7 @@ impl RethRpcConfig for RpcServerArgs { } fn gas_price_oracle_config(&self) -> GasPriceOracleConfig { - GasPriceOracleConfig::new( - self.gas_price_oracle.blocks, - self.gas_price_oracle.ignore_price, - self.gas_price_oracle.max_price, - self.gas_price_oracle.percentile, - ) + self.gas_price_oracle.gas_price_oracle_config() } fn transport_rpc_module_config(&self) -> TransportRpcModuleConfig { diff --git a/crates/rpc/rpc-builder/src/constants.rs b/crates/rpc/rpc-builder/src/constants.rs index 6659174123fc..f468fb03112a 100644 --- a/crates/rpc/rpc-builder/src/constants.rs +++ b/crates/rpc/rpc-builder/src/constants.rs @@ -1,3 +1,9 @@ +/// 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, +}; + /// The default port for the http server pub const DEFAULT_HTTP_RPC_PORT: u16 = 8545; diff --git a/crates/rpc/rpc/src/eth/gas_oracle.rs b/crates/rpc/rpc/src/eth/gas_oracle.rs index 4eaaa5cdcfc5..8812b19c9eac 100644 --- a/crates/rpc/rpc/src/eth/gas_oracle.rs +++ b/crates/rpc/rpc/src/eth/gas_oracle.rs @@ -19,11 +19,18 @@ 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; -/// The default maximum gas price to use for the estimate -pub const DEFAULT_MAX_PRICE: U256 = U256::from_limbs([500_000_000_000u64, 0, 0, 0]); +/// 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_PRICE: U256 = U256::from_limbs([2u64, 0, 0, 0]); +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)] @@ -54,33 +61,13 @@ pub struct GasPriceOracleConfig { impl Default for GasPriceOracleConfig { fn default() -> Self { GasPriceOracleConfig { - blocks: 20, - percentile: 60, - max_header_history: MAX_HEADER_HISTORY, - max_block_history: MAX_HEADER_HISTORY, - default: None, - max_price: Some(DEFAULT_MAX_PRICE), - ignore_price: Some(DEFAULT_IGNORE_PRICE), - } - } -} - -impl GasPriceOracleConfig { - /// Creating a new gpo config with blocks, ignoreprice, maxprice and percentile - pub fn new( - blocks: Option, - ignore_price: Option, - max_price: Option, - percentile: Option, - ) -> Self { - Self { - blocks: blocks.unwrap_or(20), - percentile: percentile.unwrap_or(60), + blocks: DEFAULT_GAS_PRICE_BLOCKS, + percentile: DEFAULT_GAS_PRICE_PERCENTILE, max_header_history: MAX_HEADER_HISTORY, max_block_history: MAX_HEADER_HISTORY, default: None, - max_price: max_price.map(U256::from).or(Some(DEFAULT_MAX_PRICE)), - ignore_price: ignore_price.map(U256::from).or(Some(DEFAULT_IGNORE_PRICE)), + max_price: Some(DEFAULT_MAX_GAS_PRICE), + ignore_price: Some(DEFAULT_IGNORE_GAS_PRICE), } } } @@ -323,12 +310,12 @@ mod tests { #[test] fn max_price_sanity() { - assert_eq!(DEFAULT_MAX_PRICE, U256::from(500_000_000_000u64)); - assert_eq!(DEFAULT_MAX_PRICE, U256::from(500 * GWEI_TO_WEI)) + assert_eq!(DEFAULT_MAX_GAS_PRICE, U256::from(500_000_000_000u64)); + assert_eq!(DEFAULT_MAX_GAS_PRICE, U256::from(500 * GWEI_TO_WEI)) } #[test] fn ignore_price_sanity() { - assert_eq!(DEFAULT_IGNORE_PRICE, U256::from(2u64)); + assert_eq!(DEFAULT_IGNORE_GAS_PRICE, U256::from(2u64)); } }