Skip to content

Commit

Permalink
chore: add default gpo constants (#5662)
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Cline <[email protected]>
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
3 people authored Dec 8, 2023
1 parent cd4d6c5 commit 32bf97d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 54 deletions.
55 changes: 37 additions & 18 deletions bin/reth/src/args/gas_price_oracle_args.rs
Original file line number Diff line number Diff line change
@@ -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<u32>,
#[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<u64>,
#[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<u64>,
#[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<u32>,
#[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,
}
}
}
Expand All @@ -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<T: Args> {
Expand All @@ -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,
}
);
}
Expand Down
7 changes: 1 addition & 6 deletions bin/reth/src/args/rpc_server_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions crates/rpc/rpc-builder/src/constants.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
47 changes: 17 additions & 30 deletions crates/rpc/rpc/src/eth/gas_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<u32>,
ignore_price: Option<u64>,
max_price: Option<u64>,
percentile: Option<u32>,
) -> 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),
}
}
}
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 32bf97d

Please sign in to comment.