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

chore: Benchmark msp_stop_storing_bucket and revoke_storage_request #296

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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
86 changes: 43 additions & 43 deletions pallets/file-system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,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",
]
181 changes: 175 additions & 6 deletions pallets/file-system/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use super::{types::*, *};
use frame_benchmarking::v2::*;
use frame_system::pallet_prelude::BlockNumberFor;
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: crate::Config<Fingerprint = <T as frame_system::Config>::Hash, Providers = pallet_storage_providers::Pallet<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>,
<T as crate::Config>::Providers: shp_traits::ReadBucketsInterface<AccountId = <T as frame_system::Config>::AccountId, ProviderId = <T as frame_system::Config>::Hash> + shp_traits::MutateBucketsInterface<ValuePropId = <T as pallet_storage_providers::Config>::ValuePropId>,
<T as crate::Config>::ProofDealer: shp_traits::ProofsDealerInterface<TickNumber = BlockNumberFor<T>>,
)]
mod benchmarks {
use super::*;
Expand Down Expand Up @@ -109,6 +111,131 @@ mod benchmarks {
Ok(())
}

#[benchmark]
fn revoke_storage_request(
n: Linear<
1,
{
Into::<u64>::into(MaxReplicationTarget::<T>::get())
.try_into()
.unwrap()
},
>,
) -> 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: FileLocation<T> = 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),
)?;

Pallet::<T>::issue_storage_request(
signed_origin.clone().into(),
bucket_id,
location.clone(),
fingerprint,
size,
Some(msp_id),
peer_ids,
Some(n.into()),
)?;

let file_key = Pallet::<T>::compute_file_key(user, bucket_id, location, size, fingerprint);

// The `revoke_storage_request` executes the `drain_prefix` function to remove all sub keys including the primary key
// from `StorageRequestBsps`.
for i in 0..n {
let bsp_user: T::AccountId = account("bsp", i as u32, i as u32);
mint_into_account::<T>(bsp_user.clone(), 1_000_000_000_000_000)?;
let bsp_id = add_bsp_to_provider_storage::<T>(&bsp_user.clone());

StorageRequestBsps::<T>::insert(
file_key,
bsp_id,
StorageRequestBspsMetadata::<T> {
confirmed: true,
_phantom: Default::default(),
},
);
}

// Mutate the storage request to have bsps_volunteered equal to MaxReplicationTarget
StorageRequests::<T>::mutate(file_key, |storage_request| {
storage_request.as_mut().unwrap().bsps_volunteered = n.into();
// Setting this greater than 0 triggers a priority challenge
storage_request.as_mut().unwrap().bsps_confirmed = n.into();
});

#[extrinsic_call]
_(signed_origin, file_key);

Ok(())
}

#[benchmark]
fn msp_stop_storing_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();
let bucket_id = <<T as crate::Config>::Providers as ReadBucketsInterface>::derive_bucket_id(
&user,
name.clone(),
);

// 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]
_(RawOrigin::Signed(msp), bucket_id);

Ok(())
}

fn mint_into_account<T: crate::Config>(
account: T::AccountId,
amount: u128,
Expand Down Expand Up @@ -180,4 +307,46 @@ mod benchmarks {

(msp_hash, value_prop_id)
}

fn add_bsp_to_provider_storage<T>(bsp_account: &T::AccountId) -> ProviderIdFor<T>
where
T: crate::Config
+ pallet_storage_providers::Config<
ProviderId = <T as frame_system::Config>::Hash,
StorageDataUnit = u64,
>,
T: crate::Config<Providers = pallet_storage_providers::Pallet<T>>,
{
// Derive the BSP ID from the hash of its account
let bsp_id = T::Hashing::hash_of(&bsp_account);

// Create the BSP info
let bsp_info = pallet_storage_providers::types::BackupStorageProvider {
capacity: 1024 * 1024 * 1024,
capacity_used: 0,
multiaddresses: BoundedVec::default(),
root: <T as pallet_storage_providers::Config>::DefaultMerkleRoot::get(),
last_capacity_change: frame_system::Pallet::<T>::block_number(),
owner_account: bsp_account.clone(),
payment_account: bsp_account.clone(),
reputation_weight:
<T as pallet_storage_providers::Config>::StartingReputationWeight::get(),
sign_up_block: frame_system::Pallet::<T>::block_number(),
};

// Insert the BSP info into storage
pallet_storage_providers::BackupStorageProviders::<T>::insert(bsp_id, bsp_info);
pallet_storage_providers::AccountIdToBackupStorageProviderId::<T>::insert(
bsp_account.clone(),
bsp_id,
);

// Update the Global Reputation Weight
pallet_storage_providers::GlobalBspsReputationWeight::<T>::mutate(|reputation_weight| {
*reputation_weight =
<T as pallet_storage_providers::Config>::StartingReputationWeight::get();
});

bsp_id
}
}
14 changes: 8 additions & 6 deletions pallets/file-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,12 @@

/// Revoke storage request
#[pallet::call_index(7)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight({
let confirmed = StorageRequests::<T>::get(file_key).map_or(0, |metadata| metadata.bsps_confirmed.into());
let weight = T::WeightInfo::revoke_storage_request(confirmed as u32);

weight.saturating_add(T::DbWeight::get().reads_writes(1, 0))
})]
pub fn revoke_storage_request(
origin: OriginFor<T>,
file_key: MerkleHash<T>,
Expand Down Expand Up @@ -1048,7 +1053,7 @@
}

#[pallet::call_index(9)]
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())]
#[pallet::weight(T::WeightInfo::msp_stop_storing_bucket())]
pub fn msp_stop_storing_bucket(
origin: OriginFor<T>,
bucket_id: BucketIdFor<T>,
Expand Down Expand Up @@ -1338,11 +1343,8 @@
}
}

#[pallet::hooks]

Check warning on line 1346 in pallets/file-system/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check lint with clippy

using `map_err` over `inspect_err`
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T>
where
u32: TryFrom<BlockNumberFor<T>>,
{
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_poll(_n: BlockNumberFor<T>, weight: &mut frame_support::weights::WeightMeter) {
// TODO: Benchmark computational weight cost of this hook.

Expand Down
Loading
Loading