From 2b575d74bbab45051b6f62b547a49e3f4b5a878d Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 01:48:20 +0200 Subject: [PATCH 01/23] Update system weights --- bin/node/runtime/src/lib.rs | 2 +- frame/system/src/lib.rs | 68 ++++++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index e3613ea75abfc..1cc9936f18938 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -171,7 +171,7 @@ impl pallet_utility::Trait for Runtime { } parameter_types! { - pub const MaximumWeight: Weight = 2_000_000; + pub const MaximumWeight: Weight = MaximumBlockWeight; } impl pallet_scheduler::Trait for Runtime { diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 5a3097e62ef62..12919fdaaf7d9 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -512,8 +512,10 @@ decl_module! { /// /// # /// - `O(1)` + /// - Base Weight: 0.665 µs, independent of remark length. + /// - No DB operations. /// # - #[weight = 0] + #[weight = 700_000] fn remark(origin, _remark: Vec) { ensure_signed(origin)?; } @@ -523,8 +525,10 @@ decl_module! { /// # /// - `O(1)` /// - 1 storage write. + /// - Base Weight: 1.405 µs + /// - 1 write to HEAP_PAGES /// # - #[weight = (0, DispatchClass::Operational)] + #[weight = (T::DbWeight::get().writes(1) + 1_500_000, DispatchClass::Operational)] fn set_heap_pages(origin, pages: u64) { ensure_root(origin)?; storage::unhashed::put_raw(well_known_keys::HEAP_PAGES, &pages.encode()); @@ -537,8 +541,10 @@ decl_module! { /// - 1 storage write (codec `O(C)`). /// - 1 call to `can_set_code`: `O(S)` (calls `sp_io::misc::runtime_version` which is expensive). /// - 1 event. + /// The weight of this function is dependent on the runtime, but generally this is very expensive. + /// We will treat this as a full block. /// # - #[weight = (200_000_000, DispatchClass::Operational)] + #[weight = (crate::Module::::max_extrinsic_weight(DispatchClass::Operational), DispatchClass::Operational)] pub fn set_code(origin, code: Vec) { Self::can_set_code(origin, &code)?; @@ -552,8 +558,9 @@ decl_module! { /// - `O(C)` where `C` length of `code` /// - 1 storage write (codec `O(C)`). /// - 1 event. + /// The weight of this function is dependent on the runtime. We will treat this as a full block. /// # - #[weight = (200_000_000, DispatchClass::Operational)] + #[weight = (crate::Module::::max_extrinsic_weight(DispatchClass::Operational), DispatchClass::Operational)] pub fn set_code_without_checks(origin, code: Vec) { ensure_root(origin)?; storage::unhashed::put_raw(well_known_keys::CODE, &code); @@ -566,8 +573,12 @@ decl_module! { /// - `O(D)` where `D` length of `Digest` /// - 1 storage write or delete (codec `O(1)`). /// - 1 call to `deposit_log`: `O(D)` (which depends on the length of `Digest`) + /// - Base Weight: 9.29 + 0.511 * d µs + /// - DB Weight: + /// - Reads: System Digest + /// - Writes: Changes Trie, System Digest /// # - #[weight = (20_000_000, DispatchClass::Operational)] + #[weight = (T::DbWeight::get().reads_writes(1, 2) + 10_000_000, DispatchClass::Operational)] pub fn set_changes_trie_config(origin, changes_trie_config: Option) { ensure_root(origin)?; match changes_trie_config.clone() { @@ -589,8 +600,17 @@ decl_module! { /// # /// - `O(I)` where `I` length of `items` /// - `I` storage writes (`O(1)`). + /// - Base Weight: 0.568 * i + /// - Writes: Number of items /// # - #[weight = (0, DispatchClass::Operational)] + #[weight = FunctionOf( + |args: (&Vec,)| { + T::DbWeight::get().writes(args.0.len() as Weight) + .saturating_add((args.0.len() as Weight).saturating_mul(600_000)) + }, + DispatchClass::Operational, + Pays::Yes, + )] fn set_storage(origin, items: Vec) { ensure_root(origin)?; for i in &items { @@ -601,10 +621,19 @@ decl_module! { /// Kill some items from storage. /// /// # - /// - `O(VK)` where `V` length of `keys` and `K` length of one key - /// - `V` storage deletions. + /// - `O(IK)` where `I` length of `keys` and `K` length of one key + /// - `I` storage deletions. + /// - Base Weight: .378 * i µs + /// - Writes: Number of items /// # - #[weight = (0, DispatchClass::Operational)] + #[weight = FunctionOf( + |args: (&Vec,)| { + T::DbWeight::get().writes(args.0.len() as Weight) + .saturating_add((args.0.len() as Weight).saturating_mul(400_000)) + }, + DispatchClass::Operational, + Pays::Yes, + )] fn kill_storage(origin, keys: Vec) { ensure_root(origin)?; for key in &keys { @@ -614,12 +643,24 @@ decl_module! { /// Kill all storage items with a key that starts with the given prefix. /// + /// **NOTE:** We rely on the Root origin to provide us the number of subkeys under + /// the prefix we are removing to accurately calculate the weight of this function. + /// /// # /// - `O(P)` where `P` amount of keys with prefix `prefix` /// - `P` storage deletions. + /// - Base Weight: 0.834 * P µs + /// - Writes: Number of subkeys + 1 /// # - #[weight = (0, DispatchClass::Operational)] - fn kill_prefix(origin, prefix: Key) { + #[weight = FunctionOf( + |args: (&Key, &u32)| { + T::DbWeight::get().writes(Weight::from(*args.1) + 1) + .saturating_add((Weight::from(*args.1) + 1).saturating_mul(850_000)) + }, + DispatchClass::Operational, + Pays::Yes, + )] + fn kill_prefix(origin, prefix: Key, _subkeys: u32) { ensure_root(origin)?; storage::unhashed::kill_prefix(&prefix); } @@ -630,8 +671,11 @@ decl_module! { /// # /// - `O(1)` /// - 1 storage read and deletion. + /// -------------------- + /// Base Weight: 8.626 µs + /// No DB Read or Write operations because caller is already in overlay /// # - #[weight = (25_000_000, DispatchClass::Operational)] + #[weight = (10_000_000, DispatchClass::Operational)] fn suicide(origin) { let who = ensure_signed(origin)?; let account = Account::::get(&who); From fa7fed16c686f797c20a872a2baa6045e7a65122 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 02:05:20 +0200 Subject: [PATCH 02/23] Use maximum block weight for scheduler --- bin/node/runtime/src/lib.rs | 6 +----- frame/democracy/src/tests.rs | 5 ++--- frame/scheduler/src/lib.rs | 7 ++----- frame/system/benchmarking/src/lib.rs | 2 +- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 1cc9936f18938..9742a8a9be9aa 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -170,15 +170,11 @@ impl pallet_utility::Trait for Runtime { type MaxSignatories = MaxSignatories; } -parameter_types! { - pub const MaximumWeight: Weight = MaximumBlockWeight; -} - impl pallet_scheduler::Trait for Runtime { type Event = Event; type Origin = Origin; type Call = Call; - type MaximumWeight = MaximumWeight; + type MaximumWeight = MaximumBlockWeight; } parameter_types! { diff --git a/frame/democracy/src/tests.rs b/frame/democracy/src/tests.rs index 076eb66d6bb56..354caee648285 100644 --- a/frame/democracy/src/tests.rs +++ b/frame/democracy/src/tests.rs @@ -77,7 +77,7 @@ impl_outer_event! { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; - pub const MaximumBlockWeight: Weight = 1024; + pub const MaximumBlockWeight: Weight = 1_000_000; pub const MaximumBlockLength: u32 = 2 * 1024; pub const AvailableBlockRatio: Perbill = Perbill::one(); } @@ -107,13 +107,12 @@ impl frame_system::Trait for Test { } parameter_types! { pub const ExistentialDeposit: u64 = 1; - pub const MaximumWeight: u32 = 1000000; } impl pallet_scheduler::Trait for Test { type Event = Event; type Origin = Origin; type Call = Call; - type MaximumWeight = MaximumWeight; + type MaximumWeight = MaximumBlockWeight; } impl pallet_balances::Trait for Test { type Balance = u64; diff --git a/frame/scheduler/src/lib.rs b/frame/scheduler/src/lib.rs index a18a48da0807e..1410bd838e120 100644 --- a/frame/scheduler/src/lib.rs +++ b/frame/scheduler/src/lib.rs @@ -331,7 +331,7 @@ mod tests { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; - pub const MaximumBlockWeight: Weight = 1024; + pub const MaximumBlockWeight: Weight = 10_000; pub const MaximumBlockLength: u32 = 2 * 1024; pub const AvailableBlockRatio: Perbill = Perbill::one(); } @@ -362,14 +362,11 @@ mod tests { impl logger::Trait for Test { type Event = (); } - parameter_types! { - pub const MaximumWeight: Weight = 10_000; - } impl Trait for Test { type Event = (); type Origin = Origin; type Call = Call; - type MaximumWeight = MaximumWeight; + type MaximumWeight = MaximumBlockWeight; } type System = system::Module; type Logger = logger::Module; diff --git a/frame/system/benchmarking/src/lib.rs b/frame/system/benchmarking/src/lib.rs index b9b9619c09bee..22e6dba842741 100644 --- a/frame/system/benchmarking/src/lib.rs +++ b/frame/system/benchmarking/src/lib.rs @@ -134,7 +134,7 @@ benchmarks! { let value = storage::unhashed::get_raw(&last_key).ok_or("No value stored")?; assert_eq!(value, last_key); - }: _(RawOrigin::Root, prefix) + }: _(RawOrigin::Root, prefix, p) verify { assert_eq!(storage::unhashed::get_raw(&last_key), None); } From 0bbe0ce18e9419b032157f7d37dea6481078cdc0 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 02:15:50 +0200 Subject: [PATCH 03/23] Update offences to use full block --- frame/offences/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index b877d1e2b20dd..3e6e26e498288 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -27,7 +27,7 @@ mod tests; use sp_std::vec::Vec; use frame_support::{ decl_module, decl_event, decl_storage, Parameter, debug, - weights::Weight, + weights::Weight, traits::Get, }; use sp_runtime::{traits::Hash, Perbill}; use sp_staking::{ @@ -117,7 +117,8 @@ decl_module! { }) } - 0 + // This on-initialize function can become very expensive, so we allow it to take a full block. + T::MaximumBlockWeight::get() } } } From 87277d07913a7d1868eeee85ef4673f51ee4013b Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 02:40:57 +0200 Subject: [PATCH 04/23] Move weight inside if statement --- frame/offences/src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index 3e6e26e498288..699298e1e1083 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -114,11 +114,12 @@ decl_module! { ); }).is_err() }) - }) + }); + // This on-initialize function can become very expensive, so we allow it to take a full block. + T::MaximumBlockWeight::get() + } else { + 0 } - - // This on-initialize function can become very expensive, so we allow it to take a full block. - T::MaximumBlockWeight::get() } } } From 7df7ebc73625ed79b14086f13c247d4058ee87d6 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 02:44:34 +0200 Subject: [PATCH 05/23] Add one read to offences `on_initialize` --- frame/offences/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index 699298e1e1083..af80456c771ab 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -99,6 +99,8 @@ decl_module! { pub struct Module for enum Call where origin: T::Origin { fn deposit_event() = default; + // One read for `OnOffenceHandler::can_report()`: + // - Staking `era_election_status` fn on_initialize(now: T::BlockNumber) -> Weight { // only decode storage if we can actually submit anything again. if T::OnOffenceHandler::can_report() { @@ -118,7 +120,7 @@ decl_module! { // This on-initialize function can become very expensive, so we allow it to take a full block. T::MaximumBlockWeight::get() } else { - 0 + T::DbWeight::get().reads(1) } } } From 8f95aacd63a028ef1b415185b45367b4140d86fd Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 10:43:46 +0200 Subject: [PATCH 06/23] Delete factory test --- bin/node/cli/tests/factory.rs | 40 ----------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 bin/node/cli/tests/factory.rs diff --git a/bin/node/cli/tests/factory.rs b/bin/node/cli/tests/factory.rs deleted file mode 100644 index 2930cd52e2e16..0000000000000 --- a/bin/node/cli/tests/factory.rs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2020 Parity Technologies (UK) Ltd. -// This file is part of Substrate. - -// Substrate is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Substrate is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Substrate. If not, see . - -#![cfg(unix)] - -use assert_cmd::cargo::cargo_bin; -use std::process::{Command, Stdio}; -use tempfile::tempdir; - -mod common; - -#[test] -fn factory_works() { - let base_path = tempdir().expect("could not create a temp dir"); - - let status = Command::new(cargo_bin("substrate")) - .stdout(Stdio::null()) - .args(&["factory", "--dev", "-d"]) - .arg(base_path.path()) - .status() - .unwrap(); - assert!(status.success()); - - // Make sure that the `dev` chain folder exists & `db` - assert!(base_path.path().join("chains/dev/").exists()); - assert!(base_path.path().join("chains/dev/db").exists()); -} From 2dd84b0a2f3330efc9d722093e34e95d15b45930 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 10:47:37 +0200 Subject: [PATCH 07/23] Revert "Delete factory test" This reverts commit 8f95aacd63a028ef1b415185b45367b4140d86fd. --- bin/node/cli/tests/factory.rs | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 bin/node/cli/tests/factory.rs diff --git a/bin/node/cli/tests/factory.rs b/bin/node/cli/tests/factory.rs new file mode 100644 index 0000000000000..2930cd52e2e16 --- /dev/null +++ b/bin/node/cli/tests/factory.rs @@ -0,0 +1,40 @@ +// Copyright 2020 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Substrate. If not, see . + +#![cfg(unix)] + +use assert_cmd::cargo::cargo_bin; +use std::process::{Command, Stdio}; +use tempfile::tempdir; + +mod common; + +#[test] +fn factory_works() { + let base_path = tempdir().expect("could not create a temp dir"); + + let status = Command::new(cargo_bin("substrate")) + .stdout(Stdio::null()) + .args(&["factory", "--dev", "-d"]) + .arg(base_path.path()) + .status() + .unwrap(); + assert!(status.success()); + + // Make sure that the `dev` chain folder exists & `db` + assert!(base_path.path().join("chains/dev/").exists()); + assert!(base_path.path().join("chains/dev/db").exists()); +} From 6e2bf913e360b13d6bb0c9fdde17ead2c1dd2c26 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 10:47:46 +0200 Subject: [PATCH 08/23] Revert "Add one read to offences `on_initialize`" This reverts commit 7df7ebc73625ed79b14086f13c247d4058ee87d6. --- frame/offences/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index af80456c771ab..699298e1e1083 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -99,8 +99,6 @@ decl_module! { pub struct Module for enum Call where origin: T::Origin { fn deposit_event() = default; - // One read for `OnOffenceHandler::can_report()`: - // - Staking `era_election_status` fn on_initialize(now: T::BlockNumber) -> Weight { // only decode storage if we can actually submit anything again. if T::OnOffenceHandler::can_report() { @@ -120,7 +118,7 @@ decl_module! { // This on-initialize function can become very expensive, so we allow it to take a full block. T::MaximumBlockWeight::get() } else { - T::DbWeight::get().reads(1) + 0 } } } From 65a29a80c030618157f00cbd776c4d7c2f8447c3 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 10:47:48 +0200 Subject: [PATCH 09/23] Revert "Move weight inside if statement" This reverts commit 87277d07913a7d1868eeee85ef4673f51ee4013b. --- frame/offences/src/lib.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index 699298e1e1083..3e6e26e498288 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -114,12 +114,11 @@ decl_module! { ); }).is_err() }) - }); - // This on-initialize function can become very expensive, so we allow it to take a full block. - T::MaximumBlockWeight::get() - } else { - 0 + }) } + + // This on-initialize function can become very expensive, so we allow it to take a full block. + T::MaximumBlockWeight::get() } } } From 820aa6edd0e5b9fa11d779b3019e717fdccb5fa7 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 10:47:54 +0200 Subject: [PATCH 10/23] Revert "Update offences to use full block" This reverts commit 0bbe0ce18e9419b032157f7d37dea6481078cdc0. --- frame/offences/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index 3e6e26e498288..b877d1e2b20dd 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -27,7 +27,7 @@ mod tests; use sp_std::vec::Vec; use frame_support::{ decl_module, decl_event, decl_storage, Parameter, debug, - weights::Weight, traits::Get, + weights::Weight, }; use sp_runtime::{traits::Hash, Perbill}; use sp_staking::{ @@ -117,8 +117,7 @@ decl_module! { }) } - // This on-initialize function can become very expensive, so we allow it to take a full block. - T::MaximumBlockWeight::get() + 0 } } } From 95bd2768dfea100bdf682cf4fe6c0f46e8e1f66e Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 18:59:33 +0200 Subject: [PATCH 11/23] Use scheduler in Sudo --- bin/node-template/runtime/src/lib.rs | 1 + bin/node/runtime/src/lib.rs | 1 + frame/sudo/src/lib.rs | 52 +++++++++++++++++++++------- frame/system/src/lib.rs | 4 +-- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 44332f61a1583..32aaa5966edd2 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -230,6 +230,7 @@ impl transaction_payment::Trait for Runtime { impl sudo::Trait for Runtime { type Event = Event; type Call = Call; + type Scheduler = Sudo; } /// Used for the module template in `./template.rs` diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 9742a8a9be9aa..98c5f654065e8 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -488,6 +488,7 @@ impl pallet_contracts::Trait for Runtime { impl pallet_sudo::Trait for Runtime { type Event = Event; type Call = Call; + type Scheduler = Scheduler; } parameter_types! { diff --git a/frame/sudo/src/lib.rs b/frame/sudo/src/lib.rs index d19c92358c287..3de9740f6510f 100644 --- a/frame/sudo/src/lib.rs +++ b/frame/sudo/src/lib.rs @@ -58,7 +58,7 @@ //! //! decl_module! { //! pub struct Module for enum Call where origin: T::Origin { -//! #[weight = 0] +//! #[weight = 0] //! pub fn privileged_function(origin) -> dispatch::DispatchResult { //! ensure_root(origin)?; //! @@ -87,12 +87,13 @@ #![cfg_attr(not(feature = "std"), no_std)] use sp_std::prelude::*; -use sp_runtime::{DispatchResult, traits::{StaticLookup, Dispatchable}}; +use sp_runtime::{DispatchResult, traits::{StaticLookup, Dispatchable, One}}; use frame_support::{ Parameter, decl_module, decl_event, decl_storage, decl_error, ensure, }; use frame_support::weights::{GetDispatchInfo, FunctionOf, Pays}; +use frame_support::traits::schedule::{self, Anon as ScheduleAnon}; use frame_system::{self as system, ensure_signed}; pub trait Trait: frame_system::Trait { @@ -101,6 +102,9 @@ pub trait Trait: frame_system::Trait { /// A sudo-able call. type Call: Parameter + Dispatchable + GetDispatchInfo; + + /// The Scheduler. + type Scheduler: ScheduleAnon::Call>; } decl_module! { @@ -110,7 +114,7 @@ decl_module! { fn deposit_event() = default; - /// Authenticates the sudo key and dispatches a function call with `Root` origin. + /// Authenticates the sudo key and schedules a function call with `Root` origin. /// /// The dispatch origin for this call must be _Signed_. /// @@ -120,18 +124,19 @@ decl_module! { /// - One DB write (event). /// - Weight of derivative `call` execution + 10,000. /// # - #[weight = FunctionOf( - |args: (&Box<::Call>,)| args.0.get_dispatch_info().weight + 10_000, - |args: (&Box<::Call>,)| args.0.get_dispatch_info().class, - Pays::Yes, - )] - fn sudo(origin, call: Box<::Call>) { + #[weight = 10_000_000] + fn sudo(origin, call: Box<::Call>, priority: schedule::Priority) { // This is a public call, so we ensure that the origin is some signed account. let sender = ensure_signed(origin)?; ensure!(sender == Self::key(), Error::::RequireSudo); - - let res = call.dispatch(frame_system::RawOrigin::Root.into()); - Self::deposit_event(RawEvent::Sudid(res.map(|_| ()).map_err(|e| e.error))); + let block_number = frame_system::Module::::block_number(); + + T::Scheduler::schedule( + block_number + T::BlockNumber::one(), + None, + priority, + *call, + ); } /// Authenticates the current sudo key and sets the given AccountId (`new`) as the new sudo key. @@ -219,3 +224,26 @@ decl_error! { RequireSudo, } } + +// An implementation of scheduler that directly dispatches a call. +// Useful if you do not want to include a proper scheduler, but does not +// respect block Weight limits! +impl schedule::Anon::Call> for Module { + type Address = (); + + /// This directly dispatches a call in the current block. + fn schedule( + _when: T::BlockNumber, + _maybe_periodic: Option>, + _priority: schedule::Priority, + call: ::Call + ) -> Self::Address { + let res = call.dispatch(frame_system::RawOrigin::Root.into()); + Self::deposit_event(RawEvent::Sudid(res.map(|_| ()).map_err(|e| e.error))); + } + + /// You cannot cancel a direct dispatch, so this will always return `Err`. + fn cancel(_address: Self::Address) -> Result<(), ()> { + Err(()) + } +} diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 12919fdaaf7d9..850292a568fce 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -544,7 +544,7 @@ decl_module! { /// The weight of this function is dependent on the runtime, but generally this is very expensive. /// We will treat this as a full block. /// # - #[weight = (crate::Module::::max_extrinsic_weight(DispatchClass::Operational), DispatchClass::Operational)] + #[weight = (T::MaximumBlockWeight::get(), DispatchClass::Mandatory)] pub fn set_code(origin, code: Vec) { Self::can_set_code(origin, &code)?; @@ -560,7 +560,7 @@ decl_module! { /// - 1 event. /// The weight of this function is dependent on the runtime. We will treat this as a full block. /// # - #[weight = (crate::Module::::max_extrinsic_weight(DispatchClass::Operational), DispatchClass::Operational)] + #[weight = (T::MaximumBlockWeight::get(), DispatchClass::Mandatory)] pub fn set_code_without_checks(origin, code: Vec) { ensure_root(origin)?; storage::unhashed::put_raw(well_known_keys::CODE, &code); From b48d9b13c26ce0cb86262c6d58b71dfd75727c19 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 19:11:03 +0200 Subject: [PATCH 12/23] Apply suggestions from code review Co-authored-by: Alexander Popiak --- frame/system/src/lib.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 850292a568fce..604a348a2dc68 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -573,7 +573,7 @@ decl_module! { /// - `O(D)` where `D` length of `Digest` /// - 1 storage write or delete (codec `O(1)`). /// - 1 call to `deposit_log`: `O(D)` (which depends on the length of `Digest`) - /// - Base Weight: 9.29 + 0.511 * d µs + /// - Base Weight: 9.29 + 0.511 * D µs /// - DB Weight: /// - Reads: System Digest /// - Writes: Changes Trie, System Digest @@ -604,9 +604,9 @@ decl_module! { /// - Writes: Number of items /// # #[weight = FunctionOf( - |args: (&Vec,)| { - T::DbWeight::get().writes(args.0.len() as Weight) - .saturating_add((args.0.len() as Weight).saturating_mul(600_000)) + |(items,): (&Vec,)| { + T::DbWeight::get().writes(items.len() as Weight) + .saturating_add((items.len() as Weight).saturating_mul(600_000)) }, DispatchClass::Operational, Pays::Yes, @@ -627,9 +627,9 @@ decl_module! { /// - Writes: Number of items /// # #[weight = FunctionOf( - |args: (&Vec,)| { - T::DbWeight::get().writes(args.0.len() as Weight) - .saturating_add((args.0.len() as Weight).saturating_mul(400_000)) + |(keys,): (&Vec,)| { + T::DbWeight::get().writes(keys.len() as Weight) + .saturating_add((keys.len() as Weight).saturating_mul(400_000)) }, DispatchClass::Operational, Pays::Yes, @@ -653,9 +653,9 @@ decl_module! { /// - Writes: Number of subkeys + 1 /// # #[weight = FunctionOf( - |args: (&Key, &u32)| { - T::DbWeight::get().writes(Weight::from(*args.1) + 1) - .saturating_add((Weight::from(*args.1) + 1).saturating_mul(850_000)) + |(_, &subkeys): (&Key, &u32)| { + T::DbWeight::get().writes(Weight::from(subkeys) + 1) + .saturating_add((Weight::from(subkeys) + 1).saturating_mul(850_000)) }, DispatchClass::Operational, Pays::Yes, From e875f5d446c88d631086a7bce3183273fc278c0f Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 20:43:12 +0200 Subject: [PATCH 13/23] Revert "Use scheduler in Sudo" This reverts commit 95bd2768dfea100bdf682cf4fe6c0f46e8e1f66e. --- bin/node-template/runtime/src/lib.rs | 1 - bin/node/runtime/src/lib.rs | 1 - frame/sudo/src/lib.rs | 52 +++++++--------------------- frame/system/src/lib.rs | 4 +-- 4 files changed, 14 insertions(+), 44 deletions(-) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 32aaa5966edd2..44332f61a1583 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -230,7 +230,6 @@ impl transaction_payment::Trait for Runtime { impl sudo::Trait for Runtime { type Event = Event; type Call = Call; - type Scheduler = Sudo; } /// Used for the module template in `./template.rs` diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 98c5f654065e8..9742a8a9be9aa 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -488,7 +488,6 @@ impl pallet_contracts::Trait for Runtime { impl pallet_sudo::Trait for Runtime { type Event = Event; type Call = Call; - type Scheduler = Scheduler; } parameter_types! { diff --git a/frame/sudo/src/lib.rs b/frame/sudo/src/lib.rs index 3de9740f6510f..d19c92358c287 100644 --- a/frame/sudo/src/lib.rs +++ b/frame/sudo/src/lib.rs @@ -58,7 +58,7 @@ //! //! decl_module! { //! pub struct Module for enum Call where origin: T::Origin { -//! #[weight = 0] +//! #[weight = 0] //! pub fn privileged_function(origin) -> dispatch::DispatchResult { //! ensure_root(origin)?; //! @@ -87,13 +87,12 @@ #![cfg_attr(not(feature = "std"), no_std)] use sp_std::prelude::*; -use sp_runtime::{DispatchResult, traits::{StaticLookup, Dispatchable, One}}; +use sp_runtime::{DispatchResult, traits::{StaticLookup, Dispatchable}}; use frame_support::{ Parameter, decl_module, decl_event, decl_storage, decl_error, ensure, }; use frame_support::weights::{GetDispatchInfo, FunctionOf, Pays}; -use frame_support::traits::schedule::{self, Anon as ScheduleAnon}; use frame_system::{self as system, ensure_signed}; pub trait Trait: frame_system::Trait { @@ -102,9 +101,6 @@ pub trait Trait: frame_system::Trait { /// A sudo-able call. type Call: Parameter + Dispatchable + GetDispatchInfo; - - /// The Scheduler. - type Scheduler: ScheduleAnon::Call>; } decl_module! { @@ -114,7 +110,7 @@ decl_module! { fn deposit_event() = default; - /// Authenticates the sudo key and schedules a function call with `Root` origin. + /// Authenticates the sudo key and dispatches a function call with `Root` origin. /// /// The dispatch origin for this call must be _Signed_. /// @@ -124,19 +120,18 @@ decl_module! { /// - One DB write (event). /// - Weight of derivative `call` execution + 10,000. /// # - #[weight = 10_000_000] - fn sudo(origin, call: Box<::Call>, priority: schedule::Priority) { + #[weight = FunctionOf( + |args: (&Box<::Call>,)| args.0.get_dispatch_info().weight + 10_000, + |args: (&Box<::Call>,)| args.0.get_dispatch_info().class, + Pays::Yes, + )] + fn sudo(origin, call: Box<::Call>) { // This is a public call, so we ensure that the origin is some signed account. let sender = ensure_signed(origin)?; ensure!(sender == Self::key(), Error::::RequireSudo); - let block_number = frame_system::Module::::block_number(); - - T::Scheduler::schedule( - block_number + T::BlockNumber::one(), - None, - priority, - *call, - ); + + let res = call.dispatch(frame_system::RawOrigin::Root.into()); + Self::deposit_event(RawEvent::Sudid(res.map(|_| ()).map_err(|e| e.error))); } /// Authenticates the current sudo key and sets the given AccountId (`new`) as the new sudo key. @@ -224,26 +219,3 @@ decl_error! { RequireSudo, } } - -// An implementation of scheduler that directly dispatches a call. -// Useful if you do not want to include a proper scheduler, but does not -// respect block Weight limits! -impl schedule::Anon::Call> for Module { - type Address = (); - - /// This directly dispatches a call in the current block. - fn schedule( - _when: T::BlockNumber, - _maybe_periodic: Option>, - _priority: schedule::Priority, - call: ::Call - ) -> Self::Address { - let res = call.dispatch(frame_system::RawOrigin::Root.into()); - Self::deposit_event(RawEvent::Sudid(res.map(|_| ()).map_err(|e| e.error))); - } - - /// You cannot cancel a direct dispatch, so this will always return `Err`. - fn cancel(_address: Self::Address) -> Result<(), ()> { - Err(()) - } -} diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 604a348a2dc68..ddc0572e4c913 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -544,7 +544,7 @@ decl_module! { /// The weight of this function is dependent on the runtime, but generally this is very expensive. /// We will treat this as a full block. /// # - #[weight = (T::MaximumBlockWeight::get(), DispatchClass::Mandatory)] + #[weight = (crate::Module::::max_extrinsic_weight(DispatchClass::Operational), DispatchClass::Operational)] pub fn set_code(origin, code: Vec) { Self::can_set_code(origin, &code)?; @@ -560,7 +560,7 @@ decl_module! { /// - 1 event. /// The weight of this function is dependent on the runtime. We will treat this as a full block. /// # - #[weight = (T::MaximumBlockWeight::get(), DispatchClass::Mandatory)] + #[weight = (crate::Module::::max_extrinsic_weight(DispatchClass::Operational), DispatchClass::Operational)] pub fn set_code_without_checks(origin, code: Vec) { ensure_root(origin)?; storage::unhashed::put_raw(well_known_keys::CODE, &code); From edd51969d713393db80de656e2c830291e60503d Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 22:05:58 +0200 Subject: [PATCH 14/23] remove max extrinsic weight (it does nothing useful) --- frame/system/src/lib.rs | 68 +++++------------------------------------ 1 file changed, 7 insertions(+), 61 deletions(-) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index ddc0572e4c913..6a8e08bf020ba 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -544,7 +544,7 @@ decl_module! { /// The weight of this function is dependent on the runtime, but generally this is very expensive. /// We will treat this as a full block. /// # - #[weight = (crate::Module::::max_extrinsic_weight(DispatchClass::Operational), DispatchClass::Operational)] + #[weight = (T::MaximumBlockWeight::get(), DispatchClass::Operational)] pub fn set_code(origin, code: Vec) { Self::can_set_code(origin, &code)?; @@ -560,7 +560,7 @@ decl_module! { /// - 1 event. /// The weight of this function is dependent on the runtime. We will treat this as a full block. /// # - #[weight = (crate::Module::::max_extrinsic_weight(DispatchClass::Operational), DispatchClass::Operational)] + #[weight = (T::MaximumBlockWeight::get(), DispatchClass::Operational)] pub fn set_code_without_checks(origin, code: Vec) { ensure_root(origin)?; storage::unhashed::put_raw(well_known_keys::CODE, &code); @@ -924,23 +924,6 @@ impl Module { AllExtrinsicsWeight::get().unwrap_or_default() } - /// Get the quota ratio of each dispatch class type. This indicates that all operational and mandatory - /// dispatches can use the full capacity of any resource, while user-triggered ones can consume - /// a portion. - pub fn get_dispatch_limit_ratio(class: DispatchClass) -> Perbill { - match class { - DispatchClass::Operational | DispatchClass::Mandatory - => ::one(), - DispatchClass::Normal => T::AvailableBlockRatio::get(), - } - } - - /// The maximum weight of an allowable extrinsic. Only one of these could exist in a block. - pub fn max_extrinsic_weight(class: DispatchClass) -> Weight { - let limit = Self::get_dispatch_limit_ratio(class) * T::MaximumBlockWeight::get(); - limit - (T::BlockExecutionWeight::get() + T::ExtrinsicBaseWeight::get()) - } - pub fn all_extrinsics_len() -> u32 { AllExtrinsicsLen::get().unwrap_or_default() } @@ -1309,7 +1292,11 @@ impl CheckWeight where /// dispatches can use the full capacity of any resource, while user-triggered ones can consume /// a portion. fn get_dispatch_limit_ratio(class: DispatchClass) -> Perbill { - Module::::get_dispatch_limit_ratio(class) + match class { + DispatchClass::Operational | DispatchClass::Mandatory + => ::one(), + DispatchClass::Normal => T::AvailableBlockRatio::get(), + } } /// Checks if the current extrinsic can fit into the block with respect to block weight limits. @@ -2134,47 +2121,6 @@ pub(crate) mod tests { }) } - #[test] - fn max_extrinsic_weight_is_allowed_but_nothing_more() { - // Dispatch Class Normal - new_test_ext().execute_with(|| { - let one = DispatchInfo { weight: 1, ..Default::default() }; - let max = DispatchInfo { weight: System::max_extrinsic_weight(DispatchClass::Normal), ..Default::default() }; - let len = 0_usize; - - assert_ok!(CheckWeight::::do_pre_dispatch(&max, len)); - assert_err!( - CheckWeight::::do_pre_dispatch(&one, len), - InvalidTransaction::ExhaustsResources, - ); - // Weight should be 75% of 1024 (max block weight) - assert_eq!(System::all_extrinsics_weight(), 768); - }); - - // Dispatch Class Operational - new_test_ext().execute_with(|| { - let one = DispatchInfo { - weight: 1, - class: DispatchClass::Operational, - ..Default::default() - }; - let max = DispatchInfo { - weight: System::max_extrinsic_weight(DispatchClass::Operational), - class: DispatchClass::Operational, - ..Default::default() - }; - let len = 0_usize; - - assert_ok!(CheckWeight::::do_pre_dispatch(&max, len)); - assert_err!( - CheckWeight::::do_pre_dispatch(&one, len), - InvalidTransaction::ExhaustsResources, - ); - // Weight should be 100% of max block weight - assert_eq!(System::all_extrinsics_weight(), ::MaximumBlockWeight::get()); - }); - } - #[test] fn mandatory_extrinsic_doesnt_care_about_limits() { new_test_ext().execute_with(|| { From 45f4c2671baab1b41bae87ffe11b021ce365504b Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 22:12:55 +0200 Subject: [PATCH 15/23] fix tests --- frame/system/src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 6a8e08bf020ba..034bcfeeedb3a 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -1719,7 +1719,7 @@ pub(crate) mod tests { use sp_std::cell::RefCell; use sp_core::H256; use sp_runtime::{traits::{BlakeTwo256, IdentityLookup, SignedExtension}, testing::Header, DispatchError}; - use frame_support::{impl_outer_origin, parameter_types, assert_ok, assert_err}; + use frame_support::{impl_outer_origin, parameter_types, assert_ok}; impl_outer_origin! { pub enum Origin for Test where system = super {} @@ -2154,9 +2154,6 @@ pub(crate) mod tests { // 10 is taken for block execution weight // So normal extrinsic can be 758 weight (-5 for base extrinsic weight) // And Operational can be 256 to produce a full block (-5 for base) - - assert_eq!(System::max_extrinsic_weight(DispatchClass::Normal), 753); - let max_normal = DispatchInfo { weight: 753, ..Default::default() }; let rest_operational = DispatchInfo { weight: 251, class: DispatchClass::Operational, ..Default::default() }; From eb986b260e78d4ca38c6a6220868e44f636ef37c Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 22:40:21 +0200 Subject: [PATCH 16/23] introduce `sudo_unchecked_weight` --- frame/democracy/src/lib.rs | 2 +- frame/sudo/src/lib.rs | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/frame/democracy/src/lib.rs b/frame/democracy/src/lib.rs index ddc3b36f4c877..dc946c36e038c 100644 --- a/frame/democracy/src/lib.rs +++ b/frame/democracy/src/lib.rs @@ -1253,7 +1253,7 @@ decl_module! { } /// Enact a proposal from a referendum. For now we just make the weight be the maximum. - #[weight = frame_system::Module::::max_extrinsic_weight(DispatchClass::Normal)] + #[weight = T::MaximumBlockWeight::get()] fn enact_proposal(origin, proposal_hash: T::Hash, index: ReferendumIndex) -> DispatchResult { ensure_root(origin)?; Self::do_enact_proposal(proposal_hash, index) diff --git a/frame/sudo/src/lib.rs b/frame/sudo/src/lib.rs index d19c92358c287..2e7a53140fe7b 100644 --- a/frame/sudo/src/lib.rs +++ b/frame/sudo/src/lib.rs @@ -92,7 +92,7 @@ use sp_runtime::{DispatchResult, traits::{StaticLookup, Dispatchable}}; use frame_support::{ Parameter, decl_module, decl_event, decl_storage, decl_error, ensure, }; -use frame_support::weights::{GetDispatchInfo, FunctionOf, Pays}; +use frame_support::weights::{Weight, GetDispatchInfo, FunctionOf, Pays}; use frame_system::{self as system, ensure_signed}; pub trait Trait: frame_system::Trait { @@ -134,6 +134,30 @@ decl_module! { Self::deposit_event(RawEvent::Sudid(res.map(|_| ()).map_err(|e| e.error))); } + /// Authenticates the sudo key and dispatches a function call with `Root` origin. + /// This function does not check the weight of the call, and instead allows the + /// Sudo user to specify the weight of the call. + /// + /// The dispatch origin for this call must be _Signed_. + /// + /// # + /// - O(1). + /// - The weight of this call is defined by the caller. + /// # + #[weight = FunctionOf( + |(_, &weight): (&Box<::Call>,&Weight,)| weight, + |(call, _): (&Box<::Call>,&Weight,)| call.get_dispatch_info().class, + Pays::Yes, + )] + fn sudo_unchecked_weight(origin, call: Box<::Call>, _weight: Weight) { + // This is a public call, so we ensure that the origin is some signed account. + let sender = ensure_signed(origin)?; + ensure!(sender == Self::key(), Error::::RequireSudo); + + let res = call.dispatch(frame_system::RawOrigin::Root.into()); + Self::deposit_event(RawEvent::Sudid(res.map(|_| ()).map_err(|e| e.error))); + } + /// Authenticates the current sudo key and sets the given AccountId (`new`) as the new sudo key. /// /// The dispatch origin for this call must be _Signed_. From 45009c4455ae1b530d480980b5fa8e78569931b5 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 5 May 2020 22:42:41 +0200 Subject: [PATCH 17/23] bump spec version --- bin/node/runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 9742a8a9be9aa..c7c5b77b9f205 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -86,7 +86,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 246, + spec_version: 247, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From 32e86c9e19c9410c867d250facc351a3592b03c0 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 6 May 2020 21:28:14 +0200 Subject: [PATCH 18/23] scheduler 80 percent of maximum --- bin/node/runtime/src/lib.rs | 6 +++++- frame/democracy/src/tests.rs | 6 +++++- frame/scheduler/src/lib.rs | 8 ++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index c7c5b77b9f205..28e6d397e3b3d 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -170,11 +170,15 @@ impl pallet_utility::Trait for Runtime { type MaxSignatories = MaxSignatories; } +parameter_types! { + pub const MaximumWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get(); +} + impl pallet_scheduler::Trait for Runtime { type Event = Event; type Origin = Origin; type Call = Call; - type MaximumWeight = MaximumBlockWeight; + type MaximumWeight = MaximumWeight ; } parameter_types! { diff --git a/frame/democracy/src/tests.rs b/frame/democracy/src/tests.rs index 354caee648285..8dbc3454a674a 100644 --- a/frame/democracy/src/tests.rs +++ b/frame/democracy/src/tests.rs @@ -106,7 +106,8 @@ impl frame_system::Trait for Test { type OnKilledAccount = (); } parameter_types! { - pub const ExistentialDeposit: u64 = 1; + // 10_000 + pub const MaximumWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get(); } impl pallet_scheduler::Trait for Test { type Event = Event; @@ -114,6 +115,9 @@ impl pallet_scheduler::Trait for Test { type Call = Call; type MaximumWeight = MaximumBlockWeight; } +parameter_types! { + pub const ExistentialDeposit: u64 = 1; +} impl pallet_balances::Trait for Test { type Balance = u64; type Event = Event; diff --git a/frame/scheduler/src/lib.rs b/frame/scheduler/src/lib.rs index feebd8dcb5786..8b7e58972378b 100644 --- a/frame/scheduler/src/lib.rs +++ b/frame/scheduler/src/lib.rs @@ -331,7 +331,7 @@ mod tests { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; - pub const MaximumBlockWeight: Weight = 10_000; + pub const MaximumBlockWeight: Weight = 12_500; pub const MaximumBlockLength: u32 = 2 * 1024; pub const AvailableBlockRatio: Perbill = Perbill::one(); } @@ -362,11 +362,15 @@ mod tests { impl logger::Trait for Test { type Event = (); } + parameter_types! { + // 10_000 + pub const MaximumWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get(); + } impl Trait for Test { type Event = (); type Origin = Origin; type Call = Call; - type MaximumWeight = MaximumBlockWeight; + type MaximumWeight = MaximumWeight; } type System = system::Module; type Logger = logger::Module; From db15aaac941502d0b1c6b8a5df994f8e341fd213 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 7 May 2020 00:34:54 +0200 Subject: [PATCH 19/23] Update `set_changes_trie_config` weight --- frame/system/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index c41522af8fc63..8d449963f8a39 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -573,12 +573,11 @@ decl_module! { /// - `O(D)` where `D` length of `Digest` /// - 1 storage write or delete (codec `O(1)`). /// - 1 call to `deposit_log`: `O(D)` (which depends on the length of `Digest`) - /// - Base Weight: 9.29 + 0.511 * D µs + /// - Base Weight: 7.218 + 0.001 * D µs /// - DB Weight: - /// - Reads: System Digest /// - Writes: Changes Trie, System Digest /// # - #[weight = (T::DbWeight::get().reads_writes(1, 2) + 10_000_000, DispatchClass::Operational)] + #[weight = (T::DbWeight::get().writes(2) + 10_000_000, DispatchClass::Operational)] pub fn set_changes_trie_config(origin, changes_trie_config: Option) { ensure_root(origin)?; match changes_trie_config.clone() { From dea831a4abb1ba04f70f4734b1fc4c99067078ad Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 7 May 2020 00:39:43 +0200 Subject: [PATCH 20/23] Update bin/node/runtime/src/lib.rs --- bin/node/runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index c28c59c05c16a..428f21f312a3d 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -182,7 +182,7 @@ impl pallet_scheduler::Trait for Runtime { type Event = Event; type Origin = Origin; type Call = Call; - type MaximumWeight = MaximumWeight ; + type MaximumWeight = MaximumWeight; } parameter_types! { From 15f8ad2f269e43d29fbfa5e80166ea7d071c252f Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 7 May 2020 00:40:26 +0200 Subject: [PATCH 21/23] Update frame/democracy/src/tests.rs --- frame/democracy/src/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/frame/democracy/src/tests.rs b/frame/democracy/src/tests.rs index 8dbc3454a674a..8578487e4f19a 100644 --- a/frame/democracy/src/tests.rs +++ b/frame/democracy/src/tests.rs @@ -106,7 +106,6 @@ impl frame_system::Trait for Test { type OnKilledAccount = (); } parameter_types! { - // 10_000 pub const MaximumWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get(); } impl pallet_scheduler::Trait for Test { From 259359592164b017a72af604d4653a125ad73acc Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 7 May 2020 01:00:26 +0200 Subject: [PATCH 22/23] Update tests.rs --- frame/democracy/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/democracy/src/tests.rs b/frame/democracy/src/tests.rs index 8578487e4f19a..32560273c0d70 100644 --- a/frame/democracy/src/tests.rs +++ b/frame/democracy/src/tests.rs @@ -112,7 +112,7 @@ impl pallet_scheduler::Trait for Test { type Event = Event; type Origin = Origin; type Call = Call; - type MaximumWeight = MaximumBlockWeight; + type MaximumWeight = MaximumWeight; } parameter_types! { pub const ExistentialDeposit: u64 = 1; From f1bdc7ee0a7efebabf6c3e7516a22539ec20ffad Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 7 May 2020 12:30:10 +0200 Subject: [PATCH 23/23] update based on feedback --- bin/node/runtime/src/lib.rs | 4 ++-- frame/democracy/src/tests.rs | 4 ++-- frame/scheduler/src/lib.rs | 6 +++--- frame/system/src/lib.rs | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 428f21f312a3d..596a74b7dbcea 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -175,14 +175,14 @@ impl pallet_utility::Trait for Runtime { } parameter_types! { - pub const MaximumWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get(); + pub const MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get(); } impl pallet_scheduler::Trait for Runtime { type Event = Event; type Origin = Origin; type Call = Call; - type MaximumWeight = MaximumWeight; + type MaximumWeight = MaximumSchedulerWeight; } parameter_types! { diff --git a/frame/democracy/src/tests.rs b/frame/democracy/src/tests.rs index 32560273c0d70..a835a0ff6ee1e 100644 --- a/frame/democracy/src/tests.rs +++ b/frame/democracy/src/tests.rs @@ -106,13 +106,13 @@ impl frame_system::Trait for Test { type OnKilledAccount = (); } parameter_types! { - pub const MaximumWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get(); + pub const MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get(); } impl pallet_scheduler::Trait for Test { type Event = Event; type Origin = Origin; type Call = Call; - type MaximumWeight = MaximumWeight; + type MaximumWeight = MaximumSchedulerWeight; } parameter_types! { pub const ExistentialDeposit: u64 = 1; diff --git a/frame/scheduler/src/lib.rs b/frame/scheduler/src/lib.rs index 8b7e58972378b..c3d0a0e168be8 100644 --- a/frame/scheduler/src/lib.rs +++ b/frame/scheduler/src/lib.rs @@ -363,14 +363,14 @@ mod tests { type Event = (); } parameter_types! { - // 10_000 - pub const MaximumWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get(); + // 0.8 * 12_500 = 10_000 + pub const MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get(); } impl Trait for Test { type Event = (); type Origin = Origin; type Call = Call; - type MaximumWeight = MaximumWeight; + type MaximumWeight = MaximumSchedulerWeight; } type System = system::Module; type Logger = logger::Module; diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 5dc803ee6838a..8346b727b254b 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -570,10 +570,10 @@ decl_module! { /// Set the new changes trie configuration. /// /// # - /// - `O(D)` where `D` length of `Digest` + /// - `O(1)` /// - 1 storage write or delete (codec `O(1)`). - /// - 1 call to `deposit_log`: `O(D)` (which depends on the length of `Digest`) - /// - Base Weight: 7.218 + 0.001 * D µs + /// - 1 call to `deposit_log`: Uses `append` API, so O(1) + /// - Base Weight: 7.218 µs /// - DB Weight: /// - Writes: Changes Trie, System Digest /// # @@ -599,7 +599,7 @@ decl_module! { /// # /// - `O(I)` where `I` length of `items` /// - `I` storage writes (`O(1)`). - /// - Base Weight: 0.568 * i + /// - Base Weight: 0.568 * i µs /// - Writes: Number of items /// # #[weight = FunctionOf(