diff --git a/src/bin/importer_offline.rs b/src/bin/importer_offline.rs index 6ead9c00b..cf0cd7b3f 100644 --- a/src/bin/importer_offline.rs +++ b/src/bin/importer_offline.rs @@ -19,6 +19,7 @@ use stratus::eth::storage::InMemoryPermanentStorage; use stratus::eth::storage::StratusStorage; use stratus::eth::BlockMiner; use stratus::eth::Executor; +use stratus::utils::calculate_tps_and_bpm; use stratus::GlobalServices; use stratus::GlobalState; use tokio::runtime::Handle; @@ -175,21 +176,15 @@ async fn execute_block_importer( } } - let seconds_elapsed = match instant_before_execution.elapsed().as_secs() as usize { - // avoid division by zero - 0 => 1, - non_zero => non_zero, - }; - let tps = transaction_count.checked_div(seconds_elapsed).unwrap_or(transaction_count); - let minutes_elapsed = seconds_elapsed as f64 / 60.0; - let blocks_per_minute = blocks_len as f64 / minutes_elapsed; + let duration = instant_before_execution.elapsed(); + let (tps, bpm) = calculate_tps_and_bpm(duration, transaction_count, blocks_len); + tracing::info!( tps, - blocks_per_minute = format_args!("{blocks_per_minute:.2}"), - seconds_elapsed, + blocks_per_minute = format_args!("{bpm:.2}"), + ?duration, %block_start, %block_end, - transaction_count, receipts = receipts.len(), "reexecuted blocks batch", ); diff --git a/src/bin/importer_online.rs b/src/bin/importer_online.rs index 9b3c095db..2a816772b 100644 --- a/src/bin/importer_online.rs +++ b/src/bin/importer_online.rs @@ -22,12 +22,14 @@ use stratus::infra::metrics; use stratus::infra::tracing::warn_task_rx_closed; use stratus::infra::tracing::warn_task_tx_closed; use stratus::infra::BlockchainClient; +use stratus::utils::calculate_tps; use stratus::GlobalServices; use stratus::GlobalState; use tokio::sync::mpsc; use tokio::task::yield_now; use tokio::time::sleep; use tokio::time::timeout; +use tokio::time::Instant; // ----------------------------------------------------------------------------- // Globals @@ -134,11 +136,24 @@ async fn start_block_executor(executor: Arc, miner: Arc, m // execute and mine let receipts = ExternalReceipts::from(receipts); - tracing::info!(number = %block.number(), txs_len = block.transactions.len(), "reexecuting external block"); + let instant_before_execution = Instant::now(); + if executor.reexecute_external(&block, &receipts).await.is_err() { GlobalState::shutdown_from(TASK_NAME, "failed to re-execute external block"); return; }; + + let duration = instant_before_execution.elapsed(); + let tps = calculate_tps(duration, block.transactions.len()); + + tracing::info!( + tps, + ?duration, + block_number = ?block.number(), + receipts = receipts.len(), + "reexecuted external block", + ); + if miner.mine_external_mixed_and_commit().await.is_err() { GlobalState::shutdown_from(TASK_NAME, "failed to mine external block"); return; diff --git a/src/utils.rs b/src/utils.rs index db5c5d3dd..36a5c1bf9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use tokio::select; use tokio::signal::unix::signal; use tokio::signal::unix::SignalKind; @@ -36,3 +38,15 @@ pub async fn spawn_signal_handler() -> anyhow::Result<()> { Ok(()) } + +pub fn calculate_tps_and_bpm(duration: Duration, transaction_count: usize, block_count: usize) -> (f64, f64) { + let seconds_elapsed = duration.as_secs_f64() + f64::EPSILON; + let tps = transaction_count as f64 / seconds_elapsed; + let blocks_per_minute = block_count as f64 / (seconds_elapsed / 60.0); + (tps, blocks_per_minute) +} + +pub fn calculate_tps(duration: Duration, transaction_count: usize) -> f64 { + let seconds_elapsed = duration.as_secs_f64() + f64::EPSILON; + transaction_count as f64 / seconds_elapsed +}