Skip to content

Commit

Permalink
chore: simplify metrics-only blocks (#5952)
Browse files Browse the repository at this point in the history
Description
---
Does minor refactoring of metrics-only blocks to make the analyzer
happy.

Motivation and Context
---
Recent work in #5944 makes metrics a non-default feature. This made the
analyzer unhappy due to some unused variables, but it turns out this can
be fixed while simplifying some of the handling of metrics-only blocks.
This PR makes these changes and keeps the analyzer happy.

How Has This Been Tested?
---
Existing tests pass.

What process can a PR reviewer use to test or verify this change?
---
Check that the new metrics-only block handling is identical to that
introduced by #5944.
  • Loading branch information
AaronFeickert committed Nov 14, 2023
1 parent 4cbdfec commit b99d6f5
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions base_layer/core/src/base_node/comms_interface/inbound_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,9 @@ where B: BlockchainBackend + 'static
BlockAddResult::ChainReorg { .. } => true,
};

#[cfg(feature = "metrics")]
self.update_block_result_metrics(&block_add_result).await?;

self.publish_block_event(BlockEvent::ValidBlockAdded(block.clone(), block_add_result));

if should_propagate {
Expand Down Expand Up @@ -947,9 +949,9 @@ where B: BlockchainBackend + 'static
}
}

#[cfg(feature = "metrics")]
async fn update_block_result_metrics(&self, block_add_result: &BlockAddResult) -> Result<(), CommsInterfaceError> {
fn update_target_difficulty(block: &ChainBlock) {
#[cfg(feature = "metrics")]
match block.header().pow_algo() {
PowAlgorithm::Sha3x => {
metrics::target_difficulty_sha()
Expand All @@ -965,18 +967,12 @@ where B: BlockchainBackend + 'static
match block_add_result {
BlockAddResult::Ok(ref block) => {
update_target_difficulty(block);

#[cfg(feature = "metrics")]
{
#[allow(clippy::cast_possible_wrap)]
metrics::tip_height().set(block.height() as i64);
let utxo_set_size = self.blockchain_db.utxo_count().await?;
metrics::utxo_set_size().set(utxo_set_size.try_into().unwrap_or(i64::MAX));
}
#[allow(clippy::cast_possible_wrap)]
metrics::tip_height().set(block.height() as i64);
let utxo_set_size = self.blockchain_db.utxo_count().await?;
metrics::utxo_set_size().set(utxo_set_size.try_into().unwrap_or(i64::MAX));
},
#[allow(unused_variables)] // `removed` variable is used if metrics are compiled
BlockAddResult::ChainReorg { added, removed } => {
#[cfg(feature = "metrics")]
if let Some(fork_height) = added.last().map(|b| b.height()) {
#[allow(clippy::cast_possible_wrap)]
metrics::tip_height().set(fork_height as i64);
Expand All @@ -990,7 +986,6 @@ where B: BlockchainBackend + 'static
}
},
BlockAddResult::OrphanBlock => {
#[cfg(feature = "metrics")]
metrics::orphaned_blocks().inc();
},
_ => {},
Expand Down

0 comments on commit b99d6f5

Please sign in to comment.