Skip to content

Commit

Permalink
fix(core/metrics): set target difficulty as single value (#4902)
Browse files Browse the repository at this point in the history
Description
---
`base_node::blockchain::target_difficulty_sha` and `base_node::blockchain::target_difficulty_monero` are now single values at any point in time rather than qualified by block height. 


Motivation and Context
---
 I was not able to find a way (many months ago) to produce a performant non-timeseries graph that used height as the x-axis. The way I found tended to produce a lot of data on the endpoint side and crash grafana. We used a timeseries graph with an aggregation, producing an average difficulty over a time period. This PR allows for the precise difficulty w.r.t real time which I feel is better.

How Has This Been Tested?
---
Not tested, code compiles and this is how a lot of metric values work.
  • Loading branch information
sdbondi committed Nov 7, 2022
1 parent be9755d commit f625f73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 43 deletions.
29 changes: 4 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -774,11 +774,11 @@ where B: BlockchainBackend + 'static
fn update_target_difficulty(block: &ChainBlock) {
match block.header().pow_algo() {
PowAlgorithm::Sha3 => {
metrics::target_difficulty_sha(block.height())
metrics::target_difficulty_sha()
.set(i64::try_from(block.accumulated_data().target_difficulty.as_u64()).unwrap_or(i64::MAX));
},
PowAlgorithm::Monero => {
metrics::target_difficulty_monero(block.height())
metrics::target_difficulty_monero()
.set(i64::try_from(block.accumulated_data().target_difficulty.as_u64()).unwrap_or(i64::MAX));
},
}
Expand Down
30 changes: 14 additions & 16 deletions base_layer/core/src/base_node/metrics.rs
Expand Up @@ -25,38 +25,36 @@ use tari_common_types::types::FixedHash;
use tari_metrics::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec};
use tari_utilities::hex::Hex;

pub fn tip_height() -> IntGauge {
pub fn tip_height() -> &'static IntGauge {
static METER: Lazy<IntGauge> = Lazy::new(|| {
tari_metrics::register_int_gauge("base_node::blockchain::tip_height", "The current tip height").unwrap()
});

METER.clone()
&METER
}

pub fn target_difficulty_sha(height: u64) -> IntGauge {
static METER: Lazy<IntGaugeVec> = Lazy::new(|| {
tari_metrics::register_int_gauge_vec(
pub fn target_difficulty_sha() -> &'static IntGauge {
static METER: Lazy<IntGauge> = Lazy::new(|| {
tari_metrics::register_int_gauge(
"base_node::blockchain::target_difficulty_sha",
"The current miner target difficulty for the sha3 PoW algo",
&["height"],
)
.unwrap()
});

METER.with_label_values(&[&height.to_string()])
&METER
}

pub fn target_difficulty_monero(height: u64) -> IntGauge {
static METER: Lazy<IntGaugeVec> = Lazy::new(|| {
tari_metrics::register_int_gauge_vec(
pub fn target_difficulty_monero() -> &'static IntGauge {
static METER: Lazy<IntGauge> = Lazy::new(|| {
tari_metrics::register_int_gauge(
"base_node::blockchain::target_difficulty_monero",
"The current miner target difficulty for the monero PoW algo",
&["height"],
)
.unwrap()
});

METER.with_label_values(&[&height.to_string()])
&METER
}

pub fn reorg(fork_height: u64, num_added: usize, num_removed: usize) -> IntGauge {
Expand Down Expand Up @@ -153,7 +151,7 @@ pub fn rejected_local_blocks(height: u64, hash: &FixedHash) -> IntCounter {
METER.with_label_values(&[&height.to_string(), &hash.to_hex()])
}

pub fn active_sync_peers() -> IntGauge {
pub fn active_sync_peers() -> &'static IntGauge {
static METER: Lazy<IntGauge> = Lazy::new(|| {
tari_metrics::register_int_gauge(
"base_node::sync::active_peers",
Expand All @@ -162,10 +160,10 @@ pub fn active_sync_peers() -> IntGauge {
.unwrap()
});

METER.clone()
&METER
}

pub fn utxo_set_size() -> IntGauge {
pub fn utxo_set_size() -> &'static IntGauge {
static METER: Lazy<IntGauge> = Lazy::new(|| {
tari_metrics::register_int_gauge(
"base_node::blockchain::utxo_set_size",
Expand All @@ -174,5 +172,5 @@ pub fn utxo_set_size() -> IntGauge {
.unwrap()
});

METER.clone()
&METER
}

0 comments on commit f625f73

Please sign in to comment.