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
+29 -22
View File
@@ -101,14 +101,11 @@ pub use bench::BenchmarkingState;
const CACHE_HEADERS: usize = 8;
/// DB-backed patricia trie state, transaction type is an overlay of changes to commit.
pub type DbState<B> =
sp_state_machine::TrieBackend<Arc<dyn sp_state_machine::Storage<HashingFor<B>>>, HashingFor<B>>;
pub type DbState<H> = sp_state_machine::TrieBackend<Arc<dyn sp_state_machine::Storage<H>>, H>;
/// Builder for [`DbState`].
pub type DbStateBuilder<B> = sp_state_machine::TrieBackendBuilder<
Arc<dyn sp_state_machine::Storage<HashingFor<B>>>,
HashingFor<B>,
>;
pub type DbStateBuilder<Hasher> =
sp_state_machine::TrieBackendBuilder<Arc<dyn sp_state_machine::Storage<Hasher>>, Hasher>;
/// Length of a [`DbHash`].
const DB_HASH_LEN: usize = 32;
@@ -135,13 +132,17 @@ enum DbExtrinsic<B: BlockT> {
/// It makes sure that the hash we are using stays pinned in storage
/// until this structure is dropped.
pub struct RefTrackingState<Block: BlockT> {
state: DbState<Block>,
state: DbState<HashingFor<Block>>,
storage: Arc<StorageDb<Block>>,
parent_hash: Option<Block::Hash>,
}
impl<B: BlockT> RefTrackingState<B> {
fn new(state: DbState<B>, storage: Arc<StorageDb<B>>, parent_hash: Option<B::Hash>) -> Self {
fn new(
state: DbState<HashingFor<B>>,
storage: Arc<StorageDb<B>>,
parent_hash: Option<B::Hash>,
) -> Self {
RefTrackingState { state, parent_hash, storage }
}
}
@@ -162,12 +163,12 @@ impl<Block: BlockT> std::fmt::Debug for RefTrackingState<Block> {
/// A raw iterator over the `RefTrackingState`.
pub struct RawIter<B: BlockT> {
inner: <DbState<B> as StateBackend<HashingFor<B>>>::RawIter,
inner: <DbState<HashingFor<B>> as StateBackend<HashingFor<B>>>::RawIter,
}
impl<B: BlockT> StorageIterator<HashingFor<B>> for RawIter<B> {
type Backend = RefTrackingState<B>;
type Error = <DbState<B> as StateBackend<HashingFor<B>>>::Error;
type Error = <DbState<HashingFor<B>> as StateBackend<HashingFor<B>>>::Error;
fn next_key(&mut self, backend: &Self::Backend) -> Option<Result<StorageKey, Self::Error>> {
self.inner.next_key(&backend.state)
@@ -186,8 +187,9 @@ impl<B: BlockT> StorageIterator<HashingFor<B>> for RawIter<B> {
}
impl<B: BlockT> StateBackend<HashingFor<B>> for RefTrackingState<B> {
type Error = <DbState<B> as StateBackend<HashingFor<B>>>::Error;
type TrieBackendStorage = <DbState<B> as StateBackend<HashingFor<B>>>::TrieBackendStorage;
type Error = <DbState<HashingFor<B>> as StateBackend<HashingFor<B>>>::Error;
type TrieBackendStorage =
<DbState<HashingFor<B>> as StateBackend<HashingFor<B>>>::TrieBackendStorage;
type RawIter = RawIter<B>;
fn storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
@@ -284,7 +286,8 @@ impl<B: BlockT> StateBackend<HashingFor<B>> for RefTrackingState<B> {
}
impl<B: BlockT> AsTrieBackend<HashingFor<B>> for RefTrackingState<B> {
type TrieBackendStorage = <DbState<B> as StateBackend<HashingFor<B>>>::TrieBackendStorage;
type TrieBackendStorage =
<DbState<HashingFor<B>> as StateBackend<HashingFor<B>>>::TrieBackendStorage;
fn as_trie_backend(
&self,
@@ -1936,7 +1939,7 @@ impl<Block: BlockT> Backend<Block> {
fn empty_state(&self) -> RecordStatsState<RefTrackingState<Block>, Block> {
let root = EmptyStorage::<Block>::new().0; // Empty trie
let db_state = DbStateBuilder::<Block>::new(self.storage.clone(), root)
let db_state = DbStateBuilder::<HashingFor<Block>>::new(self.storage.clone(), root)
.with_optional_cache(self.shared_trie_cache.as_ref().map(|c| c.local_cache()))
.build();
let state = RefTrackingState::new(db_state, self.storage.clone(), None);
@@ -2428,9 +2431,12 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> {
if hash == self.blockchain.meta.read().genesis_hash {
if let Some(genesis_state) = &*self.genesis_state.read() {
let root = genesis_state.root;
let db_state = DbStateBuilder::<Block>::new(genesis_state.clone(), root)
.with_optional_cache(self.shared_trie_cache.as_ref().map(|c| c.local_cache()))
.build();
let db_state =
DbStateBuilder::<HashingFor<Block>>::new(genesis_state.clone(), root)
.with_optional_cache(
self.shared_trie_cache.as_ref().map(|c| c.local_cache()),
)
.build();
let state = RefTrackingState::new(db_state, self.storage.clone(), None);
return Ok(RecordStatsState::new(state, None, self.state_usage.clone()))
@@ -2449,11 +2455,12 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> {
self.storage.state_db.pin(&hash, hdr.number.saturated_into::<u64>(), hint)
{
let root = hdr.state_root;
let db_state = DbStateBuilder::<Block>::new(self.storage.clone(), root)
.with_optional_cache(
self.shared_trie_cache.as_ref().map(|c| c.local_cache()),
)
.build();
let db_state =
DbStateBuilder::<HashingFor<Block>>::new(self.storage.clone(), root)
.with_optional_cache(
self.shared_trie_cache.as_ref().map(|c| c.local_cache()),
)
.build();
let state = RefTrackingState::new(db_state, self.storage.clone(), Some(hash));
Ok(RecordStatsState::new(state, Some(hash), self.state_usage.clone()))
} else {