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

Benchmark pallet sudo #13880

Merged
merged 24 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
086d363
set_key
Doordashcon Apr 11, 2023
0319a2f
v2
Doordashcon Apr 18, 2023
6e16a8c
Merge branch 'master' into ddc-OG
Doordashcon Apr 18, 2023
6cccbfe
update runtime
Doordashcon Apr 18, 2023
7b5817b
Merge branch 'ddc-OG' of https://github.com/Doordashcon/substrate int…
Doordashcon Apr 18, 2023
fd71be3
cargo fmt
Doordashcon Apr 18, 2023
823d98b
remove commenting
Doordashcon Apr 18, 2023
e082fec
Merge branch 'master' of https://github.com/paritytech/substrate into…
Apr 19, 2023
5078939
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_sudo
Apr 19, 2023
01a597a
impl weights
Doordashcon Apr 20, 2023
811d52d
Merge branch 'paritytech:master' into ddc-OG
Doordashcon Apr 23, 2023
fe6e0a4
Merge branch 'paritytech:master' into ddc-OG
Doordashcon Apr 24, 2023
1890456
sudo & sudo_as
Doordashcon May 1, 2023
885b7b0
Merge branch 'paritytech:master' into ddc-OG
Doordashcon May 3, 2023
28878cf
where
Doordashcon May 6, 2023
6d9d079
Merge branch 'paritytech:master' into ddc-OG
Doordashcon May 6, 2023
f931e7b
Merge branch 'paritytech:master' into ddc-OG
Doordashcon May 8, 2023
e38b8a9
Merge branch 'master' of https://github.com/paritytech/substrate into…
May 9, 2023
891a949
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_sudo
May 9, 2023
8ffafdf
update weights
Doordashcon May 9, 2023
ff180b3
cargo fmt
Doordashcon May 9, 2023
ff4405c
Merge branch 'paritytech:master' into ddc-OG
Doordashcon May 10, 2023
fc1d8af
Merge branch 'paritytech:master' into ddc-OG
Doordashcon May 10, 2023
6c3acef
Update Cargo.lock
ggwpez May 10, 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
3 changes: 2 additions & 1 deletion 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 bin/node-template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-grandpa/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
"pallet-template/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
Expand Down
2 changes: 2 additions & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ impl pallet_transaction_payment::Config for Runtime {
impl pallet_sudo::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
}

/// Configure the pallet-template in pallets/template.
Expand Down Expand Up @@ -336,6 +337,7 @@ mod benches {
[frame_system, SystemBench::<Runtime>]
[pallet_balances, Balances]
[pallet_timestamp, Timestamp]
[pallet_sudo, Sudo]
[pallet_template, TemplateModule]
);
}
Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ runtime-benchmarks = [
"pallet-society/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"pallet-state-trie-migration/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-tips/runtime-benchmarks",
"pallet-transaction-storage/runtime-benchmarks",
Expand Down
2 changes: 2 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,7 @@ impl pallet_contracts::Config for Runtime {
impl pallet_sudo::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
Expand Down Expand Up @@ -1962,6 +1963,7 @@ mod benches {
[pallet_session, SessionBench::<Runtime>]
[pallet_staking, Staking]
[pallet_state_trie_migration, StateTrieMigration]
[pallet_sudo, Sudo]
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
[frame_system, SystemBench::<Runtime>]
[pallet_timestamp, Timestamp]
[pallet_tips, Tips]
Expand Down
7 changes: 7 additions & 0 deletions frame/sudo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
scale-info = { version = "2.5.0", 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" }
Expand All @@ -28,11 +29,17 @@ sp-core = { version = "7.0.0", path = "../../primitives/core" }
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
79 changes: 79 additions & 0 deletions frame/sudo/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// This file is part of Substrate.

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

//! Benchmarks for Sudo Pallet

use super::*;
use crate::Pallet;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;

const SEED: u32 = 0;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

#[benchmarks( where <T as Config>::RuntimeCall: From<frame_system::Call<T>>)]
mod benchmarks {
use super::*;

#[benchmark]
fn set_key() {
let caller: T::AccountId = whitelisted_caller();
Key::<T>::put(caller.clone());

let new_sudoer: T::AccountId = account("sudoer", 0, SEED);
let new_sudoer_lookup = T::Lookup::unlookup(new_sudoer.clone());

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), new_sudoer_lookup);

assert_last_event::<T>(Event::KeyChanged { old_sudoer: Some(caller) }.into());
}

#[benchmark]
fn sudo() {
let caller: T::AccountId = whitelisted_caller();
Key::<T>::put(caller.clone());

let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into();

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), Box::new(call.clone()));

assert_last_event::<T>(Event::Sudid { sudo_result: Ok(()) }.into())
}

#[benchmark]
fn sudo_as() {
let caller: T::AccountId = whitelisted_caller();
Key::<T>::put(caller.clone());

let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into();

let who: T::AccountId = account("as", 0, SEED);
let who_lookup = T::Lookup::unlookup(who.clone());

#[extrinsic_call]
_(RawOrigin::Signed(caller), who_lookup, Box::new(call.clone()));

assert_last_event::<T>(Event::SudoAsDone { sudo_result: Ok(()) }.into())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_bench_ext(), crate::mock::Test);
}
19 changes: 14 additions & 5 deletions frame/sudo/src/lib.rs
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ mod mock;
#[cfg(test)]
mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;
pub use weights::WeightInfo;

pub use extension::CheckOnlySudoAccount;
pub use pallet::*;

Expand All @@ -130,6 +135,9 @@ pub mod pallet {
type RuntimeCall: Parameter
+ UnfilteredDispatchable<RuntimeOrigin = Self::RuntimeOrigin>
+ GetDispatchInfo;

/// Type representing the weight of this pallet
type WeightInfo: WeightInfo;
}

#[pallet::pallet]
Expand All @@ -146,7 +154,10 @@ pub mod pallet {
#[pallet::call_index(0)]
#[pallet::weight({
let dispatch_info = call.get_dispatch_info();
(dispatch_info.weight, dispatch_info.class)
(
T::WeightInfo::sudo().saturating_add(dispatch_info.weight),
dispatch_info.class
)
})]
pub fn sudo(
origin: OriginFor<T>,
Expand Down Expand Up @@ -195,7 +206,7 @@ pub mod pallet {
/// ## Complexity
/// - O(1).
#[pallet::call_index(2)]
#[pallet::weight({0})] // FIXME
#[pallet::weight(T::WeightInfo::set_key())]
pub fn set_key(
origin: OriginFor<T>,
new: AccountIdLookupOf<T>,
Expand All @@ -222,9 +233,7 @@ pub mod pallet {
#[pallet::weight({
let dispatch_info = call.get_dispatch_info();
(
dispatch_info.weight
// AccountData for inner call origin accountdata.
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
T::WeightInfo::sudo_as().saturating_add(dispatch_info.weight),
dispatch_info.class,
)
})]
Expand Down
6 changes: 6 additions & 0 deletions frame/sudo/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl logger::Config for Test {
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type WeightInfo = ();
}

// New types for dispatchable functions.
Expand All @@ -162,3 +163,8 @@ pub fn new_test_ext(root_key: u64) -> sp_io::TestExternalities {
.unwrap();
t.into()
}

#[cfg(feature = "runtime-benchmarks")]
pub fn new_bench_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
}
127 changes: 127 additions & 0 deletions frame/sudo/src/weights.rs

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