diff --git a/bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs b/bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs index cc11533c86bb..e3ed02f14ad9 100644 --- a/bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs +++ b/bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs @@ -19,8 +19,8 @@ #![cfg(feature = "runtime-benchmarks")] use crate::{BridgeState, Bridges, Call, ResolveBridgeId, MINIMAL_DELIVERY_FEE_FACTOR}; -use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError, BenchmarkResult}; -use frame_support::traits::{EnsureOriginWithArg, Hooks, UnfilteredDispatchable}; +use frame_benchmarking::v2::*; +use frame_support::traits::{EnsureOriginWithArg, Hooks}; use sp_runtime::{traits::Zero, Saturating}; use xcm::prelude::*; @@ -29,32 +29,43 @@ pub struct Pallet, I: 'static = ()>(crate::Pallet); /// Trait that must be implemented by runtime to be able to benchmark pallet properly. pub trait Config: crate::Config { - // /// Fill up queue so it becomes congested. - // fn make_congested(); - // /// Returns destination which is valid for this router instance. fn ensure_bridged_target_destination() -> Result; + /// Returns valid origin for `report_bridge_status` (if `T::BridgeHubOrigin` is supported). + fn report_bridge_status_origin() -> Option; } -benchmarks_instance_pallet! { - on_initialize_when_bridge_state_removed { +#[instance_benchmarks] +mod benchmarks { + use super::*; + + #[benchmark] + fn on_initialize_when_bridge_state_removed() -> Result<(), BenchmarkError> { 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::::insert(&bridge_id, BridgeState { delivery_fee_factor: 0.into(), is_congested: false, }); assert!(Bridges::::get(&bridge_id).is_some()); - }: { - crate::Pallet::::on_initialize(Zero::zero()) - } verify { + + #[block] + { + let _ = crate::Pallet::::on_initialize(Zero::zero()); + } + assert!(Bridges::::get(bridge_id).is_none()); + + Ok(()) } - on_initialize_when_bridge_state_updated { + #[benchmark] + fn on_initialize_when_bridge_state_updated() -> Result<(), BenchmarkError> { 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::::insert(&bridge_id, BridgeState { @@ -62,21 +73,29 @@ benchmarks_instance_pallet! { is_congested: false, }); assert!(Bridges::::get(&bridge_id).is_some()); - }: { - crate::Pallet::::on_initialize(Zero::zero()) - } verify { + + #[block] + { + let _ = crate::Pallet::::on_initialize(Zero::zero()); + } + assert!(Bridges::::get(bridge_id).unwrap().delivery_fee_factor < old_delivery_fee_factor); + Ok(()) } - report_bridge_status { + #[benchmark] + fn report_bridge_status() -> Result<(), BenchmarkError> { 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 origin = T::report_bridge_status_origin() + .ok_or(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; + let _ = T::BridgeHubOrigin::try_origin(origin.clone(), &bridge_id) + .map_err(|_| BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; let is_congested = true; - let call = Call::::report_bridge_status { bridge_id: bridge_id.clone(), is_congested }; - }: { call.dispatch_bypass_filter(origin)? } - verify { + #[extrinsic_call] + report_bridge_status(origin as T::RuntimeOrigin, bridge_id.clone(), is_congested); + assert_eq!( Bridges::::get(&bridge_id), Some(BridgeState { @@ -84,7 +103,8 @@ benchmarks_instance_pallet! { is_congested, }) ); + Ok(()) } - impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime) + impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime); } diff --git a/bridges/modules/xcm-bridge-hub-router/src/mock.rs b/bridges/modules/xcm-bridge-hub-router/src/mock.rs index e7c53579775d..16dd9b09629f 100644 --- a/bridges/modules/xcm-bridge-hub-router/src/mock.rs +++ b/bridges/modules/xcm-bridge-hub-router/src/mock.rs @@ -225,4 +225,7 @@ impl crate::benchmarking::Config<()> for TestRuntime { fn ensure_bridged_target_destination() -> Result { Ok(Location::new(2, [GlobalConsensus(BridgedNetworkId::get())])) } + fn report_bridge_status_origin() -> Option { + Some(RuntimeOrigin::root()) + } } diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs index 7b8eb43b7848..342f1a506e35 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs @@ -1713,6 +1713,9 @@ impl_runtime_apis! { fn ensure_bridged_target_destination() -> Result { Ok(xcm_config::bridging::to_westend::AssetHubWestend::get()) } + fn report_bridge_status_origin() -> Option { + Some(pallet_xcm::Origin::Xcm(xcm_config::bridging::SiblingBridgeHub::get()).into()) + } } use xcm_config::{TokenLocation, MaxAssetsIntoHolding}; diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index 7da24dc02495..1bc55901ea42 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -1894,6 +1894,9 @@ impl_runtime_apis! { fn ensure_bridged_target_destination() -> Result { Ok(xcm_config::bridging::to_rococo::AssetHubRococo::get()) } + fn report_bridge_status_origin() -> Option { + Some(pallet_xcm::Origin::Xcm(xcm_config::bridging::SiblingBridgeHub::get()).into()) + } } use xcm_config::{MaxAssetsIntoHolding, WestendLocation};