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

Commit 68a153d

Browse files
Szegooggwpezbkchrkianenigmakoute
authored andcommitted
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 <[email protected]> * 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 <[email protected]> * Update frame/pov-limit/src/lib.rs Co-authored-by: Bastian Köcher <[email protected]> * 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 <[email protected]> * Update frame/pov-limit/src/lib.rs Co-authored-by: Bastian Köcher <[email protected]> * Update frame/pov-limit/Cargo.toml Co-authored-by: Bastian Köcher <[email protected]> * 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 <[email protected]> * or values * fix * generate weight * Update frame/glutton/Cargo.toml Co-authored-by: Oliver Tale-Yazdi <[email protected]> * fix nits * Adjustments Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Fixes Signed-off-by: Oliver Tale-Yazdi <[email protected]> * fix * Update weights Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Tweak constants Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Update weights Signed-off-by: Oliver Tale-Yazdi <[email protected]> * remove genesis config * passing tests * More precise results Signed-off-by: Oliver Tale-Yazdi <[email protected]> * update node-executor test * Calculate number of iterations in advance Signed-off-by: Oliver Tale-Yazdi <[email protected]> * increase criteria * Final fixes Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Update frame/glutton/src/tests.rs Co-authored-by: Kian Paimani <[email protected]> * Update frame/glutton/src/lib.rs Co-authored-by: Kian Paimani <[email protected]> * Update frame/glutton/src/lib.rs Co-authored-by: Kian Paimani <[email protected]> * fix typos * Update frame/glutton/src/lib.rs Co-authored-by: Bastian Köcher <[email protected]> * Update frame/glutton/src/lib.rs Co-authored-by: Bastian Köcher <[email protected]> * expand/shrink * fmt * Revert "Update frame/glutton/src/lib.rs" This reverts commit 98a237a. Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Use CountedStorageMap Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Add benchmark Signed-off-by: Oliver Tale-Yazdi <[email protected]> * 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 <[email protected]> * Use new template Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Doc+typos Signed-off-by: Oliver Tale-Yazdi <[email protected]> * ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_glutton * Apply suggestions from code review Co-authored-by: Koute <[email protected]> * Add minimal README Signed-off-by: Oliver Tale-Yazdi <[email protected]> * ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_glutton --------- Signed-off-by: Oliver Tale-Yazdi <[email protected]> Co-authored-by: Oliver Tale-Yazdi <[email protected]> Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Kian Paimani <[email protected]> Co-authored-by: command-bot <> Co-authored-by: Koute <[email protected]>
1 parent 3cc4705 commit 68a153d

File tree

12 files changed

+1113
-0
lines changed

12 files changed

+1113
-0
lines changed

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ members = [
167167
"frame/uniques",
168168
"frame/utility",
169169
"frame/vesting",
170+
"frame/glutton",
170171
"frame/whitelist",
171172
"primitives/api",
172173
"primitives/api/proc-macro",

bin/node/executor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ node-testing = { version = "3.0.0-dev", path = "../testing" }
3535
pallet-balances = { version = "4.0.0-dev", path = "../../../frame/balances" }
3636
pallet-contracts = { version = "4.0.0-dev", path = "../../../frame/contracts" }
3737
pallet-im-online = { version = "4.0.0-dev", path = "../../../frame/im-online" }
38+
pallet-glutton = { version = "4.0.0-dev", path = "../../../frame/glutton" }
3839
pallet-sudo = { version = "4.0.0-dev", path = "../../../frame/sudo" }
3940
pallet-timestamp = { version = "4.0.0-dev", path = "../../../frame/timestamp" }
4041
pallet-treasury = { version = "4.0.0-dev", path = "../../../frame/treasury" }

bin/node/runtime/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pallet-nomination-pools-benchmarking = { version = "1.0.0", default-features = f
8585
pallet-nomination-pools-runtime-api = { version = "1.0.0-dev", default-features = false, path = "../../../frame/nomination-pools/runtime-api" }
8686
pallet-offences = { version = "4.0.0-dev", default-features = false, path = "../../../frame/offences" }
8787
pallet-offences-benchmarking = { version = "4.0.0-dev", path = "../../../frame/offences/benchmarking", default-features = false, optional = true }
88+
pallet-glutton = { version = "4.0.0-dev", default-features = false, path = "../../../frame/glutton" }
8889
pallet-preimage = { version = "4.0.0-dev", default-features = false, path = "../../../frame/preimage" }
8990
pallet-proxy = { version = "4.0.0-dev", default-features = false, path = "../../../frame/proxy" }
9091
pallet-insecure-randomness-collective-flip = { version = "4.0.0-dev", default-features = false, path = "../../../frame/insecure-randomness-collective-flip" }
@@ -165,6 +166,7 @@ std = [
165166
"node-primitives/std",
166167
"sp-offchain/std",
167168
"pallet-offences/std",
169+
"pallet-glutton/std",
168170
"pallet-preimage/std",
169171
"pallet-proxy/std",
170172
"sp-core/std",
@@ -244,6 +246,7 @@ runtime-benchmarks = [
244246
"pallet-multisig/runtime-benchmarks",
245247
"pallet-nomination-pools-benchmarking/runtime-benchmarks",
246248
"pallet-offences-benchmarking/runtime-benchmarks",
249+
"pallet-glutton/runtime-benchmarks",
247250
"pallet-preimage/runtime-benchmarks",
248251
"pallet-proxy/runtime-benchmarks",
249252
"pallet-scheduler/runtime-benchmarks",
@@ -300,6 +303,7 @@ try-runtime = [
300303
"pallet-multisig/try-runtime",
301304
"pallet-nomination-pools/try-runtime",
302305
"pallet-offences/try-runtime",
306+
"pallet-glutton/try-runtime",
303307
"pallet-preimage/try-runtime",
304308
"pallet-proxy/try-runtime",
305309
"pallet-insecure-randomness-collective-flip/try-runtime",

bin/node/runtime/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ impl pallet_scheduler::Config for Runtime {
362362
type Preimages = Preimage;
363363
}
364364

365+
impl pallet_glutton::Config for Runtime {
366+
type RuntimeEvent = RuntimeEvent;
367+
type WeightInfo = pallet_glutton::weights::SubstrateWeight<Runtime>;
368+
}
369+
365370
parameter_types! {
366371
pub const PreimageMaxSize: u32 = 4096 * 1024;
367372
pub const PreimageBaseDeposit: Balance = 1 * DOLLARS;
@@ -1747,6 +1752,7 @@ construct_runtime!(
17471752
Recovery: pallet_recovery,
17481753
Vesting: pallet_vesting,
17491754
Scheduler: pallet_scheduler,
1755+
Glutton: pallet_glutton,
17501756
Preimage: pallet_preimage,
17511757
Proxy: pallet_proxy,
17521758
Multisig: pallet_multisig,
@@ -1878,6 +1884,7 @@ mod benches {
18781884
[pallet_recovery, Recovery]
18791885
[pallet_remark, Remark]
18801886
[pallet_scheduler, Scheduler]
1887+
[pallet_glutton, Glutton]
18811888
[pallet_session, SessionBench::<Runtime>]
18821889
[pallet_staking, Staking]
18831890
[pallet_state_trie_migration, StateTrieMigration]

frame/glutton/Cargo.toml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[package]
2+
name = "pallet-glutton"
3+
version = "4.0.0-dev"
4+
authors = ["Parity Technologies <[email protected]>"]
5+
edition = "2021"
6+
license = "Apache-2.0"
7+
homepage = "https://substrate.io"
8+
repository = "https://github.com/paritytech/substrate/"
9+
description = "FRAME pallet for pushing a chain to its weight limits"
10+
readme = "README.md"
11+
12+
[package.metadata.docs.rs]
13+
targets = ["x86_64-unknown-linux-gnu"]
14+
15+
[dependencies]
16+
blake2 = { version = "0.10.4", default-features = false }
17+
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
18+
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
19+
log = { version = "0.4.14", default-features = false }
20+
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
21+
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
22+
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
23+
sp-core = { version = "7.0.0", default-features = false, path = "../../primitives/core" }
24+
sp-io = { version = "7.0.0", default-features = false, path = "../../primitives/io" }
25+
sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" }
26+
sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" }
27+
28+
[dev-dependencies]
29+
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
30+
31+
[features]
32+
default = ["std"]
33+
std = [
34+
"blake2/std",
35+
"codec/std",
36+
"frame-benchmarking?/std",
37+
"frame-support/std",
38+
"frame-system/std",
39+
"scale-info/std",
40+
"sp-core/std",
41+
"sp-io/std",
42+
"sp-runtime/std",
43+
"sp-std/std",
44+
]
45+
try-runtime = ["frame-support/try-runtime"]
46+
47+
runtime-benchmarks = [
48+
"frame-benchmarking/runtime-benchmarks",
49+
"frame-system/runtime-benchmarks",
50+
"sp-runtime/runtime-benchmarks",
51+
]

frame/glutton/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# WARNING
2+
3+
Do not use on value-bearing chains. This pallet is **only** intended for usage on test-chains.
4+
5+
# Glutton Pallet
6+
7+
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.
8+
9+
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`.

frame/glutton/src/benchmarking.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! Glutton pallet benchmarking.
19+
//!
20+
//! Has to be compiled and run twice to calibrate on new hardware.
21+
22+
#[cfg(feature = "runtime-benchmarks")]
23+
use super::*;
24+
25+
use frame_benchmarking::benchmarks;
26+
use frame_support::{pallet_prelude::*, weights::constants::*};
27+
use frame_system::RawOrigin as SystemOrigin;
28+
29+
use crate::Pallet as Glutton;
30+
use frame_system::Pallet as System;
31+
32+
benchmarks! {
33+
initialize_pallet_grow {
34+
let n in 0 .. 1_000;
35+
}: {
36+
Glutton::<T>::initialize_pallet(SystemOrigin::Root.into(), n, None).unwrap()
37+
} verify {
38+
assert_eq!(TrashDataCount::<T>::get(), n);
39+
}
40+
41+
initialize_pallet_shrink {
42+
let n in 0 .. 1_000;
43+
44+
Glutton::<T>::initialize_pallet(SystemOrigin::Root.into(), n, None).unwrap();
45+
}: {
46+
Glutton::<T>::initialize_pallet(SystemOrigin::Root.into(), 0, Some(n)).unwrap()
47+
} verify {
48+
assert_eq!(TrashDataCount::<T>::get(), 0);
49+
}
50+
51+
waste_ref_time_iter {
52+
let i in 0..100_000;
53+
}: {
54+
Glutton::<T>::waste_ref_time_iter(vec![0u8; 64], i);
55+
}
56+
57+
waste_proof_size_some {
58+
let i in 0..5_000;
59+
60+
(0..5000).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
61+
}: {
62+
(0..i).for_each(|i| {
63+
TrashData::<T>::get(i);
64+
})
65+
}
66+
67+
// For manual verification only.
68+
on_idle_high_proof_waste {
69+
(0..5000).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
70+
let _ = Glutton::<T>::set_compute(SystemOrigin::Root.into(), Perbill::from_percent(100));
71+
let _ = Glutton::<T>::set_storage(SystemOrigin::Root.into(), Perbill::from_percent(100));
72+
}: {
73+
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));
74+
}
75+
76+
// For manual verification only.
77+
on_idle_low_proof_waste {
78+
(0..5000).for_each(|i| TrashData::<T>::insert(i, [i as u8; 1024]));
79+
let _ = Glutton::<T>::set_compute(SystemOrigin::Root.into(), Perbill::from_percent(100));
80+
let _ = Glutton::<T>::set_storage(SystemOrigin::Root.into(), Perbill::from_percent(100));
81+
}: {
82+
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));
83+
}
84+
85+
empty_on_idle {
86+
}: {
87+
// Enough weight do do nothing.
88+
Glutton::<T>::on_idle(System::<T>::block_number(), T::WeightInfo::empty_on_idle());
89+
}
90+
91+
set_compute {
92+
}: _(SystemOrigin::Root, Perbill::from_percent(50))
93+
94+
set_storage {
95+
}: _(SystemOrigin::Root, Perbill::from_percent(50))
96+
97+
impl_benchmark_test_suite!(Glutton, crate::mock::new_test_ext(), crate::mock::Test);
98+
}

0 commit comments

Comments
 (0)