Coverage Report

Created: 2022-11-10 19:56

/home/runner/work/creditcoin/creditcoin/primitives/src/lib.rs
Line
Count
Source (jump to first uncovered line)
1
1
#![cfg_attr(not(feature = "std"), no_std)]#![cfg_attr(not(feature = "std"), no_std)]
2
3
use sp_core::U256;
4
5
pub type Difficulty = U256;
6
7
#[cfg(all(feature = "std", feature = "prometheus"))]
8
pub mod metrics {
9
  use std::sync::Arc;
10
  use substrate_prometheus_endpoint::{prometheus::IntCounter, PrometheusError, Registry};
11
12
1
  #[derive(Clone)]
13
  pub struct MiningMetrics {
14
    inner: Arc<MiningMetricsInner>,
15
  }
16
17
  impl MiningMetrics {
18
4
    pub fn new(registry: Option<&Registry>) -> Result<Self, PrometheusError> {
19
4
      Ok(MiningMetrics { inner: Arc::new(MiningMetricsInner::register(registry)
?0
) })
20
4
    }
21
22
3
    pub fn inc(&self) {
23
3
      self.inner.count.inc();
24
3
    }
25
26
2
    pub fn add(&self, count: u64) {
27
2
      self.inner.count.inc_by(count);
28
2
    }
29
30
8
    pub fn count(&self) -> u64 {
31
8
      self.inner.count.get()
32
8
    }
33
34
3
    pub fn elapsed(&self) -> std::time::Duration {
35
3
      self.inner.start.elapsed()
36
3
    }
37
  }
38
39
  pub struct MiningMetricsInner {
40
    count: IntCounter,
41
    start: std::time::Instant,
42
  }
43
44
  impl MiningMetricsInner {
45
4
    fn register(registry: Option<&Registry>) -> Result<Self, PrometheusError> {
46
      Ok(MiningMetricsInner {
47
4
        count: if let Some(
registry1
) = registry {
48
          substrate_prometheus_endpoint::register(
49
1
            IntCounter::new(
50
1
              "creditcoin_node_hash_count",
51
1
              "number of hashes produced by the node while mining",
52
1
            )
?0
,
53
1
            registry,
54
0
          )?
55
        } else {
56
3
          IntCounter::new(
57
3
            "creditcoin_node_hash_count",
58
3
            "number of hashes produced by the node while mining",
59
3
          )
?0
60
        },
61
4
        start: std::time::Instant::now(),
62
      })
63
4
    }
64
  }
65
}
66
67
#[cfg(test)]
68
mod test {
69
  use super::metrics::*;
70
  use substrate_prometheus_endpoint::Registry;
71
72
1
  #[test]
73
1
  fn metrics_works_when_starting_without_registry() {
74
1
    let metrics = MiningMetrics::new(None).unwrap();
75
1
    let initial_count = metrics.count();
76
1
    assert_eq!(initial_count, 0);
77
78
    // increase
79
1
    metrics.inc();
80
1
    assert_eq!(metrics.count(), initial_count + 1);
81
82
    // add
83
1
    metrics.add(4);
84
1
    assert_eq!(metrics.count(), initial_count + 5);
85
86
    // elapsed time
87
1
    assert!(metrics.elapsed().as_nanos() > 0);
88
1
  }
89
90
1
  #[test]
91
1
  fn metrics_works_when_starting_with_registry() {
92
1
    let registry = Registry::new();
93
1
    let metrics = MiningMetrics::new(Some(&registry)).unwrap();
94
1
    let initial_count = metrics.count();
95
1
    assert_eq!(initial_count, 0);
96
97
    // increase
98
1
    metrics.inc();
99
1
    assert_eq!(metrics.count(), initial_count + 1);
100
101
    // add
102
1
    metrics.add(4);
103
1
    assert_eq!(metrics.count(), initial_count + 5);
104
105
    // elapsed time
106
1
    assert!(metrics.elapsed().as_nanos() > 0);
107
108
    // there's only 1 metric counter registered in MiningMetricsInner::register()
109
1
    let results = registry.gather();
110
1
    assert_eq!(results.len(), 1);
111
1
  }
112
113
1
  #[test]
114
1
  fn metrics_clone_trait_works() {
115
1
    let metrics = MiningMetrics::new(None).unwrap();
116
1
    metrics.inc();
117
1
118
1
    let new_metrics = metrics.clone();
119
1
    drop(metrics);
120
1
    assert_eq!(new_metrics.count(), 1);
121
1
  }
122
}