Use trie-cache for validate_block (#2813)

* Simple cache

* Fix node insertion

* Switch to hashbrown hashmap

* Remove unused phantomdata

* Return error when fetch_node fails

* Remove cargo patches

* Move trie cache to extra module

* Add ReadOnceBackend

* Add readonlybackend

* Improve naming and get_or_insert

* Stylistic improvements

* Improve naming, add docs

* Revert unwanted changes

* Remove unused dependencies

* Improve docs

* Use RefCell

* lockfile

* Remove ReadOnceBackend

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Code review

* Do not use value cache when calculating storage roots

* Apply suggestions from code review

Co-authored-by: Davide Galassi <davxy@datawok.net>

* Remove hash-db dep

* Update pallets/parachain-system/src/validate_block/trie_cache.rs

Co-authored-by: Anton <anton.kalyaev@gmail.com>

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Davide Galassi <davxy@datawok.net>
Co-authored-by: Anton <anton.kalyaev@gmail.com>
This commit is contained in:
Sebastian Kunert
2023-07-19 13:46:58 +02:00
committed by GitHub
parent 18340b304b
commit 5b5942ef60
5 changed files with 124 additions and 4 deletions
@@ -16,7 +16,7 @@
//! The actual implementation of the validate block functionality.
use super::MemoryOptimizedValidationParams;
use super::{trie_cache, MemoryOptimizedValidationParams};
use cumulus_primitives_core::{
relay_chain::Hash as RHash, ParachainBlockData, PersistedValidationData,
};
@@ -34,7 +34,11 @@ use sp_runtime::traits::{Block as BlockT, Extrinsic, HashFor, Header as HeaderT}
use sp_std::prelude::*;
use sp_trie::MemoryDB;
type TrieBackend<B> = sp_state_machine::TrieBackend<MemoryDB<HashFor<B>>, HashFor<B>>;
type TrieBackend<B> = sp_state_machine::TrieBackend<
MemoryDB<HashFor<B>>,
HashFor<B>,
trie_cache::CacheProvider<HashFor<B>>,
>;
type Ext<'a, B> = sp_state_machine::Ext<'a, HashFor<B>, TrieBackend<B>>;
@@ -114,10 +118,15 @@ where
sp_std::mem::drop(storage_proof);
let cache_provider = trie_cache::CacheProvider::new();
// We use the storage root of the `parent_head` to ensure that it is the correct root.
// This is already being done above while creating the in-memory db, but let's be paranoid!!
let backend =
sp_state_machine::TrieBackendBuilder::new(db, *parent_header.state_root()).build();
let backend = sp_state_machine::TrieBackendBuilder::new_with_cache(
db,
*parent_header.state_root(),
cache_provider,
)
.build();
let _guard = (
// Replace storage calls with our own implementations