Skip to content

Commit

Permalink
Benchmarks for pallet-xcm-bridge-hub-router
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Nov 7, 2024
1 parent 3f86dfd commit 07b4c52
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 188 deletions.
72 changes: 51 additions & 21 deletions bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,73 @@

#![cfg(feature = "runtime-benchmarks")]

use crate::{DeliveryFeeFactor, MINIMAL_DELIVERY_FEE_FACTOR};
use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError};
use frame_support::traits::{Get, Hooks};
use sp_runtime::traits::Zero;
use crate::{BridgeState, ResolveBridgeId, Bridges, Call, MINIMAL_DELIVERY_FEE_FACTOR};
use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError, BenchmarkResult};
use frame_support::traits::{UnfilteredDispatchable, EnsureOriginWithArg, Hooks};
use sp_runtime::{Saturating, traits::Zero};
use xcm::prelude::*;

/// Pallet we're benchmarking here.
pub struct Pallet<T: Config<I>, I: 'static = ()>(crate::Pallet<T, I>);

/// Trait that must be implemented by runtime to be able to benchmark pallet properly.
pub trait Config<I: 'static>: crate::Config<I> {
/// Fill up queue so it becomes congested.
fn make_congested();

// /// Fill up queue so it becomes congested.
// fn make_congested();
//
/// Returns destination which is valid for this router instance.
/// (Needs to pass `T::Bridges`)
/// Make sure that `SendXcm` will pass.
fn ensure_bridged_target_destination() -> Result<Location, BenchmarkError> {
Ok(Location::new(
Self::UniversalLocation::get().len() as u8,
[GlobalConsensus(Self::BridgedNetworkId::get().unwrap())],
))
}
fn ensure_bridged_target_destination() -> Result<Location, BenchmarkError>;
}

benchmarks_instance_pallet! {
on_initialize_when_non_congested {
DeliveryFeeFactor::<T, I>::put(MINIMAL_DELIVERY_FEE_FACTOR + MINIMAL_DELIVERY_FEE_FACTOR);
on_initialize_when_bridge_state_removed {
let bridge_id = T::BridgeIdResolver::resolve_for_dest(&T::ensure_bridged_target_destination()?)
.ok_or(BenchmarkError::Weightless)?;
// uncongested and less than a minimal factor is removed
Bridges::<T, I>::insert(&bridge_id, BridgeState {
delivery_fee_factor: 0.into(),
is_congested: false,
});
assert!(Bridges::<T, I>::get(&bridge_id).is_some());
}: {
crate::Pallet::<T, I>::on_initialize(Zero::zero())
} verify {
assert!(Bridges::<T, I>::get(bridge_id).is_none());
}

on_initialize_when_congested {
DeliveryFeeFactor::<T, I>::put(MINIMAL_DELIVERY_FEE_FACTOR + MINIMAL_DELIVERY_FEE_FACTOR);
let _ = T::ensure_bridged_target_destination()?;
T::make_congested();
on_initialize_when_bridge_state_updated {
let bridge_id = T::BridgeIdResolver::resolve_for_dest(&T::ensure_bridged_target_destination()?)
.ok_or(BenchmarkError::Weightless)?;
// uncongested and higher than a minimal factor is decreased
let old_delivery_fee_factor = MINIMAL_DELIVERY_FEE_FACTOR.saturating_mul(1000.into());
Bridges::<T, I>::insert(&bridge_id, BridgeState {
delivery_fee_factor: old_delivery_fee_factor,
is_congested: false,
});
assert!(Bridges::<T, I>::get(&bridge_id).is_some());
}: {
crate::Pallet::<T, I>::on_initialize(Zero::zero())
} verify {
assert!(Bridges::<T, I>::get(bridge_id).unwrap().delivery_fee_factor < old_delivery_fee_factor);
}

report_bridge_status {
let bridge_id = T::BridgeIdResolver::resolve_for_dest(&T::ensure_bridged_target_destination()?)
.ok_or(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?;
let origin: T::RuntimeOrigin = T::BridgeHubOrigin::try_successful_origin(&bridge_id).map_err(|_| BenchmarkError::Weightless)?;
let is_congested = true;

let call = Call::<T, I>::report_bridge_status { bridge_id: bridge_id.clone(), is_congested };
}: { call.dispatch_bypass_filter(origin)? }
verify {
assert_eq!(
Bridges::<T, I>::get(&bridge_id),
Some(BridgeState {
delivery_fee_factor: MINIMAL_DELIVERY_FEE_FACTOR,
is_congested,
})
);
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
}
7 changes: 7 additions & 0 deletions bridges/modules/xcm-bridge-hub-router/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,10 @@ pub(crate) fn get_bridge_state_for<T: pallet_xcm_bridge_hub_router::Config<I>, I
let bridge_id = <T::BridgeIdResolver as ResolveBridgeId>::resolve_for_dest(dest).unwrap();
pallet_xcm_bridge_hub_router::Bridges::<T, I>::get(bridge_id)
}

#[cfg(feature = "runtime-benchmarks")]
impl crate::benchmarking::Config<()> for TestRuntime {
fn ensure_bridged_target_destination() -> Result<Location, frame_benchmarking::BenchmarkError> {
Ok(Location::new(2, [GlobalConsensus(BridgedNetworkId::get())]))
}
}
59 changes: 0 additions & 59 deletions bridges/modules/xcm-bridge-hub-router/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ use sp_std::marker::PhantomData;
pub trait WeightInfo {
fn on_initialize_when_bridge_state_removed() -> Weight;
fn on_initialize_when_bridge_state_updated() -> Weight;
fn on_initialize_when_non_congested() -> Weight;
fn on_initialize_when_congested() -> Weight;
fn report_bridge_status() -> Weight;
}

Expand All @@ -69,32 +67,6 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
fn on_initialize_when_bridge_state_updated() -> Weight {
RocksDbWeight::get().writes(1)
}
///
/// Storage: `XcmBridgeHubRouter::DeliveryFeeFactor` (r:1 w:1)
///
/// Proof: `XcmBridgeHubRouter::DeliveryFeeFactor` (`max_values`: Some(1), `max_size`: Some(16),
/// added: 511, mode: `MaxEncodedLen`)
fn on_initialize_when_non_congested() -> Weight {
// Proof Size summary in bytes:
// Measured: `52`
// Estimated: `3517`
// Minimum execution time: 11_141 nanoseconds.
Weight::from_parts(11_339_000, 3517)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: UNKNOWN KEY `0x456d756c617465645369626c696e6758636d704368616e6e656c2e436f6e6765`
/// (r:1 w:0)
///
/// Proof: UNKNOWN KEY `0x456d756c617465645369626c696e6758636d704368616e6e656c2e436f6e6765` (r:1
/// w:0)
fn on_initialize_when_congested() -> Weight {
// Proof Size summary in bytes:
// Measured: `82`
// Estimated: `3547`
// Minimum execution time: 4_239 nanoseconds.
Weight::from_parts(4_383_000, 3547).saturating_add(T::DbWeight::get().reads(1_u64))
}
/// Storage: `XcmBridgeHubRouter::Bridge` (r:1 w:1)
///
/// Proof: `XcmBridgeHubRouter::Bridge` (`max_values`: Some(1), `max_size`: Some(17), added:
Expand All @@ -120,37 +92,6 @@ impl WeightInfo for () {
RocksDbWeight::get().writes(1)
}

/// Storage: UNKNOWN KEY `0x456d756c617465645369626c696e6758636d704368616e6e656c2e436f6e6765`
/// (r:1 w:0)
///
/// Proof: UNKNOWN KEY `0x456d756c617465645369626c696e6758636d704368616e6e656c2e436f6e6765` (r:1
/// w:0)
///
/// Storage: `XcmBridgeHubRouter::DeliveryFeeFactor` (r:1 w:1)
///
/// Proof: `XcmBridgeHubRouter::DeliveryFeeFactor` (`max_values`: Some(1), `max_size`: Some(16),
/// added: 511, mode: `MaxEncodedLen`)
fn on_initialize_when_non_congested() -> Weight {
// Proof Size summary in bytes:
// Measured: `52`
// Estimated: `3517`
// Minimum execution time: 11_141 nanoseconds.
Weight::from_parts(11_339_000, 3517)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: UNKNOWN KEY `0x456d756c617465645369626c696e6758636d704368616e6e656c2e436f6e6765`
/// (r:1 w:0)
///
/// Proof: UNKNOWN KEY `0x456d756c617465645369626c696e6758636d704368616e6e656c2e436f6e6765` (r:1
/// w:0)
fn on_initialize_when_congested() -> Weight {
// Proof Size summary in bytes:
// Measured: `82`
// Estimated: `3547`
// Minimum execution time: 4_239 nanoseconds.
Weight::from_parts(4_383_000, 3547).saturating_add(RocksDbWeight::get().reads(1_u64))
}
/// Storage: `XcmBridgeHubRouter::Bridge` (r:1 w:1)
///
/// Proof: `XcmBridgeHubRouter::Bridge` (`max_values`: Some(1), `max_size`: Some(17), added:
Expand Down
27 changes: 2 additions & 25 deletions cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
#[cfg(feature = "runtime-benchmarks")]
use xcm::latest::prelude::{
Asset, Assets as XcmAssets, Fungible, Here, InteriorLocation, Junction, Junction::*, Location,
NetworkId, NonFungible, Parent, ParentThen, Response, XCM_VERSION,
NetworkId, NonFungible, Parent, ParentThen, Response,
};
use xcm::{
latest::prelude::{AssetId, BodyId},
Expand Down Expand Up @@ -1710,31 +1710,8 @@ impl_runtime_apis! {
}

impl XcmBridgeHubRouterConfig<ToWestendXcmRouterInstance> for Runtime {
fn make_congested() {
cumulus_pallet_xcmp_queue::bridging::suspend_channel_for_benchmarks::<Runtime>(
xcm_config::bridging::SiblingBridgeHubParaId::get().into()
);
}
fn ensure_bridged_target_destination() -> Result<Location, BenchmarkError> {
ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests(
xcm_config::bridging::SiblingBridgeHubParaId::get().into()
);
let bridged_asset_hub = xcm_config::bridging::to_westend::AssetHubWestend::get();
let _ = PolkadotXcm::force_xcm_version(
RuntimeOrigin::root(),
alloc::boxed::Box::new(bridged_asset_hub.clone()),
XCM_VERSION,
).map_err(|e| {
log::error!(
"Failed to dispatch `force_xcm_version({:?}, {:?}, {:?})`, error: {:?}",
RuntimeOrigin::root(),
bridged_asset_hub,
XCM_VERSION,
e
);
BenchmarkError::Stop("XcmVersion was not stored!")
})?;
Ok(bridged_asset_hub)
Ok(xcm_config::bridging::to_westend::AssetHubWestend::get())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,7 @@ impl<T: frame_system::Config> pallet_xcm_bridge_hub_router::WeightInfo for Weigh
fn on_initialize_when_bridge_state_updated() -> Weight {
RocksDbWeight::get().writes(1)
}
/// Storage: `XcmpQueue::InboundXcmpSuspended` (r:1 w:0)
/// Proof: `XcmpQueue::InboundXcmpSuspended` (`max_values`: Some(1), `max_size`: Some(4002), added: 4497, mode: `MaxEncodedLen`)
/// Storage: `XcmpQueue::OutboundXcmpStatus` (r:1 w:0)
/// Proof: `XcmpQueue::OutboundXcmpStatus` (`max_values`: Some(1), `max_size`: Some(1282), added: 1777, mode: `MaxEncodedLen`)
/// Storage: `ToWestendXcmRouter::DeliveryFeeFactor` (r:1 w:1)
/// Proof: `ToWestendXcmRouter::DeliveryFeeFactor` (`max_values`: Some(1), `max_size`: Some(16), added: 511, mode: `MaxEncodedLen`)
fn on_initialize_when_non_congested() -> Weight {
// Proof Size summary in bytes:
// Measured: `153`
// Estimated: `5487`
// Minimum execution time: 12_993_000 picoseconds.
Weight::from_parts(13_428_000, 0)
.saturating_add(Weight::from_parts(0, 5487))
.saturating_add(T::DbWeight::get().reads(3))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `XcmpQueue::InboundXcmpSuspended` (r:1 w:0)
/// Proof: `XcmpQueue::InboundXcmpSuspended` (`max_values`: Some(1), `max_size`: Some(4002), added: 4497, mode: `MaxEncodedLen`)
/// Storage: `XcmpQueue::OutboundXcmpStatus` (r:1 w:0)
/// Proof: `XcmpQueue::OutboundXcmpStatus` (`max_values`: Some(1), `max_size`: Some(1282), added: 1777, mode: `MaxEncodedLen`)
fn on_initialize_when_congested() -> Weight {
// Proof Size summary in bytes:
// Measured: `144`
// Estimated: `5487`
// Minimum execution time: 6_305_000 picoseconds.
Weight::from_parts(6_536_000, 0)
.saturating_add(Weight::from_parts(0, 5487))
.saturating_add(T::DbWeight::get().reads(2))
}

// TODO: FAIL-CI
fn report_bridge_status() -> Weight {
// Proof Size summary in bytes:
Expand Down
27 changes: 2 additions & 25 deletions cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ use xcm::{
#[cfg(feature = "runtime-benchmarks")]
use xcm::latest::prelude::{
Asset, Assets as XcmAssets, Fungible, Here, InteriorLocation, Junction, Junction::*, Location,
NetworkId, NonFungible, Parent, ParentThen, Response, XCM_VERSION,
NetworkId, NonFungible, Parent, ParentThen, Response,
};

use xcm_runtime_apis::{
Expand Down Expand Up @@ -1891,31 +1891,8 @@ impl_runtime_apis! {
};

impl XcmBridgeHubRouterConfig<ToRococoXcmRouterInstance> for Runtime {
fn make_congested() {
cumulus_pallet_xcmp_queue::bridging::suspend_channel_for_benchmarks::<Runtime>(
xcm_config::bridging::SiblingBridgeHubParaId::get().into()
);
}
fn ensure_bridged_target_destination() -> Result<Location, BenchmarkError> {
ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests(
xcm_config::bridging::SiblingBridgeHubParaId::get().into()
);
let bridged_asset_hub = xcm_config::bridging::to_rococo::AssetHubRococo::get();
let _ = PolkadotXcm::force_xcm_version(
RuntimeOrigin::root(),
alloc::boxed::Box::new(bridged_asset_hub.clone()),
XCM_VERSION,
).map_err(|e| {
log::error!(
"Failed to dispatch `force_xcm_version({:?}, {:?}, {:?})`, error: {:?}",
RuntimeOrigin::root(),
bridged_asset_hub,
XCM_VERSION,
e
);
BenchmarkError::Stop("XcmVersion was not stored!")
})?;
Ok(bridged_asset_hub)
Ok(xcm_config::bridging::to_rococo::AssetHubRococo::get())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,7 @@ impl<T: frame_system::Config> pallet_xcm_bridge_hub_router::WeightInfo for Weigh
fn on_initialize_when_bridge_state_updated() -> Weight {
RocksDbWeight::get().writes(1)
}
/// Storage: `XcmpQueue::InboundXcmpSuspended` (r:1 w:0)
/// Proof: `XcmpQueue::InboundXcmpSuspended` (`max_values`: Some(1), `max_size`: Some(4002), added: 4497, mode: `MaxEncodedLen`)
/// Storage: `XcmpQueue::OutboundXcmpStatus` (r:1 w:0)
/// Proof: `XcmpQueue::OutboundXcmpStatus` (`max_values`: Some(1), `max_size`: Some(1282), added: 1777, mode: `MaxEncodedLen`)
/// Storage: `ToRococoXcmRouter::DeliveryFeeFactor` (r:1 w:1)
/// Proof: `ToRococoXcmRouter::DeliveryFeeFactor` (`max_values`: Some(1), `max_size`: Some(16), added: 511, mode: `MaxEncodedLen`)
fn on_initialize_when_non_congested() -> Weight {
// Proof Size summary in bytes:
// Measured: `225`
// Estimated: `5487`
// Minimum execution time: 13_483_000 picoseconds.
Weight::from_parts(13_862_000, 0)
.saturating_add(Weight::from_parts(0, 5487))
.saturating_add(T::DbWeight::get().reads(3))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `XcmpQueue::InboundXcmpSuspended` (r:1 w:0)
/// Proof: `XcmpQueue::InboundXcmpSuspended` (`max_values`: Some(1), `max_size`: Some(4002), added: 4497, mode: `MaxEncodedLen`)
/// Storage: `XcmpQueue::OutboundXcmpStatus` (r:1 w:0)
/// Proof: `XcmpQueue::OutboundXcmpStatus` (`max_values`: Some(1), `max_size`: Some(1282), added: 1777, mode: `MaxEncodedLen`)
fn on_initialize_when_congested() -> Weight {
// Proof Size summary in bytes:
// Measured: `111`
// Estimated: `5487`
// Minimum execution time: 5_078_000 picoseconds.
Weight::from_parts(5_233_000, 0)
.saturating_add(Weight::from_parts(0, 5487))
.saturating_add(T::DbWeight::get().reads(2))
}

// TODO: FAIL-CI
fn report_bridge_status() -> Weight {
// Proof Size summary in bytes:
Expand Down

0 comments on commit 07b4c52

Please sign in to comment.