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

Glutton pallet #12833

Merged
merged 103 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
9de0eee
Pov-Limit pallet
Szegoo Dec 3, 2022
7c0b552
use Perbill & fixes
Szegoo Dec 6, 2022
1a50132
fixes
Szegoo Dec 6, 2022
0e79cd3
reads & writes
Szegoo Dec 6, 2022
24ac38a
update docs
Szegoo Dec 6, 2022
fa70f0f
tests
Szegoo Dec 6, 2022
c1ecfe4
calculate weight
Szegoo Dec 7, 2022
b99945d
Merge branch 'master' into pov-limit
Szegoo Dec 7, 2022
25153a1
fmt
Szegoo Dec 8, 2022
27868bd
benchmark
Szegoo Dec 8, 2022
54f3117
logic fix
Szegoo Dec 8, 2022
91178fb
naming fix
Szegoo Dec 8, 2022
3565ff1
caclulate computation weight limit
Szegoo Dec 13, 2022
24edc74
Merge branch 'paritytech:master' into pov-limit
Szegoo Dec 13, 2022
9f48aba
make the Hasher generic
Szegoo Dec 13, 2022
f2abdfa
make the code compile
Szegoo Dec 13, 2022
36b049c
generate weight
Szegoo Dec 13, 2022
79c7816
fix on_idle
Szegoo Dec 14, 2022
b3f72d5
fix
Szegoo Dec 14, 2022
810bcd6
Merge branch 'paritytech:master' into pov-limit
Szegoo Dec 15, 2022
3f8a005
fix
Szegoo Dec 15, 2022
114404f
make reading generic
Szegoo Dec 15, 2022
90fe1ba
fix?
Szegoo Dec 15, 2022
17ef8d2
fixes
Szegoo Dec 16, 2022
28e03d7
remove warning
Szegoo Dec 16, 2022
bb83203
fix
Szegoo Dec 16, 2022
33a5a74
hasher fix :D
Szegoo Dec 16, 2022
a8ee2f5
change value
Szegoo Dec 16, 2022
6f1a430
test
Szegoo Dec 16, 2022
60ae9e1
actual weight and expected weight are the same
Szegoo Dec 18, 2022
00110cf
update
Szegoo Dec 18, 2022
ccdd60c
fix
Szegoo Dec 18, 2022
d1f7ea5
add events
Szegoo Dec 18, 2022
3d05e36
remove useless line
Szegoo Dec 18, 2022
9f12092
using actual hashing algorithm
Szegoo Dec 20, 2022
039bc21
better readability
Szegoo Dec 21, 2022
b24304b
fix nits
Szegoo Dec 21, 2022
980c24f
Update
ggwpez Dec 21, 2022
2802eb7
Merge remote-tracking branch 'szegoo/pov-limit' into pov-limit
ggwpez Dec 21, 2022
543bf11
add migration
Szegoo Dec 21, 2022
06d15eb
hardcode proof_size in weights.rs
Szegoo Dec 22, 2022
0faa4fe
format
Szegoo Dec 22, 2022
58e64a1
fixes
Szegoo Dec 22, 2022
90f8273
Fix weight
Szegoo Dec 22, 2022
e803cf9
Merge branch 'paritytech:master' into pov-limit
Szegoo Dec 29, 2022
7ee0054
docs
Szegoo Jan 1, 2023
aad6d29
fix
Szegoo Jan 1, 2023
932b4f7
Update frame/pov-limit/src/lib.rs
Szegoo Jan 3, 2023
5733118
Update frame/pov-limit/src/lib.rs
Szegoo Jan 3, 2023
dc0e8fd
empty on_ilde & update weight.rs
Szegoo Jan 3, 2023
ea63f7f
remove migration & fix benchmark
Szegoo Jan 3, 2023
aa9815a
remove migration from migrations
Szegoo Jan 3, 2023
93cefc2
initialize_pallet
Szegoo Jan 3, 2023
91ceea9
use blake2
Szegoo Jan 4, 2023
f5fc362
Update frame/pov-limit/Cargo.toml
Szegoo Jan 13, 2023
f238762
Update frame/pov-limit/src/lib.rs
Szegoo Jan 13, 2023
a8f4488
Update frame/pov-limit/Cargo.toml
Szegoo Jan 13, 2023
c12f907
rename pallet
Szegoo Jan 13, 2023
4dc6ed5
make the wasters non-generic
Szegoo Jan 13, 2023
9d2c281
rename to glutton
Szegoo Jan 13, 2023
630ca33
fix
Szegoo Jan 13, 2023
809ef50
Merge branch 'paritytech:master' into pov-limit
Szegoo Jan 27, 2023
2c8e766
small fixes & run benchmark
Szegoo Jan 27, 2023
c327247
increase left over ref-time in tests
Szegoo Jan 27, 2023
6e4e2c9
Update frame/glutton/src/lib.rs
Szegoo Feb 11, 2023
e105481
or values
Szegoo Feb 11, 2023
1c41c68
fix
Szegoo Feb 11, 2023
7a59fea
generate weight
Szegoo Feb 11, 2023
fb73955
Update frame/glutton/Cargo.toml
Szegoo Feb 14, 2023
895d439
fix nits
Szegoo Feb 14, 2023
177f605
Adjustments
ggwpez Feb 14, 2023
046c8a1
Fixes
ggwpez Feb 14, 2023
daa7c1a
fix
Szegoo Feb 15, 2023
7ddb68e
Update weights
ggwpez Feb 15, 2023
5e622e5
Tweak constants
ggwpez Feb 15, 2023
7e212ed
Update weights
ggwpez Feb 15, 2023
2610b20
remove genesis config
Szegoo Feb 16, 2023
257d29b
passing tests
Szegoo Feb 16, 2023
b65902b
More precise results
ggwpez Feb 16, 2023
80202f8
update node-executor test
Szegoo Feb 16, 2023
9c3b5df
Calculate number of iterations in advance
ggwpez Feb 17, 2023
323c365
increase criteria
Szegoo Feb 17, 2023
154c3d7
Final fixes
ggwpez Feb 17, 2023
6834abf
Update frame/glutton/src/tests.rs
Szegoo Feb 17, 2023
a42b1d2
Update frame/glutton/src/lib.rs
Szegoo Feb 17, 2023
98a237a
Update frame/glutton/src/lib.rs
Szegoo Feb 17, 2023
f6903a7
fix typos
Szegoo Feb 17, 2023
861de1e
Update frame/glutton/src/lib.rs
Szegoo Feb 17, 2023
b335d3a
Update frame/glutton/src/lib.rs
Szegoo Feb 17, 2023
f88b946
expand/shrink
Szegoo Feb 17, 2023
0fc53d9
fmt
Szegoo Feb 17, 2023
f944813
Revert "Update frame/glutton/src/lib.rs"
ggwpez Feb 19, 2023
889c90f
Use CountedStorageMap
ggwpez Feb 19, 2023
99a9774
Add benchmark
ggwpez Feb 19, 2023
ad687d3
Use manual map counter
ggwpez Feb 20, 2023
fb973bb
Merge remote-tracking branch 'origin/master' into pov-limit
ggwpez Feb 20, 2023
2869993
Use new template
ggwpez Feb 20, 2023
502240d
Doc+typos
ggwpez Feb 20, 2023
af6b3e4
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_glutton
Feb 20, 2023
2c8b112
Apply suggestions from code review
ggwpez Feb 20, 2023
0db5a70
Add minimal README
ggwpez Feb 20, 2023
62f2d8d
Merge remote-tracking branch 'origin/master' into pov-limit
Feb 20, 2023
f27c73b
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_glutton
Feb 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 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 @@ -113,6 +113,7 @@ members = [
"frame/nicks",
"frame/node-authorization",
"frame/offences",
"frame/pov-limit",
"frame/preimage",
"frame/proxy",
"frame/nomination-pools",
Expand Down
37 changes: 37 additions & 0 deletions frame/pov-limit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "pallet-pov-limit"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name = "pallet-pov-limit"
name = "pallet-weight-limit"

Maybe to be more generic?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could even use an actual name, like glutton or something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds funny :P

Copy link
Contributor Author

@Szegoo Szegoo Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should I rename this to pallet-glutton then? Would the parachains that will run the code from this pallet be called glutton parachains? :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually quite like "Glutton Parachains". It has the double meaning of one who over-consumes (the parachain), but also one who relishes in enduring something (i.e. "glutton for punishment"), and it is seeing how much validators can endure from parachains.

I also didn't know this before but it's another name for wolverines (https://en.wikipedia.org/wiki/Wolverine), so it can have a nice logo :).

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 nick management"
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
readme = "README.md"

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

[dependencies]
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"] }
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-io = { version = "7.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" }
sp-core = { version = "7.0.0", path = "../../primitives/core" }

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

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-io/std",
"sp-runtime/std",
]
try-runtime = ["frame-support/try-runtime"]
6 changes: 6 additions & 0 deletions frame/pov-limit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Pov Limit Pallet

Pallet that controls compute usage and PoV size usage. The parameters for
the usage are configured by root and are stored as `StorageValue`s.

NOTE: This is only meant to be used for testing.
231 changes: 231 additions & 0 deletions frame/pov-limit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// 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.

//! # Pov Limit Pallet
//!
//! Pallet that controls compute usage and PoV size usage. The parameters for
//! the usage are configured by root and are stored as `StorageValue`s.
//!
//! NOTE: This is only meant to be used for testing.

#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{pallet_prelude::*, traits::GenesisBuild};
use sp_core::{Blake2Hasher, Hasher};
use sp_runtime::Perbill;

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_system::pallet_prelude::*;

#[pallet::config]
pub trait Config: frame_system::Config {
/// The number of times the hash function should be called to fill up
/// all the block's weight.
#[pallet::constant]
type HashesForFull: Get<u32>;
}

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

#[pallet::storage]
pub(crate) type Compute<T: Config> = StorageValue<_, Perbill, ValueQuery>;

#[pallet::storage]
pub(crate) type Storage<T: Config> = StorageValue<_, u32, ValueQuery>;

#[pallet::genesis_config]
pub struct GenesisConfig {
pub compute: Perbill,
pub storage: u32,
}

#[cfg(feature = "std")]
impl Default for GenesisConfig {
fn default() -> Self {
Self { compute: Default::default(), storage: Default::default() }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
Compute::<T>::set(self.compute);
Storage::<T>::set(self.storage);
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
for i in 0..(Compute::<T>::get().mul_ceil(T::HashesForFull::get())) {
Blake2Hasher::hash(&i.to_le_bytes());
}

for i in 0..Storage::<T>::get() {
storage::unhashed::put(&i.to_le_bytes(), &i.to_le_bytes());
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
let _: Option<Vec<u8>> = storage::unhashed::get(&i.to_le_bytes());
}
Weight::zero()
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Set the `Compute` storage value that determines how much of the
/// block's weight to use during `on_initialize`.
///
/// Only callable by Root.
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn set_compute(origin: OriginFor<T>, compute: Perbill) -> DispatchResult {
let _ = ensure_root(origin)?;
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
Compute::<T>::set(compute);

Ok(())
}

/// Set the `Storage` storage value that determines the PoV size usage
/// for each block.
///
/// Only callable by Root.
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn set_storage(origin: OriginFor<T>, storage: u32) -> DispatchResult {
let _ = ensure_root(origin)?;
Szegoo marked this conversation as resolved.
Show resolved Hide resolved
Storage::<T>::set(storage);

Ok(())
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate as pallet_pov_limit;

use frame_support::{
assert_noop, assert_ok,
traits::{ConstU32, ConstU64},
};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
PovLimit: pallet_pov_limit::{Pallet},
}
);

impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type RuntimeCall = RuntimeCall;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
}

impl Config for Test {
type HashesForFull = ConstU32<1000000>;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();

let genesis = pallet::GenesisConfig { compute: Perbill::from_percent(50), storage: 10000 };

GenesisBuild::<Test>::assimilate_storage(&genesis, &mut t).unwrap();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}

#[test]
fn setting_compute_works() {
new_test_ext().execute_with(|| {
assert_eq!(Compute::<Test>::get(), Perbill::from_percent(50));

assert_ok!(PovLimit::set_compute(RuntimeOrigin::root(), Perbill::from_percent(70)));

assert_eq!(Compute::<Test>::get(), Perbill::from_percent(70));

assert_noop!(
PovLimit::set_compute(RuntimeOrigin::signed(1), Perbill::from_percent(30)),
DispatchError::BadOrigin
);
assert_noop!(
PovLimit::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(), 10000);

assert_ok!(PovLimit::set_storage(RuntimeOrigin::root(), 5000));

assert_eq!(Storage::<Test>::get(), 5000);

assert_noop!(
PovLimit::set_storage(RuntimeOrigin::signed(1), 15000),
DispatchError::BadOrigin
);
assert_noop!(
PovLimit::set_storage(RuntimeOrigin::none(), 15000),
DispatchError::BadOrigin
);
});
}
}