From 7a3b4ac2ef1b2ee340fd9af7fdd8c80dfc5d6463 Mon Sep 17 00:00:00 2001 From: Facundo Farall <37149322+ffarall@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:18:42 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Enable=20benchmarking=20pro?= =?UTF-8?q?ject-wide=20and=20benchmark=20`challenge`=20extrinsic=20(#232)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * build: :construction_worker: Make build work with `runtime-benchmarks` feature * feat: :sparkles: Run benchmarkings and set weights for `challenge` transaction --- Cargo.lock | 1 + Cargo.toml | 5 ++ pallets/bucket-nfts/Cargo.toml | 1 + pallets/bucket-nfts/src/benchmarking.rs | 30 -------- pallets/bucket-nfts/src/lib.rs | 4 +- pallets/file-system/src/benchmarking.rs | 30 -------- pallets/file-system/src/lib.rs | 4 +- pallets/file-system/src/mock.rs | 1 + pallets/payment-streams/src/benchmarking.rs | 24 ------- pallets/payment-streams/src/lib.rs | 4 +- pallets/proofs-dealer/Cargo.toml | 1 + pallets/proofs-dealer/src/benchmarking.rs | 53 ++++++++++++++ pallets/proofs-dealer/src/lib.rs | 13 ++-- pallets/proofs-dealer/src/mock.rs | 1 + pallets/proofs-dealer/src/types.rs | 3 + pallets/proofs-dealer/src/weights.rs | 76 +++++++++++++++++++++ pallets/providers/src/benchmarking.rs | 24 ------- pallets/providers/src/lib.rs | 4 +- pallets/providers/src/mock.rs | 1 + runtime/Cargo.toml | 1 - runtime/src/configs/mod.rs | 1 + runtime/src/lib.rs | 1 + xcm-simulator/src/storagehub/configs/mod.rs | 1 + xcm-simulator/src/storagehub/mod.rs | 3 + 24 files changed, 166 insertions(+), 121 deletions(-) delete mode 100644 pallets/bucket-nfts/src/benchmarking.rs delete mode 100644 pallets/file-system/src/benchmarking.rs delete mode 100644 pallets/payment-streams/src/benchmarking.rs create mode 100644 pallets/proofs-dealer/src/benchmarking.rs create mode 100644 pallets/proofs-dealer/src/weights.rs delete mode 100644 pallets/providers/src/benchmarking.rs diff --git a/Cargo.lock b/Cargo.lock index dd27f0854..11a858542 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7632,6 +7632,7 @@ dependencies = [ "shp-traits", "sp-core 34.0.0", "sp-io 38.0.0", + "sp-keyring", "sp-runtime 39.0.1", "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk.git?branch=stable2409)", "sp-trie 37.0.0", diff --git a/Cargo.toml b/Cargo.toml index bba1b67d8..5d4f39617 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,11 @@ homepage = "https://moonsonglabs.com/" [profile.release] panic = "unwind" +[profile.production] +codegen-units = 1 +inherits = "release" +lto = true + [workspace] members = [ "runtime", diff --git a/pallets/bucket-nfts/Cargo.toml b/pallets/bucket-nfts/Cargo.toml index 394226e26..3fa543ad9 100644 --- a/pallets/bucket-nfts/Cargo.toml +++ b/pallets/bucket-nfts/Cargo.toml @@ -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 } diff --git a/pallets/bucket-nfts/src/benchmarking.rs b/pallets/bucket-nfts/src/benchmarking.rs deleted file mode 100644 index 05ffaaee2..000000000 --- a/pallets/bucket-nfts/src/benchmarking.rs +++ /dev/null @@ -1,30 +0,0 @@ -//! Benchmarking setup for pallet-file-system - -use super::*; - -#[allow(unused)] -use crate::Pallet as BucketNfts; -use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller}; -use frame_system::RawOrigin; -use pallet_nfts::BenchmarkHelper as NftsBenchmarkHelper; -use sp_core::H256; -use sp_runtime::traits::StaticLookup; -use types::{BucketIdFor, ReadAccessRegex}; - -use crate::pallet::BenchmarkHelper as BucketNftsBenchmarkHelper; - -benchmarks! { - share_access { - let s in 0 .. 100; - let caller: T::AccountId = whitelisted_caller(); - let recipient = T::Lookup::unlookup(caller.clone()); - let bucket: BucketIdFor = ::Helper::bucket(H256::default()); - let item_id: T::ItemId = ::Helper::item(0); - let read_access: ReadAccessRegex = sp_runtime::BoundedVec::::StringLimit>::default(); - }: _(RawOrigin::Signed(caller), recipient, bucket, item_id, Some(read_access)) - verify { - // assert!(BucketNfts::::read_access((bucket, item_id)).is_some()); - } -} - -impl_benchmark_test_suite!(BucketNfts, crate::mock::new_test_ext(), crate::mock::Test); diff --git a/pallets/bucket-nfts/src/lib.rs b/pallets/bucket-nfts/src/lib.rs index b83be2233..3e9ea721a 100644 --- a/pallets/bucket-nfts/src/lib.rs +++ b/pallets/bucket-nfts/src/lib.rs @@ -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 { diff --git a/pallets/file-system/src/benchmarking.rs b/pallets/file-system/src/benchmarking.rs deleted file mode 100644 index 470e95319..000000000 --- a/pallets/file-system/src/benchmarking.rs +++ /dev/null @@ -1,30 +0,0 @@ -//! Benchmarking setup for pallet-file-system - -// use super::*; - -// #[allow(unused)] -// use crate::Pallet as FileSystem; -// use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller}; -// use frame_system::RawOrigin; -// use sp_runtime::testing::H256; - -// use crate::types::{FileLocation, Fingerprint, PeerIds, StorageData}; - -// benchmarks! { -// issue_storage_request { -// let s in 0 .. 100; -// let caller: T::AccountId = whitelisted_caller(); -// let location: FileLocation = Default::default(); -// let fingerprint: Fingerprint = Default::default(); -// let size: StorageData = Default::default(); -// let msp = H256::default(); -// let peer_ids: PeerIds = Default::default(); -// }: _(RawOrigin::Signed(caller), location.clone(), fingerprint, size, msp, peer_ids) -// verify { -// assert!(FileSystem::::storage_requests(location).is_some()); -// } - -// // TODO: add benchmarking for `on_idle` -// } - -// impl_benchmark_test_suite!(FileSystem, crate::mock::new_test_ext(), crate::mock::Test); diff --git a/pallets/file-system/src/lib.rs b/pallets/file-system/src/lib.rs index f7e4bc4ab..1f3f7a222 100644 --- a/pallets/file-system/src/lib.rs +++ b/pallets/file-system/src/lib.rs @@ -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 { diff --git a/pallets/file-system/src/mock.rs b/pallets/file-system/src/mock.rs index 5e87eb52d..9d3ab5783 100644 --- a/pallets/file-system/src/mock.rs +++ b/pallets/file-system/src/mock.rs @@ -353,6 +353,7 @@ impl Get for MinNotFullBlocksRatio { impl pallet_proofs_dealer::Config for Test { type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); type ProvidersPallet = Providers; type NativeBalance = Balances; type MerkleTrieHash = H256; diff --git a/pallets/payment-streams/src/benchmarking.rs b/pallets/payment-streams/src/benchmarking.rs deleted file mode 100644 index 18651678e..000000000 --- a/pallets/payment-streams/src/benchmarking.rs +++ /dev/null @@ -1,24 +0,0 @@ -//! Benchmarking setup for pallet-payment-streams - -/* use super::*; - -#[allow(unused)] -use crate::Pallet as PaymentStreams; -use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller}; -use frame_system::RawOrigin; - -benchmarks! { - do_something { - let s in 0 .. 100; - let caller: T::AccountId = whitelisted_caller(); - }: _(RawOrigin::Signed(caller), s) - verify { - assert_eq!(Something::::get(), Some(s)); - } -} - -impl_benchmark_test_suite!( - PaymentStreams, - crate::mock::new_test_ext(), - crate::mock::Test, -); */ diff --git a/pallets/payment-streams/src/lib.rs b/pallets/payment-streams/src/lib.rs index 2cb0fe20a..fad569954 100644 --- a/pallets/payment-streams/src/lib.rs +++ b/pallets/payment-streams/src/lib.rs @@ -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; diff --git a/pallets/proofs-dealer/Cargo.toml b/pallets/proofs-dealer/Cargo.toml index 5f80bef5d..e9114b9c4 100644 --- a/pallets/proofs-dealer/Cargo.toml +++ b/pallets/proofs-dealer/Cargo.toml @@ -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 } diff --git a/pallets/proofs-dealer/src/benchmarking.rs b/pallets/proofs-dealer/src/benchmarking.rs new file mode 100644 index 000000000..c2d94ca7d --- /dev/null +++ b/pallets/proofs-dealer/src/benchmarking.rs @@ -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::::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!(::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 = ::RuntimeEvent::from(Event::::NewChallenge { + who: caller, + key_challenged: file_key, + }); + frame_system::Pallet::::assert_last_event(expected_event.into()); + + // Verify that the challenge is in the queue. + let challenges_queue = ChallengesQueue::::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, + } +} diff --git a/pallets/proofs-dealer/src/lib.rs b/pallets/proofs-dealer/src/lib.rs index 242679edd..4fec508d1 100644 --- a/pallets/proofs-dealer/src/lib.rs +++ b/pallets/proofs-dealer/src/lib.rs @@ -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 { @@ -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> + IsType<::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< @@ -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, key: KeyFor) -> DispatchResultWithPostInfo { // Check that the extrinsic was signed and get the signer. let who = ensure_signed(origin)?; diff --git a/pallets/proofs-dealer/src/mock.rs b/pallets/proofs-dealer/src/mock.rs index 7eb4b4d7d..b198b1678 100644 --- a/pallets/proofs-dealer/src/mock.rs +++ b/pallets/proofs-dealer/src/mock.rs @@ -272,6 +272,7 @@ impl Get for MinNotFullBlocksRatio { impl crate::Config for Test { type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); type ProvidersPallet = Providers; type NativeBalance = Balances; type MerkleTrieHash = H256; diff --git a/pallets/proofs-dealer/src/types.rs b/pallets/proofs-dealer/src/types.rs index 024751ecb..bd45b7900 100644 --- a/pallets/proofs-dealer/src/types.rs +++ b/pallets/proofs-dealer/src/types.rs @@ -67,6 +67,9 @@ pub type AccountIdFor = ::AccountId; /// Syntactic sugar for the MerkleHash type used in the proofs pallet. pub type MerkleHashFor = ::MerkleTrieHash; +/// Syntactic sugar for the MerkleTrieHashing type used in the proofs pallet. +pub type MerkleTrieHashingFor = ::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 = ::MerkleTrieHash; diff --git a/pallets/proofs-dealer/src/weights.rs b/pallets/proofs-dealer/src/weights.rs new file mode 100644 index 000000000..65f31f8e9 --- /dev/null +++ b/pallets/proofs-dealer/src/weights.rs @@ -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: `` +//! 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(PhantomData); +impl WeightInfo for SubstrateWeight { + /// 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)) + } +} diff --git a/pallets/providers/src/benchmarking.rs b/pallets/providers/src/benchmarking.rs deleted file mode 100644 index 36db313f2..000000000 --- a/pallets/providers/src/benchmarking.rs +++ /dev/null @@ -1,24 +0,0 @@ -//! Benchmarking setup for pallet-providers - -/* use super::*; - -#[allow(unused)] -use crate::Pallet as StorageProviders; -use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller}; -use frame_system::RawOrigin; - -benchmarks! { - do_something { - let s in 0 .. 100; - let caller: T::AccountId = whitelisted_caller(); - }: _(RawOrigin::Signed(caller), s) - verify { - assert_eq!(Something::::get(), Some(s)); - } -} - -impl_benchmark_test_suite!( - StorageProviders, - crate::mock::new_test_ext(), - crate::mock::Test, -); */ diff --git a/pallets/providers/src/lib.rs b/pallets/providers/src/lib.rs index 37d070d3b..2d2eb2f00 100644 --- a/pallets/providers/src/lib.rs +++ b/pallets/providers/src/lib.rs @@ -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; diff --git a/pallets/providers/src/mock.rs b/pallets/providers/src/mock.rs index cba6928f3..abe9cf53f 100644 --- a/pallets/providers/src/mock.rs +++ b/pallets/providers/src/mock.rs @@ -120,6 +120,7 @@ impl Get 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; diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index a2d1b97ea..9261ef405 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -185,7 +185,6 @@ runtime-benchmarks = [ "cumulus-pallet-parachain-system/runtime-benchmarks", "cumulus-pallet-session-benchmarking/runtime-benchmarks", "cumulus-pallet-xcmp-queue/runtime-benchmarks", - "cumulus-primitives-aura/std", "cumulus-primitives-core/runtime-benchmarks", "cumulus-primitives-utility/runtime-benchmarks", "frame-benchmarking/runtime-benchmarks", diff --git a/runtime/src/configs/mod.rs b/runtime/src/configs/mod.rs index fdc3782ea..701963c7a 100644 --- a/runtime/src/configs/mod.rs +++ b/runtime/src/configs/mod.rs @@ -558,6 +558,7 @@ parameter_types! { impl pallet_proofs_dealer::Config for Runtime { type RuntimeEvent = RuntimeEvent; + type WeightInfo = pallet_proofs_dealer::weights::SubstrateWeight; type ProvidersPallet = Providers; type NativeBalance = Balances; type MerkleTrieHash = Hash; diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 8704507e7..d2925ca98 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -304,6 +304,7 @@ mod benches { [cumulus_pallet_xcmp_queue, XcmpQueue] [nfts, Nfts] [pallet_parameters, Parameters] + [pallet_proofs_dealer, ProofsDealer] ); } diff --git a/xcm-simulator/src/storagehub/configs/mod.rs b/xcm-simulator/src/storagehub/configs/mod.rs index aeeaf143a..4f43d57f6 100644 --- a/xcm-simulator/src/storagehub/configs/mod.rs +++ b/xcm-simulator/src/storagehub/configs/mod.rs @@ -576,6 +576,7 @@ parameter_types! { impl pallet_proofs_dealer::Config for Runtime { type RuntimeEvent = RuntimeEvent; + type WeightInfo = pallet_proofs_dealer::weights::SubstrateWeight; type ProvidersPallet = Providers; type NativeBalance = Balances; type MerkleTrieHash = Hash; diff --git a/xcm-simulator/src/storagehub/mod.rs b/xcm-simulator/src/storagehub/mod.rs index a7513aef5..56c01dd8f 100644 --- a/xcm-simulator/src/storagehub/mod.rs +++ b/xcm-simulator/src/storagehub/mod.rs @@ -287,6 +287,9 @@ frame_benchmarking::define_benchmarks!( [pallet_collator_selection, CollatorSelection] [cumulus_pallet_parachain_system, ParachainSystem] [cumulus_pallet_xcmp_queue, XcmpQueue] + [nfts, Nfts] + [pallet_parameters, Parameters] + [pallet_proofs_dealer, ProofsDealer] ); cumulus_pallet_parachain_system::register_validate_block! {