Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Glutton pallet (#12833)
Browse files Browse the repository at this point in the history
* 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 98a237a.

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>
  • Loading branch information
5 people authored and Ank4n committed Feb 28, 2023
1 parent 3cc4705 commit 68a153d
Show file tree
Hide file tree
Showing 12 changed files with 1,113 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ members = [
"frame/uniques",
"frame/utility",
"frame/vesting",
"frame/glutton",
"frame/whitelist",
"primitives/api",
"primitives/api/proc-macro",
Expand Down
1 change: 1 addition & 0 deletions bin/node/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ node-testing = { version = "3.0.0-dev", path = "../testing" }
pallet-balances = { version = "4.0.0-dev", path = "../../../frame/balances" }
pallet-contracts = { version = "4.0.0-dev", path = "../../../frame/contracts" }
pallet-im-online = { version = "4.0.0-dev", path = "../../../frame/im-online" }
pallet-glutton = { version = "4.0.0-dev", path = "../../../frame/glutton" }
pallet-sudo = { version = "4.0.0-dev", path = "../../../frame/sudo" }
pallet-timestamp = { version = "4.0.0-dev", path = "../../../frame/timestamp" }
pallet-treasury = { version = "4.0.0-dev", path = "../../../frame/treasury" }
Expand Down
4 changes: 4 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pallet-nomination-pools-benchmarking = { version = "1.0.0", default-features = f
pallet-nomination-pools-runtime-api = { version = "1.0.0-dev", default-features = false, path = "../../../frame/nomination-pools/runtime-api" }
pallet-offences = { version = "4.0.0-dev", default-features = false, path = "../../../frame/offences" }
pallet-offences-benchmarking = { version = "4.0.0-dev", path = "../../../frame/offences/benchmarking", default-features = false, optional = true }
pallet-glutton = { version = "4.0.0-dev", default-features = false, path = "../../../frame/glutton" }
pallet-preimage = { version = "4.0.0-dev", default-features = false, path = "../../../frame/preimage" }
pallet-proxy = { version = "4.0.0-dev", default-features = false, path = "../../../frame/proxy" }
pallet-insecure-randomness-collective-flip = { version = "4.0.0-dev", default-features = false, path = "../../../frame/insecure-randomness-collective-flip" }
Expand Down Expand Up @@ -165,6 +166,7 @@ std = [
"node-primitives/std",
"sp-offchain/std",
"pallet-offences/std",
"pallet-glutton/std",
"pallet-preimage/std",
"pallet-proxy/std",
"sp-core/std",
Expand Down Expand Up @@ -244,6 +246,7 @@ runtime-benchmarks = [
"pallet-multisig/runtime-benchmarks",
"pallet-nomination-pools-benchmarking/runtime-benchmarks",
"pallet-offences-benchmarking/runtime-benchmarks",
"pallet-glutton/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
"pallet-proxy/runtime-benchmarks",
"pallet-scheduler/runtime-benchmarks",
Expand Down Expand Up @@ -300,6 +303,7 @@ try-runtime = [
"pallet-multisig/try-runtime",
"pallet-nomination-pools/try-runtime",
"pallet-offences/try-runtime",
"pallet-glutton/try-runtime",
"pallet-preimage/try-runtime",
"pallet-proxy/try-runtime",
"pallet-insecure-randomness-collective-flip/try-runtime",
Expand Down
7 changes: 7 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ impl pallet_scheduler::Config for Runtime {
type Preimages = Preimage;
}

impl pallet_glutton::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_glutton::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
pub const PreimageMaxSize: u32 = 4096 * 1024;
pub const PreimageBaseDeposit: Balance = 1 * DOLLARS;
Expand Down Expand Up @@ -1747,6 +1752,7 @@ construct_runtime!(
Recovery: pallet_recovery,
Vesting: pallet_vesting,
Scheduler: pallet_scheduler,
Glutton: pallet_glutton,
Preimage: pallet_preimage,
Proxy: pallet_proxy,
Multisig: pallet_multisig,
Expand Down Expand Up @@ -1878,6 +1884,7 @@ mod benches {
[pallet_recovery, Recovery]
[pallet_remark, Remark]
[pallet_scheduler, Scheduler]
[pallet_glutton, Glutton]
[pallet_session, SessionBench::<Runtime>]
[pallet_staking, Staking]
[pallet_state_trie_migration, StateTrieMigration]
Expand Down
51 changes: 51 additions & 0 deletions frame/glutton/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[package]
name = "pallet-glutton"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME pallet for pushing a chain to its weight limits"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
blake2 = { version = "0.10.4", default-features = false }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
log = { version = "0.4.14", default-features = false }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
sp-core = { version = "7.0.0", default-features = false, path = "../../primitives/core" }
sp-io = { version = "7.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" }

[dev-dependencies]
pallet-balances = { version = "4.0.0-dev", path = "../balances" }

[features]
default = ["std"]
std = [
"blake2/std",
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
try-runtime = ["frame-support/try-runtime"]

runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
9 changes: 9 additions & 0 deletions frame/glutton/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# WARNING

Do not use on value-bearing chains. This pallet is **only** intended for usage on test-chains.

# Glutton Pallet

The `Glutton` pallet gets the name from its property to consume vast amounts of resources. It can be used to push para-chains and their relay-chains to the limits. This is good for testing out theoretical limits in a practical way.

The `Glutton` can be set to consume a fraction of the available unused weight of a chain. It accomplishes this by utilizing the `on_idle` hook and consuming a specific ration of the remaining weight. The rations can be set via `set_compute` and `set_storage`. Initially the `Glutton` needs to be initialized once with `initialize_pallet`.
98 changes: 98 additions & 0 deletions frame/glutton/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// This file is part of Substrate.

// Copyright (C) 2022 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.

//! Glutton pallet benchmarking.
//!
//! Has to be compiled and run twice to calibrate on new hardware.
#[cfg(feature = "runtime-benchmarks")]
use super::*;

use frame_benchmarking::benchmarks;
use frame_support::{pallet_prelude::*, weights::constants::*};
use frame_system::RawOrigin as SystemOrigin;

use crate::Pallet as Glutton;
use frame_system::Pallet as System;

benchmarks! {
initialize_pallet_grow {
let n in 0 .. 1_000;
}: {
Glutton::<T>::initialize_pallet(SystemOrigin::Root.into(), n, None).unwrap()
} verify {
assert_eq!(TrashDataCount::<T>::get(), n);
}

initialize_pallet_shrink {
let n in 0 .. 1_000;

Glutton::<T>::initialize_pallet(SystemOrigin::Root.into(), n, None).unwrap();
}: {
Glutton::<T>::initialize_pallet(SystemOrigin::Root.into(), 0, Some(n)).unwrap()
} verify {
assert_eq!(TrashDataCount::<T>::get(), 0);
}

waste_ref_time_iter {
let i in 0..100_000;
}: {
Glutton::<T>::waste_ref_time_iter(vec![0u8; 64], i);
}

waste_proof_size_some {
let i in 0..5_000;

(0..5000).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
}: {
(0..i).for_each(|i| {
TrashData::<T>::get(i);
})
}

// For manual verification only.
on_idle_high_proof_waste {
(0..5000).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
let _ = Glutton::<T>::set_compute(SystemOrigin::Root.into(), Perbill::from_percent(100));
let _ = Glutton::<T>::set_storage(SystemOrigin::Root.into(), Perbill::from_percent(100));
}: {
let weight = Glutton::<T>::on_idle(System::<T>::block_number(), Weight::from_parts(WEIGHT_REF_TIME_PER_MILLIS * 100, WEIGHT_PROOF_SIZE_PER_MB * 5));
}

// For manual verification only.
on_idle_low_proof_waste {
(0..5000).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
let _ = Glutton::<T>::set_compute(SystemOrigin::Root.into(), Perbill::from_percent(100));
let _ = Glutton::<T>::set_storage(SystemOrigin::Root.into(), Perbill::from_percent(100));
}: {
let weight = Glutton::<T>::on_idle(System::<T>::block_number(), Weight::from_parts(WEIGHT_REF_TIME_PER_MILLIS * 100, WEIGHT_PROOF_SIZE_PER_KB * 20));
}

empty_on_idle {
}: {
// Enough weight do do nothing.
Glutton::<T>::on_idle(System::<T>::block_number(), T::WeightInfo::empty_on_idle());
}

set_compute {
}: _(SystemOrigin::Root, Perbill::from_percent(50))

set_storage {
}: _(SystemOrigin::Root, Perbill::from_percent(50))

impl_benchmark_test_suite!(Glutton, crate::mock::new_test_ext(), crate::mock::Test);
}
Loading

0 comments on commit 68a153d

Please sign in to comment.