Skip to content

Commit

Permalink
Relay balance metrics (#1291)
Browse files Browse the repository at this point in the history
* relay balance metrics

* convert balance to "main" tokens in balance metrics

* add balances widgets to maintenance dashboard
  • Loading branch information
svyatonik authored and bkchr committed Apr 10, 2024
1 parent 25008a5 commit fe34a52
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 43 deletions.
22 changes: 20 additions & 2 deletions bridges/relays/bin-substrate/src/cli/relay_headers_and_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl RelayHeadersAndMessages {
let right_to_left_metrics = left_to_right_metrics.clone().reverse();

// start conversion rate update loops for left/right chains
if let Some(left_messages_pallet_owner) = left_messages_pallet_owner {
if let Some(left_messages_pallet_owner) = left_messages_pallet_owner.clone() {
let left_client = left_client.clone();
let format_err = || {
anyhow::format_err!(
Expand Down Expand Up @@ -417,7 +417,7 @@ impl RelayHeadersAndMessages {
},
);
}
if let Some(right_messages_pallet_owner) = right_messages_pallet_owner {
if let Some(right_messages_pallet_owner) = right_messages_pallet_owner.clone() {
let right_client = right_client.clone();
let format_err = || {
anyhow::format_err!(
Expand Down Expand Up @@ -500,6 +500,24 @@ impl RelayHeadersAndMessages {
}
}

// add balance-related metrics
let metrics_params =
substrate_relay_helper::messages_metrics::add_relay_balances_metrics(
left_client.clone(),
metrics_params,
Some(left_sign.public().into()),
left_messages_pallet_owner.map(|kp| kp.public().into()),
)
.await?;
let metrics_params =
substrate_relay_helper::messages_metrics::add_relay_balances_metrics(
right_client.clone(),
metrics_params,
Some(right_sign.public().into()),
right_messages_pallet_owner.map(|kp| kp.public().into()),
)
.await?;

// start on-demand header relays
let left_to_right_transaction_params = TransactionParams {
mortality: right_transactions_mortality,
Expand Down
3 changes: 2 additions & 1 deletion bridges/relays/client-substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ frame-system = { git = "https://github.com/paritytech/substrate", branch = "mast
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-chain-spec = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-rpc-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-transaction-pool-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-rpc = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
2 changes: 1 addition & 1 deletion bridges/relays/client-substrate/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub type WeightToFeeOf<C> = <C as Chain>::WeightToFee;
/// Transaction status of the chain.
pub type TransactionStatusOf<C> = TransactionStatus<HashOf<C>, HashOf<C>>;

/// Substrate-based chain with `frame_system::Config::AccountData` set to
/// Substrate-based chain with `AccountData` generic argument of `frame_system::AccountInfo` set to
/// the `pallet_balances::AccountData<Balance>`.
pub trait ChainWithBalances: Chain {
/// Return runtime storage key for getting `frame_system::AccountInfo` of given account.
Expand Down
9 changes: 9 additions & 0 deletions bridges/relays/client-substrate/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,15 @@ impl<C: Chain> Client<C> {
.await
}

/// Return `tokenDecimals` property from the set of chain properties.
pub async fn token_decimals(&self) -> Result<Option<u64>> {
self.jsonrpsee_execute(move |client| async move {
let system_properties = Substrate::<C>::system_properties(&*client).await?;
Ok(system_properties.get("tokenDecimals").and_then(|v| v.as_u64()))
})
.await
}

/// Return new justifications stream.
pub async fn subscribe_justifications(&self) -> Result<Subscription<Bytes>> {
let subscription = self
Expand Down
80 changes: 56 additions & 24 deletions bridges/relays/client-substrate/src/metrics/float_storage_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,84 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use crate::{chain::Chain, client::Client};
use crate::{chain::Chain, client::Client, Error as SubstrateError};

use async_std::sync::{Arc, RwLock};
use async_trait::async_trait;
use codec::Decode;
use num_traits::One;
use relay_utils::metrics::{
metric_name, register, F64SharedRef, Gauge, Metric, PrometheusError, Registry,
StandaloneMetric, F64,
};
use sp_core::storage::StorageKey;
use sp_runtime::{traits::UniqueSaturatedInto, FixedPointNumber};
use std::time::Duration;
use sp_core::storage::{StorageData, StorageKey};
use sp_runtime::{traits::UniqueSaturatedInto, FixedPointNumber, FixedU128};
use std::{marker::PhantomData, time::Duration};

/// Storage value update interval (in blocks).
const UPDATE_INTERVAL_IN_BLOCKS: u32 = 5;

/// Fied-point storage value and the way it is decoded from the raw storage value.
pub trait FloatStorageValue: 'static + Clone + Send + Sync {
/// Type of the value.
type Value: FixedPointNumber;
/// Try to decode value from the raw storage value.
fn decode(
&self,
maybe_raw_value: Option<StorageData>,
) -> Result<Option<Self::Value>, SubstrateError>;
}

/// Implementation of `FloatStorageValue` that expects encoded `FixedU128` value and returns `1` if
/// value is missing from the storage.
#[derive(Clone, Debug, Default)]
pub struct FixedU128OrOne;

impl FloatStorageValue for FixedU128OrOne {
type Value = FixedU128;

fn decode(
&self,
maybe_raw_value: Option<StorageData>,
) -> Result<Option<Self::Value>, SubstrateError> {
maybe_raw_value
.map(|raw_value| {
FixedU128::decode(&mut &raw_value.0[..])
.map_err(SubstrateError::ResponseParseFailed)
.map(Some)
})
.unwrap_or_else(|| Ok(Some(FixedU128::one())))
}
}

/// Metric that represents fixed-point runtime storage value as float gauge.
#[derive(Clone, Debug)]
pub struct FloatStorageValueMetric<C: Chain, T: Clone> {
pub struct FloatStorageValueMetric<C: Chain, V: FloatStorageValue> {
value_converter: V,
client: Client<C>,
storage_key: StorageKey,
maybe_default_value: Option<T>,
metric: Gauge<F64>,
shared_value_ref: F64SharedRef,
_phantom: PhantomData<V>,
}

impl<C: Chain, T: Decode + FixedPointNumber> FloatStorageValueMetric<C, T> {
impl<C: Chain, V: FloatStorageValue> FloatStorageValueMetric<C, V> {
/// Create new metric.
pub fn new(
value_converter: V,
client: Client<C>,
storage_key: StorageKey,
maybe_default_value: Option<T>,
name: String,
help: String,
) -> Result<Self, PrometheusError> {
let shared_value_ref = Arc::new(RwLock::new(None));
Ok(FloatStorageValueMetric {
value_converter,
client,
storage_key,
maybe_default_value,
metric: Gauge::new(metric_name(None, &name), help)?,
shared_value_ref,
_phantom: Default::default(),
})
}

Expand All @@ -65,37 +101,33 @@ impl<C: Chain, T: Decode + FixedPointNumber> FloatStorageValueMetric<C, T> {
}
}

impl<C: Chain, T> Metric for FloatStorageValueMetric<C, T>
where
T: 'static + Decode + Send + Sync + FixedPointNumber,
{
impl<C: Chain, V: FloatStorageValue> Metric for FloatStorageValueMetric<C, V> {
fn register(&self, registry: &Registry) -> Result<(), PrometheusError> {
register(self.metric.clone(), registry).map(drop)
}
}

#[async_trait]
impl<C: Chain, T> StandaloneMetric for FloatStorageValueMetric<C, T>
where
T: 'static + Decode + Send + Sync + FixedPointNumber,
{
impl<C: Chain, V: FloatStorageValue> StandaloneMetric for FloatStorageValueMetric<C, V> {
fn update_interval(&self) -> Duration {
C::AVERAGE_BLOCK_INTERVAL * UPDATE_INTERVAL_IN_BLOCKS
}

async fn update(&self) {
let value = self
.client
.storage_value::<T>(self.storage_key.clone(), None)
.raw_storage_value(self.storage_key.clone(), None)
.await
.map(|maybe_storage_value| {
maybe_storage_value.or(self.maybe_default_value).map(|storage_value| {
storage_value.into_inner().unique_saturated_into() as f64 /
T::DIV.unique_saturated_into() as f64
.and_then(|maybe_storage_value| {
self.value_converter.decode(maybe_storage_value).map(|maybe_fixed_point_value| {
maybe_fixed_point_value.map(|fixed_point_value| {
fixed_point_value.into_inner().unique_saturated_into() as f64 /
V::Value::DIV.unique_saturated_into() as f64
})
})
})
.map_err(drop);
relay_utils::metrics::set_gauge_value(&self.metric, value);
.map_err(|e| e.to_string());
relay_utils::metrics::set_gauge_value(&self.metric, value.clone());
*self.shared_value_ref.write().await = value.ok().and_then(|x| x);
}
}
2 changes: 1 addition & 1 deletion bridges/relays/client-substrate/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Contains several Substrate-specific metrics that may be exposed by relay.

pub use float_storage_value::FloatStorageValueMetric;
pub use float_storage_value::{FixedU128OrOne, FloatStorageValue, FloatStorageValueMetric};
pub use storage_proof_overhead::StorageProofOverheadMetric;

mod float_storage_value;
Expand Down
2 changes: 2 additions & 0 deletions bridges/relays/client-substrate/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jsonrpsee_proc_macros::rpc_client_api! {
pub(crate) Substrate<C: Chain> {
#[rpc(method = "system_health", positional_params)]
fn system_health() -> Health;
#[rpc(method = "system_properties", positional_params)]
fn system_properties() -> sc_chain_spec::Properties;
#[rpc(method = "chain_getHeader", positional_params)]
fn chain_get_header(block_hash: Option<C::Hash>) -> C::Header;
#[rpc(method = "chain_getFinalizedHead", positional_params)]
Expand Down
2 changes: 2 additions & 0 deletions bridges/relays/lib-substrate-relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ bp-messages = { path = "../../primitives/messages" }
# Substrate Dependencies

frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
Loading

0 comments on commit fe34a52

Please sign in to comment.