From 45608c1927c56123648e998adfd92ee2eeab5c4e Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Mon, 4 Nov 2024 18:13:20 +0100 Subject: [PATCH 1/2] return total issuance metric as decimal value --- app-libs/stf/src/getter.rs | 11 +++++++++-- core-primitives/stf-executor/src/executor.rs | 15 ++++++++++----- core-primitives/stf-primitives/src/traits.rs | 4 ++++ core-primitives/test/src/mock/stf_mock.rs | 9 ++++++++- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/app-libs/stf/src/getter.rs b/app-libs/stf/src/getter.rs index c65516f59..acb58163a 100644 --- a/app-libs/stf/src/getter.rs +++ b/app-libs/stf/src/getter.rs @@ -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}; @@ -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 { match self { diff --git a/core-primitives/stf-executor/src/executor.rs b/core-primitives/stf-executor/src/executor.rs index 566cf669c..d1de1ca55 100644 --- a/core-primitives/stf-executor/src/executor.rs +++ b/core-primitives/stf-executor/src/executor.rs @@ -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}; @@ -302,7 +302,7 @@ where From, Option>>>, >::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; @@ -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; @@ -414,14 +415,18 @@ pub fn shards_key_hash() -> Vec { /// 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) -> RuntimeMetrics +fn gather_runtime_metrics(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)) + .unwrap_or(-1.0) + }) .unwrap_or(-1.0); // fallback to zero is fine here let parentchain_integritee_processed_block_number: u32 = state diff --git a/core-primitives/stf-primitives/src/traits.rs b/core-primitives/stf-primitives/src/traits.rs index 82d7bffc3..4ef1ccb5b 100644 --- a/core-primitives/stf-primitives/src/traits.rs +++ b/core-primitives/stf-primitives/src/traits.rs @@ -81,3 +81,7 @@ where shard: &ShardIdentifier, ) -> Result; } + +pub trait GetDecimals { + fn get_shielding_target_decimals() -> u8; +} diff --git a/core-primitives/test/src/mock/stf_mock.rs b/core-primitives/test/src/mock/stf_mock.rs index c65caf775..249b44772 100644 --- a/core-primitives/test/src/mock/stf_mock.rs +++ b/core-primitives/test/src/mock/stf_mock.rs @@ -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}, }; @@ -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 { From 703d2c889ff72d21971be7a96c701c07c30667a2 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Mon, 4 Nov 2024 20:38:32 +0100 Subject: [PATCH 2/2] fix execute_with --- core-primitives/stf-executor/src/executor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-primitives/stf-executor/src/executor.rs b/core-primitives/stf-executor/src/executor.rs index d1de1ca55..02f927bd4 100644 --- a/core-primitives/stf-executor/src/executor.rs +++ b/core-primitives/stf-executor/src/executor.rs @@ -360,7 +360,7 @@ where }); let state_size_bytes = state.size(); - let decimals = G::get_shielding_target_decimals(); + let decimals = state.execute_with(|| 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();