Skip to content

Commit 699b1bc

Browse files
Neopalliumadamdossa
authored andcommitted
Permissions benchmarks cleanup (#1193)
* Some comments and cleanup. * Add docs to helper functions. * Add more comments to benchmark.
1 parent 9df2569 commit 699b1bc

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

pallets/common/src/traits/identity.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ pub trait WeightInfo {
102102
fn add_investor_uniqueness_claim_v2() -> Weight;
103103
fn revoke_claim_by_index() -> Weight;
104104

105-
// Helpers for extrinsics with Permissions.
105+
/// Add complexity cost of Permissions to `add_secondary_keys_with_authorization` extrinsic.
106106
fn add_secondary_keys_full<AccountId>(
107107
additional_keys: &[SecondaryKeyWithAuth<AccountId>],
108108
) -> Weight {
109109
let perm_cost = additional_keys.iter().fold(0u64, |cost, key_with_auth| {
110110
let (assets, portfolios, pallets, extrinsics) =
111-
key_with_auth.secondary_key.permissions.get_weights();
111+
key_with_auth.secondary_key.permissions.counts();
112112
let perm_cost = Self::permissions_cost(assets, portfolios, pallets, extrinsics);
113113
cost.saturating_add(perm_cost)
114114
});
@@ -117,10 +117,11 @@ pub trait WeightInfo {
117117
))
118118
}
119119

120+
/// Add complexity cost of Permissions to `add_authorization` extrinsic.
120121
fn add_authorization_full<AccountId>(data: &AuthorizationData<AccountId>) -> Weight {
121122
let perm_cost = match data {
122123
AuthorizationData::JoinIdentity(perms) => {
123-
let (assets, portfolios, pallets, extrinsics) = perms.get_weights();
124+
let (assets, portfolios, pallets, extrinsics) = perms.counts();
124125
Self::permissions_cost(assets, portfolios, pallets, extrinsics)
125126
}
126127
_ => 0,
@@ -129,13 +130,16 @@ pub trait WeightInfo {
129130
perm_cost.saturating_add(Self::add_authorization())
130131
}
131132

133+
/// Add complexity cost of Permissions to `set_permission_to_signer` extrinsic.
132134
fn set_permission_to_signer_full(perms: &Permissions) -> Weight {
133-
let (assets, portfolios, pallets, extrinsics) = perms.get_weights();
135+
let (assets, portfolios, pallets, extrinsics) = perms.counts();
134136
Self::permissions_cost(assets, portfolios, pallets, extrinsics)
135137
.saturating_add(Self::set_permission_to_signer())
136138
}
139+
140+
/// Add complexity cost of Permissions to `legacy_set_permission_to_signer` extrinsic.
137141
fn legacy_set_permission_to_signer_full(perms: &LegacyPermissions) -> Weight {
138-
let (assets, portfolios, pallets, extrinsics) = perms.get_weights();
142+
let (assets, portfolios, pallets, extrinsics) = perms.counts();
139143
Self::permissions_cost(assets, portfolios, pallets, extrinsics)
140144
.saturating_add(Self::set_permission_to_signer())
141145
}

pallets/identity/src/benchmarking.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,22 @@ benchmarks! {
305305
Module::<T>::unsafe_join_identity(target.did(), Permissions::empty(), key.account());
306306
}: _(target.origin, signatory, Permissions::default().into())
307307

308+
// Benchmark the memory/cpu complexity of Permissions.
308309
permissions_cost {
309310
// Number of assets/portfolios/pallets/extrinsics.
310311
let a in 0 .. MAX_ASSETS; // a=(A)ssets
311312
let p in 0 .. MAX_PORTFOLIOS; // p=(P)ortfolios
312313
let l in 0 .. MAX_PALLETS; // l=pa(L)lets
313314
let e in 0 .. MAX_EXTRINSICS; // e=(E)xtrinsics
315+
// When the benchmarks run for parameter `e` (number of extrinsics)
316+
// it will use `l == MAX_PALLETS`. `e` will be the number of
317+
// extrinsics per pallet. So the total number of extrinsics in
318+
// the `Permissions` will be `MAX_PALLETS * e`.
319+
//
320+
// When calculating the weight of a `Permissions` value in a
321+
// transaction, we use the total number of extrinsics in the
322+
// permissions. This is to make sure that the worst-case cost
323+
// is covered.
314324

315325
let asset = AssetPermissions::elems(
316326
(0..a as u64).map(Ticker::generate_into)
@@ -339,14 +349,19 @@ benchmarks! {
339349
extrinsic,
340350
portfolio
341351
};
342-
343-
let orig = permissions.clone();
344352
}: {
345-
let enc = permissions.encode();
346-
// Panic the benchmarks if decoding fails.
347-
let perm = Permissions::decode(&mut enc.as_slice())
348-
.expect("Permissions should decode correctly.");
349-
assert_eq!(orig, perm);
353+
// For this benchmark we need to do some "work" based on
354+
// how complex the permissions object is.
355+
356+
// 1. Encode the Permissions value.
357+
let encoded = permissions.encode();
358+
// 2. Decode the Permissions value.
359+
let decoded = Permissions::decode(&mut encoded.as_slice())
360+
.expect("This shouldn't fail since we just encoded a Permissions value.");
361+
// 3. Compare the original and decoded values. This will touch the full value.
362+
if !permissions.eq(&decoded) {
363+
panic!("This shouldn't fail.");
364+
}
350365
}
351366

352367
freeze_secondary_keys {

primitives/src/secondary_key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Permissions {
190190
/// Return number of assets, portfolios, pallets, and extrinsics.
191191
///
192192
/// This is used for weight calculation.
193-
pub fn get_weights(&self) -> (u32, u32, u32, u32) {
193+
pub fn counts(&self) -> (u32, u32, u32, u32) {
194194
// Count the number of assets.
195195
let assets = self.asset.complexity().try_into().unwrap_or(u32::MAX);
196196
// Count the number of portfolios.
@@ -452,7 +452,7 @@ pub mod api {
452452
/// Return number of assets, portfolios, pallets, and extrinsics.
453453
///
454454
/// This is used for weight calculation.
455-
pub fn get_weights(&self) -> (u32, u32, u32, u32) {
455+
pub fn counts(&self) -> (u32, u32, u32, u32) {
456456
// Count the number of assets.
457457
let assets = self.asset.complexity().try_into().unwrap_or(u32::MAX);
458458
// Count the number of portfolios.

0 commit comments

Comments
 (0)