Skip to content

Commit

Permalink
Use constant for min extra computation time in on_idle
Browse files Browse the repository at this point in the history
  • Loading branch information
sea212 committed Feb 6, 2024
1 parent a771cb2 commit e56e18e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
9 changes: 5 additions & 4 deletions zrml/asset-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Config, A>:
Create<T::AccountId, AssetId = A, Balance = T::Balance>
Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -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,
Expand Down
25 changes: 15 additions & 10 deletions zrml/asset-router/src/tests/managed_destroy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -134,7 +134,7 @@ fn destroys_assets_fully_works_properly() {
assert_ok!(managed_destroy_multi_transactional(assets.clone()));
assert_eq!(crate::DestroyAssets::<Runtime>::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));
Expand All @@ -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);
})
}
Expand All @@ -162,17 +163,19 @@ fn destroys_assets_partially_properly() {
assert_ok!(managed_destroy_multi_transactional(assets.clone()));
assert_eq!(crate::DestroyAssets::<Runtime>::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::<Runtime>::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::<Runtime>::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::<Runtime>::get().len(), 1);

Expand All @@ -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::<Runtime>::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));
Expand Down Expand Up @@ -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::<Runtime>::get().len(), 0);
assert_eq!(crate::IndestructibleAssets::<Runtime>::get().len(), 1);
Expand All @@ -264,8 +268,9 @@ fn does_not_execute_on_insufficient_weight() {
assert_eq!(crate::DestroyAssets::<Runtime>::get().len(), 1);

let db_weight: RuntimeDbWeight = <Runtime as frame_system::Config>::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::<Runtime>::get().len(), 1);
Expand Down

0 comments on commit e56e18e

Please sign in to comment.