Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate pallet-recovery to benchmark V2 syntax #6299

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
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
153 changes: 64 additions & 89 deletions substrate/frame/recovery/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::*;

use crate::Pallet;
use alloc::{boxed::Box, vec, vec::Vec};
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller};
use frame_benchmarking::v2::*;
use frame_support::traits::{Currency, Get};
use frame_system::RawOrigin;
use sp_runtime::traits::Bounded;
Expand Down Expand Up @@ -103,85 +103,79 @@ fn insert_recovery_account<T: Config>(caller: &T::AccountId, account: &T::Accoun
<Recoverable<T>>::insert(&account, recovery_config);
}

benchmarks! {
as_recovered {
#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn as_recovered() {
let caller: T::AccountId = whitelisted_caller();
let recovered_account: T::AccountId = account("recovered_account", 0, SEED);
let recovered_account_lookup = T::Lookup::unlookup(recovered_account.clone());
let call: <T as Config>::RuntimeCall = frame_system::Call::<T>::remark { remark: vec![] }.into();
let call: <T as Config>::RuntimeCall =
frame_system::Call::<T>::remark { remark: vec![] }.into();

Proxy::<T>::insert(&caller, &recovered_account);
}: _(
RawOrigin::Signed(caller),
recovered_account_lookup,
Box::new(call)
)

set_recovered {
#[extrinsic_call]
_(RawOrigin::Signed(caller), recovered_account_lookup, Box::new(call))
}

#[benchmark]
fn set_recovered() {
let lost: T::AccountId = whitelisted_caller();
let lost_lookup = T::Lookup::unlookup(lost.clone());
let rescuer: T::AccountId = whitelisted_caller();
let rescuer_lookup = T::Lookup::unlookup(rescuer.clone());
}: _(
RawOrigin::Root,
lost_lookup,
rescuer_lookup
) verify {

#[extrinsic_call]
_(RawOrigin::Root, lost_lookup, rescuer_lookup);

assert_last_event::<T>(
Event::AccountRecovered {
lost_account: lost,
rescuer_account: rescuer,
}.into()
Event::AccountRecovered { lost_account: lost, rescuer_account: rescuer }.into(),
);
}

create_recovery {
let n in 1 .. T::MaxFriends::get();

#[benchmark]
fn create_recovery(n: Linear<1, { T::MaxFriends::get() }>) {
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());

// Create friends
let friends = generate_friends::<T>(n);
}: _(
RawOrigin::Signed(caller.clone()),
friends,
n as u16,
DEFAULT_DELAY.into()
) verify {

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), friends, n as u16, DEFAULT_DELAY.into());

assert_last_event::<T>(Event::RecoveryCreated { account: caller }.into());
}

initiate_recovery {
#[benchmark]
fn initiate_recovery() {
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());

let lost_account: T::AccountId = account("lost_account", 0, SEED);
let lost_account_lookup = T::Lookup::unlookup(lost_account.clone());

insert_recovery_account::<T>(&caller, &lost_account);
}: _(
RawOrigin::Signed(caller.clone()),
lost_account_lookup
) verify {

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

assert_last_event::<T>(
Event::RecoveryInitiated {
lost_account: lost_account,
rescuer_account: caller,
}.into()
Event::RecoveryInitiated { lost_account, rescuer_account: caller }.into(),
);
}

vouch_recovery {
let n in 1 .. T::MaxFriends::get();

#[benchmark]
fn vouch_recovery(n: Linear<1, { T::MaxFriends::get() }>) {
let caller: T::AccountId = whitelisted_caller();
let lost_account: T::AccountId = account("lost_account", 0, SEED);
let lost_account_lookup = T::Lookup::unlookup(lost_account.clone());
let rescuer_account: T::AccountId = account("rescuer_account", 0, SEED);
let rescuer_account_lookup = T::Lookup::unlookup(rescuer_account.clone());


// Create friends
let friends = add_caller_and_generate_friends::<T>(caller.clone(), n);
let bounded_friends: FriendsOf<T> = friends.try_into().unwrap();
Expand Down Expand Up @@ -212,23 +206,15 @@ benchmarks! {
// Create the active recovery storage item
<ActiveRecoveries<T>>::insert(&lost_account, &rescuer_account, recovery_status);

}: _(
RawOrigin::Signed(caller.clone()),
lost_account_lookup,
rescuer_account_lookup
) verify {
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), lost_account_lookup, rescuer_account_lookup);
assert_last_event::<T>(
Event::RecoveryVouched {
lost_account: lost_account,
rescuer_account: rescuer_account,
sender: caller,
}.into()
Event::RecoveryVouched { lost_account, rescuer_account, sender: caller }.into(),
);
}

claim_recovery {
let n in 1 .. T::MaxFriends::get();

#[benchmark]
fn claim_recovery(n: Linear<1, { T::MaxFriends::get() }>) {
let caller: T::AccountId = whitelisted_caller();
let lost_account: T::AccountId = account("lost_account", 0, SEED);
let lost_account_lookup = T::Lookup::unlookup(lost_account.clone());
Expand Down Expand Up @@ -264,25 +250,20 @@ benchmarks! {

// Create the active recovery storage item
<ActiveRecoveries<T>>::insert(&lost_account, &caller, recovery_status);
}: _(
RawOrigin::Signed(caller.clone()),
lost_account_lookup
) verify {

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), lost_account_lookup);
assert_last_event::<T>(
Event::AccountRecovered {
lost_account: lost_account,
rescuer_account: caller,
}.into()
Event::AccountRecovered { lost_account, rescuer_account: caller }.into(),
);
}

close_recovery {
#[benchmark]
fn close_recovery(n: Linear<1, { T::MaxFriends::get() }>) {
let caller: T::AccountId = whitelisted_caller();
let rescuer_account: T::AccountId = account("rescuer_account", 0, SEED);
let rescuer_account_lookup = T::Lookup::unlookup(rescuer_account.clone());

let n in 1 .. T::MaxFriends::get();

T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
T::Currency::make_free_balance_be(&rescuer_account, BalanceOf::<T>::max_value());

Expand Down Expand Up @@ -315,21 +296,16 @@ benchmarks! {

// Create the active recovery storage item
<ActiveRecoveries<T>>::insert(&caller, &rescuer_account, recovery_status);
}: _(
RawOrigin::Signed(caller.clone()),
rescuer_account_lookup
) verify {

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), rescuer_account_lookup);
assert_last_event::<T>(
Event::RecoveryClosed {
lost_account: caller,
rescuer_account: rescuer_account,
}.into()
Event::RecoveryClosed { lost_account: caller, rescuer_account }.into(),
);
}

remove_recovery {
let n in 1 .. T::MaxFriends::get();

#[benchmark]
fn remove_recovery(n: Linear<1, { T::MaxFriends::get() }>) {
let caller: T::AccountId = whitelisted_caller();

T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
Expand All @@ -353,17 +329,14 @@ benchmarks! {

// Reserve deposit for recovery
T::Currency::reserve(&caller, total_deposit).unwrap();
}: _(
RawOrigin::Signed(caller.clone())
) verify {
assert_last_event::<T>(
Event::RecoveryRemoved {
lost_account: caller
}.into()
);

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()));
assert_last_event::<T>(Event::RecoveryRemoved { lost_account: caller }.into());
}

cancel_recovered {
#[benchmark]
fn cancel_recovered() -> Result<(), BenchmarkError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you dont use a ? then you can also remove the return type and just return ().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just preserved the previous behavior, but happy to change it. I was wondering if there is any good use for BenchmarkError at all.

let caller: T::AccountId = whitelisted_caller();
let account: T::AccountId = account("account", 0, SEED);
let account_lookup = T::Lookup::unlookup(account.clone());
Expand All @@ -373,10 +346,12 @@ benchmarks! {
frame_system::Pallet::<T>::inc_consumers(&caller)?;

Proxy::<T>::insert(&caller, &account);
}: _(
RawOrigin::Signed(caller),
account_lookup
)

#[extrinsic_call]
_(RawOrigin::Signed(caller), account_lookup);

Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
}
Loading