Skip to content

Commit

Permalink
chore: benchmark approve
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Jul 23, 2024
1 parent c10a8d4 commit a8c4bb5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
1 change: 1 addition & 0 deletions pallets/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
std = [
Expand Down
60 changes: 60 additions & 0 deletions pallets/api/src/fungibles/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Benchmarking setup for pallet-cards

use super::{AccountIdOf, AssetIdOf, Assets, AssetsInstanceOf, BalanceOf, Call, Config, Pallet};
use frame_benchmarking::{account, v2::*};
use frame_support::{
assert_ok,
traits::{
fungibles::{
approvals::{Inspect as ApprovalInspect, Mutate},
Create, Inspect,
},
Currency,
},
};
use frame_system::RawOrigin;
use sp_runtime::traits::Zero;

const SEED: u32 = 1;

#[benchmarks(
where
<pallet_assets::Pallet<T, AssetsInstanceOf<T>> as Inspect<<T as frame_system::Config>::AccountId>>::AssetId: Zero,
)]
mod benchmarks {
use super::*;

// The worst case scenario is when the allowance is set to a value which is lower than the
// current allowance.
#[benchmark]
fn approve() -> Result<(), BenchmarkError> {
let asset = AssetIdOf::<T>::zero();
let decreased_value = <BalanceOf<T>>::from(50u32);
let min_balance = <BalanceOf<T>>::from(1u32);
let owner: AccountIdOf<T> = account("Alice", 0, SEED);
let spender: AccountIdOf<T> = account("Bob", 0, SEED);
let value = <BalanceOf<T>>::from(100u32);
T::Currency::make_free_balance_be(&owner, 100u32.into());
assert_ok!(<Assets<T> as Create<AccountIdOf<T>>>::create(
asset.clone().into(),

Check warning on line 39 in pallets/api/src/fungibles/benchmarking.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `<T as pallet_assets::Config<<T as fungibles::pallet::Config>::AssetsInstance>>::AssetId`

warning: useless conversion to the same type: `<T as pallet_assets::Config<<T as fungibles::pallet::Config>::AssetsInstance>>::AssetId` --> pallets/api/src/fungibles/benchmarking.rs:39:4 | 39 | asset.clone().into(), | ^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `asset.clone()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default
owner.clone(),
true,
min_balance
));
assert_ok!(<Assets<T> as Mutate<AccountIdOf<T>>>::approve(
asset.clone(),
&owner,
&spender,
value
));

#[extrinsic_call]
_(RawOrigin::Signed(owner.clone()), asset.clone(), spender.clone(), decreased_value);

assert_eq!(Assets::<T>::allowance(asset, &owner, &spender), decreased_value);

Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
}
13 changes: 8 additions & 5 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/// API that adheres to standards in the smart contract space.
pub use pallet::*;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod tests;

Expand All @@ -26,8 +28,8 @@ pub mod pallet {
>>::AssetId;
type AssetIdParameterOf<T> =
<T as pallet_assets::Config<AssetsInstanceOf<T>>>::AssetIdParameter;
type Assets<T> = pallet_assets::Pallet<T, AssetsInstanceOf<T>>;
type AssetsInstanceOf<T> = <T as Config>::AssetsInstance;
pub(crate) type Assets<T> = pallet_assets::Pallet<T, AssetsInstanceOf<T>>;
pub(crate) type AssetsInstanceOf<T> = <T as Config>::AssetsInstance;
type AssetsWeightInfo<T> = <T as pallet_assets::Config<AssetsInstanceOf<T>>>::WeightInfo;
pub(crate) type BalanceOf<T> = <pallet_assets::Pallet<T, AssetsInstanceOf<T>> as Inspect<
<T as frame_system::Config>::AccountId,
Expand Down Expand Up @@ -94,14 +96,15 @@ pub mod pallet {
/// # Returns
/// Returns `Ok(())` if successful, or an error if the approval fails.
#[pallet::call_index(2)]
#[pallet::weight(T::DbWeight::get().reads(2) + AssetsWeightInfo::<T>::cancel_approval() + AssetsWeightInfo::<T>::approve_transfer())]
#[pallet::weight(T::DbWeight::get().reads(1) + AssetsWeightInfo::<T>::cancel_approval() + AssetsWeightInfo::<T>::approve_transfer())]
pub fn approve(
origin: OriginFor<T>,
id: AssetIdOf<T>,
spender: AccountIdOf<T>,
mut value: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())
// To have the caller pay some fees.
.map_err(|e| e.with_weight(T::DbWeight::get().reads(1)))?;
let current_allowance = Assets::<T>::allowance(id.clone(), &who, &spender);
let spender = T::Lookup::unlookup(spender);
Expand All @@ -115,15 +118,15 @@ pub mod pallet {
value.saturating_reduce(current_allowance);
Assets::<T>::approve_transfer(origin, id, spender, value).map_err(|e| {
e.with_weight(
T::DbWeight::get().reads(2) + AssetsWeightInfo::<T>::approve_transfer(),
T::DbWeight::get().reads(1) + AssetsWeightInfo::<T>::approve_transfer(),
)
})?;
} else {
// If the new value is less than the current allowance, cancel the approval and set the new value
Assets::<T>::cancel_approval(origin.clone(), id.clone(), spender.clone()).map_err(
|e| {
e.with_weight(
T::DbWeight::get().reads(2) + AssetsWeightInfo::<T>::cancel_approval(),
T::DbWeight::get().reads(1) + AssetsWeightInfo::<T>::cancel_approval(),
)
},
)?;
Expand Down

0 comments on commit a8c4bb5

Please sign in to comment.