diff --git a/Cargo.lock b/Cargo.lock index 38f323b6c32c..54f5d3f694f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7293,6 +7293,7 @@ dependencies = [ "reth-node-core", "reth-payload-builder", "reth-primitives", + "reth-primitives-traits", "reth-provider", "reth-prune-types", "reth-revm", @@ -7755,6 +7756,7 @@ dependencies = [ "reth-network", "reth-network-api", "reth-primitives", + "reth-primitives-traits", "reth-provider", "reth-prune", "reth-stages", @@ -8409,6 +8411,7 @@ dependencies = [ "reth-network-p2p", "reth-network-peers", "reth-primitives", + "reth-primitives-traits", "reth-provider", "reth-prune-types", "reth-revm", diff --git a/crates/exex/exex/Cargo.toml b/crates/exex/exex/Cargo.toml index a911f45997a7..e2a364988bad 100644 --- a/crates/exex/exex/Cargo.toml +++ b/crates/exex/exex/Cargo.toml @@ -19,6 +19,7 @@ reth-metrics.workspace = true reth-node-api.workspace = true reth-node-core.workspace = true reth-primitives.workspace = true +reth-primitives-traits.workspace = true reth-provider.workspace = true reth-tasks.workspace = true reth-tracing.workspace = true diff --git a/crates/exex/exex/src/backfill.rs b/crates/exex/exex/src/backfill.rs index f46c82d24101..212082081b22 100644 --- a/crates/exex/exex/src/backfill.rs +++ b/crates/exex/exex/src/backfill.rs @@ -2,10 +2,11 @@ use reth_db_api::database::Database; use reth_evm::execute::{BatchExecutor, BlockExecutionError, BlockExecutorProvider}; use reth_node_api::FullNodeComponents; use reth_primitives::{Block, BlockNumber}; +use reth_primitives_traits::format_gas_throughput; use reth_provider::{Chain, FullProvider, ProviderError, TransactionVariant}; use reth_prune_types::PruneModes; use reth_revm::database::StateProviderDatabase; -use reth_stages_api::{format_gas_throughput, ExecutionStageThresholds}; +use reth_stages_api::ExecutionStageThresholds; use reth_tracing::tracing::{debug, trace}; use std::{ marker::PhantomData, diff --git a/crates/node/events/Cargo.toml b/crates/node/events/Cargo.toml index 1b14c74de5f3..d592d25f3902 100644 --- a/crates/node/events/Cargo.toml +++ b/crates/node/events/Cargo.toml @@ -20,6 +20,7 @@ reth-prune.workspace = true reth-static-file.workspace = true reth-db-api.workspace = true reth-primitives.workspace = true +reth-primitives-traits.workspace = true # alloy alloy-rpc-types-engine.workspace = true diff --git a/crates/node/events/src/node.rs b/crates/node/events/src/node.rs index 8a25c370037b..77c8fd4684fc 100644 --- a/crates/node/events/src/node.rs +++ b/crates/node/events/src/node.rs @@ -10,6 +10,7 @@ use reth_db_api::{database::Database, database_metrics::DatabaseMetadata}; use reth_network::{NetworkEvent, NetworkHandle}; use reth_network_api::PeersInfo; use reth_primitives::{constants, BlockNumber, B256}; +use reth_primitives_traits::{format_gas, format_gas_throughput}; use reth_prune::PrunerEvent; use reth_stages::{EntitiesCheckpoint, ExecOutput, PipelineEvent, StageCheckpoint, StageId}; use reth_static_file::StaticFileProducerEvent; @@ -279,8 +280,8 @@ impl NodeState { hash=?block.hash(), peers=self.num_connected_peers(), txs=block.body.len(), - mgas=%format!("{:.3}MGas", block.header.gas_used as f64 / constants::MGAS_TO_GAS as f64), - mgas_throughput=%format!("{:.3}MGas/s", block.header.gas_used as f64 / elapsed.as_secs_f64() / constants::MGAS_TO_GAS as f64), + gas=format_gas(block.header.gas_used), + gas_throughput=format_gas_throughput(block.header.gas_used, elapsed), full=%format!("{:.1}%", block.header.gas_used as f64 * 100.0 / block.header.gas_limit as f64), base_fee=%format!("{:.2}gwei", block.header.base_fee_per_gas.unwrap_or(0) as f64 / constants::GWEI_TO_WEI as f64), blobs=block.header.blob_gas_used.unwrap_or(0) / constants::eip4844::DATA_GAS_PER_BLOB, diff --git a/crates/primitives-traits/src/constants/gas_units.rs b/crates/primitives-traits/src/constants/gas_units.rs index 9916de864a63..0495a1755ef6 100644 --- a/crates/primitives-traits/src/constants/gas_units.rs +++ b/crates/primitives-traits/src/constants/gas_units.rs @@ -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"); + } +} diff --git a/crates/primitives-traits/src/constants/mod.rs b/crates/primitives-traits/src/constants/mod.rs index 32d41ddcd8dd..7ed018e8c8fb 100644 --- a/crates/primitives-traits/src/constants/mod.rs +++ b/crates/primitives-traits/src/constants/mod.rs @@ -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 = diff --git a/crates/primitives-traits/src/lib.rs b/crates/primitives-traits/src/lib.rs index 590e9573dc4c..b7a42d9c7b5c 100644 --- a/crates/primitives-traits/src/lib.rs +++ b/crates/primitives-traits/src/lib.rs @@ -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; diff --git a/crates/stages/api/src/metrics/execution.rs b/crates/stages/api/src/metrics/execution.rs deleted file mode 100644 index b54ed02f653c..000000000000 --- a/crates/stages/api/src/metrics/execution.rs +++ /dev/null @@ -1,20 +0,0 @@ -use std::time::Duration; - -use reth_primitives_traits::constants::gas_units::{GIGAGAS, KILOGAS, MEGAGAS}; - -/// 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) - } -} diff --git a/crates/stages/api/src/metrics/listener.rs b/crates/stages/api/src/metrics/listener.rs index e703367bcd67..e37eaa3d7267 100644 --- a/crates/stages/api/src/metrics/listener.rs +++ b/crates/stages/api/src/metrics/listener.rs @@ -1,6 +1,6 @@ use crate::{metrics::SyncMetrics, StageCheckpoint, StageId}; use alloy_primitives::BlockNumber; -use reth_primitives_traits::constants::MGAS_TO_GAS; +use reth_primitives_traits::constants::MEGAGAS; use std::{ future::Future, pin::Pin, @@ -83,7 +83,7 @@ impl MetricsListener { } } MetricEvent::ExecutionStageGas { gas } => { - self.sync_metrics.execution_stage.mgas_processed_total.increment(gas / MGAS_TO_GAS) + self.sync_metrics.execution_stage.mgas_processed_total.increment(gas / MEGAGAS) } } } diff --git a/crates/stages/api/src/metrics/mod.rs b/crates/stages/api/src/metrics/mod.rs index 983247ae9c0b..bed2742c25fc 100644 --- a/crates/stages/api/src/metrics/mod.rs +++ b/crates/stages/api/src/metrics/mod.rs @@ -1,7 +1,5 @@ -mod execution; mod listener; mod sync_metrics; -pub use execution::format_gas_throughput; pub use listener::{MetricEvent, MetricEventsSender, MetricsListener}; use sync_metrics::*; diff --git a/crates/stages/stages/Cargo.toml b/crates/stages/stages/Cargo.toml index ab0dd6c689a7..75531e1ce580 100644 --- a/crates/stages/stages/Cargo.toml +++ b/crates/stages/stages/Cargo.toml @@ -24,6 +24,7 @@ reth-evm.workspace = true reth-exex.workspace = true reth-network-p2p.workspace = true reth-primitives.workspace = true +reth-primitives-traits.workspace = true reth-provider.workspace = true reth-execution-types.workspace = true reth-prune-types.workspace = true diff --git a/crates/stages/stages/src/stages/execution.rs b/crates/stages/stages/src/stages/execution.rs index 26db6f50f8ae..4294132c7554 100644 --- a/crates/stages/stages/src/stages/execution.rs +++ b/crates/stages/stages/src/stages/execution.rs @@ -7,6 +7,7 @@ use reth_evm::execute::{BatchExecutor, BlockExecutorProvider}; use reth_execution_types::{Chain, ExecutionOutcome}; use reth_exex::{ExExManagerHandle, ExExNotification}; use reth_primitives::{BlockNumber, Header, StaticFileSegment}; +use reth_primitives_traits::format_gas_throughput; use reth_provider::{ providers::{StaticFileProvider, StaticFileProviderRWRefMut, StaticFileWriter}, BlockReader, DatabaseProviderRW, HeaderProvider, LatestStateProviderRef, OriginalValuesKnown, @@ -15,9 +16,9 @@ use reth_provider::{ use reth_prune_types::PruneModes; use reth_revm::database::StateProviderDatabase; use reth_stages_api::{ - format_gas_throughput, BlockErrorKind, CheckpointBlockRange, EntitiesCheckpoint, ExecInput, - ExecOutput, ExecutionCheckpoint, ExecutionStageThresholds, MetricEvent, MetricEventsSender, - Stage, StageCheckpoint, StageError, StageId, UnwindInput, UnwindOutput, + BlockErrorKind, CheckpointBlockRange, EntitiesCheckpoint, ExecInput, ExecOutput, + ExecutionCheckpoint, ExecutionStageThresholds, MetricEvent, MetricEventsSender, Stage, + StageCheckpoint, StageError, StageId, UnwindInput, UnwindOutput, }; use std::{ cmp::Ordering, @@ -640,7 +641,7 @@ mod tests { StaticFileProviderFactory, }; use reth_prune_types::{PruneMode, ReceiptsLogPruneConfig}; - use reth_stages_api::{format_gas_throughput, StageUnitCheckpoint}; + use reth_stages_api::StageUnitCheckpoint; use std::collections::BTreeMap; fn stage() -> ExecutionStage { @@ -661,22 +662,6 @@ mod tests { ) } - #[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"); - } - #[test] fn execution_checkpoint_matches() { let factory = create_test_provider_factory();