-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Pallet file system benchmarking setup (#285)
* wip: benchmarking setup * fix benchmarks * add weight benchmarks to extrinsics * fmt * fix mock config * amend * max peer ids
- Loading branch information
Showing
11 changed files
with
438 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
use super::{types::*, *}; | ||
use frame_benchmarking::v2::*; | ||
use sp_runtime::traits::One; | ||
|
||
#[benchmarks(where | ||
T: crate::Config<Fingerprint = <T as frame_system::Config>::Hash>, | ||
T: pallet_storage_providers::Config< | ||
ProviderId = <T as frame_system::Config>::Hash, | ||
StorageDataUnit = u64 | ||
>, | ||
<T as crate::Config>::Providers: shp_traits::MutateStorageProvidersInterface<StorageDataUnit = u64> | ||
+ shp_traits::ReadProvidersInterface<ProviderId = <T as frame_system::Config>::Hash>, | ||
// Ensure the ValuePropId from our Providers trait matches that from pallet_storage_providers: | ||
<T as crate::Config>::Providers: shp_traits::MutateBucketsInterface<ValuePropId = <T as pallet_storage_providers::Config>::ValuePropId>, | ||
)] | ||
mod benchmarks { | ||
use super::*; | ||
use frame_support::{ | ||
assert_ok, | ||
traits::{fungible::Mutate, Get}, | ||
BoundedVec, | ||
}; | ||
use frame_system::RawOrigin; | ||
use pallet_storage_providers::types::ValueProposition; | ||
use shp_traits::ReadBucketsInterface; | ||
use sp_core::Hasher; | ||
use sp_runtime::traits::Hash; | ||
use sp_std::vec; | ||
|
||
#[benchmark] | ||
fn create_bucket() -> Result<(), BenchmarkError> { | ||
let user: T::AccountId = account("Alice", 0, 0); | ||
let signed_origin = RawOrigin::Signed(user.clone()); | ||
mint_into_account::<T>(user.clone(), 1_000_000_000_000_000)?; | ||
|
||
let name: BucketNameFor<T> = vec![1; BucketNameLimitFor::<T>::get().try_into().unwrap()] | ||
.try_into() | ||
.unwrap(); | ||
|
||
// Register MSP with value proposition | ||
let msp: T::AccountId = account("MSP", 0, 0); | ||
mint_into_account::<T>(msp.clone(), 1_000_000_000_000_000)?; | ||
let (msp_id, value_prop_id) = add_msp_to_provider_storage::<T>(&msp); | ||
|
||
#[extrinsic_call] | ||
_( | ||
signed_origin.clone(), | ||
Some(msp_id), | ||
name, | ||
true, | ||
Some(value_prop_id), | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[benchmark] | ||
fn issue_storage_request() -> Result<(), BenchmarkError> { | ||
let user: T::AccountId = account("Alice", 0, 0); | ||
let signed_origin = RawOrigin::Signed(user.clone()); | ||
mint_into_account::<T>(user.clone(), 1_000_000_000_000_000)?; | ||
|
||
let name: BucketNameFor<T> = vec![1; BucketNameLimitFor::<T>::get().try_into().unwrap()] | ||
.try_into() | ||
.unwrap(); | ||
let bucket_id = <<T as crate::Config>::Providers as ReadBucketsInterface>::derive_bucket_id( | ||
&user, | ||
name.clone(), | ||
); | ||
let location = vec![1; MaxFilePathSize::<T>::get().try_into().unwrap()] | ||
.try_into() | ||
.unwrap(); | ||
let fingerprint = | ||
<<T as frame_system::Config>::Hashing as Hasher>::hash(b"benchmark_fingerprint"); | ||
let size: StorageData<T> = 100; | ||
let peer_id: PeerId<T> = vec![1; MaxPeerIdSize::<T>::get().try_into().unwrap()] | ||
.try_into() | ||
.unwrap(); | ||
let peer_ids: PeerIds<T> = | ||
vec![peer_id; MaxNumberOfPeerIds::<T>::get().try_into().unwrap()] | ||
.try_into() | ||
.unwrap(); | ||
|
||
// Register MSP with value proposition | ||
let msp: T::AccountId = account("MSP", 0, 0); | ||
mint_into_account::<T>(msp.clone(), 1_000_000_000_000_000)?; | ||
let (msp_id, value_prop_id) = add_msp_to_provider_storage::<T>(&msp); | ||
|
||
Pallet::<T>::create_bucket( | ||
signed_origin.clone().into(), | ||
Some(msp_id), | ||
name, | ||
true, | ||
Some(value_prop_id), | ||
)?; | ||
|
||
#[extrinsic_call] | ||
_( | ||
signed_origin, | ||
bucket_id, | ||
location, | ||
fingerprint, | ||
size, | ||
Some(msp_id), | ||
peer_ids, | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn mint_into_account<T: crate::Config>( | ||
account: T::AccountId, | ||
amount: u128, | ||
) -> Result<(), BenchmarkError> { | ||
let user_balance = amount | ||
.try_into() | ||
.map_err(|_| BenchmarkError::Stop("Balance conversion failed."))?; | ||
assert_ok!(<T as crate::Config>::Currency::mint_into( | ||
&account, | ||
user_balance, | ||
)); | ||
Ok(()) | ||
} | ||
|
||
fn add_msp_to_provider_storage<T>(msp: &T::AccountId) -> (ProviderIdFor<T>, ValuePropId<T>) | ||
where | ||
T: crate::Config<Fingerprint = <T as frame_system::Config>::Hash>, | ||
T: pallet_storage_providers::Config< | ||
ProviderId = <T as frame_system::Config>::Hash, | ||
StorageDataUnit = u64, | ||
>, | ||
<T as crate::Config>::Providers: shp_traits::MutateStorageProvidersInterface<StorageDataUnit = u64> | ||
+ shp_traits::ReadProvidersInterface<ProviderId = <T as frame_system::Config>::Hash>, | ||
// Ensure the ValuePropId from our Providers trait matches that from pallet_storage_providers | ||
<T as crate::Config>::Providers: shp_traits::MutateBucketsInterface< | ||
ValuePropId = <T as pallet_storage_providers::Config>::ValuePropId, | ||
>, | ||
{ | ||
let msp_hash = T::Hashing::hash_of(&msp); | ||
|
||
let capacity: StorageData<T> = 1024 * 1024 * 1024; | ||
let capacity_used: StorageData<T> = 0; | ||
|
||
let msp_info = pallet_storage_providers::types::MainStorageProvider { | ||
capacity, | ||
capacity_used, | ||
multiaddresses: BoundedVec::default(), | ||
last_capacity_change: frame_system::Pallet::<T>::block_number(), | ||
owner_account: msp.clone(), | ||
payment_account: msp.clone(), | ||
sign_up_block: frame_system::Pallet::<T>::block_number(), | ||
}; | ||
|
||
pallet_storage_providers::MainStorageProviders::<T>::insert(msp_hash, msp_info); | ||
pallet_storage_providers::AccountIdToMainStorageProviderId::<T>::insert( | ||
msp.clone(), | ||
msp_hash, | ||
); | ||
|
||
let commitment = vec![ | ||
1; | ||
<T as pallet_storage_providers::Config>::MaxCommitmentSize::get() | ||
.try_into() | ||
.unwrap() | ||
] | ||
.try_into() | ||
.unwrap(); | ||
|
||
let value_prop_storage: StorageData<T> = 1000; | ||
// Use One::one() or a conversion that matches the expected balance type: | ||
let value_prop = ValueProposition::<T>::new(One::one(), commitment, value_prop_storage); | ||
let value_prop_id = value_prop.derive_id(); | ||
|
||
pallet_storage_providers::MainStorageProviderIdsToValuePropositions::<T>::insert( | ||
msp_hash, | ||
value_prop_id, | ||
value_prop, | ||
); | ||
|
||
(msp_hash, value_prop_id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.