瀏覽代碼

runtime: Add weights generation for the storage v2 pallet.

Shamil Gadelshin 3 年之前
父節點
當前提交
73b618bbf7

+ 1 - 0
Cargo.lock

@@ -2401,6 +2401,7 @@ dependencies = [
  "frame-system",
  "frame-system-benchmarking",
  "frame-system-rpc-runtime-api",
+ "hex-literal",
  "pallet-authority-discovery",
  "pallet-authorship",
  "pallet-babe",

+ 3 - 0
runtime-modules/storage-v2/src/benchmarking.rs

@@ -3,6 +3,9 @@
 use frame_benchmarking::{benchmarks};
 use frame_system::RawOrigin;
 use frame_support::storage::StorageMap;
+use sp_std::vec::{Vec};
+use sp_std::vec;
+use sp_std::boxed::Box;
 
 use crate::{StorageBucketById, Trait, Call, Module};
 

+ 1 - 0
runtime-modules/storage-v2/src/lib.rs

@@ -33,6 +33,7 @@ use sp_arithmetic::traits::One;
 use sp_runtime::traits::{MaybeSerialize, Member};
 use sp_std::collections::btree_map::BTreeMap;
 use sp_std::collections::btree_set::BTreeSet;
+use sp_std::vec::Vec;
 
 /// Storage trait.
 pub trait Trait: frame_system::Trait + balances::Trait + membership::Trait {

+ 2 - 0
runtime/Cargo.toml

@@ -58,6 +58,7 @@ frame-benchmarking = { git = 'https://github.com/paritytech/substrate.git', rev
 frame-system-benchmarking = { git = 'https://github.com/paritytech/substrate.git', rev = '2cd20966cc09b059817c3ebe12fc130cdd850d62', default-features = false, optional = true }
 pallet-offences-benchmarking = { git = 'https://github.com/paritytech/substrate.git', rev = '2cd20966cc09b059817c3ebe12fc130cdd850d62', default-features = false, optional = true }
 pallet-session-benchmarking = { git = 'https://github.com/paritytech/substrate.git', rev = '2cd20966cc09b059817c3ebe12fc130cdd850d62', default-features = false, optional = true }
+hex-literal = { optional = true, version = '0.3.1' }
 
 # Joystream
 common = { package = 'pallet-common', default-features = false, path = '../runtime-modules/common'}
@@ -174,6 +175,7 @@ runtime-benchmarks = [
 	'pallet-session-benchmarking',
     'substrate-utility/runtime-benchmarks',
     'storage-v2/runtime-benchmarks',
+    "hex-literal",
 ]
 
 

+ 12 - 0
runtime/src/lib.rs

@@ -672,6 +672,17 @@ parameter_types! {
     pub const SurchargeReward: Balance = 0; // no reward
 }
 
+parameter_types! {
+    pub const MaxStorageBucketNumber: u64 = 1;
+}
+
+impl storage_v2::Trait for Runtime {
+    type Event = Event;
+    type DataObjectId = DataObjectId;
+    type StorageBucketId = StorageBucketId;
+    type MaxStorageBucketNumber = MaxStorageBucketNumber;
+}
+
 /// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
 /// the specifics of the runtime. They can then be made to be agnostic over specific formats
 /// of data like extrinsics, allowing for them to continue syncing the network through upgrades
@@ -740,5 +751,6 @@ construct_runtime!(
         // reserved for the future use: ForumWorkingGroup: working_group::<Instance1>::{Module, Call, Storage, Event<T>},
         StorageWorkingGroup: working_group::<Instance2>::{Module, Call, Storage, Config<T>, Event<T>},
         ContentDirectoryWorkingGroup: working_group::<Instance3>::{Module, Call, Storage, Config<T>, Event<T>},
+        StorageV2: storage_v2::{Module, Call, Storage, Event<T>},
     }
 );

+ 6 - 0
runtime/src/primitives.rs

@@ -65,6 +65,12 @@ pub type ActorId = u64;
 /// Represent an member in membership group, which is the same in the working groups.
 pub type MemberId = u64;
 
+/// Represent a data object from the storage pallet.
+pub type DataObjectId = u64;
+
+/// Represent a storage bucket from the storage pallet.
+pub type StorageBucketId = u64;
+
 /// App-specific crypto used for reporting equivocation/misbehavior in BABE and
 /// GRANDPA. Any rewards for misbehavior reporting will be paid out to this
 /// account.

+ 46 - 0
runtime/src/runtime_api.rs

@@ -244,4 +244,50 @@ impl_runtime_apis! {
             SessionKeys::decode_into_raw_public_keys(&encoded)
         }
     }
+
+
+    #[cfg(feature = "runtime-benchmarks")]
+    impl frame_benchmarking::Benchmark<Block> for Runtime {
+        fn dispatch_benchmark(
+            config: frame_benchmarking::BenchmarkConfig
+        ) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
+            use sp_std::vec;
+            use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};
+
+            use crate::StorageV2;
+
+
+            // Trying to add benchmarks directly to the Session Pallet caused cyclic dependency issues.
+            // To get around that, we separated the Session benchmarks into its own crate, which is why
+            // we need these two lines below.
+            impl pallet_session_benchmarking::Trait for Runtime {}
+            impl frame_system_benchmarking::Trait for Runtime {}
+
+            let whitelist: Vec<TrackedStorageKey> = vec![
+                // Block Number
+                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(),
+                // Total Issuance
+                hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec().into(),
+                // Execution Phase
+                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec().into(),
+                // Event Count
+                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec().into(),
+                // System Events
+                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7").to_vec().into(),
+                // Caller 0 Account
+                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da946c154ffd9992e395af90b5b13cc6f295c77033fce8a9045824a6690bbf99c6db269502f0a8d1d2a008542d5690a0749").to_vec().into(),
+                // Treasury Account
+                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da95ecffd7b6c0f78751baa9d281e0bfa3a6d6f646c70792f74727372790000000000000000000000000000000000000000").to_vec().into(),
+            ];
+
+            let mut batches = Vec::<BenchmarkBatch>::new();
+            let params = (&config, &whitelist);
+
+            // Joystream Benchmarks
+            add_benchmark!(params, batches, storage_v2, StorageV2);
+
+            if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
+            Ok(batches)
+        }
+    }
 }

+ 15 - 0
runtime/src/weights/storage_v2.rs

@@ -0,0 +1,15 @@
+//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.1
+
+#![allow(unused_parens)]
+#![allow(unused_imports)]
+
+use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
+
+pub struct WeightInfo;
+impl storage_v2::WeightInfo for WeightInfo {
+	fn create_storage_bucket() -> Weight {
+		(157_000_000 as Weight)
+			.saturating_add(DbWeight::get().reads(2 as Weight))
+			.saturating_add(DbWeight::get().writes(3 as Weight))
+	}
+}

+ 36 - 0
scripts/generate-weights.sh

@@ -0,0 +1,36 @@
+#!/usr/bin/env bash
+
+# Executes and replaces all benchmarks with the new weights
+
+SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
+
+STEPS=${1:-50}
+REPEAT=${2:-20}
+
+benchmark() {
+  echo "Generating weights for $1..."
+  start=`date +%s`
+  ERROR=$($SCRIPT_DIR/../target/release/joystream-node benchmark \
+      --pallet=$1 \
+      --extrinsic=* \
+      --chain=dev \
+      --steps=$STEPS \
+      --repeat=$REPEAT \
+      --execution=wasm \
+      --output=. 2>&1 > /dev/null)
+
+
+  if [[ $ERROR != *"Error"* ]]; then
+      end=`date +%s`
+      mv $SCRIPT_DIR/../*.rs $SCRIPT_DIR/../runtime/src/weights/
+      echo "Weights generated successfully for $1"
+      echo "It took $((end-start)) seconds"
+  else
+      >&2 echo "$ERROR"
+      >&2 echo "There was a problem generating the weights for $1, check the error above"
+      exit 1
+  fi
+}
+
+# Joystream benchmarks
+benchmark storage_v2