Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return total issuance metric as decimal value #1632

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app-libs/stf/src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ use crate::evm_helpers::{get_evm_account, get_evm_account_codes, get_evm_account

use crate::{
guess_the_number::{GuessTheNumberPublicGetter, GuessTheNumberTrustedGetter},
helpers::{shielding_target, wrap_bytes},
helpers::{shielding_target, shielding_target_genesis_hash, wrap_bytes},
};
use ita_parentchain_specs::MinimalChainSpec;
use itp_sgx_runtime_primitives::types::Moment;
use itp_stf_primitives::traits::PoolTransactionValidation;
use itp_stf_primitives::traits::{GetDecimals, PoolTransactionValidation};
use itp_types::parentchain::{BlockNumber, Hash, ParentchainId};
#[cfg(feature = "evm")]
use sp_core::{H160, H256};
Expand Down Expand Up @@ -82,6 +83,12 @@ impl GetterAuthorization for Getter {
}
}

impl GetDecimals for Getter {
fn get_shielding_target_decimals() -> u8 {
MinimalChainSpec::decimals(shielding_target_genesis_hash().unwrap_or_default())
}
}

impl PoolTransactionValidation for Getter {
fn validate(&self) -> Result<ValidTransaction, TransactionValidityError> {
match self {
Expand Down
15 changes: 10 additions & 5 deletions core-primitives/stf-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use itp_stf_interface::{
};
use itp_stf_primitives::{
error::StfError,
traits::TrustedCallVerification,
traits::{GetDecimals, TrustedCallVerification},
types::{ShardIdentifier, TrustedOperation, TrustedOperationOrHash},
};
use itp_stf_state_handler::{handle_state::HandleState, query_shard_state::QueryShardState};
Expand Down Expand Up @@ -302,7 +302,7 @@ where
From<BTreeMap<Vec<u8>, Option<Vec<u8>>>>,
<Stf as StateCallInterface<TCS, StateHandler::StateT, NodeMetadataRepository>>::Error: Debug,
TCS: PartialEq + Encode + Decode + Debug + Clone + Send + Sync + TrustedCallVerification,
G: PartialEq + Encode + Decode + Debug + Clone + Send + Sync,
G: PartialEq + Encode + Decode + Debug + Clone + Send + Sync + GetDecimals,
{
type Externalities = StateHandler::StateT;

Expand Down Expand Up @@ -360,7 +360,8 @@ where
});

let state_size_bytes = state.size();
let runtime_metrics = gather_runtime_metrics(&state);
let decimals = G::get_shielding_target_decimals();
let runtime_metrics = gather_runtime_metrics(&state, decimals);
let successful_call_count =
executed_and_failed_calls.iter().filter(|call| call.is_success()).count();
let failed_call_count = executed_and_failed_calls.len() - successful_call_count;
Expand Down Expand Up @@ -414,14 +415,18 @@ pub fn shards_key_hash() -> Vec<u8> {
/// assumes a common structure of sgx_runtime and extracts interesting metrics
/// while this may not be the best abstraction, it avoids circular dependencies
/// with app-libs and will be suitable in 99% of cases
fn gather_runtime_metrics<State>(state: &State) -> RuntimeMetrics
fn gather_runtime_metrics<State>(state: &State, decimals: u8) -> RuntimeMetrics
where
State: SgxExternalitiesTrait + Encode,
{
// prometheus has no support for NaN, therefore we fall back to -1
let total_issuance: f64 = state
.get(&storage_value_key("Balances", "TotalIssuance"))
.map(|v| Balance::decode(&mut v.as_slice()).map(|b| b as f64).unwrap_or(-1.0))
.map(|v| {
Balance::decode(&mut v.as_slice())
.map(|b| (b as f64) / 10f64.powi(decimals as i32))
brenzi marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or(-1.0)
})
.unwrap_or(-1.0);
// fallback to zero is fine here
let parentchain_integritee_processed_block_number: u32 = state
Expand Down
4 changes: 4 additions & 0 deletions core-primitives/stf-primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ where
shard: &ShardIdentifier,
) -> Result<TCS, Error>;
}

pub trait GetDecimals {
fn get_shielding_target_decimals() -> u8;
}
9 changes: 8 additions & 1 deletion core-primitives/test/src/mock/stf_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use itp_stf_interface::{
};
use itp_stf_primitives::{
traits::{
GetterAuthorization, PoolTransactionValidation, TrustedCallSigning, TrustedCallVerification,
GetDecimals, GetterAuthorization, PoolTransactionValidation, TrustedCallSigning,
TrustedCallVerification,
},
types::{KeyPair, Nonce, TrustedOperation},
};
Expand Down Expand Up @@ -238,6 +239,12 @@ impl GetterAuthorization for GetterMock {
}
}

impl GetDecimals for GetterMock {
fn get_shielding_target_decimals() -> u8 {
12u8
}
}

#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub enum PublicGetterMock {
Expand Down
Loading