Skip to content

Commit

Permalink
feat: ✨ Enable benchmarking project-wide and benchmark challenge ex…
Browse files Browse the repository at this point in the history
…trinsic (#232)

* build: 👷 Make build work with `runtime-benchmarks` feature

* feat: ✨ Run benchmarkings and set weights for `challenge` transaction
  • Loading branch information
ffarall authored Oct 18, 2024
1 parent f73fdf8 commit 7a3b4ac
Show file tree
Hide file tree
Showing 24 changed files with 166 additions and 121 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ homepage = "https://moonsonglabs.com/"
[profile.release]
panic = "unwind"

[profile.production]
codegen-units = 1
inherits = "release"
lto = true

[workspace]
members = [
"runtime",
Expand Down
1 change: 1 addition & 0 deletions pallets/bucket-nfts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ frame-support = { workspace = true }
frame-system = { workspace = true }

pallet-nfts = { workspace = true }
sp-keyring = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
Expand Down
30 changes: 0 additions & 30 deletions pallets/bucket-nfts/src/benchmarking.rs

This file was deleted.

4 changes: 2 additions & 2 deletions pallets/bucket-nfts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ mod mock;
#[cfg(test)]
mod tests;

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

#[frame_support::pallet]
pub mod pallet {
Expand Down
30 changes: 0 additions & 30 deletions pallets/file-system/src/benchmarking.rs

This file was deleted.

4 changes: 2 additions & 2 deletions pallets/file-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ mod mock;
#[cfg(test)]
mod tests;

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

#[frame_support::pallet]
pub mod pallet {
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 @@ -353,6 +353,7 @@ impl Get<Perbill> for MinNotFullBlocksRatio {

impl pallet_proofs_dealer::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type ProvidersPallet = Providers;
type NativeBalance = Balances;
type MerkleTrieHash = H256;
Expand Down
24 changes: 0 additions & 24 deletions pallets/payment-streams/src/benchmarking.rs

This file was deleted.

4 changes: 2 additions & 2 deletions pallets/payment-streams/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
pub mod types;
mod utils;

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

#[cfg(test)]
mod mock;
Expand Down
1 change: 1 addition & 0 deletions pallets/proofs-dealer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }

sp-keyring = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
sp-weights = { workspace = true }
Expand Down
53 changes: 53 additions & 0 deletions pallets/proofs-dealer/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Benchmarking setup for pallet-proofs-dealer
use frame_benchmarking::v2::*;

#[benchmarks]
mod benchmarks {
use frame_support::{assert_ok, traits::fungible::Mutate};
use frame_system::RawOrigin;
use sp_runtime::traits::Hash;

use super::*;
use crate::{
pallet, types::MerkleTrieHashingFor, Call, ChallengesQueue, Config, Event, Pallet,
};

#[benchmark]
fn challenge() -> Result<(), BenchmarkError> {
// Setup initial conditions.
let caller: T::AccountId = whitelisted_caller();
let file_key = MerkleTrieHashingFor::<T>::hash(b"file_key");
let user_balance = match 1_000_000_000_000_000u128.try_into() {
Ok(balance) => balance,
Err(_) => return Err(BenchmarkError::Stop("Balance conversion failed.")),
};
assert_ok!(<T as crate::Config>::NativeBalance::mint_into(
&caller,
user_balance,
));

// Call some extrinsic.
#[extrinsic_call]
Pallet::challenge(RawOrigin::Signed(caller.clone()), file_key);

// Verify the challenge event was emitted.
let expected_event = <T as pallet::Config>::RuntimeEvent::from(Event::<T>::NewChallenge {
who: caller,
key_challenged: file_key,
});
frame_system::Pallet::<T>::assert_last_event(expected_event.into());

// Verify that the challenge is in the queue.
let challenges_queue = ChallengesQueue::<T>::get();
assert_eq!(challenges_queue.len(), 1);
assert_eq!(challenges_queue[0], file_key);

Ok(())
}

impl_benchmark_test_suite! {
Pallet,
crate::mock::new_test_ext(),
crate::mock::Test,
}
}
13 changes: 9 additions & 4 deletions pallets/proofs-dealer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ mod mock;
#[cfg(test)]
mod tests;

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

pub mod types;
pub mod utils;
pub mod weights;

#[frame_support::pallet]
pub mod pallet {
Expand All @@ -35,14 +37,17 @@ pub mod pallet {
use sp_std::vec::Vec;
use types::{KeyFor, ProviderIdFor};

use crate::types::*;
use crate::*;
use crate::{types::*, weights::*};

#[pallet::config]
pub trait Config: frame_system::Config {
/// 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 Providers pallet.
/// To check if whoever submits a proof is a registered Provider.
type ProvidersPallet: ReadChallengeableProvidersInterface<
Expand Down Expand Up @@ -537,7 +542,7 @@ pub mod pallet {
/// Users are charged a small fee for submitting a challenge, which
/// goes to the Treasury.
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
#[pallet::weight(T::WeightInfo::challenge())]
pub fn challenge(origin: OriginFor<T>, key: KeyFor<T>) -> DispatchResultWithPostInfo {
// Check that the extrinsic was signed and get the signer.
let who = ensure_signed(origin)?;
Expand Down
1 change: 1 addition & 0 deletions pallets/proofs-dealer/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ impl Get<Perbill> for MinNotFullBlocksRatio {

impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type ProvidersPallet = Providers;
type NativeBalance = Balances;
type MerkleTrieHash = H256;
Expand Down
3 changes: 3 additions & 0 deletions pallets/proofs-dealer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ pub type AccountIdFor<T> = <T as frame_system::Config>::AccountId;
/// Syntactic sugar for the MerkleHash type used in the proofs pallet.
pub type MerkleHashFor<T> = <T as crate::Config>::MerkleTrieHash;

/// Syntactic sugar for the MerkleTrieHashing type used in the proofs pallet.
pub type MerkleTrieHashingFor<T> = <T as crate::Config>::MerkleTrieHashing;

/// The type for keys that identify a file within a Merkle Patricia Forest.
/// Syntactic sugar for the MerkleHash type used in the proofs pallet.
pub type KeyFor<T> = <T as crate::Config>::MerkleTrieHash;
Expand Down
76 changes: 76 additions & 0 deletions pallets/proofs-dealer/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

//! Autogenerated weights for `pallet_proofs_dealer`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 43.0.0
//! DATE: 2024-10-17, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `Facundos-MacBook-Pro.local`, CPU: `<UNKNOWN>`
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024`

// Executed Command:
// ./target/release/storage-hub-node
// benchmark
// pallet
// --chain
// dev
// --wasm-execution=compiled
// --pallet
// pallet_proofs_dealer
// --extrinsic
// *
// --steps
// 50
// --repeat
// 20
// --output
// pallets/proofs-dealer/src/weights.rs
// --template
// ../polkadot-sdk/substrate/.maintain/frame-weight-template.hbs

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(missing_docs)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use core::marker::PhantomData;

/// Weight functions needed for `pallet_proofs_dealer`.
pub trait WeightInfo {
fn challenge() -> Weight;
}

/// Weights for `pallet_proofs_dealer` using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `ProofsDealer::ChallengesQueue` (r:1 w:1)
/// Proof: `ProofsDealer::ChallengesQueue` (`max_values`: Some(1), `max_size`: Some(3202), added: 3697, mode: `MaxEncodedLen`)
fn challenge() -> Weight {
// Proof Size summary in bytes:
// Measured: `94`
// Estimated: `4687`
// Minimum execution time: 40_000_000 picoseconds.
Weight::from_parts(43_000_000, 4687)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
}

// For backwards compatibility and tests.
impl WeightInfo for () {
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `ProofsDealer::ChallengesQueue` (r:1 w:1)
/// Proof: `ProofsDealer::ChallengesQueue` (`max_values`: Some(1), `max_size`: Some(3202), added: 3697, mode: `MaxEncodedLen`)
fn challenge() -> Weight {
// Proof Size summary in bytes:
// Measured: `94`
// Estimated: `4687`
// Minimum execution time: 40_000_000 picoseconds.
Weight::from_parts(43_000_000, 4687)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
}
24 changes: 0 additions & 24 deletions pallets/providers/src/benchmarking.rs

This file was deleted.

4 changes: 2 additions & 2 deletions pallets/providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
pub mod types;
mod utils;

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

#[cfg(test)]
mod mock;
Expand Down
1 change: 1 addition & 0 deletions pallets/providers/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl Get<AccountId> for TreasuryAccount {
// Proofs dealer pallet:
impl pallet_proofs_dealer::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type ProvidersPallet = StorageProviders;
type NativeBalance = Balances;
type MerkleTrieHash = H256;
Expand Down
Loading

0 comments on commit 7a3b4ac

Please sign in to comment.