Skip to content

Commit

Permalink
feat: Pallet file system benchmarking setup (#285)
Browse files Browse the repository at this point in the history
* wip: benchmarking setup

* fix benchmarks

* add weight benchmarks to extrinsics

* fmt

* fix mock config

* amend

* max peer ids
  • Loading branch information
snowmead authored Dec 17, 2024
1 parent 39ac4f8 commit e722b3c
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 50 deletions.
1 change: 1 addition & 0 deletions pallets/bucket-nfts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ impl ProofsDealerInterface for MockProofsDealer {

impl pallet_file_system::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Providers = Providers;
type ProofDealer = MockProofsDealer;
type PaymentStreams = PaymentStreams;
Expand Down
87 changes: 44 additions & 43 deletions pallets/file-system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sp-weights = { workspace = true }

# Local
pallet-file-system-runtime-api = { workspace = true }
pallet-storage-providers = { workspace = true, optional = true }

shp-constants = { workspace = true }
shp-file-metadata = { workspace = true }
Expand All @@ -59,51 +60,51 @@ pallet-balances = { workspace = true, features = ["std"] }
[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"pallet-bucket-nfts/std",
"pallet-cr-randomness/std",
"pallet-file-system-runtime-api/std",
"pallet-nfts/std",
"pallet-payment-streams/std",
"pallet-proofs-dealer/std",
"pallet-storage-providers/std",
"scale-info/std",
"shp-constants/std",
"shp-file-metadata/std",
"shp-traits/std",
"shp-treasury-funding/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-trie/std",
"sp-weights/std",
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"pallet-bucket-nfts/std",
"pallet-cr-randomness/std",
"pallet-file-system-runtime-api/std",
"pallet-nfts/std",
"pallet-payment-streams/std",
"pallet-proofs-dealer/std",
"pallet-storage-providers/std",
"scale-info/std",
"shp-constants/std",
"shp-file-metadata/std",
"shp-traits/std",
"shp-treasury-funding/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-trie/std",
"sp-weights/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-cr-randomness/runtime-benchmarks",
"pallet-proofs-dealer/runtime-benchmarks",
"pallet-storage-providers/runtime-benchmarks",
"pallet-bucket-nfts/runtime-benchmarks",
"shp-constants/runtime-benchmarks",
"shp-file-metadata/runtime-benchmarks",
"shp-traits/runtime-benchmarks",
"shp-treasury-funding/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-cr-randomness/runtime-benchmarks",
"pallet-proofs-dealer/runtime-benchmarks",
"pallet-storage-providers/runtime-benchmarks",
"pallet-bucket-nfts/runtime-benchmarks",
"shp-constants/runtime-benchmarks",
"shp-file-metadata/runtime-benchmarks",
"shp-traits/runtime-benchmarks",
"shp-treasury-funding/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-proofs-dealer/try-runtime",
"pallet-storage-providers/try-runtime",
"pallet-bucket-nfts/try-runtime",
"shp-constants/try-runtime",
"shp-file-metadata/try-runtime",
"shp-traits/try-runtime",
"shp-treasury-funding/try-runtime",
"sp-runtime/try-runtime",
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-proofs-dealer/try-runtime",
"pallet-storage-providers/try-runtime",
"pallet-bucket-nfts/try-runtime",
"shp-constants/try-runtime",
"shp-file-metadata/try-runtime",
"shp-traits/try-runtime",
"shp-treasury-funding/try-runtime",
"sp-runtime/try-runtime",
]
182 changes: 182 additions & 0 deletions pallets/file-system/src/benchmarking.rs
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)
}
}
13 changes: 10 additions & 3 deletions pallets/file-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@

pub use pallet::*;

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

pub mod types;
mod utils;
pub mod weights;

#[cfg(test)]
mod mock;
Expand All @@ -43,7 +47,7 @@ mod tests;

#[frame_support::pallet]
pub mod pallet {
use super::types::*;
use super::{types::*, weights::WeightInfo};
use codec::HasCompact;
use frame_support::{
dispatch::DispatchResult,
Expand Down Expand Up @@ -71,6 +75,9 @@ pub mod pallet {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: crate::weights::WeightInfo;

/// The trait for reading and mutating Storage Provider and Bucket data.
type Providers: shp_traits::ReadProvidersInterface<AccountId = Self::AccountId>
+ shp_traits::MutateProvidersInterface<
Expand Down Expand Up @@ -815,7 +822,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(T::WeightInfo::create_bucket())]
pub fn create_bucket(
origin: OriginFor<T>,
msp_id: Option<ProviderIdFor<T>>,
Expand Down Expand Up @@ -957,7 +964,7 @@ pub mod pallet {

/// Issue a new storage request for a file
#[pallet::call_index(6)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(T::WeightInfo::issue_storage_request())]
pub fn issue_storage_request(
origin: OriginFor<T>,
bucket_id: BucketIdFor<T>,
Expand Down
1 change: 1 addition & 0 deletions pallets/file-system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ parameter_types! {

impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Providers = Providers;
type ProofDealer = ProofsDealer;
type PaymentStreams = PaymentStreams;
Expand Down
2 changes: 0 additions & 2 deletions pallets/file-system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2530,11 +2530,9 @@ mod msp_respond_storage_request {
let peer_id = BoundedVec::try_from(vec![1]).unwrap();
let peer_ids: PeerIds<Test> = BoundedVec::try_from(vec![peer_id]).unwrap();

// Register the MSP.
// Register the MSP.
let (msp_id, value_prop_id) = add_msp_to_provider_storage(&msp);

// Create the bucket that will hold the file.
// Create the bucket that will hold the file.
let name = BoundedVec::try_from(b"bucket".to_vec()).unwrap();
let bucket_id =
Expand Down
Loading

0 comments on commit e722b3c

Please sign in to comment.