Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 368903f

Browse files
shawntabrizigui1117apopiak
authored
Dynamic Benchmarking DB Whitelist (#6815)
* Add `get_whitelist` api * add whitelisted caller * Whitelist caller * remove caller 0 * initial piping of origin (not actual value yet) * remove attempt to pass origin around * Add whitelist for `DidUpdate` storage on `pallet_timestamp` * fix traits * only add to whitelist if !contains * PassBy not implemented error * Whitelist read/writes explicitly per key * update docs * reduce trait constraint * copy pasta * Apply suggestions from code review Co-authored-by: Guillaume Thiolliere <[email protected]> Co-authored-by: Alexander Popiak <[email protected]> * rename functions @apopiak * missed some renaming * enable doc tests * Update docs Co-authored-by: Guillaume Thiolliere <[email protected]> Co-authored-by: Alexander Popiak <[email protected]>
1 parent 3c3461d commit 368903f

File tree

26 files changed

+421
-205
lines changed

26 files changed

+421
-205
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/runtime/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ impl_runtime_apis! {
11301130
repeat: u32,
11311131
extra: bool,
11321132
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
1133-
use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark};
1133+
use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};
11341134
// Trying to add benchmarks directly to the Session Pallet caused cyclic dependency issues.
11351135
// To get around that, we separated the Session benchmarks into its own crate, which is why
11361136
// we need these two lines below.
@@ -1142,21 +1142,19 @@ impl_runtime_apis! {
11421142
impl pallet_offences_benchmarking::Trait for Runtime {}
11431143
impl frame_system_benchmarking::Trait for Runtime {}
11441144

1145-
let whitelist: Vec<Vec<u8>> = vec![
1145+
let whitelist: Vec<TrackedStorageKey> = vec![
11461146
// Block Number
1147-
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec(),
1147+
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(),
11481148
// Total Issuance
1149-
hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec(),
1149+
hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec().into(),
11501150
// Execution Phase
1151-
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec(),
1151+
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec().into(),
11521152
// Event Count
1153-
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec(),
1153+
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec().into(),
11541154
// System Events
1155-
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7").to_vec(),
1156-
// Caller 0 Account
1157-
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da946c154ffd9992e395af90b5b13cc6f295c77033fce8a9045824a6690bbf99c6db269502f0a8d1d2a008542d5690a0749").to_vec(),
1155+
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7").to_vec().into(),
11581156
// Treasury Account
1159-
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da95ecffd7b6c0f78751baa9d281e0bfa3a6d6f646c70792f74727372790000000000000000000000000000000000000000").to_vec(),
1157+
hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da95ecffd7b6c0f78751baa9d281e0bfa3a6d6f646c70792f74727372790000000000000000000000000000000000000000").to_vec().into(),
11601158
];
11611159

11621160
let mut batches = Vec::<BenchmarkBatch>::new();

client/db/src/bench.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ use std::collections::HashMap;
2424

2525
use hash_db::{Prefix, Hasher};
2626
use sp_trie::{MemoryDB, prefixed_key};
27-
use sp_core::{storage::ChildInfo, hexdisplay::HexDisplay};
27+
use sp_core::{
28+
storage::{ChildInfo, TrackedStorageKey},
29+
hexdisplay::HexDisplay
30+
};
2831
use sp_runtime::traits::{Block as BlockT, HashFor};
2932
use sp_runtime::Storage;
3033
use sp_state_machine::{DBValue, backend::Backend as StateBackend, StorageCollection};
@@ -95,7 +98,7 @@ pub struct BenchmarkingState<B: BlockT> {
9598
shared_cache: SharedCache<B>, // shared cache is always empty
9699
key_tracker: RefCell<HashMap<Vec<u8>, KeyTracker>>,
97100
read_write_tracker: RefCell<ReadWriteTracker>,
98-
whitelist: RefCell<Vec<Vec<u8>>>,
101+
whitelist: RefCell<Vec<TrackedStorageKey>>,
99102
}
100103

101104
impl<B: BlockT> BenchmarkingState<B> {
@@ -155,15 +158,14 @@ impl<B: BlockT> BenchmarkingState<B> {
155158
fn add_whitelist_to_tracker(&self) {
156159
let mut key_tracker = self.key_tracker.borrow_mut();
157160

158-
let whitelisted = KeyTracker {
159-
has_been_read: true,
160-
has_been_written: true,
161-
};
162-
163161
let whitelist = self.whitelist.borrow();
164162

165163
whitelist.iter().for_each(|key| {
166-
key_tracker.insert(key.to_vec(), whitelisted);
164+
let whitelisted = KeyTracker {
165+
has_been_read: key.has_been_read,
166+
has_been_written: key.has_been_written,
167+
};
168+
key_tracker.insert(key.key.clone(), whitelisted);
167169
});
168170
}
169171

@@ -181,18 +183,21 @@ impl<B: BlockT> BenchmarkingState<B> {
181183

182184
let maybe_tracker = key_tracker.get(key);
183185

184-
let has_been_read = KeyTracker {
185-
has_been_read: true,
186-
has_been_written: false,
187-
};
188-
189186
match maybe_tracker {
190187
None => {
188+
let has_been_read = KeyTracker {
189+
has_been_read: true,
190+
has_been_written: false,
191+
};
191192
key_tracker.insert(key.to_vec(), has_been_read);
192193
read_write_tracker.add_read();
193194
},
194195
Some(tracker) => {
195196
if !tracker.has_been_read {
197+
let has_been_read = KeyTracker {
198+
has_been_read: true,
199+
has_been_written: tracker.has_been_written,
200+
};
196201
key_tracker.insert(key.to_vec(), has_been_read);
197202
read_write_tracker.add_read();
198203
} else {
@@ -426,7 +431,11 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
426431
self.wipe_tracker()
427432
}
428433

429-
fn set_whitelist(&self, new: Vec<Vec<u8>>) {
434+
fn get_whitelist(&self) -> Vec<TrackedStorageKey> {
435+
self.whitelist.borrow().to_vec()
436+
}
437+
438+
fn set_whitelist(&self, new: Vec<TrackedStorageKey>) {
430439
*self.whitelist.borrow_mut() = new;
431440
}
432441

frame/balances/src/benchmarking.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use super::*;
2323

2424
use frame_system::RawOrigin;
25-
use frame_benchmarking::{benchmarks, account};
25+
use frame_benchmarking::{benchmarks, account, whitelisted_caller};
2626
use sp_runtime::traits::Bounded;
2727

2828
use crate::Module as Balances;
@@ -40,7 +40,7 @@ benchmarks! {
4040
// * Transfer will create the recipient account.
4141
transfer {
4242
let existential_deposit = T::ExistentialDeposit::get();
43-
let caller = account("caller", 0, SEED);
43+
let caller = whitelisted_caller();
4444

4545
// Give some multiple of the existential deposit + creation fee + transfer fee
4646
let balance = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
@@ -60,7 +60,7 @@ benchmarks! {
6060
// * Both accounts exist and will continue to exist.
6161
#[extra]
6262
transfer_best_case {
63-
let caller = account("caller", 0, SEED);
63+
let caller = whitelisted_caller();
6464
let recipient: T::AccountId = account("recipient", 0, SEED);
6565
let recipient_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(recipient.clone());
6666

@@ -80,7 +80,7 @@ benchmarks! {
8080
// Benchmark `transfer_keep_alive` with the worst possible condition:
8181
// * The recipient account is created.
8282
transfer_keep_alive {
83-
let caller = account("caller", 0, SEED);
83+
let caller = whitelisted_caller();
8484
let recipient: T::AccountId = account("recipient", 0, SEED);
8585
let recipient_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(recipient.clone());
8686

frame/benchmarking/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ sp-runtime-interface = { version = "2.0.0-rc5", path = "../../primitives/runtime
2020
sp-runtime = { version = "2.0.0-rc5", path = "../../primitives/runtime", default-features = false }
2121
sp-std = { version = "2.0.0-rc5", path = "../../primitives/std", default-features = false }
2222
sp-io = { version = "2.0.0-rc5", path = "../../primitives/io", default-features = false }
23+
sp-storage = { version = "2.0.0-rc5", path = "../../primitives/storage", default-features = false }
2324
frame-support = { version = "2.0.0-rc5", default-features = false, path = "../support" }
2425
frame-system = { version = "2.0.0-rc5", default-features = false, path = "../system" }
2526

27+
[dev-dependencies]
28+
hex-literal = "0.2.1"
29+
2630
[features]
2731
default = [ "std" ]
2832
std = [

0 commit comments

Comments
 (0)