From e56e18e041d6b453a4c7a63e869489287d2139a5 Mon Sep 17 00:00:00 2001 From: Harald Heckmann Date: Tue, 6 Feb 2024 10:50:38 +0100 Subject: [PATCH] Use constant for min extra computation time in on_idle --- zrml/asset-router/src/lib.rs | 9 ++++--- .../asset-router/src/tests/managed_destroy.rs | 25 +++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/zrml/asset-router/src/lib.rs b/zrml/asset-router/src/lib.rs index cfa1e7e6d..d93180614 100644 --- a/zrml/asset-router/src/lib.rs +++ b/zrml/asset-router/src/lib.rs @@ -73,6 +73,8 @@ pub mod pallet { const MAX_ASSET_DESTRUCTIONS_PER_BLOCK: u8 = 128; pub(crate) const MAX_ASSETS_IN_DESTRUCTION: u32 = 2048; const MAX_INDESTRUCTIBLE_ASSETS: u32 = 256; + // 1 ms minimum computation time. + pub(crate) const MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT: u64 = 1_000_000_000; pub trait AssetTraits: Create @@ -229,9 +231,8 @@ pub mod pallet { max_proof_size_destructibles.saturating_add(max_proof_size_indestructibles); let max_extra_weight = Weight::from_parts( - // 1ms extra execution time - 1_000_000_000, - // Maximum proof size assuming writes on full storage + MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT, + // Maximum proof size assuming writes on full storage. max_proof_size_total, ); @@ -300,7 +301,7 @@ pub mod pallet { safety_counter_inner = safety_counter_inner.saturating_add(1); } - // Only reachable if there is an error in the destruction state machine + // Only reachable if there is an error in the destruction state machine. #[cfg(test)] assert!( safety_counter_inner != safety_counter_inner_max, diff --git a/zrml/asset-router/src/tests/managed_destroy.rs b/zrml/asset-router/src/tests/managed_destroy.rs index 6605ba813..f070e18bd 100644 --- a/zrml/asset-router/src/tests/managed_destroy.rs +++ b/zrml/asset-router/src/tests/managed_destroy.rs @@ -18,7 +18,7 @@ #![cfg(test)] use super::*; -use crate::{AssetInDestruction, Weight}; +use crate::{AssetInDestruction, Weight, MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT}; use frame_support::{ traits::{tokens::fungibles::Inspect, Get}, weights::RuntimeDbWeight, @@ -134,7 +134,7 @@ fn destroys_assets_fully_works_properly() { assert_ok!(managed_destroy_multi_transactional(assets.clone())); assert_eq!(crate::DestroyAssets::::get().len(), 3); - let available_weight = 10_000_000_000.into(); + let available_weight = (2 * MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT).into(); let remaining_weight = AssetRouter::on_idle(0, available_weight); assert!(!AssetRouter::asset_exists(CAMPAIGN_ASSET)); assert!(!AssetRouter::asset_exists(CUSTOM_ASSET)); @@ -144,7 +144,8 @@ fn destroys_assets_fully_works_properly() { let mut consumed_weight = available_weight - 3u64 * 3u64 * DESTROY_WEIGHT; // Consider safety buffer for extra execution time and storage proof size - consumed_weight = consumed_weight.saturating_sub(Weight::from_parts(1_000_000_000, 45_824)); + consumed_weight = consumed_weight + .saturating_sub(Weight::from_parts(MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT, 45_824)); assert_eq!(remaining_weight, consumed_weight); }) } @@ -162,17 +163,19 @@ fn destroys_assets_partially_properly() { assert_ok!(managed_destroy_multi_transactional(assets.clone())); assert_eq!(crate::DestroyAssets::::get().len(), 3); - let mut available_weight: Weight = Weight::from_all(1_000_000_000) + 2u64 * DESTROY_WEIGHT; + let mut available_weight: Weight = + Weight::from_all(MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT) + 2u64 * DESTROY_WEIGHT; // Make on_idle only partially delete the first asset let _ = AssetRouter::on_idle(0, available_weight); assert_eq!(crate::DestroyAssets::::get().len(), 3); // Now delete each asset one by one by supplying exactly the required weight - available_weight = Weight::from_all(1_000_000_000) + DESTROY_WEIGHT; + available_weight = Weight::from_all(MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT) + DESTROY_WEIGHT; let _ = AssetRouter::on_idle(0, available_weight); assert_eq!(crate::DestroyAssets::::get().len(), 2); - available_weight = Weight::from_all(1_000_000_000) + 3u64 * DESTROY_WEIGHT; + available_weight = + Weight::from_all(MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT) + 3u64 * DESTROY_WEIGHT; let _ = AssetRouter::on_idle(0, available_weight); assert_eq!(crate::DestroyAssets::::get().len(), 1); @@ -190,7 +193,7 @@ fn properly_handles_indestructible_assets() { ExtBuilder::default().build().execute_with(|| { let assets_raw = vec![CAMPAIGN_ASSET, CUSTOM_ASSET, MARKET_ASSET]; let mut destroy_assets = crate::DestroyAssets::::get(); - let available_weight = 10_000_000_000.into(); + let available_weight = (4 * MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT).into(); for asset in assets_raw { destroy_assets.force_push(AssetInDestruction::new(asset)); @@ -245,7 +248,8 @@ fn properly_handles_indestructible_assets() { let remaining_weight = AssetRouter::on_idle(0, available_weight); let mut consumed_weight = available_weight - 2u32 * 3u32 * DESTROY_WEIGHT - DESTROY_WEIGHT; // Consider safety buffer for extra execution time and storage proof size - consumed_weight = consumed_weight.saturating_sub(Weight::from_parts(1_000_000_000, 45_824)); + consumed_weight = consumed_weight + .saturating_sub(Weight::from_parts(MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT, 45_824)); assert_eq!(remaining_weight, consumed_weight); assert_eq!(crate::DestroyAssets::::get().len(), 0); assert_eq!(crate::IndestructibleAssets::::get().len(), 1); @@ -264,8 +268,9 @@ fn does_not_execute_on_insufficient_weight() { assert_eq!(crate::DestroyAssets::::get().len(), 1); let db_weight: RuntimeDbWeight = ::DbWeight::get(); - let mut available_weight = - Weight::from_parts(1_000_000_000, 45_824) + db_weight.reads(1) - 1u64.into(); + let mut available_weight = Weight::from_parts(MIN_ON_IDLE_EXTRA_COMPUTATION_WEIGHT, 45_824) + + db_weight.reads(1) + - 1u64.into(); let mut remaining_weight = AssetRouter::on_idle(0, available_weight); assert_eq!(available_weight, remaining_weight); assert_eq!(crate::DestroyAssets::::get().len(), 1);