From 18309c933d28a02e33764e8daded0829ef174caf Mon Sep 17 00:00:00 2001
From: Shaopeng Wang <spxwang@gmail.com>
Date: Tue, 25 Aug 2020 19:30:01 +1200
Subject: [PATCH] Update benchmarking macro. (#259)

---
 benchmarking/Cargo.toml |  3 +++
 benchmarking/src/lib.rs | 38 +++++++++++++++++++++++---------------
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/benchmarking/Cargo.toml b/benchmarking/Cargo.toml
index 98a86a2..77515fe 100644
--- a/benchmarking/Cargo.toml
+++ b/benchmarking/Cargo.toml
@@ -19,6 +19,9 @@ frame-support = { version = "2.0.0-rc6", default-features = false }
 frame-system = { version = "2.0.0-rc6", default-features = false }
 frame-benchmarking = { version = "2.0.0-rc6", default-features = false }
 
+[dev-dependencies]
+hex-literal = "0.2.1"
+
 [features]
 default = [ "std" ]
 std = [
diff --git a/benchmarking/src/lib.rs b/benchmarking/src/lib.rs
index 4f6d942..a5f89f0 100644
--- a/benchmarking/src/lib.rs
+++ b/benchmarking/src/lib.rs
@@ -7,6 +7,7 @@ mod tests;
 
 pub use frame_benchmarking::{
 	benchmarking, BenchmarkBatch, BenchmarkParameter, BenchmarkResults, Benchmarking, BenchmarkingSetup,
+	TrackedStorageKey,
 };
 #[cfg(feature = "std")]
 pub use frame_benchmarking::{Analysis, BenchmarkSelector};
@@ -718,7 +719,7 @@ macro_rules! impl_benchmark {
 				highest_range_values: &[u32],
 				steps: &[u32],
 				repeat: u32,
-				whitelist: &[Vec<u8>]
+				whitelist: &[$crate::TrackedStorageKey]
 			) -> Result<Vec<$crate::BenchmarkResults>, &'static str> {
 				// Map the input to the selected benchmark.
 				let extrinsic = sp_std::str::from_utf8(extrinsic)
@@ -757,8 +758,8 @@ macro_rules! impl_benchmark {
 						>::instance(&selected_benchmark, &c)?;
 
 						// Set the block number to at least 1 so events are deposited.
-						if $crate::Zero::is_zero(&frame_system::Module::<T>::block_number()) {
-							frame_system::Module::<T>::set_block_number(1u8.into());
+						if $crate::Zero::is_zero(&frame_system::Module::<$runtime>::block_number()) {
+							frame_system::Module::<$runtime>::set_block_number(1u8.into());
 						}
 
 						// Commit the externalities to the database, flushing the DB cache.
@@ -937,35 +938,42 @@ macro_rules! impl_benchmark_test {
 /// let params = (&pallet, &benchmark, &lowest_range_values, &highest_range_values, &steps, repeat, &whitelist);
 /// ```
 ///
-/// The `whitelist` is a `Vec<Vec<u8>>` of storage keys that you would like to
-/// skip for DB tracking. For example:
+/// The `whitelist` is a parameter you pass to control the DB read/write
+/// tracking. We use a vector of [TrackedStorageKey], which is a simple struct
+/// used to set if a key has been read or written to.
 ///
-/// ```ignore
-/// let whitelist: Vec<Vec<u8>> = vec![
+/// For values that should be skipped entirely, we can just pass `key.into()`.
+/// For example:
+///
+/// ```
+/// use frame_benchmarking::TrackedStorageKey;
+/// use hex_literal;
+/// let whitelist: Vec<TrackedStorageKey> = vec![
 ///     // Block Number
-///     hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec(),
+///     hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(),
 ///     // Total Issuance
-///     hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec(),
+///     hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec().into(),
 ///     // Execution Phase
-///     hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec(),
+///     hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec().into(),
 ///     // Event Count
-///     hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec(),
+///     hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec().into(),
 /// ];
+/// ```
 ///
 /// Then define a mutable local variable to hold your `BenchmarkBatch` object:
 /// ```ignore
 /// let mut batches = Vec::<BenchmarkBatch>::new();
 /// ````
-/// 
-/// Then add the pallets you want to benchmark to this object, using their crate name and generated
-/// module struct:
+///
+/// Then add the pallets you want to benchmark to this object, using their crate
+/// name and generated module struct:
 /// ```ignore
 /// add_benchmark!(params, batches, pallet_balances, Balances);
 /// add_benchmark!(params, batches, pallet_session, SessionBench::<Runtime>);
 /// add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
 /// ...
 /// ```
-/// 
+///
 /// At the end of `dispatch_benchmark`, you should return this batches object.
 #[macro_export]
 macro_rules! add_benchmark {
-- 
GitLab