feat: Enhance block import log line (#6095)

Signed-off-by: Danno Ferrin <danno.ferrin@gmail.com>
This commit is contained in:
Danno Ferrin
2024-02-09 05:14:37 -07:00
committed by GitHub
parent 51ffa822c3
commit 3f57fc8cc9
3 changed files with 76 additions and 27 deletions

View File

@ -1,13 +1,14 @@
//! Support for handling events emitted by node components. //! Support for handling events emitted by node components.
use crate::commands::node::cl_events::ConsensusLayerHealthEvent; use crate::{commands::node::cl_events::ConsensusLayerHealthEvent, primitives::B256};
use futures::Stream; use futures::Stream;
use reth_beacon_consensus::BeaconConsensusEngineEvent; use reth_beacon_consensus::{BeaconConsensusEngineEvent, ForkchoiceStatus};
use reth_db::{database::Database, database_metrics::DatabaseMetadata}; use reth_db::{database::Database, database_metrics::DatabaseMetadata};
use reth_interfaces::consensus::ForkchoiceState; use reth_interfaces::consensus::ForkchoiceState;
use reth_network::{NetworkEvent, NetworkHandle}; use reth_network::{NetworkEvent, NetworkHandle};
use reth_network_api::PeersInfo; use reth_network_api::PeersInfo;
use reth_primitives::{ use reth_primitives::{
constants,
stage::{EntitiesCheckpoint, StageCheckpoint, StageId}, stage::{EntitiesCheckpoint, StageCheckpoint, StageId},
BlockNumber, BlockNumber,
}; };
@ -18,7 +19,7 @@ use std::{
future::Future, future::Future,
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
time::{Duration, Instant}, time::{Duration, Instant, SystemTime, UNIX_EPOCH},
}; };
use tokio::time::Interval; use tokio::time::Interval;
use tracing::{info, warn}; use tracing::{info, warn};
@ -38,11 +39,28 @@ struct NodeState<DB> {
current_stage: Option<CurrentStage>, current_stage: Option<CurrentStage>,
/// The latest block reached by either pipeline or consensus engine. /// The latest block reached by either pipeline or consensus engine.
latest_block: Option<BlockNumber>, latest_block: Option<BlockNumber>,
/// The time of the latest block seen by the pipeline
latest_block_time: Option<u64>,
/// Hash of the head block last set by fork choice update
head_block_hash: Option<B256>,
/// Hash of the safe block last set by fork choice update
safe_block_hash: Option<B256>,
/// Hash of finalized block last set by fork choice update
finalized_block_hash: Option<B256>,
} }
impl<DB> NodeState<DB> { impl<DB> NodeState<DB> {
fn new(db: DB, network: Option<NetworkHandle>, latest_block: Option<BlockNumber>) -> Self { fn new(db: DB, network: Option<NetworkHandle>, latest_block: Option<BlockNumber>) -> Self {
Self { db, network, current_stage: None, latest_block } Self {
db,
network,
current_stage: None,
latest_block,
latest_block_time: None,
head_block_hash: None,
safe_block_hash: None,
finalized_block_hash: None,
}
} }
fn num_connected_peers(&self) -> usize { fn num_connected_peers(&self) -> usize {
@ -155,19 +173,40 @@ impl<DB> NodeState<DB> {
BeaconConsensusEngineEvent::ForkchoiceUpdated(state, status) => { BeaconConsensusEngineEvent::ForkchoiceUpdated(state, status) => {
let ForkchoiceState { head_block_hash, safe_block_hash, finalized_block_hash } = let ForkchoiceState { head_block_hash, safe_block_hash, finalized_block_hash } =
state; state;
info!( if status != ForkchoiceStatus::Valid ||
?head_block_hash, (self.safe_block_hash != Some(safe_block_hash) &&
?safe_block_hash, self.finalized_block_hash != Some(finalized_block_hash))
?finalized_block_hash, {
?status, info!(
"Forkchoice updated" ?head_block_hash,
); ?safe_block_hash,
?finalized_block_hash,
?status,
"Forkchoice updated"
);
}
self.head_block_hash = Some(head_block_hash);
self.safe_block_hash = Some(safe_block_hash);
self.finalized_block_hash = Some(finalized_block_hash);
} }
BeaconConsensusEngineEvent::CanonicalBlockAdded(block) => { BeaconConsensusEngineEvent::CanonicalBlockAdded(block, elapsed) => {
info!(number=block.number, hash=?block.hash(), "Block added to canonical chain"); info!(
number=block.number,
hash=?block.hash(),
peers=self.num_connected_peers(),
txs=block.body.len(),
mgas=%format!("{:.3}", block.header.gas_used as f64 / constants::MGAS_TO_GAS as f64),
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,
excess_blobs=block.header.excess_blob_gas.unwrap_or(0) / constants::eip4844::DATA_GAS_PER_BLOB,
?elapsed,
"Block added to canonical chain"
);
} }
BeaconConsensusEngineEvent::CanonicalChainCommitted(head, elapsed) => { BeaconConsensusEngineEvent::CanonicalChainCommitted(head, elapsed) => {
self.latest_block = Some(head.number); self.latest_block = Some(head.number);
self.latest_block_time = Some(head.timestamp);
info!(number=head.number, hash=?head.hash(), ?elapsed, "Canonical chain committed"); info!(number=head.number, hash=?head.hash(), ?elapsed, "Canonical chain committed");
} }
@ -357,13 +396,19 @@ where
); );
} }
} else if let Some(latest_block) = this.state.latest_block { } else if let Some(latest_block) = this.state.latest_block {
info!( let now =
target: "reth::cli", SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
connected_peers = this.state.num_connected_peers(), if now - this.state.latest_block_time.unwrap_or(0) > 60 {
%freelist, // Once we start receiving consensus nodes, don't emit status unless stalled for
%latest_block, // 1 minute
"Status" info!(
); target: "reth::cli",
connected_peers = this.state.num_connected_peers(),
%freelist,
%latest_block,
"Status"
);
}
} else { } else {
info!( info!(
target: "reth::cli", target: "reth::cli",
@ -415,7 +460,9 @@ struct Eta {
impl Eta { impl Eta {
/// Update the ETA given the checkpoint, if possible. /// Update the ETA given the checkpoint, if possible.
fn update(&mut self, checkpoint: StageCheckpoint) { fn update(&mut self, checkpoint: StageCheckpoint) {
let Some(current) = checkpoint.entities() else { return }; let Some(current) = checkpoint.entities() else {
return;
};
if let Some(last_checkpoint_time) = &self.last_checkpoint_time { if let Some(last_checkpoint_time) = &self.last_checkpoint_time {
let processed_since_last = current.processed - self.last_checkpoint.processed; let processed_since_last = current.processed - self.last_checkpoint.processed;
@ -456,7 +503,7 @@ impl Display for Eta {
f, f,
"{}", "{}",
humantime::format_duration(Duration::from_secs(remaining.as_secs())) humantime::format_duration(Duration::from_secs(remaining.as_secs()))
) );
} }
} }

View File

@ -6,11 +6,11 @@ use std::{sync::Arc, time::Duration};
/// Events emitted by [crate::BeaconConsensusEngine]. /// Events emitted by [crate::BeaconConsensusEngine].
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum BeaconConsensusEngineEvent { pub enum BeaconConsensusEngineEvent {
/// The fork choice state was updated. /// The fork choice state was updated, and the current fork choice status
ForkchoiceUpdated(ForkchoiceState, ForkchoiceStatus), ForkchoiceUpdated(ForkchoiceState, ForkchoiceStatus),
/// A block was added to the canonical chain. /// A block was added to the canonical chain, and the elapsed time validating the block
CanonicalBlockAdded(Arc<SealedBlock>), CanonicalBlockAdded(Arc<SealedBlock>, Duration),
/// A canonical chain was committed. /// A canonical chain was committed, and the elapsed time committing the data
CanonicalChainCommitted(Box<SealedHeader>, Duration), CanonicalChainCommitted(Box<SealedHeader>, Duration),
/// A block was added to the fork chain. /// A block was added to the fork chain.
ForkBlockAdded(Arc<SealedBlock>), ForkBlockAdded(Arc<SealedBlock>),

View File

@ -1271,16 +1271,18 @@ where
debug_assert!(self.sync.is_pipeline_idle(), "pipeline must be idle"); debug_assert!(self.sync.is_pipeline_idle(), "pipeline must be idle");
let block_hash = block.hash(); let block_hash = block.hash();
let start = Instant::now();
let status = self let status = self
.blockchain .blockchain
.insert_block_without_senders(block.clone(), BlockValidationKind::Exhaustive)?; .insert_block_without_senders(block.clone(), BlockValidationKind::Exhaustive)?;
let elapsed = start.elapsed();
let mut latest_valid_hash = None; let mut latest_valid_hash = None;
let block = Arc::new(block); let block = Arc::new(block);
let status = match status { let status = match status {
InsertPayloadOk::Inserted(BlockStatus::Valid(attachment)) => { InsertPayloadOk::Inserted(BlockStatus::Valid(attachment)) => {
latest_valid_hash = Some(block_hash); latest_valid_hash = Some(block_hash);
let event = if attachment.is_canonical() { let event = if attachment.is_canonical() {
BeaconConsensusEngineEvent::CanonicalBlockAdded(block) BeaconConsensusEngineEvent::CanonicalBlockAdded(block, elapsed)
} else { } else {
BeaconConsensusEngineEvent::ForkBlockAdded(block) BeaconConsensusEngineEvent::ForkBlockAdded(block)
}; };