diff --git a/bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs b/bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs index 0b7061db5d6c..a44d478a5b7a 100644 --- a/bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs +++ b/bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs @@ -40,7 +40,7 @@ mod benchmarks { use super::*; #[benchmark] - fn on_initialize_when_bridge_state_removed() -> Result<(), BenchmarkError> { + fn on_idle_when_bridge_state_removed() -> Result<(), BenchmarkError> { let bridge_id = T::BridgeIdResolver::resolve_for_dest(&T::ensure_bridged_target_destination()?) .ok_or(BenchmarkError::Weightless)?; @@ -54,7 +54,7 @@ mod benchmarks { #[block] { - let _ = crate::Pallet::::on_initialize(Zero::zero()); + let _ = crate::Pallet::::on_idle(Zero::zero(), Weight::MAX); } assert!(Bridges::::get(bridge_id).is_none()); @@ -63,7 +63,7 @@ mod benchmarks { } #[benchmark] - fn on_initialize_when_bridge_state_updated() -> Result<(), BenchmarkError> { + fn on_indle_when_bridge_state_updated() -> Result<(), BenchmarkError> { let bridge_id = T::BridgeIdResolver::resolve_for_dest(&T::ensure_bridged_target_destination()?) .ok_or(BenchmarkError::Weightless)?; @@ -78,7 +78,7 @@ mod benchmarks { #[block] { - let _ = crate::Pallet::::on_initialize(Zero::zero()); + let _ = crate::Pallet::::on_idle(Zero::zero(), Weight::MAX); } assert!( diff --git a/bridges/modules/xcm-bridge-hub-router/src/lib.rs b/bridges/modules/xcm-bridge-hub-router/src/lib.rs index 26db874bb8f6..e2962341a46c 100644 --- a/bridges/modules/xcm-bridge-hub-router/src/lib.rs +++ b/bridges/modules/xcm-bridge-hub-router/src/lib.rs @@ -106,6 +106,7 @@ pub const LOG_TARGET: &str = "runtime::bridge-xcm-router"; pub mod pallet { use super::*; use frame_support::pallet_prelude::*; + use frame_support::weights::WeightMeter; use frame_system::pallet_prelude::*; /// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`]. @@ -182,14 +183,14 @@ pub mod pallet { #[pallet::hooks] impl, I: 'static> Hooks> for Pallet { - fn on_initialize(_n: BlockNumberFor) -> Weight { - let mut weight_used = Weight::zero(); + fn on_idle(_n: BlockNumberFor, remaining_weight: Weight) -> Weight { + let mut meter = WeightMeter::with_limit(remaining_weight); - // Iterate all uncongested bridges + // Iterate all congested bridges let mut bridges_to_update = Vec::new(); let mut bridges_to_remove = Vec::new(); for (bridge_id, mut bridge_state) in Bridges::::iter() { - weight_used.saturating_accrue(T::DbWeight::get().reads(1)); + meter.consume(T::DbWeight::get().reads(1)); // If no longer congested, we can start decreasing the fee factor. if !bridge_state.is_congested { @@ -197,8 +198,14 @@ pub mod pallet { let new_factor = previous_factor / EXPONENTIAL_FEE_BASE; if new_factor >= MINIMAL_DELIVERY_FEE_FACTOR { bridge_state.delivery_fee_factor = new_factor; + if meter.try_consume(T::WeightInfo::on_idle_when_bridge_state_updated()).is_err() { + break; + } bridges_to_update.push((bridge_id, previous_factor, bridge_state)); } else { + if meter.try_consume(T::WeightInfo::on_idle_when_bridge_state_removed()).is_err() { + break; + } bridges_to_remove.push((bridge_id, previous_factor)); } } @@ -217,8 +224,6 @@ pub mod pallet { new_value: 0.into(), bridge_id, }); - weight_used - .saturating_accrue(T::WeightInfo::on_initialize_when_bridge_state_removed()); } // update for (bridge_id, previous_value, bridge_state) in bridges_to_update.into_iter() { @@ -236,11 +241,9 @@ pub mod pallet { new_value, bridge_id, }); - weight_used - .saturating_accrue(T::WeightInfo::on_initialize_when_bridge_state_updated()); } - weight_used + meter.consumed() } } @@ -539,6 +542,7 @@ mod tests { run_test(|| { let dest = Location::new(2, [GlobalConsensus(BridgedNetworkId::get())]); let initial_fee_factor = FixedU128::from_rational(125, 100); + let mut remaining_weight = Weight::MAX; // make bridge uncongested + update fee factor let bridge_id = set_bridge_state_for::( @@ -550,8 +554,14 @@ mod tests { let mut last_delivery_fee_factor = initial_fee_factor; while let Some(bridge_state) = get_bridge_state_for::(&dest) { last_delivery_fee_factor = bridge_state.delivery_fee_factor; - XcmBridgeHubRouter::on_initialize(One::one()); + remaining_weight = XcmBridgeHubRouter::on_idle(One::one(), remaining_weight.clone()); + + // avoid infinite loops (decreasing is expected) + if let Some(bridge_state) = get_bridge_state_for::(&dest) { + assert!(bridge_state.delivery_fee_factor < last_delivery_fee_factor); + } } + assert!(remaining_weight.all_lt(Weight::MAX)); // check emitted event // (first one for updating) diff --git a/bridges/modules/xcm-bridge-hub-router/src/weights.rs b/bridges/modules/xcm-bridge-hub-router/src/weights.rs index 788b4e48c7aa..a4eef004cb4e 100644 --- a/bridges/modules/xcm-bridge-hub-router/src/weights.rs +++ b/bridges/modules/xcm-bridge-hub-router/src/weights.rs @@ -50,8 +50,8 @@ use sp_std::marker::PhantomData; /// Weight functions needed for pallet_xcm_bridge_hub_router. pub trait WeightInfo { - fn on_initialize_when_bridge_state_removed() -> Weight; - fn on_initialize_when_bridge_state_updated() -> Weight; + fn on_idle_when_bridge_state_removed() -> Weight; + fn on_idle_when_bridge_state_updated() -> Weight; fn report_bridge_status() -> Weight; } @@ -60,7 +60,7 @@ impl WeightInfo for () { /// Storage: `ToUnknownXcmRouter::Bridges` (r:2 w:1) /// Proof: `ToUnknownXcmRouter::Bridges` (`max_values`: None, `max_size`: Some(65), added: 2540, /// mode: `MaxEncodedLen`) - fn on_initialize_when_bridge_state_removed() -> Weight { + fn on_idle_when_bridge_state_removed() -> Weight { // Proof Size summary in bytes: // Measured: `204` // Estimated: `6070` @@ -73,7 +73,7 @@ impl WeightInfo for () { /// Storage: `ToUnknownXcmRouter::Bridges` (r:2 w:1) /// Proof: `ToUnknownXcmRouter::Bridges` (`max_values`: None, `max_size`: Some(65), added: 2540, /// mode: `MaxEncodedLen`) - fn on_initialize_when_bridge_state_updated() -> Weight { + fn on_idle_when_bridge_state_updated() -> Weight { // Proof Size summary in bytes: // Measured: `204` // Estimated: `6070` diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/weights/pallet_xcm_bridge_hub_router.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/weights/pallet_xcm_bridge_hub_router.rs index 3ef17f80b9ea..3a5f55864c08 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/weights/pallet_xcm_bridge_hub_router.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/weights/pallet_xcm_bridge_hub_router.rs @@ -50,7 +50,7 @@ pub struct WeightInfo(PhantomData); impl pallet_xcm_bridge_hub_router::WeightInfo for WeightInfo { /// Storage: `ToWestendXcmRouter::Bridges` (r:2 w:1) /// Proof: `ToWestendXcmRouter::Bridges` (`max_values`: None, `max_size`: Some(65), added: 2540, mode: `MaxEncodedLen`) - fn on_initialize_when_bridge_state_removed() -> Weight { + fn on_idle_when_bridge_state_removed() -> Weight { // Proof Size summary in bytes: // Measured: `204` // Estimated: `6070` @@ -62,7 +62,7 @@ impl pallet_xcm_bridge_hub_router::WeightInfo for Weigh } /// Storage: `ToWestendXcmRouter::Bridges` (r:2 w:1) /// Proof: `ToWestendXcmRouter::Bridges` (`max_values`: None, `max_size`: Some(65), added: 2540, mode: `MaxEncodedLen`) - fn on_initialize_when_bridge_state_updated() -> Weight { + fn on_idle_when_bridge_state_updated() -> Weight { // Proof Size summary in bytes: // Measured: `204` // Estimated: `6070` diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/weights/pallet_xcm_bridge_hub_router.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/weights/pallet_xcm_bridge_hub_router.rs index 142701e0b2bd..5089e21b91cb 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/weights/pallet_xcm_bridge_hub_router.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/weights/pallet_xcm_bridge_hub_router.rs @@ -50,7 +50,7 @@ pub struct WeightInfo(PhantomData); impl pallet_xcm_bridge_hub_router::WeightInfo for WeightInfo { /// Storage: `ToRococoXcmRouter::Bridges` (r:2 w:1) /// Proof: `ToRococoXcmRouter::Bridges` (`max_values`: None, `max_size`: Some(65), added: 2540, mode: `MaxEncodedLen`) - fn on_initialize_when_bridge_state_removed() -> Weight { + fn on_idle_when_bridge_state_removed() -> Weight { // Proof Size summary in bytes: // Measured: `204` // Estimated: `6070` @@ -62,7 +62,7 @@ impl pallet_xcm_bridge_hub_router::WeightInfo for Weigh } /// Storage: `ToRococoXcmRouter::Bridges` (r:2 w:1) /// Proof: `ToRococoXcmRouter::Bridges` (`max_values`: None, `max_size`: Some(65), added: 2540, mode: `MaxEncodedLen`) - fn on_initialize_when_bridge_state_updated() -> Weight { + fn on_idle_when_bridge_state_updated() -> Weight { // Proof Size summary in bytes: // Measured: `204` // Estimated: `6070`