chore: use format_gas and format_gas_throughput for gas logs (#9247)

This commit is contained in:
Dan Cline
2024-07-02 13:54:49 -04:00
committed by GitHub
parent 95f228155f
commit e95c6dba9d
13 changed files with 93 additions and 51 deletions

View File

@ -1,3 +1,5 @@
use std::time::Duration;
/// Represents one Kilogas, or `1_000` gas.
pub const KILOGAS: u64 = 1_000;
@ -6,3 +8,73 @@ pub const MEGAGAS: u64 = KILOGAS * 1_000;
/// Represents one Gigagas, or `1_000_000_000` gas.
pub const GIGAGAS: u64 = MEGAGAS * 1_000;
/// Returns a formatted gas throughput log, showing either:
/// * "Kgas/s", or 1,000 gas per second
/// * "Mgas/s", or 1,000,000 gas per second
/// * "Ggas/s", or 1,000,000,000 gas per second
///
/// Depending on the magnitude of the gas throughput.
pub fn format_gas_throughput(gas: u64, execution_duration: Duration) -> String {
let gas_per_second = gas as f64 / execution_duration.as_secs_f64();
if gas_per_second < MEGAGAS as f64 {
format!("{:.} Kgas/second", gas_per_second / KILOGAS as f64)
} else if gas_per_second < GIGAGAS as f64 {
format!("{:.} Mgas/second", gas_per_second / MEGAGAS as f64)
} else {
format!("{:.} Ggas/second", gas_per_second / GIGAGAS as f64)
}
}
/// Returns a formatted gas log, showing either:
/// * "Kgas", or 1,000 gas
/// * "Mgas", or 1,000,000 gas
/// * "Ggas", or 1,000,000,000 gas
///
/// Depending on the magnitude of gas.
pub fn format_gas(gas: u64) -> String {
let gas = gas as f64;
if gas < MEGAGAS as f64 {
format!("{:.} Kgas", gas / KILOGAS as f64)
} else if gas < GIGAGAS as f64 {
format!("{:.} Mgas", gas / MEGAGAS as f64)
} else {
format!("{:.} Ggas", gas / GIGAGAS as f64)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gas_fmt() {
let gas = 100_000;
let gas_unit = format_gas(gas);
assert_eq!(gas_unit, "100 Kgas");
let gas = 100_000_000;
let gas_unit = format_gas(gas);
assert_eq!(gas_unit, "100 Mgas");
let gas = 100_000_000_000;
let gas_unit = format_gas(gas);
assert_eq!(gas_unit, "100 Ggas");
}
#[test]
fn test_gas_throughput_fmt() {
let duration = Duration::from_secs(1);
let gas = 100_000;
let throughput = format_gas_throughput(gas, duration);
assert_eq!(throughput, "100 Kgas/second");
let gas = 100_000_000;
let throughput = format_gas_throughput(gas, duration);
assert_eq!(throughput, "100 Mgas/second");
let gas = 100_000_000_000;
let throughput = format_gas_throughput(gas, duration);
assert_eq!(throughput, "100 Ggas/second");
}
}

View File

@ -3,8 +3,9 @@
use alloy_primitives::{address, b256, Address, B256, U256};
use core::time::Duration;
/// Gas units, for example [`GIGAGAS`](gas_units::GIGAGAS).
/// Gas units, for example [`GIGAGAS`].
pub mod gas_units;
pub use gas_units::{GIGAGAS, KILOGAS, MEGAGAS};
/// The client version: `reth/v{major}.{minor}.{patch}`
pub const RETH_CLIENT_VERSION: &str = concat!("reth/v", env!("CARGO_PKG_VERSION"));
@ -98,9 +99,6 @@ pub const FINNEY_TO_WEI: u128 = (GWEI_TO_WEI as u128) * 1_000_000;
/// Multiplier for converting ether to wei.
pub const ETH_TO_WEI: u128 = FINNEY_TO_WEI * 1000;
/// Multiplier for converting mgas to gas.
pub const MGAS_TO_GAS: u64 = 1_000_000u64;
/// The Ethereum mainnet genesis hash:
/// `0x0d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3`
pub const MAINNET_GENESIS_HASH: B256 =

View File

@ -14,6 +14,7 @@ mod alloy_compat;
/// Common constants.
pub mod constants;
pub use constants::gas_units::{format_gas, format_gas_throughput};
/// Minimal account
pub mod account;