Skip to content

Commit

Permalink
[fix] #3709: Correct how tx_amounts histogram is calculated.
Browse files Browse the repository at this point in the history
Signed-off-by: Sam H. Smith <[email protected]>
  • Loading branch information
SamHSmith committed Aug 17, 2023
1 parent 46ab977 commit 2bf79ce
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
11 changes: 5 additions & 6 deletions core/src/smartcontracts/isi/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ pub mod isi {

#[allow(clippy::float_arithmetic)]
{
wsv.metric_tx_amounts += new_quantity.into_metric();
wsv.metric_tx_amounts_counter += 1;
wsv.new_tx_amounts.lock().push(new_quantity.into_metric());
wsv.increase_asset_total_amount(&asset_id.definition_id, mint.object)?;
}

Expand Down Expand Up @@ -284,8 +283,7 @@ pub mod isi {

#[allow(clippy::float_arithmetic)]
{
wsv.metric_tx_amounts += burn_quantity.into_metric();
wsv.metric_tx_amounts_counter += 1;
wsv.new_tx_amounts.lock().push(burn_quantity.into_metric());
wsv.decrease_asset_total_amount(&asset_id.definition_id, burn.object)?;
}

Expand Down Expand Up @@ -353,8 +351,9 @@ pub mod isi {

#[allow(clippy::float_arithmetic)]
{
wsv.metric_tx_amounts += transfer_quantity.into_metric();
wsv.metric_tx_amounts_counter += 1;
wsv.new_tx_amounts
.lock()
.push(transfer_quantity.into_metric());
}

wsv.emit_events([
Expand Down
18 changes: 7 additions & 11 deletions core/src/sumeragi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ the function.
#[derive(Debug)]
struct LastUpdateMetricsData {
block_height: u64,
metric_tx_amounts: f64,
metric_tx_amounts_counter: u64,
}

/// Handle to `Sumeragi` actor
Expand Down Expand Up @@ -141,14 +139,14 @@ impl SumeragiHandle {
last_guard.block_height = block_index;
}

let diff_count = wsv.metric_tx_amounts_counter - last_guard.metric_tx_amounts_counter;
let diff_amount_per_count =
(wsv.metric_tx_amounts - last_guard.metric_tx_amounts) / (diff_count as f64);
for _ in 0..diff_count {
last_guard.metric_tx_amounts_counter += 1;
last_guard.metric_tx_amounts += diff_amount_per_count;
let new_tx_amounts = {
let mut new_buf = Vec::new();
core::mem::swap(&mut new_buf, &mut wsv.new_tx_amounts.lock());
new_buf
};

self.metrics.tx_amounts.observe(diff_amount_per_count);
for amount in &new_tx_amounts {
self.metrics.tx_amounts.observe(*amount);
}

#[allow(clippy::cast_possible_truncation)]
Expand Down Expand Up @@ -321,8 +319,6 @@ impl SumeragiHandle {
metrics: Metrics::default(),
last_update_metrics_mutex: Arc::new(Mutex::new(LastUpdateMetricsData {
block_height: 0,
metric_tx_amounts: 0.0_f64,
metric_tx_amounts_counter: 0,
})),
_thread_handle: Arc::new(thread_handle),
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ impl Validator {

match self {
Self::Initial => {
let (_authority, Executable::Instructions(instructions)) = transaction.into() else {
let (_authority, Executable::Instructions(instructions)) = transaction.into()
else {
return Ok(());
};
for isi in instructions {
Expand Down
19 changes: 7 additions & 12 deletions core/src/wsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use iroha_data_model::{
};
use iroha_logger::prelude::*;
use iroha_primitives::small::SmallVec;
use parking_lot::Mutex;
use serde::{
de::{DeserializeSeed, MapAccess, Visitor},
Deserializer, Serialize,
Expand Down Expand Up @@ -278,19 +279,16 @@ pub struct WorldStateView {
/// Buffer containing events generated during `WorldStateView::apply`. Renewed on every block commit.
#[serde(skip)]
pub events_buffer: Vec<Event>,
/// Accumulated amount of any asset that has been transacted.
#[serde(skip)]
pub metric_tx_amounts: f64,
/// Count of how many mints, transfers and burns have happened.
#[serde(skip)]
pub metric_tx_amounts_counter: u64,
/// Engine for WASM [`Runtime`](wasm::Runtime) to execute triggers.
#[serde(skip)]
pub engine: wasmtime::Engine,

/// Reference to Kura subsystem.
#[serde(skip)]
kura: Arc<Kura>,
/// Temporary metrics buffer of amounts of any asset that has been transacted.
#[serde(skip)]
pub new_tx_amounts: Arc<Mutex<Vec<f64>>>,
}

/// Context necessary for deserializing [`WorldStateView`]
Expand Down Expand Up @@ -361,8 +359,7 @@ impl<'de> DeserializeSeed<'de> for WsvSeed {
kura: self.loader.kura,
engine,
events_buffer: Vec::new(),
metric_tx_amounts: 0f64,
metric_tx_amounts_counter: 0u64,
new_tx_amounts: Arc::new(Mutex::new(Vec::new())),
})
}
}
Expand All @@ -383,8 +380,7 @@ impl Clone for WorldStateView {
block_hashes: self.block_hashes.clone(),
transactions: self.transactions.clone(),
events_buffer: Vec::new(),
metric_tx_amounts: 0.0_f64,
metric_tx_amounts_counter: 0,
new_tx_amounts: Arc::clone(&self.new_tx_amounts),
engine: self.engine.clone(),
kura: Arc::clone(&self.kura),
}
Expand Down Expand Up @@ -910,8 +906,7 @@ impl WorldStateView {
transactions: HashMap::new(),
block_hashes: Vec::new(),
events_buffer: Vec::new(),
metric_tx_amounts: 0.0_f64,
metric_tx_amounts_counter: 0,
new_tx_amounts: Arc::new(Mutex::new(Vec::new())),
engine: wasm::create_engine(),
kura,
}
Expand Down

0 comments on commit 2bf79ce

Please sign in to comment.