Skip to content

Commit

Permalink
fix bench
Browse files Browse the repository at this point in the history
  • Loading branch information
nanocryk committed May 23, 2024
1 parent afb18f3 commit 22fcabb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
31 changes: 20 additions & 11 deletions pallets/data-preservers/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use {
BoundedVec,
},
frame_system::RawOrigin,
sp_runtime::traits::Zero,
sp_std::vec,
tp_traits::ParaId,
};
Expand All @@ -48,7 +49,7 @@ where
}

#[benchmarks(
where T::Currency: Mutate<T::AccountId>
where T::Currency: Mutate<T::AccountId>, T::ProfileId: Zero
)]
mod benchmarks {
use super::*;
Expand Down Expand Up @@ -95,7 +96,7 @@ mod benchmarks {
Pallet::<T>::create_profile(RawOrigin::Signed(caller.clone()), profile.clone());

assert_eq!(
Profiles::<T>::get(0),
Profiles::<T>::get(T::ProfileId::zero()),
Some(RegisteredProfile {
account: caller,
deposit,
Expand Down Expand Up @@ -128,7 +129,7 @@ mod benchmarks {
);

assert_eq!(
Profiles::<T>::get(0),
Profiles::<T>::get(T::ProfileId::zero()),
Some(RegisteredProfile {
account: owner,
deposit: 0u32.into(),
Expand Down Expand Up @@ -166,10 +167,14 @@ mod benchmarks {
let deposit = T::ProfileDeposit::profile_deposit(&profile).expect("deposit to be computed");

#[extrinsic_call]
Pallet::<T>::update_profile(RawOrigin::Signed(caller.clone()), 0, profile.clone());
Pallet::<T>::update_profile(
RawOrigin::Signed(caller.clone()),
T::ProfileId::zero(),
profile.clone(),
);

assert_eq!(
Profiles::<T>::get(0),
Profiles::<T>::get(T::ProfileId::zero()),
Some(RegisteredProfile {
account: caller,
deposit,
Expand Down Expand Up @@ -208,10 +213,14 @@ mod benchmarks {
.expect("failed to create ForceSetProfileOrigin");

#[extrinsic_call]
Pallet::<T>::force_update_profile(origin_force as T::RuntimeOrigin, 0, profile.clone());
Pallet::<T>::force_update_profile(
origin_force as T::RuntimeOrigin,
T::ProfileId::zero(),
profile.clone(),
);

assert_eq!(
Profiles::<T>::get(0),
Profiles::<T>::get(T::ProfileId::zero()),
Some(RegisteredProfile {
account: caller,
deposit: 0u32.into(),
Expand All @@ -237,9 +246,9 @@ mod benchmarks {
.expect("to create profile");

#[extrinsic_call]
Pallet::<T>::delete_profile(RawOrigin::Signed(caller.clone()), 0);
Pallet::<T>::delete_profile(RawOrigin::Signed(caller.clone()), T::ProfileId::zero());

assert_eq!(Profiles::<T>::get(0), None);
assert_eq!(Profiles::<T>::get(T::ProfileId::zero()), None);
}

#[benchmark]
Expand All @@ -262,9 +271,9 @@ mod benchmarks {
.expect("failed to create ForceSetProfileOrigin");

#[extrinsic_call]
Pallet::<T>::force_delete_profile(origin_force as T::RuntimeOrigin, 0);
Pallet::<T>::force_delete_profile(origin_force as T::RuntimeOrigin, T::ProfileId::zero());

assert_eq!(Profiles::<T>::get(0), None);
assert_eq!(Profiles::<T>::get(T::ProfileId::zero()), None);
}

impl_benchmark_test_suite!(
Expand Down
26 changes: 20 additions & 6 deletions pallets/data-preservers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub use weights::WeightInfo;
use serde::{Deserialize, Serialize};

use {
core::fmt::Debug,
dp_core::ParaId,
frame_support::{
dispatch::DispatchErrorWithPostInfo,
Expand All @@ -49,13 +50,12 @@ use {
DefaultNoBound,
},
frame_system::pallet_prelude::*,
parity_scale_codec::FullCodec,
sp_runtime::{
traits::{CheckedAdd, CheckedMul, CheckedSub, Get, Zero, One},
traits::{CheckedAdd, CheckedMul, CheckedSub, Get, One, Zero},
ArithmeticError,
},
sp_std::vec::Vec,
parity_scale_codec::FullCodec,
core::fmt::Debug,
};

#[frame_support::pallet]
Expand Down Expand Up @@ -112,7 +112,15 @@ pub mod pallet {
+ Balanced<Self::AccountId>
+ MutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason>;

type ProfileId: Default + FullCodec + TypeInfo + Copy + Clone + Debug + Eq + CheckedAdd + One;
type ProfileId: Default
+ FullCodec
+ TypeInfo
+ Copy
+ Clone
+ Debug
+ Eq
+ CheckedAdd
+ One;

// Who can call set_boot_nodes?
type SetBootNodesOrigin: EnsureOriginWithArg<Self::RuntimeOrigin, ParaId>;
Expand Down Expand Up @@ -312,7 +320,10 @@ pub mod pallet {
T::Currency::hold(&HoldReason::ProfileDeposit.into(), &account, deposit)?;

let id = NextProfileId::<T>::get();
NextProfileId::<T>::set(id.checked_add(&One::one()).ok_or(ArithmeticError::Overflow)?);
NextProfileId::<T>::set(
id.checked_add(&One::one())
.ok_or(ArithmeticError::Overflow)?,
);

ensure!(
!Profiles::<T>::contains_key(id),
Expand Down Expand Up @@ -441,7 +452,10 @@ pub mod pallet {
T::ForceSetProfileOrigin::ensure_origin(origin)?;

let id = NextProfileId::<T>::get();
NextProfileId::<T>::set(id.checked_add(&One::one()).ok_or(ArithmeticError::Overflow)?);
NextProfileId::<T>::set(
id.checked_add(&One::one())
.ok_or(ArithmeticError::Overflow)?,
);

ensure!(
!Profiles::<T>::contains_key(id),
Expand Down

0 comments on commit 22fcabb

Please sign in to comment.