mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 23:57:56 +00:00
Glutton pallet (#12833)
* Pov-Limit pallet * use Perbill & fixes * fixes * reads & writes * update docs * tests * calculate weight * fmt * benchmark * logic fix * naming fix * caclulate computation weight limit * make the Hasher generic * make the code compile * generate weight * fix on_idle * fix * fix * make reading generic * fix? * fixes * remove warning * fix * hasher fix :D * change value * test * actual weight and expected weight are the same * update * fix * add events * remove useless line * using actual hashing algorithm * better readability * fix nits * Update Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * add migration * hardcode proof_size in weights.rs * format * fixes * Fix weight * docs * fix * Update frame/pov-limit/src/lib.rs Co-authored-by: Bastian Köcher <git@kchr.de> * Update frame/pov-limit/src/lib.rs Co-authored-by: Bastian Köcher <git@kchr.de> * empty on_ilde & update weight.rs * remove migration & fix benchmark * remove migration from migrations * initialize_pallet * use blake2 * Update frame/pov-limit/Cargo.toml Co-authored-by: Bastian Köcher <git@kchr.de> * Update frame/pov-limit/src/lib.rs Co-authored-by: Bastian Köcher <git@kchr.de> * Update frame/pov-limit/Cargo.toml Co-authored-by: Bastian Köcher <git@kchr.de> * rename pallet * make the wasters non-generic * rename to glutton * fix * small fixes & run benchmark * increase left over ref-time in tests * Update frame/glutton/src/lib.rs Co-authored-by: Bastian Köcher <git@kchr.de> * or values * fix * generate weight * Update frame/glutton/Cargo.toml Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * fix nits * Adjustments Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fixes Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * fix * Update weights Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Tweak constants Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update weights Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * remove genesis config * passing tests * More precise results Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * update node-executor test * Calculate number of iterations in advance Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * increase criteria * Final fixes Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update frame/glutton/src/tests.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/glutton/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/glutton/src/lib.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * fix typos * Update frame/glutton/src/lib.rs Co-authored-by: Bastian Köcher <git@kchr.de> * Update frame/glutton/src/lib.rs Co-authored-by: Bastian Köcher <git@kchr.de> * expand/shrink * fmt * Revert "Update frame/glutton/src/lib.rs" This reverts commit 98a237afd27de3deb15ba381871e12f71a9d71b1. Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Use CountedStorageMap Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Add benchmark Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Use manual map counter Something with the R/W count in the benchmarking result did not add up. Need to investigate but for now just using a manual counter. Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Use new template Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Doc+typos Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_glutton * Apply suggestions from code review Co-authored-by: Koute <koute@users.noreply.github.com> * Add minimal README Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_glutton --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Bastian Köcher <git@kchr.de> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: command-bot <> Co-authored-by: Koute <koute@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2023 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Tests for the glutton pallet.
|
||||
|
||||
use super::*;
|
||||
use mock::{new_test_ext, Glutton, RuntimeOrigin, System, Test};
|
||||
|
||||
use frame_support::{assert_err, assert_noop, assert_ok, weights::constants::*};
|
||||
|
||||
#[test]
|
||||
fn initialize_pallet_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(TrashData::<Test>::get(0), None);
|
||||
|
||||
assert_noop!(
|
||||
Glutton::initialize_pallet(RuntimeOrigin::signed(1), 3, None),
|
||||
DispatchError::BadOrigin
|
||||
);
|
||||
assert_noop!(
|
||||
Glutton::initialize_pallet(RuntimeOrigin::none(), 3, None),
|
||||
DispatchError::BadOrigin
|
||||
);
|
||||
|
||||
assert_ok!(Glutton::initialize_pallet(RuntimeOrigin::root(), 2, None));
|
||||
System::assert_last_event(Event::PalletInitialized { reinit: false }.into());
|
||||
assert_err!(
|
||||
Glutton::initialize_pallet(RuntimeOrigin::root(), 2, None),
|
||||
Error::<Test>::AlreadyInitialized
|
||||
);
|
||||
|
||||
assert_eq!(TrashData::<Test>::get(0), Some([0; 1024]));
|
||||
assert_eq!(TrashData::<Test>::get(1), Some([1; 1024]));
|
||||
assert_eq!(TrashData::<Test>::get(2), None);
|
||||
|
||||
assert_eq!(TrashDataCount::<Test>::get(), 2);
|
||||
|
||||
assert_ok!(Glutton::initialize_pallet(RuntimeOrigin::root(), 20, Some(2)));
|
||||
|
||||
assert_eq!(TrashDataCount::<Test>::get(), 20);
|
||||
assert_eq!(TrashData::<Test>::iter_keys().count(), 20);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expand_and_shrink_trash_data_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(TrashDataCount::<Test>::get(), 0);
|
||||
|
||||
assert_ok!(Glutton::initialize_pallet(RuntimeOrigin::root(), 5000, None));
|
||||
assert_eq!(TrashDataCount::<Test>::get(), 5000);
|
||||
assert_eq!(TrashData::<Test>::iter_keys().count(), 5000);
|
||||
|
||||
assert_ok!(Glutton::initialize_pallet(RuntimeOrigin::root(), 8000, Some(5000)));
|
||||
assert_eq!(TrashDataCount::<Test>::get(), 8000);
|
||||
assert_eq!(TrashData::<Test>::iter_keys().count(), 8000);
|
||||
|
||||
assert_ok!(Glutton::initialize_pallet(RuntimeOrigin::root(), 6000, Some(8000)));
|
||||
assert_eq!(TrashDataCount::<Test>::get(), 6000);
|
||||
assert_eq!(TrashData::<Test>::iter_keys().count(), 6000);
|
||||
|
||||
assert_noop!(
|
||||
Glutton::initialize_pallet(RuntimeOrigin::root(), 0, None),
|
||||
Error::<Test>::AlreadyInitialized
|
||||
);
|
||||
assert_ok!(Glutton::initialize_pallet(RuntimeOrigin::root(), 0, Some(6000)));
|
||||
assert_eq!(TrashDataCount::<Test>::get(), 0);
|
||||
assert_eq!(TrashData::<Test>::iter_keys().count(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn setting_compute_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(Compute::<Test>::get(), Perbill::from_percent(0));
|
||||
|
||||
assert_ok!(Glutton::set_compute(RuntimeOrigin::root(), Perbill::from_percent(70)));
|
||||
assert_eq!(Compute::<Test>::get(), Perbill::from_percent(70));
|
||||
System::assert_last_event(
|
||||
Event::ComputationLimitSet { compute: Perbill::from_percent(70) }.into(),
|
||||
);
|
||||
|
||||
assert_noop!(
|
||||
Glutton::set_compute(RuntimeOrigin::signed(1), Perbill::from_percent(30)),
|
||||
DispatchError::BadOrigin
|
||||
);
|
||||
assert_noop!(
|
||||
Glutton::set_compute(RuntimeOrigin::none(), Perbill::from_percent(30)),
|
||||
DispatchError::BadOrigin
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn setting_storage_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(Storage::<Test>::get(), Perbill::from_percent(0));
|
||||
|
||||
assert_ok!(Glutton::set_storage(RuntimeOrigin::root(), Perbill::from_percent(30)));
|
||||
assert_eq!(Storage::<Test>::get(), Perbill::from_percent(30));
|
||||
System::assert_last_event(
|
||||
Event::StorageLimitSet { storage: Perbill::from_percent(30) }.into(),
|
||||
);
|
||||
|
||||
assert_noop!(
|
||||
Glutton::set_storage(RuntimeOrigin::signed(1), Perbill::from_percent(90)),
|
||||
DispatchError::BadOrigin
|
||||
);
|
||||
assert_noop!(
|
||||
Glutton::set_storage(RuntimeOrigin::none(), Perbill::from_percent(90)),
|
||||
DispatchError::BadOrigin
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn on_idle_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Glutton::set_compute(RuntimeOrigin::root(), Perbill::from_percent(100)));
|
||||
assert_ok!(Glutton::set_storage(RuntimeOrigin::root(), Perbill::from_percent(100)));
|
||||
|
||||
Glutton::on_idle(1, Weight::from_ref_time(20_000_000));
|
||||
});
|
||||
}
|
||||
|
||||
/// Check that the expected is close enough to the consumed weight.
|
||||
#[test]
|
||||
fn on_idle_weight_high_proof_is_close_enough_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Glutton::set_compute(RuntimeOrigin::root(), Perbill::from_percent(100)));
|
||||
assert_ok!(Glutton::set_storage(RuntimeOrigin::root(), Perbill::from_percent(100)));
|
||||
|
||||
let should = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND, WEIGHT_PROOF_SIZE_PER_MB * 5);
|
||||
let got = Glutton::on_idle(1, should);
|
||||
assert!(got.all_lte(should), "Consumed too much weight");
|
||||
|
||||
let ratio = Perbill::from_rational(got.proof_size(), should.proof_size());
|
||||
assert!(
|
||||
ratio >= Perbill::from_percent(99),
|
||||
"Too few proof size consumed, was only {:?} of expected",
|
||||
ratio
|
||||
);
|
||||
|
||||
let ratio = Perbill::from_rational(got.ref_time(), should.ref_time());
|
||||
assert!(
|
||||
ratio >= Perbill::from_percent(99),
|
||||
"Too few ref time consumed, was only {:?} of expected",
|
||||
ratio
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn on_idle_weight_low_proof_is_close_enough_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Glutton::set_compute(RuntimeOrigin::root(), Perbill::from_percent(100)));
|
||||
assert_ok!(Glutton::set_storage(RuntimeOrigin::root(), Perbill::from_percent(100)));
|
||||
|
||||
let should = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND, WEIGHT_PROOF_SIZE_PER_KB * 20);
|
||||
let got = Glutton::on_idle(1, should);
|
||||
assert!(got.all_lte(should), "Consumed too much weight");
|
||||
|
||||
let ratio = Perbill::from_rational(got.proof_size(), should.proof_size());
|
||||
// Just a sanity check here.
|
||||
assert!(
|
||||
ratio >= Perbill::from_percent(80),
|
||||
"Too few proof size consumed, was only {:?} of expected",
|
||||
ratio
|
||||
);
|
||||
|
||||
let ratio = Perbill::from_rational(got.ref_time(), should.ref_time());
|
||||
assert!(
|
||||
ratio >= Perbill::from_percent(99),
|
||||
"Too few ref time consumed, was only {:?} of expected",
|
||||
ratio
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn waste_at_most_ref_time_weight_close_enough() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let mut meter =
|
||||
WeightMeter::from_limit(Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND, u64::MAX));
|
||||
// Over-spending fails defensively.
|
||||
Glutton::waste_at_most_ref_time(&mut meter);
|
||||
|
||||
// We require it to be under-spend by at most 1%.
|
||||
assert!(
|
||||
meter.consumed_ratio() >= Perbill::from_percent(99),
|
||||
"Consumed too few: {:?}",
|
||||
meter.consumed_ratio()
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn waste_at_most_proof_size_weight_close_enough() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let mut meter =
|
||||
WeightMeter::from_limit(Weight::from_parts(u64::MAX, WEIGHT_PROOF_SIZE_PER_MB * 5));
|
||||
// Over-spending fails defensively.
|
||||
Glutton::waste_at_most_proof_size(&mut meter);
|
||||
|
||||
// We require it to be under-spend by at most 1%.
|
||||
assert!(
|
||||
meter.consumed_ratio() >= Perbill::from_percent(99),
|
||||
"Consumed too few: {:?}",
|
||||
meter.consumed_ratio()
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user