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

Commit f0eef06

Browse files
authored
Weights for Indices Pallet (#6282)
* fix multisig benchmarking * add indices benchmarks * fix compile * Weights for indices
1 parent 669702e commit f0eef06

File tree

9 files changed

+153
-17
lines changed

9 files changed

+153
-17
lines changed

Cargo.lock

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

bin/node/runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ runtime-benchmarks = [
153153
"pallet-elections-phragmen/runtime-benchmarks",
154154
"pallet-identity/runtime-benchmarks",
155155
"pallet-im-online/runtime-benchmarks",
156+
"pallet-indices/runtime-benchmarks",
156157
"pallet-multisig/runtime-benchmarks",
157158
"pallet-proxy/runtime-benchmarks",
158159
"pallet-scheduler/runtime-benchmarks",

bin/node/runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,7 @@ impl_runtime_apis! {
10811081
add_benchmark!(params, batches, b"elections", Elections);
10821082
add_benchmark!(params, batches, b"identity", Identity);
10831083
add_benchmark!(params, batches, b"im-online", ImOnline);
1084+
add_benchmark!(params, batches, b"indices", Indices);
10841085
add_benchmark!(params, batches, b"multisig", Multisig);
10851086
add_benchmark!(params, batches, b"offences", OffencesBench::<Runtime>);
10861087
add_benchmark!(params, batches, b"proxy", Proxy);

frame/indices/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ sp-core = { version = "2.0.0-rc2", default-features = false, path = "../../primi
2222
frame-support = { version = "2.0.0-rc2", default-features = false, path = "../support" }
2323
frame-system = { version = "2.0.0-rc2", default-features = false, path = "../system" }
2424

25+
frame-benchmarking = { version = "2.0.0-rc2", default-features = false, path = "../benchmarking", optional = true }
26+
2527
[dev-dependencies]
2628
pallet-balances = { version = "2.0.0-rc2", path = "../balances" }
2729

@@ -38,3 +40,7 @@ std = [
3840
"sp-runtime/std",
3941
"frame-system/std",
4042
]
43+
runtime-benchmarks = [
44+
"frame-benchmarking",
45+
"frame-support/runtime-benchmarks",
46+
]

frame/indices/src/benchmarking.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2019-2020 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+
// Benchmarks for Indices Pallet
19+
20+
#![cfg(feature = "runtime-benchmarks")]
21+
22+
use super::*;
23+
use frame_system::RawOrigin;
24+
use frame_benchmarking::{benchmarks, account};
25+
use sp_runtime::traits::Bounded;
26+
27+
use crate::Module as Indices;
28+
29+
const SEED: u32 = 0;
30+
31+
benchmarks! {
32+
_ { }
33+
34+
claim {
35+
// Index being claimed
36+
let i in 0 .. 1000;
37+
let account_index = T::AccountIndex::from(i);
38+
let caller: T::AccountId = account("caller", 0, SEED);
39+
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
40+
}: _(RawOrigin::Signed(caller.clone()), account_index)
41+
verify {
42+
assert_eq!(Accounts::<T>::get(account_index).unwrap().0, caller);
43+
}
44+
45+
transfer {
46+
// Index being claimed
47+
let i in 0 .. 1000;
48+
let account_index = T::AccountIndex::from(i);
49+
// Setup accounts
50+
let caller: T::AccountId = account("caller", 0, SEED);
51+
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
52+
let recipient: T::AccountId = account("recipient", i, SEED);
53+
T::Currency::make_free_balance_be(&recipient, BalanceOf::<T>::max_value());
54+
// Claim the index
55+
Indices::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
56+
}: _(RawOrigin::Signed(caller.clone()), recipient.clone(), account_index)
57+
verify {
58+
assert_eq!(Accounts::<T>::get(account_index).unwrap().0, recipient);
59+
}
60+
61+
free {
62+
// Index being claimed
63+
let i in 0 .. 1000;
64+
let account_index = T::AccountIndex::from(i);
65+
// Setup accounts
66+
let caller: T::AccountId = account("caller", 0, SEED);
67+
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
68+
// Claim the index
69+
Indices::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
70+
}: _(RawOrigin::Signed(caller.clone()), account_index)
71+
verify {
72+
assert_eq!(Accounts::<T>::get(account_index), None);
73+
}
74+
75+
force_transfer {
76+
// Index being claimed
77+
let i in 0 .. 1000;
78+
let account_index = T::AccountIndex::from(i);
79+
// Setup accounts
80+
let original: T::AccountId = account("original", 0, SEED);
81+
T::Currency::make_free_balance_be(&original, BalanceOf::<T>::max_value());
82+
let recipient: T::AccountId = account("recipient", i, SEED);
83+
T::Currency::make_free_balance_be(&recipient, BalanceOf::<T>::max_value());
84+
// Claim the index
85+
Indices::<T>::claim(RawOrigin::Signed(original).into(), account_index)?;
86+
}: _(RawOrigin::Root, recipient.clone(), account_index)
87+
verify {
88+
assert_eq!(Accounts::<T>::get(account_index).unwrap().0, recipient);
89+
}
90+
91+
// TODO in another PR: lookup and unlookup trait weights (not critical)
92+
}
93+
94+
#[cfg(test)]
95+
mod tests {
96+
use super::*;
97+
use crate::mock::{new_test_ext, Test};
98+
use frame_support::assert_ok;
99+
100+
#[test]
101+
fn test_benchmarks() {
102+
new_test_ext().execute_with(|| {
103+
assert_ok!(test_benchmark_claim::<Test>());
104+
assert_ok!(test_benchmark_transfer::<Test>());
105+
assert_ok!(test_benchmark_free::<Test>());
106+
assert_ok!(test_benchmark_force_transfer::<Test>());
107+
});
108+
}
109+
}

frame/indices/src/lib.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ use sp_runtime::traits::{
2828
use frame_support::{Parameter, decl_module, decl_error, decl_event, decl_storage, ensure};
2929
use frame_support::dispatch::DispatchResult;
3030
use frame_support::traits::{Currency, ReservableCurrency, Get, BalanceStatus::Reserved};
31+
use frame_support::weights::constants::WEIGHT_PER_MICROS;
3132
use frame_system::{ensure_signed, ensure_root};
3233
use self::address::Address as RawAddress;
3334

3435
mod mock;
3536
pub mod address;
3637
mod tests;
38+
mod benchmarking;
3739

3840
pub type Address<T> = RawAddress<<T as frame_system::Trait>::AccountId, <T as Trait>::AccountIndex>;
3941
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
@@ -113,8 +115,11 @@ decl_module! {
113115
/// - One storage mutation (codec `O(1)`).
114116
/// - One reserve operation.
115117
/// - One event.
118+
/// -------------------
119+
/// - Base Weight: 28.69 µs
120+
/// - DB Weight: 1 Read/Write (Accounts)
116121
/// # </weight>
117-
#[weight = 0]
122+
#[weight = T::DbWeight::get().reads_writes(1, 1) + 30 * WEIGHT_PER_MICROS]
118123
fn claim(origin, index: T::AccountIndex) {
119124
let who = ensure_signed(origin)?;
120125

@@ -141,8 +146,13 @@ decl_module! {
141146
/// - One storage mutation (codec `O(1)`).
142147
/// - One transfer operation.
143148
/// - One event.
149+
/// -------------------
150+
/// - Base Weight: 33.74 µs
151+
/// - DB Weight:
152+
/// - Reads: Indices Accounts, System Account (recipient)
153+
/// - Writes: Indices Accounts, System Account (recipient)
144154
/// # </weight>
145-
#[weight = 0]
155+
#[weight = T::DbWeight::get().reads_writes(2, 2) + 35 * WEIGHT_PER_MICROS]
146156
fn transfer(origin, new: T::AccountId, index: T::AccountIndex) {
147157
let who = ensure_signed(origin)?;
148158
ensure!(who != new, Error::<T>::NotTransfer);
@@ -172,8 +182,11 @@ decl_module! {
172182
/// - One storage mutation (codec `O(1)`).
173183
/// - One reserve operation.
174184
/// - One event.
185+
/// -------------------
186+
/// - Base Weight: 25.53 µs
187+
/// - DB Weight: 1 Read/Write (Accounts)
175188
/// # </weight>
176-
#[weight = 0]
189+
#[weight = T::DbWeight::get().reads_writes(1, 1) + 25 * WEIGHT_PER_MICROS]
177190
fn free(origin, index: T::AccountIndex) {
178191
let who = ensure_signed(origin)?;
179192

@@ -201,8 +214,13 @@ decl_module! {
201214
/// - One storage mutation (codec `O(1)`).
202215
/// - Up to one reserve operation.
203216
/// - One event.
217+
/// -------------------
218+
/// - Base Weight: 26.83 µs
219+
/// - DB Weight:
220+
/// - Reads: Indices Accounts, System Account (original owner)
221+
/// - Writes: Indices Accounts, System Account (original owner)
204222
/// # </weight>
205-
#[weight = 0]
223+
#[weight = T::DbWeight::get().reads_writes(2, 2) + 25 * WEIGHT_PER_MICROS]
206224
fn force_transfer(origin, new: T::AccountId, index: T::AccountIndex) {
207225
ensure_root(origin)?;
208226

frame/multisig/src/benchmarking.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use frame_system::RawOrigin;
2424
use frame_benchmarking::{benchmarks, account};
2525
use sp_runtime::traits::Saturating;
2626

27-
use crate::Module as Utility;
27+
use crate::Module as Multisig;
2828

2929
const SEED: u32 = 0;
3030

@@ -66,9 +66,9 @@ benchmarks! {
6666
let mut signatories2 = signatories.clone();
6767
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
6868
// before the call, get the timepoint
69-
let timepoint = Utility::<T>::timepoint();
69+
let timepoint = Multisig::<T>::timepoint();
7070
// Create the multi
71-
Utility::<T>::as_multi(RawOrigin::Signed(caller).into(), s as u16, signatories, None, call.clone())?;
71+
Multisig::<T>::as_multi(RawOrigin::Signed(caller).into(), s as u16, signatories, None, call.clone())?;
7272
let caller2 = signatories2.remove(0);
7373
}: as_multi(RawOrigin::Signed(caller2), s as u16, signatories2, Some(timepoint), call)
7474

@@ -81,15 +81,15 @@ benchmarks! {
8181
let mut signatories2 = signatories.clone();
8282
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
8383
// before the call, get the timepoint
84-
let timepoint = Utility::<T>::timepoint();
84+
let timepoint = Multisig::<T>::timepoint();
8585
// Create the multi
86-
Utility::<T>::as_multi(RawOrigin::Signed(caller).into(), s as u16, signatories, None, call.clone())?;
86+
Multisig::<T>::as_multi(RawOrigin::Signed(caller).into(), s as u16, signatories, None, call.clone())?;
8787
// Everyone except the first person approves
8888
for i in 1 .. s - 1 {
8989
let mut signatories_loop = signatories2.clone();
9090
let caller_loop = signatories_loop.remove(i as usize);
9191
let o = RawOrigin::Signed(caller_loop).into();
92-
Utility::<T>::as_multi(o, s as u16, signatories_loop, Some(timepoint), call.clone())?;
92+
Multisig::<T>::as_multi(o, s as u16, signatories_loop, Some(timepoint), call.clone())?;
9393
}
9494
let caller2 = signatories2.remove(0);
9595
}: as_multi(RawOrigin::Signed(caller2), s as u16, signatories2, Some(timepoint), call)
@@ -115,9 +115,9 @@ benchmarks! {
115115
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
116116
let call_hash = call.using_encoded(blake2_256);
117117
// before the call, get the timepoint
118-
let timepoint = Utility::<T>::timepoint();
118+
let timepoint = Multisig::<T>::timepoint();
119119
// Create the multi
120-
Utility::<T>::as_multi(RawOrigin::Signed(caller).into(), s as u16, signatories, None, call.clone())?;
120+
Multisig::<T>::as_multi(RawOrigin::Signed(caller).into(), s as u16, signatories, None, call.clone())?;
121121
let caller2 = signatories2.remove(0);
122122
}: approve_as_multi(RawOrigin::Signed(caller2), s as u16, signatories2, Some(timepoint), call_hash)
123123

@@ -129,10 +129,10 @@ benchmarks! {
129129
let (mut signatories, call) = setup_multi::<T>(s, z)?;
130130
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
131131
let call_hash = call.using_encoded(blake2_256);
132-
let timepoint = Utility::<T>::timepoint();
132+
let timepoint = Multisig::<T>::timepoint();
133133
// Create the multi
134134
let o = RawOrigin::Signed(caller.clone()).into();
135-
Utility::<T>::as_multi(o, s as u16, signatories.clone(), None, call.clone())?;
135+
Multisig::<T>::as_multi(o, s as u16, signatories.clone(), None, call.clone())?;
136136
}: _(RawOrigin::Signed(caller), s as u16, signatories, timepoint, call_hash)
137137
}
138138

frame/multisig/src/tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ impl Filter<Call> for TestIsCallable {
104104
fn filter(c: &Call) -> bool {
105105
match *c {
106106
Call::Balances(_) => true,
107+
// Needed for benchmarking
108+
Call::System(frame_system::Call::remark(_)) => true,
107109
_ => false,
108110
}
109111
}
@@ -399,7 +401,7 @@ fn multisig_1_of_3_works() {
399401
#[test]
400402
fn multisig_filters() {
401403
new_test_ext().execute_with(|| {
402-
let call = Box::new(Call::System(frame_system::Call::remark(vec![])));
404+
let call = Box::new(Call::System(frame_system::Call::set_code(vec![])));
403405
assert_noop!(
404406
Multisig::as_multi(Origin::signed(1), 1, vec![], None, call.clone()),
405407
Error::<Test>::Uncallable,

frame/utility/src/benchmarking.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
use super::*;
2323
use frame_system::RawOrigin;
2424
use frame_benchmarking::{benchmarks, account};
25-
use sp_runtime::traits::Saturating;
26-
use crate::Module as Utility;
2725

2826
const SEED: u32 = 0;
2927

0 commit comments

Comments
 (0)