Skip to content

Commit e28f934

Browse files
committed
address lints
1 parent ae4a15e commit e28f934

File tree

5 files changed

+96
-81
lines changed

5 files changed

+96
-81
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.

pallets/proxy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ frame-support.workspace = true
2222
frame-system.workspace = true
2323
sp-io.workspace = true
2424
sp-runtime.workspace = true
25+
subtensor-macros.workspace = true
2526

2627
[dev-dependencies]
2728
pallet-balances = { default-features = true, workspace = true }

pallets/proxy/src/benchmarking.rs

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// This file is part of Substrate.
2-
2+
//
33
// Copyright (C) Parity Technologies (UK) Ltd.
44
// SPDX-License-Identifier: Apache-2.0
5-
5+
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
88
// You may obtain a copy of the License at
99
//
10-
// http://www.apache.org/licenses/LICENSE-2.0
10+
// http://www.apache.org/licenses/LICENSE-2.0/
1111
//
1212
// Unless required by applicable law or agreed to in writing, software
1313
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -24,17 +24,23 @@ use crate::Pallet as Proxy;
2424
use alloc::{boxed::Box, vec};
2525
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller};
2626
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
27-
use sp_runtime::traits::Bounded;
27+
use sp_runtime::traits::{Bounded, CheckedDiv};
2828

2929
const SEED: u32 = 0;
3030

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

35+
fn half_max_balance<T: Config>() -> BalanceOf<T> {
36+
BalanceOf::<T>::max_value()
37+
.checked_div(&BalanceOf::<T>::from(2_u32))
38+
.unwrap_or_else(BalanceOf::<T>::max_value)
39+
}
40+
3541
fn add_proxies<T: Config>(n: u32, maybe_who: Option<T::AccountId>) -> Result<(), &'static str> {
3642
let caller = maybe_who.unwrap_or_else(whitelisted_caller);
37-
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value() / 2u32.into());
43+
T::Currency::make_free_balance_be(&caller, half_max_balance::<T>());
3844
for i in 0..n {
3945
let real = T::Lookup::unlookup(account("target", i, SEED));
4046

@@ -55,12 +61,12 @@ fn add_announcements<T: Config>(
5561
) -> Result<(), &'static str> {
5662
let caller = maybe_who.unwrap_or_else(|| account("caller", 0, SEED));
5763
let caller_lookup = T::Lookup::unlookup(caller.clone());
58-
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value() / 2u32.into());
64+
T::Currency::make_free_balance_be(&caller, half_max_balance::<T>());
5965
let real = if let Some(real) = maybe_real {
6066
real
6167
} else {
6268
let real = account("real", 0, SEED);
63-
T::Currency::make_free_balance_be(&real, BalanceOf::<T>::max_value() / 2u32.into());
69+
T::Currency::make_free_balance_be(&real, half_max_balance::<T>());
6470
Proxy::<T>::add_proxy(
6571
RawOrigin::Signed(real.clone()).into(),
6672
caller_lookup,
@@ -82,11 +88,9 @@ fn add_announcements<T: Config>(
8288

8389
benchmarks! {
8490
proxy {
85-
let p in 1 .. (T::MaxProxies::get() - 1) => add_proxies::<T>(p, None)?;
86-
// In this case the caller is the "target" proxy
87-
let caller: T::AccountId = account("target", p - 1, SEED);
88-
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value() / 2u32.into());
89-
// ... and "real" is the traditional caller. This is not a typo.
91+
let p in 1 .. (T::MaxProxies::get().saturating_sub(1)) => add_proxies::<T>(p, None)?;
92+
let caller: T::AccountId = account("target", p.saturating_sub(1), SEED);
93+
T::Currency::make_free_balance_be(&caller, half_max_balance::<T>());
9094
let real: T::AccountId = whitelisted_caller();
9195
let real_lookup = T::Lookup::unlookup(real);
9296
let call: <T as Config>::RuntimeCall = frame_system::Call::<T>::remark { remark: vec![] }.into();
@@ -96,14 +100,12 @@ benchmarks! {
96100
}
97101

98102
proxy_announced {
99-
let a in 0 .. T::MaxPending::get() - 1;
100-
let p in 1 .. (T::MaxProxies::get() - 1) => add_proxies::<T>(p, None)?;
101-
// In this case the caller is the "target" proxy
103+
let a in 0 .. T::MaxPending::get().saturating_sub(1);
104+
let p in 1 .. (T::MaxProxies::get().saturating_sub(1)) => add_proxies::<T>(p, None)?;
102105
let caller: T::AccountId = account("pure", 0, SEED);
103-
let delegate: T::AccountId = account("target", p - 1, SEED);
106+
let delegate: T::AccountId = account("target", p.saturating_sub(1), SEED);
104107
let delegate_lookup = T::Lookup::unlookup(delegate.clone());
105-
T::Currency::make_free_balance_be(&delegate, BalanceOf::<T>::max_value() / 2u32.into());
106-
// ... and "real" is the traditional caller. This is not a typo.
108+
T::Currency::make_free_balance_be(&delegate, half_max_balance::<T>());
107109
let real: T::AccountId = whitelisted_caller();
108110
let real_lookup = T::Lookup::unlookup(real);
109111
let call: <T as Config>::RuntimeCall = frame_system::Call::<T>::remark { remark: vec![] }.into();
@@ -119,12 +121,10 @@ benchmarks! {
119121
}
120122

121123
remove_announcement {
122-
let a in 0 .. T::MaxPending::get() - 1;
123-
let p in 1 .. (T::MaxProxies::get() - 1) => add_proxies::<T>(p, None)?;
124-
// In this case the caller is the "target" proxy
125-
let caller: T::AccountId = account("target", p - 1, SEED);
126-
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value() / 2u32.into());
127-
// ... and "real" is the traditional caller. This is not a typo.
124+
let a in 0 .. T::MaxPending::get().saturating_sub(1);
125+
let p in 1 .. (T::MaxProxies::get().saturating_sub(1)) => add_proxies::<T>(p, None)?;
126+
let caller: T::AccountId = account("target", p.saturating_sub(1), SEED);
127+
T::Currency::make_free_balance_be(&caller, half_max_balance::<T>());
128128
let real: T::AccountId = whitelisted_caller();
129129
let real_lookup = T::Lookup::unlookup(real);
130130
let call: <T as Config>::RuntimeCall = frame_system::Call::<T>::remark { remark: vec![] }.into();
@@ -141,13 +141,11 @@ benchmarks! {
141141
}
142142

143143
reject_announcement {
144-
let a in 0 .. T::MaxPending::get() - 1;
145-
let p in 1 .. (T::MaxProxies::get() - 1) => add_proxies::<T>(p, None)?;
146-
// In this case the caller is the "target" proxy
147-
let caller: T::AccountId = account("target", p - 1, SEED);
144+
let a in 0 .. T::MaxPending::get().saturating_sub(1);
145+
let p in 1 .. (T::MaxProxies::get().saturating_sub(1)) => add_proxies::<T>(p, None)?;
146+
let caller: T::AccountId = account("target", p.saturating_sub(1), SEED);
148147
let caller_lookup = T::Lookup::unlookup(caller.clone());
149-
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value() / 2u32.into());
150-
// ... and "real" is the traditional caller. This is not a typo.
148+
T::Currency::make_free_balance_be(&caller, half_max_balance::<T>());
151149
let real: T::AccountId = whitelisted_caller();
152150
let real_lookup = T::Lookup::unlookup(real.clone());
153151
let call: <T as Config>::RuntimeCall = frame_system::Call::<T>::remark { remark: vec![] }.into();
@@ -164,12 +162,10 @@ benchmarks! {
164162
}
165163

166164
announce {
167-
let a in 0 .. T::MaxPending::get() - 1;
168-
let p in 1 .. (T::MaxProxies::get() - 1) => add_proxies::<T>(p, None)?;
169-
// In this case the caller is the "target" proxy
170-
let caller: T::AccountId = account("target", p - 1, SEED);
171-
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value() / 2u32.into());
172-
// ... and "real" is the traditional caller. This is not a typo.
165+
let a in 0 .. T::MaxPending::get().saturating_sub(1);
166+
let p in 1 .. (T::MaxProxies::get().saturating_sub(1)) => add_proxies::<T>(p, None)?;
167+
let caller: T::AccountId = account("target", p.saturating_sub(1), SEED);
168+
T::Currency::make_free_balance_be(&caller, half_max_balance::<T>());
173169
let real: T::AccountId = whitelisted_caller();
174170
let real_lookup = T::Lookup::unlookup(real.clone());
175171
add_announcements::<T>(a, Some(caller.clone()), None)?;
@@ -181,7 +177,7 @@ benchmarks! {
181177
}
182178

183179
add_proxy {
184-
let p in 1 .. (T::MaxProxies::get() - 1) => add_proxies::<T>(p, None)?;
180+
let p in 1 .. (T::MaxProxies::get().saturating_sub(1)) => add_proxies::<T>(p, None)?;
185181
let caller: T::AccountId = whitelisted_caller();
186182
let real = T::Lookup::unlookup(account("target", T::MaxProxies::get(), SEED));
187183
}: _(
@@ -192,11 +188,11 @@ benchmarks! {
192188
)
193189
verify {
194190
let (proxies, _) = Proxies::<T>::get(caller);
195-
assert_eq!(proxies.len() as u32, p + 1);
191+
assert_eq!(proxies.len() as u32, p.saturating_add(1));
196192
}
197193

198194
remove_proxy {
199-
let p in 1 .. (T::MaxProxies::get() - 1) => add_proxies::<T>(p, None)?;
195+
let p in 1 .. (T::MaxProxies::get().saturating_sub(1)) => add_proxies::<T>(p, None)?;
200196
let caller: T::AccountId = whitelisted_caller();
201197
let delegate = T::Lookup::unlookup(account("target", 0, SEED));
202198
}: _(
@@ -207,11 +203,11 @@ benchmarks! {
207203
)
208204
verify {
209205
let (proxies, _) = Proxies::<T>::get(caller);
210-
assert_eq!(proxies.len() as u32, p - 1);
206+
assert_eq!(proxies.len() as u32, p.saturating_sub(1));
211207
}
212208

213209
remove_proxies {
214-
let p in 1 .. (T::MaxProxies::get() - 1) => add_proxies::<T>(p, None)?;
210+
let p in 1 .. (T::MaxProxies::get().saturating_sub(1)) => add_proxies::<T>(p, None)?;
215211
let caller: T::AccountId = whitelisted_caller();
216212
}: _(RawOrigin::Signed(caller.clone()))
217213
verify {
@@ -220,7 +216,7 @@ benchmarks! {
220216
}
221217

222218
create_pure {
223-
let p in 1 .. (T::MaxProxies::get() - 1) => add_proxies::<T>(p, None)?;
219+
let p in 1 .. (T::MaxProxies::get().saturating_sub(1)) => add_proxies::<T>(p, None)?;
224220
let caller: T::AccountId = whitelisted_caller();
225221
}: _(
226222
RawOrigin::Signed(caller.clone()),
@@ -239,7 +235,7 @@ benchmarks! {
239235
}
240236

241237
kill_pure {
242-
let p in 0 .. (T::MaxProxies::get() - 2);
238+
let p in 0 .. (T::MaxProxies::get().saturating_sub(2));
243239

244240
let caller: T::AccountId = whitelisted_caller();
245241
let caller_lookup = T::Lookup::unlookup(caller.clone());

0 commit comments

Comments
 (0)