bench pallet: only require Hash instead of Block (#3244)

Preparation for https://github.com/paritytech/polkadot-sdk/issues/2664

Changes:
- Only require `Hash` instead of `Block` for the benchmarking
- Refactor DB types to do the same

## Integration

This breaking change can easily be integrated into your node via:  
```patch
- cmd.run::<Block, ()>(config)
+ cmd.run::<HashingFor<Block>, ()>(config)
```

Status: waiting for CI checks

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: cheme <emericchevalier.pro@gmail.com>
This commit is contained in:
Oliver Tale-Yazdi
2024-02-08 18:27:52 +01:00
committed by GitHub
parent a2e6256c0d
commit c36c51cac3
12 changed files with 120 additions and 86 deletions
@@ -37,7 +37,7 @@ use sp_core::{
};
use sp_externalities::Extensions;
use sp_keystore::{testing::MemoryKeystore, KeystoreExt};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use sp_runtime::traits::Hash;
use sp_state_machine::StateMachine;
use std::{collections::HashMap, fmt::Debug, fs, str::FromStr, time};
@@ -140,11 +140,10 @@ This could mean that you either did not build the node correctly with the \
not created by a node that was compiled with the flag";
impl PalletCmd {
/// Runs the command and benchmarks the chain.
pub fn run<BB, ExtraHostFunctions>(&self, config: Configuration) -> Result<()>
/// Runs the command and benchmarks a pallet.
pub fn run<Hasher, ExtraHostFunctions>(&self, config: Configuration) -> Result<()>
where
BB: BlockT + Debug,
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
Hasher: Hash,
ExtraHostFunctions: sp_wasm_interface::HostFunctions,
{
let _d = self.execution.as_ref().map(|exec| {
@@ -199,7 +198,7 @@ impl PalletCmd {
let genesis_storage = spec.build_storage()?;
let mut changes = Default::default();
let cache_size = Some(self.database_cache_size as usize);
let state_with_tracking = BenchmarkingState::<BB>::new(
let state_with_tracking = BenchmarkingState::<Hasher>::new(
genesis_storage.clone(),
cache_size,
// Record proof size
@@ -207,7 +206,7 @@ impl PalletCmd {
// Enable storage tracking
true,
)?;
let state_without_tracking = BenchmarkingState::<BB>::new(
let state_without_tracking = BenchmarkingState::<Hasher>::new(
genesis_storage,
cache_size,
// Do not record proof size
@@ -57,7 +57,7 @@ impl StorageCmd {
let best_hash = client.usage_info().chain.best_hash;
let header = client.header(best_hash)?.ok_or("Header not found")?;
let original_root = *header.state_root();
let trie = DbStateBuilder::<Block>::new(storage.clone(), original_root).build();
let trie = DbStateBuilder::<HashingFor<Block>>::new(storage.clone(), original_root).build();
info!("Preparing keys from block {}", best_hash);
// Load all KV pairs and randomly shuffle them.
@@ -189,7 +189,7 @@ fn convert_tx<B: BlockT>(
/// if `child_info` exist then it means this is a child tree key
fn measure_write<Block: BlockT>(
db: Arc<dyn sp_database::Database<DbHash>>,
trie: &DbState<Block>,
trie: &DbState<HashingFor<Block>>,
key: Vec<u8>,
new_v: Vec<u8>,
version: StateVersion,
@@ -220,7 +220,7 @@ fn measure_write<Block: BlockT>(
/// if `child_info` exist then it means this is a child tree key
fn check_new_value<Block: BlockT>(
db: Arc<dyn sp_database::Database<DbHash>>,
trie: &DbState<Block>,
trie: &DbState<HashingFor<Block>>,
key: &Vec<u8>,
new_v: &Vec<u8>,
version: StateVersion,