Skip to content

Commit

Permalink
Relayer reward metric (paritytech#1742)
Browse files Browse the repository at this point in the history
* use StorageDoubleMapKeyProvider in RelayerRewards

* add metrics

* clippy

* fixed alerts that have caused missing dashboards

* fix metric name

* fix metric name again

* add new metrics to the RialtoParachain <> Millau maintenance dashboard

* remove obsolete dashboard
  • Loading branch information
svyatonik authored and serban300 committed Apr 8, 2024
1 parent 15f48ae commit 00846ab
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 21 deletions.
2 changes: 2 additions & 0 deletions bridges/modules/relayers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ scale-info = { version = "2.1.1", default-features = false, features = ["derive"

bp-messages = { path = "../../primitives/messages", default-features = false }
bp-relayers = { path = "../../primitives/relayers", default-features = false }
bp-runtime = { path = "../../primitives/runtime", default-features = false }

# Substrate Dependencies

Expand All @@ -37,6 +38,7 @@ default = ["std"]
std = [
"bp-messages/std",
"bp-relayers/std",
"bp-runtime/std",
"codec/std",
"frame-support/std",
"frame-system/std",
Expand Down
17 changes: 11 additions & 6 deletions bridges/modules/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#![warn(missing_docs)]

use bp_messages::LaneId;
use bp_relayers::PaymentProcedure;
use bp_relayers::{PaymentProcedure, RelayerRewardsKeyProvider};
use bp_runtime::StorageDoubleMapKeyProvider;
use frame_support::sp_runtime::Saturating;
use sp_arithmetic::traits::{AtLeast32BitUnsigned, Zero};
use sp_std::marker::PhantomData;
Expand All @@ -46,6 +47,10 @@ pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

/// `RelayerRewardsKeyProvider` for given configuration.
type RelayerRewardsKeyProviderOf<T> =
RelayerRewardsKeyProvider<<T as frame_system::Config>::AccountId, <T as Config>::Reward>;

#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
Expand Down Expand Up @@ -146,11 +151,11 @@ pub mod pallet {
#[pallet::getter(fn relayer_reward)]
pub type RelayerRewards<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId,
Identity,
LaneId,
T::Reward,
<RelayerRewardsKeyProviderOf<T> as StorageDoubleMapKeyProvider>::Hasher1,
<RelayerRewardsKeyProviderOf<T> as StorageDoubleMapKeyProvider>::Key1,
<RelayerRewardsKeyProviderOf<T> as StorageDoubleMapKeyProvider>::Hasher2,
<RelayerRewardsKeyProviderOf<T> as StorageDoubleMapKeyProvider>::Key2,
<RelayerRewardsKeyProviderOf<T> as StorageDoubleMapKeyProvider>::Value,
OptionQuery,
>;
}
Expand Down
2 changes: 2 additions & 0 deletions bridges/primitives/relayers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
# Bridge Dependencies

bp-messages = { path = "../messages", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }

# Substrate Dependencies

Expand All @@ -27,6 +28,7 @@ hex-literal = "0.3"
default = ["std"]
std = [
"bp-messages/std",
"bp-runtime/std",
"frame-support/std",
"sp-runtime/std",
"sp-std/std",
Expand Down
22 changes: 21 additions & 1 deletion bridges/primitives/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
#![cfg_attr(not(feature = "std"), no_std)]

use bp_messages::LaneId;
use bp_runtime::StorageDoubleMapKeyProvider;
use frame_support::{Blake2_128Concat, Identity};
use sp_runtime::{
codec::{Decode, Encode},
codec::{Codec, Decode, Encode, EncodeLike},
traits::AccountIdConversion,
};
use sp_std::{fmt::Debug, marker::PhantomData};
Expand Down Expand Up @@ -65,6 +67,24 @@ where
}
}

/// Can be use to access the runtime storage key within the `RelayerRewards` map of the relayers
/// pallet.
pub struct RelayerRewardsKeyProvider<AccountId, Reward>(PhantomData<(AccountId, Reward)>);

impl<AccountId, Reward> StorageDoubleMapKeyProvider for RelayerRewardsKeyProvider<AccountId, Reward>
where
AccountId: Codec + EncodeLike,
Reward: Codec + EncodeLike,
{
const MAP_NAME: &'static str = "RelayerRewards";

type Hasher1 = Blake2_128Concat;
type Key1 = AccountId;
type Hasher2 = Identity;
type Key2 = LaneId;
type Value = Reward;
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ use crate::{
use bp_messages::LaneId;
use bp_runtime::BalanceOf;
use relay_substrate_client::{
AccountIdOf, AccountKeyPairOf, Chain, ChainWithBalances, ChainWithTransactions, Client,
Parachain,
AccountIdOf, AccountKeyPairOf, Chain, ChainWithBalances, ChainWithMessages,
ChainWithTransactions, Client, Parachain,
};
use relay_utils::metrics::MetricsParams;
use sp_core::Pair;
Expand Down Expand Up @@ -259,9 +259,9 @@ where
type Base: Full2WayBridgeBase<Left = Self::Left, Right = Self::Right>;

/// The left relay chain.
type Left: ChainWithTransactions + ChainWithBalances + CliChain;
type Left: ChainWithTransactions + ChainWithBalances + ChainWithMessages + CliChain;
/// The right relay chain.
type Right: ChainWithTransactions + ChainWithBalances + CliChain;
type Right: ChainWithTransactions + ChainWithBalances + ChainWithMessages + CliChain;

/// Left to Right bridge.
type L2R: MessagesCliBridge<Source = Self::Left, Target = Self::Right>;
Expand Down Expand Up @@ -317,28 +317,36 @@ where
self.mut_base().start_on_demand_headers_relayers().await?;

// add balance-related metrics
let lanes = self
.base()
.common()
.shared
.lane
.iter()
.cloned()
.map(Into::into)
.collect::<Vec<_>>();
{
let common = self.mut_base().mut_common();
substrate_relay_helper::messages_metrics::add_relay_balances_metrics(
substrate_relay_helper::messages_metrics::add_relay_balances_metrics::<_, Self::Right>(
common.left.client.clone(),
&mut common.metrics_params,
&common.left.accounts,
&lanes,
)
.await?;
substrate_relay_helper::messages_metrics::add_relay_balances_metrics(
substrate_relay_helper::messages_metrics::add_relay_balances_metrics::<_, Self::Left>(
common.right.client.clone(),
&mut common.metrics_params,
&common.right.accounts,
&lanes,
)
.await?;
}

let lanes = self.base().common().shared.lane.clone();
// Need 2x capacity since we consider both directions for each lane
let mut message_relays = Vec::with_capacity(lanes.len() * 2);
for lane in lanes {
let lane = lane.into();

let left_to_right_messages = substrate_relay_helper::messages_lane::run::<
<Self::L2R as MessagesCliBridge>::MessagesLane,
>(self.left_to_right().messages_relay_params(
Expand Down
1 change: 1 addition & 0 deletions bridges/relays/client-bridge-hub-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl ChainWithTransactions for BridgeHubRococo {
impl ChainWithMessages for BridgeHubRococo {
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
bp_bridge_hub_rococo::WITH_BRIDGE_HUB_ROCOCO_MESSAGES_PALLET_NAME;
const WITH_CHAIN_RELAYERS_PALLET_NAME: Option<&'static str> = None;

const TO_CHAIN_MESSAGE_DETAILS_METHOD: &'static str =
bp_bridge_hub_rococo::TO_BRIDGE_HUB_ROCOCO_MESSAGE_DETAILS_METHOD;
Expand Down
1 change: 1 addition & 0 deletions bridges/relays/client-bridge-hub-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl ChainWithTransactions for BridgeHubWococo {
impl ChainWithMessages for BridgeHubWococo {
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
bp_bridge_hub_wococo::WITH_BRIDGE_HUB_WOCOCO_MESSAGES_PALLET_NAME;
const WITH_CHAIN_RELAYERS_PALLET_NAME: Option<&'static str> = None;

const TO_CHAIN_MESSAGE_DETAILS_METHOD: &'static str =
bp_bridge_hub_wococo::TO_BRIDGE_HUB_WOCOCO_MESSAGE_DETAILS_METHOD;
Expand Down
2 changes: 2 additions & 0 deletions bridges/relays/client-millau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ impl ChainWithGrandpa for Millau {
impl ChainWithMessages for Millau {
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
bp_millau::WITH_MILLAU_MESSAGES_PALLET_NAME;
// TODO (https://github.com/paritytech/parity-bridges-common/issues/1692): change the name
const WITH_CHAIN_RELAYERS_PALLET_NAME: Option<&'static str> = Some("BridgeRelayers");
const TO_CHAIN_MESSAGE_DETAILS_METHOD: &'static str =
bp_millau::TO_MILLAU_MESSAGE_DETAILS_METHOD;
const FROM_CHAIN_MESSAGE_DETAILS_METHOD: &'static str =
Expand Down
2 changes: 2 additions & 0 deletions bridges/relays/client-rialto-parachain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ impl ChainWithBalances for RialtoParachain {
impl ChainWithMessages for RialtoParachain {
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
bp_rialto_parachain::WITH_RIALTO_PARACHAIN_MESSAGES_PALLET_NAME;
// TODO (https://github.com/paritytech/parity-bridges-common/issues/1692): change the name
const WITH_CHAIN_RELAYERS_PALLET_NAME: Option<&'static str> = Some("BridgeRelayers");
const TO_CHAIN_MESSAGE_DETAILS_METHOD: &'static str =
bp_rialto_parachain::TO_RIALTO_PARACHAIN_MESSAGE_DETAILS_METHOD;
const FROM_CHAIN_MESSAGE_DETAILS_METHOD: &'static str =
Expand Down
2 changes: 2 additions & 0 deletions bridges/relays/client-rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ impl ChainWithGrandpa for Rialto {
impl ChainWithMessages for Rialto {
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
bp_rialto::WITH_RIALTO_MESSAGES_PALLET_NAME;
// TODO (https://github.com/paritytech/parity-bridges-common/issues/1692): change the name
const WITH_CHAIN_RELAYERS_PALLET_NAME: Option<&'static str> = Some("BridgeRelayers");
const TO_CHAIN_MESSAGE_DETAILS_METHOD: &'static str =
bp_rialto::TO_RIALTO_MESSAGE_DETAILS_METHOD;
const FROM_CHAIN_MESSAGE_DETAILS_METHOD: &'static str =
Expand Down
10 changes: 10 additions & 0 deletions bridges/relays/client-substrate/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ pub trait ChainWithMessages: Chain {
/// the same name.
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str;

// TODO (https://github.com/paritytech/parity-bridges-common/issues/1692): check all the names
// after the issue is fixed - all names must be changed

/// Name of the bridge relayers pallet (used in `construct_runtime` macro call) that is deployed
/// at some other chain to bridge with this `ChainWithMessages`.
///
/// We assume that all chains that are bridging with this `ChainWithMessages` are using
/// the same name.
const WITH_CHAIN_RELAYERS_PALLET_NAME: Option<&'static str>;

/// Name of the `To<ChainWithMessages>OutboundLaneApi::message_details` runtime API method.
/// The method is provided by the runtime that is bridged with this `ChainWithMessages`.
const TO_CHAIN_MESSAGE_DETAILS_METHOD: &'static str;
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 @@ -12,6 +12,7 @@ async-std = "1.9.0"
async-trait = "0.1"
codec = { package = "parity-scale-codec", version = "3.1.5" }
futures = "0.3.12"
hex = "0.4"
num-traits = "0.2"
log = "0.4.17"

Expand All @@ -20,6 +21,7 @@ log = "0.4.17"
bp-header-chain = { path = "../../primitives/header-chain" }
bp-parachains = { path = "../../primitives/parachains" }
bp-polkadot-core = { path = "../../primitives/polkadot-core" }
bp-relayers = { path = "../../primitives/relayers" }
bridge-runtime-common = { path = "../../bin/runtime-common" }

finality-grandpa = { version = "0.16.0" }
Expand Down
59 changes: 54 additions & 5 deletions bridges/relays/lib-substrate-relay/src/messages_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@

use crate::TaggedAccount;

use bp_messages::LaneId;
use bp_runtime::StorageDoubleMapKeyProvider;
use codec::Decode;
use frame_system::AccountInfo;
use pallet_balances::AccountData;
use relay_substrate_client::{
metrics::{FloatStorageValue, FloatStorageValueMetric},
AccountIdOf, BalanceOf, Chain, ChainWithBalances, Client, Error as SubstrateError, IndexOf,
AccountIdOf, BalanceOf, Chain, ChainWithBalances, ChainWithMessages, Client,
Error as SubstrateError, IndexOf,
};
use relay_utils::metrics::{MetricsParams, StandaloneMetric};
use sp_core::storage::StorageData;
use sp_runtime::{FixedPointNumber, FixedU128};
use std::{convert::TryFrom, fmt::Debug, marker::PhantomData};

/// Add relay accounts balance metrics.
pub async fn add_relay_balances_metrics<C: ChainWithBalances>(
pub async fn add_relay_balances_metrics<C: ChainWithBalances, BC: ChainWithMessages>(
client: Client<C>,
metrics: &mut MetricsParams,
relay_accounts: &Vec<TaggedAccount<AccountIdOf<C>>>,
lanes: &[LaneId],
) -> anyhow::Result<()>
where
BalanceOf<C>: Into<u128> + std::fmt::Debug,
Expand Down Expand Up @@ -68,26 +72,43 @@ where

for account in relay_accounts {
let relay_account_balance_metric = FloatStorageValueMetric::new(
FreeAccountBalance::<C> { token_decimals, _phantom: Default::default() },
AccountBalanceFromAccountInfo::<C> { token_decimals, _phantom: Default::default() },
client.clone(),
C::account_info_storage_key(account.id()),
format!("at_{}_relay_{}_balance", C::NAME, account.tag()),
format!("Balance of the {} relay account at the {}", account.tag(), C::NAME),
)?;
relay_account_balance_metric.register_and_spawn(&metrics.registry)?;

if let Some(relayers_pallet_name) = BC::WITH_CHAIN_RELAYERS_PALLET_NAME {
for lane in lanes {
let relay_account_reward_metric = FloatStorageValueMetric::new(
AccountBalance::<C> { token_decimals, _phantom: Default::default() },
client.clone(),
bp_relayers::RelayerRewardsKeyProvider::<AccountIdOf<C>, BalanceOf<C>>::final_key(
relayers_pallet_name,
account.id(),
lane,
),
format!("at_{}_relay_{}_reward_for_lane_{}_with_{}", C::NAME, account.tag(), hex::encode(lane.as_ref()), BC::NAME),
format!("Reward of the {} relay account for serving lane {:?} with {} at the {}", account.tag(), lane, BC::NAME, C::NAME),
)?;
relay_account_reward_metric.register_and_spawn(&metrics.registry)?;
}
}
}

Ok(())
}

/// Adapter for `FloatStorageValueMetric` to decode account free balance.
#[derive(Clone, Debug)]
struct FreeAccountBalance<C> {
struct AccountBalanceFromAccountInfo<C> {
token_decimals: u32,
_phantom: PhantomData<C>,
}

impl<C> FloatStorageValue for FreeAccountBalance<C>
impl<C> FloatStorageValue for AccountBalanceFromAccountInfo<C>
where
C: Chain,
BalanceOf<C>: Into<u128>,
Expand All @@ -110,6 +131,34 @@ where
}
}

/// Adapter for `FloatStorageValueMetric` to decode account free balance.
#[derive(Clone, Debug)]
struct AccountBalance<C> {
token_decimals: u32,
_phantom: PhantomData<C>,
}

impl<C> FloatStorageValue for AccountBalance<C>
where
C: Chain,
BalanceOf<C>: Into<u128>,
{
type Value = FixedU128;

fn decode(
&self,
maybe_raw_value: Option<StorageData>,
) -> Result<Option<Self::Value>, SubstrateError> {
maybe_raw_value
.map(|raw_value| {
BalanceOf::<C>::decode(&mut &raw_value.0[..])
.map_err(SubstrateError::ResponseParseFailed)
.map(|balance| convert_to_token_balance(balance.into(), self.token_decimals))
})
.transpose()
}
}

/// Convert from raw `u128` balance (nominated in smallest chain token units) to the float regular
/// tokens value.
fn convert_to_token_balance(balance: u128, token_decimals: u32) -> FixedU128 {
Expand Down

0 comments on commit 00846ab

Please sign in to comment.