mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 22:51:13 +00:00
Reduce usage of Blake2Hasher (#5132)
This reduces the usage of `Blake2Hasher` in the code base and replaces it with `BlakeTwo256`. The most important change is the removal of the custom extern function for `Blake2Hasher`. The runtime `Hash` trait is now also simplified and directly requires that the implementing type implements `Hashable`.
This commit is contained in:
committed by
GitHub
parent
406fa981bb
commit
5a33228ea9
@@ -24,14 +24,14 @@ use rand::Rng;
|
||||
use hash_db::{Prefix, Hasher};
|
||||
use sp_trie::{MemoryDB, prefixed_key};
|
||||
use sp_core::storage::ChildInfo;
|
||||
use sp_runtime::traits::{Block as BlockT, HasherFor};
|
||||
use sp_runtime::traits::{Block as BlockT, HashFor};
|
||||
use sp_runtime::Storage;
|
||||
use sp_state_machine::{DBValue, backend::Backend as StateBackend};
|
||||
use kvdb::{KeyValueDB, DBTransaction};
|
||||
use kvdb_rocksdb::{Database, DatabaseConfig};
|
||||
|
||||
type DbState<B> = sp_state_machine::TrieBackend<
|
||||
Arc<dyn sp_state_machine::Storage<HasherFor<B>>>, HasherFor<B>
|
||||
Arc<dyn sp_state_machine::Storage<HashFor<B>>>, HashFor<B>
|
||||
>;
|
||||
|
||||
struct StorageDb<Block: BlockT> {
|
||||
@@ -39,9 +39,9 @@ struct StorageDb<Block: BlockT> {
|
||||
_block: std::marker::PhantomData<Block>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT> sp_state_machine::Storage<HasherFor<Block>> for StorageDb<Block> {
|
||||
impl<Block: BlockT> sp_state_machine::Storage<HashFor<Block>> for StorageDb<Block> {
|
||||
fn get(&self, key: &Block::Hash, prefix: Prefix) -> Result<Option<DBValue>, String> {
|
||||
let key = prefixed_key::<HasherFor<Block>>(key, prefix);
|
||||
let key = prefixed_key::<HashFor<Block>>(key, prefix);
|
||||
self.db.get(0, &key)
|
||||
.map_err(|e| format!("Database backend error: {:?}", e))
|
||||
}
|
||||
@@ -53,7 +53,7 @@ pub struct BenchmarkingState<B: BlockT> {
|
||||
root: Cell<B::Hash>,
|
||||
state: RefCell<Option<DbState<B>>>,
|
||||
db: Cell<Option<Arc<dyn KeyValueDB>>>,
|
||||
genesis: <DbState<B> as StateBackend<HasherFor<B>>>::Transaction,
|
||||
genesis: <DbState<B> as StateBackend<HashFor<B>>>::Transaction,
|
||||
}
|
||||
|
||||
impl<B: BlockT> BenchmarkingState<B> {
|
||||
@@ -64,8 +64,8 @@ impl<B: BlockT> BenchmarkingState<B> {
|
||||
let path = temp_dir.join(&name);
|
||||
|
||||
let mut root = B::Hash::default();
|
||||
let mut mdb = MemoryDB::<HasherFor<B>>::default();
|
||||
sp_state_machine::TrieDBMut::<HasherFor<B>>::new(&mut mdb, &mut root);
|
||||
let mut mdb = MemoryDB::<HashFor<B>>::default();
|
||||
sp_state_machine::TrieDBMut::<HashFor<B>>::new(&mut mdb, &mut root);
|
||||
|
||||
std::fs::create_dir(&path).map_err(|_| String::from("Error creating temp dir"))?;
|
||||
let mut state = BenchmarkingState {
|
||||
@@ -108,8 +108,8 @@ impl<B: BlockT> BenchmarkingState<B> {
|
||||
self.db.set(None);
|
||||
*self.state.borrow_mut() = None;
|
||||
let mut root = B::Hash::default();
|
||||
let mut mdb = MemoryDB::<HasherFor<B>>::default();
|
||||
sp_state_machine::TrieDBMut::<HasherFor<B>>::new(&mut mdb, &mut root);
|
||||
let mut mdb = MemoryDB::<HashFor<B>>::default();
|
||||
sp_state_machine::TrieDBMut::<HashFor<B>>::new(&mut mdb, &mut root);
|
||||
self.root.set(root);
|
||||
|
||||
std::fs::remove_dir_all(&self.path).map_err(|_| "Error removing database dir".into())
|
||||
@@ -126,10 +126,10 @@ fn state_err() -> String {
|
||||
"State is not open".into()
|
||||
}
|
||||
|
||||
impl<B: BlockT> StateBackend<HasherFor<B>> for BenchmarkingState<B> {
|
||||
type Error = <DbState<B> as StateBackend<HasherFor<B>>>::Error;
|
||||
type Transaction = <DbState<B> as StateBackend<HasherFor<B>>>::Transaction;
|
||||
type TrieBackendStorage = <DbState<B> as StateBackend<HasherFor<B>>>::TrieBackendStorage;
|
||||
impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
|
||||
type Error = <DbState<B> as StateBackend<HashFor<B>>>::Error;
|
||||
type Transaction = <DbState<B> as StateBackend<HashFor<B>>>::Transaction;
|
||||
type TrieBackendStorage = <DbState<B> as StateBackend<HashFor<B>>>::TrieBackendStorage;
|
||||
|
||||
fn storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
|
||||
self.state.borrow().as_ref().ok_or_else(state_err)?.storage(key)
|
||||
@@ -244,12 +244,12 @@ impl<B: BlockT> StateBackend<HasherFor<B>> for BenchmarkingState<B> {
|
||||
}
|
||||
|
||||
fn as_trie_backend(&mut self)
|
||||
-> Option<&sp_state_machine::TrieBackend<Self::TrieBackendStorage, HasherFor<B>>>
|
||||
-> Option<&sp_state_machine::TrieBackend<Self::TrieBackendStorage, HashFor<B>>>
|
||||
{
|
||||
None
|
||||
}
|
||||
|
||||
fn commit(&self, storage_root: <HasherFor<B> as Hasher>::Out, mut transaction: Self::Transaction)
|
||||
fn commit(&self, storage_root: <HashFor<B> as Hasher>::Out, mut transaction: Self::Transaction)
|
||||
-> Result<(), Self::Error>
|
||||
{
|
||||
if let Some(db) = self.db.take() {
|
||||
|
||||
@@ -28,7 +28,7 @@ use sc_client_api::backend::PrunableStateChangesTrieStorage;
|
||||
use sp_blockchain::{well_known_cache_keys, Cache as BlockchainCache};
|
||||
use sp_core::{ChangesTrieConfiguration, ChangesTrieConfigurationRange, convert_hash};
|
||||
use sp_runtime::traits::{
|
||||
Block as BlockT, Header as HeaderT, HasherFor, NumberFor, One, Zero, CheckedSub,
|
||||
Block as BlockT, Header as HeaderT, HashFor, NumberFor, One, Zero, CheckedSub,
|
||||
};
|
||||
use sp_runtime::generic::{BlockId, DigestItem, ChangesTrieSignal};
|
||||
use sp_state_machine::{DBValue, ChangesTrieBuildCache, ChangesTrieCacheAction};
|
||||
@@ -150,7 +150,7 @@ impl<Block: BlockT> DbChangesTrieStorage<Block> {
|
||||
pub fn commit(
|
||||
&self,
|
||||
tx: &mut DBTransaction,
|
||||
mut changes_trie: MemoryDB<HasherFor<Block>>,
|
||||
mut changes_trie: MemoryDB<HashFor<Block>>,
|
||||
parent_block: ComplexBlockId<Block>,
|
||||
block: ComplexBlockId<Block>,
|
||||
new_header: &Block::Header,
|
||||
@@ -377,7 +377,7 @@ impl<Block: BlockT> DbChangesTrieStorage<Block> {
|
||||
}
|
||||
|
||||
impl<Block: BlockT> PrunableStateChangesTrieStorage<Block> for DbChangesTrieStorage<Block> {
|
||||
fn storage(&self) -> &dyn sp_state_machine::ChangesTrieStorage<HasherFor<Block>, NumberFor<Block>> {
|
||||
fn storage(&self) -> &dyn sp_state_machine::ChangesTrieStorage<HashFor<Block>, NumberFor<Block>> {
|
||||
self
|
||||
}
|
||||
|
||||
@@ -396,7 +396,7 @@ impl<Block: BlockT> PrunableStateChangesTrieStorage<Block> for DbChangesTrieStor
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT> sp_state_machine::ChangesTrieRootsStorage<HasherFor<Block>, NumberFor<Block>>
|
||||
impl<Block: BlockT> sp_state_machine::ChangesTrieRootsStorage<HashFor<Block>, NumberFor<Block>>
|
||||
for DbChangesTrieStorage<Block>
|
||||
{
|
||||
fn build_anchor(
|
||||
@@ -469,12 +469,12 @@ impl<Block: BlockT> sp_state_machine::ChangesTrieRootsStorage<HasherFor<Block>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block> sp_state_machine::ChangesTrieStorage<HasherFor<Block>, NumberFor<Block>>
|
||||
impl<Block> sp_state_machine::ChangesTrieStorage<HashFor<Block>, NumberFor<Block>>
|
||||
for DbChangesTrieStorage<Block>
|
||||
where
|
||||
Block: BlockT,
|
||||
{
|
||||
fn as_roots_storage(&self) -> &dyn sp_state_machine::ChangesTrieRootsStorage<HasherFor<Block>, NumberFor<Block>> {
|
||||
fn as_roots_storage(&self) -> &dyn sp_state_machine::ChangesTrieRootsStorage<HashFor<Block>, NumberFor<Block>> {
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ use sp_runtime::{
|
||||
BuildStorage,
|
||||
};
|
||||
use sp_runtime::traits::{
|
||||
Block as BlockT, Header as HeaderT, NumberFor, Zero, One, SaturatedConversion, HasherFor,
|
||||
Block as BlockT, Header as HeaderT, NumberFor, Zero, One, SaturatedConversion, HashFor,
|
||||
};
|
||||
use sc_executor::RuntimeInfo;
|
||||
use sp_state_machine::{
|
||||
@@ -99,7 +99,7 @@ const DEFAULT_CHILD_RATIO: (usize, usize) = (1, 10);
|
||||
|
||||
/// 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<HasherFor<B>>>, HasherFor<B>
|
||||
Arc<dyn sp_state_machine::Storage<HashFor<B>>>, HashFor<B>
|
||||
>;
|
||||
|
||||
/// Re-export the KVDB trait so that one can pass an implementation of it.
|
||||
@@ -139,10 +139,10 @@ impl<Block: BlockT> std::fmt::Debug for RefTrackingState<Block> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: BlockT> StateBackend<HasherFor<B>> for RefTrackingState<B> {
|
||||
type Error = <DbState<B> as StateBackend<HasherFor<B>>>::Error;
|
||||
type Transaction = <DbState<B> as StateBackend<HasherFor<B>>>::Transaction;
|
||||
type TrieBackendStorage = <DbState<B> as StateBackend<HasherFor<B>>>::TrieBackendStorage;
|
||||
impl<B: BlockT> StateBackend<HashFor<B>> for RefTrackingState<B> {
|
||||
type Error = <DbState<B> as StateBackend<HashFor<B>>>::Error;
|
||||
type Transaction = <DbState<B> as StateBackend<HashFor<B>>>::Transaction;
|
||||
type TrieBackendStorage = <DbState<B> as StateBackend<HashFor<B>>>::TrieBackendStorage;
|
||||
|
||||
fn storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
|
||||
self.state.storage(key)
|
||||
@@ -251,7 +251,7 @@ impl<B: BlockT> StateBackend<HasherFor<B>> for RefTrackingState<B> {
|
||||
}
|
||||
|
||||
fn as_trie_backend(&mut self)
|
||||
-> Option<&sp_state_machine::TrieBackend<Self::TrieBackendStorage, HasherFor<B>>>
|
||||
-> Option<&sp_state_machine::TrieBackend<Self::TrieBackendStorage, HashFor<B>>>
|
||||
{
|
||||
self.state.as_trie_backend()
|
||||
}
|
||||
@@ -521,10 +521,10 @@ impl<Block: BlockT> HeaderMetadata<Block> for BlockchainDb<Block> {
|
||||
/// Database transaction
|
||||
pub struct BlockImportOperation<Block: BlockT> {
|
||||
old_state: CachingState<RefTrackingState<Block>, Block>,
|
||||
db_updates: PrefixedMemoryDB<HasherFor<Block>>,
|
||||
db_updates: PrefixedMemoryDB<HashFor<Block>>,
|
||||
storage_updates: StorageCollection,
|
||||
child_storage_updates: ChildStorageCollection,
|
||||
changes_trie_updates: MemoryDB<HasherFor<Block>>,
|
||||
changes_trie_updates: MemoryDB<HashFor<Block>>,
|
||||
changes_trie_build_cache_update: Option<ChangesTrieCacheAction<Block::Hash, NumberFor<Block>>>,
|
||||
changes_trie_config_update: Option<Option<ChangesTrieConfiguration>>,
|
||||
pending_block: Option<PendingBlock<Block>>,
|
||||
@@ -576,7 +576,7 @@ impl<Block: BlockT> sc_client_api::backend::BlockImportOperation<Block> for Bloc
|
||||
// Currently cache isn't implemented on full nodes.
|
||||
}
|
||||
|
||||
fn update_db_storage(&mut self, update: PrefixedMemoryDB<HasherFor<Block>>) -> ClientResult<()> {
|
||||
fn update_db_storage(&mut self, update: PrefixedMemoryDB<HashFor<Block>>) -> ClientResult<()> {
|
||||
self.db_updates = update;
|
||||
Ok(())
|
||||
}
|
||||
@@ -623,7 +623,7 @@ impl<Block: BlockT> sc_client_api::backend::BlockImportOperation<Block> for Bloc
|
||||
|
||||
fn update_changes_trie(
|
||||
&mut self,
|
||||
update: ChangesTrieTransaction<HasherFor<Block>, NumberFor<Block>>,
|
||||
update: ChangesTrieTransaction<HashFor<Block>, NumberFor<Block>>,
|
||||
) -> ClientResult<()> {
|
||||
self.changes_trie_updates = update.0;
|
||||
self.changes_trie_build_cache_update = Some(update.1);
|
||||
@@ -668,9 +668,9 @@ struct StorageDb<Block: BlockT> {
|
||||
pub state_db: StateDb<Block::Hash, Vec<u8>>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT> sp_state_machine::Storage<HasherFor<Block>> for StorageDb<Block> {
|
||||
impl<Block: BlockT> sp_state_machine::Storage<HashFor<Block>> for StorageDb<Block> {
|
||||
fn get(&self, key: &Block::Hash, prefix: Prefix) -> Result<Option<DBValue>, String> {
|
||||
let key = prefixed_key::<HasherFor<Block>>(key, prefix);
|
||||
let key = prefixed_key::<HashFor<Block>>(key, prefix);
|
||||
self.state_db.get(&key, self)
|
||||
.map_err(|e| format!("Database backend error: {:?}", e))
|
||||
}
|
||||
@@ -690,13 +690,13 @@ struct DbGenesisStorage<Block: BlockT>(pub Block::Hash);
|
||||
impl<Block: BlockT> DbGenesisStorage<Block> {
|
||||
pub fn new() -> Self {
|
||||
let mut root = Block::Hash::default();
|
||||
let mut mdb = MemoryDB::<HasherFor<Block>>::default();
|
||||
sp_state_machine::TrieDBMut::<HasherFor<Block>>::new(&mut mdb, &mut root);
|
||||
let mut mdb = MemoryDB::<HashFor<Block>>::default();
|
||||
sp_state_machine::TrieDBMut::<HashFor<Block>>::new(&mut mdb, &mut root);
|
||||
DbGenesisStorage(root)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT> sp_state_machine::Storage<HasherFor<Block>> for DbGenesisStorage<Block> {
|
||||
impl<Block: BlockT> sp_state_machine::Storage<HashFor<Block>> for DbGenesisStorage<Block> {
|
||||
fn get(&self, _key: &Block::Hash, _prefix: Prefix) -> Result<Option<DBValue>, String> {
|
||||
Ok(None)
|
||||
}
|
||||
@@ -1653,7 +1653,7 @@ pub(crate) mod tests {
|
||||
use hash_db::{HashDB, EMPTY_PREFIX};
|
||||
use super::*;
|
||||
use crate::columns;
|
||||
use sp_core::{Blake2Hasher, H256};
|
||||
use sp_core::H256;
|
||||
use sc_client_api::backend::{Backend as BTrait, BlockImportOperation as Op};
|
||||
use sc_client::blockchain::Backend as BLBTrait;
|
||||
use sp_runtime::testing::{Header, Block as RawBlock, ExtrinsicWrapper};
|
||||
@@ -1664,11 +1664,11 @@ pub(crate) mod tests {
|
||||
|
||||
pub(crate) type Block = RawBlock<ExtrinsicWrapper<u64>>;
|
||||
|
||||
pub fn prepare_changes(changes: Vec<(Vec<u8>, Vec<u8>)>) -> (H256, MemoryDB<Blake2Hasher>) {
|
||||
pub fn prepare_changes(changes: Vec<(Vec<u8>, Vec<u8>)>) -> (H256, MemoryDB<BlakeTwo256>) {
|
||||
let mut changes_root = H256::default();
|
||||
let mut changes_trie_update = MemoryDB::<Blake2Hasher>::default();
|
||||
let mut changes_trie_update = MemoryDB::<BlakeTwo256>::default();
|
||||
{
|
||||
let mut trie = TrieDBMut::<Blake2Hasher>::new(
|
||||
let mut trie = TrieDBMut::<BlakeTwo256>::new(
|
||||
&mut changes_trie_update,
|
||||
&mut changes_root
|
||||
);
|
||||
@@ -1900,7 +1900,7 @@ pub(crate) mod tests {
|
||||
backend.commit_operation(op).unwrap();
|
||||
assert_eq!(backend.storage.db.get(
|
||||
columns::STATE,
|
||||
&sp_trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
|
||||
&sp_trie::prefixed_key::<BlakeTwo256>(&key, EMPTY_PREFIX)
|
||||
).unwrap().unwrap(), &b"hello"[..]);
|
||||
hash
|
||||
};
|
||||
@@ -1937,7 +1937,7 @@ pub(crate) mod tests {
|
||||
backend.commit_operation(op).unwrap();
|
||||
assert_eq!(backend.storage.db.get(
|
||||
columns::STATE,
|
||||
&sp_trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
|
||||
&sp_trie::prefixed_key::<BlakeTwo256>(&key, EMPTY_PREFIX)
|
||||
).unwrap().unwrap(), &b"hello"[..]);
|
||||
hash
|
||||
};
|
||||
@@ -1975,7 +1975,7 @@ pub(crate) mod tests {
|
||||
|
||||
assert!(backend.storage.db.get(
|
||||
columns::STATE,
|
||||
&sp_trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
|
||||
&sp_trie::prefixed_key::<BlakeTwo256>(&key, EMPTY_PREFIX)
|
||||
).unwrap().is_some());
|
||||
hash
|
||||
};
|
||||
@@ -2009,7 +2009,7 @@ pub(crate) mod tests {
|
||||
backend.commit_operation(op).unwrap();
|
||||
assert!(backend.storage.db.get(
|
||||
columns::STATE,
|
||||
&sp_trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
|
||||
&sp_trie::prefixed_key::<BlakeTwo256>(&key, EMPTY_PREFIX)
|
||||
).unwrap().is_none());
|
||||
}
|
||||
|
||||
@@ -2018,7 +2018,7 @@ pub(crate) mod tests {
|
||||
backend.finalize_block(BlockId::Number(3), None).unwrap();
|
||||
assert!(backend.storage.db.get(
|
||||
columns::STATE,
|
||||
&sp_trie::prefixed_key::<Blake2Hasher>(&key, EMPTY_PREFIX)
|
||||
&sp_trie::prefixed_key::<BlakeTwo256>(&key, EMPTY_PREFIX)
|
||||
).unwrap().is_none());
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ use sp_blockchain::{
|
||||
use sc_client::light::blockchain::Storage as LightBlockchainStorage;
|
||||
use codec::{Decode, Encode};
|
||||
use sp_runtime::generic::{DigestItem, BlockId};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Zero, One, NumberFor, HasherFor};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Zero, One, NumberFor, HashFor};
|
||||
use crate::cache::{DbCacheSync, DbCache, ComplexBlockId, EntryType as CacheEntryType};
|
||||
use crate::utils::{self, meta_keys, DatabaseType, Meta, db_err, read_db, block_id_to_lookup_key, read_meta};
|
||||
use crate::{DatabaseSettings, FrozenForDuration};
|
||||
@@ -305,7 +305,7 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
Some(old_current_num)
|
||||
});
|
||||
|
||||
let new_header_cht_root = cht::compute_root::<Block::Header, HasherFor<Block>, _>(
|
||||
let new_header_cht_root = cht::compute_root::<Block::Header, HashFor<Block>, _>(
|
||||
cht::size(), new_cht_number, cht_range.map(|num| self.hash(num))
|
||||
)?;
|
||||
transaction.put(
|
||||
@@ -322,7 +322,7 @@ impl<Block: BlockT> LightStorage<Block> {
|
||||
current_num = current_num + One::one();
|
||||
Some(old_current_num)
|
||||
});
|
||||
let new_changes_trie_cht_root = cht::compute_root::<Block::Header, HasherFor<Block>, _>(
|
||||
let new_changes_trie_cht_root = cht::compute_root::<Block::Header, HashFor<Block>, _>(
|
||||
cht::size(), new_cht_number, cht_range
|
||||
.map(|num| self.changes_trie_root(BlockId::Number(num)))
|
||||
)?;
|
||||
|
||||
@@ -21,7 +21,7 @@ use std::sync::Arc;
|
||||
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard};
|
||||
use linked_hash_map::{LinkedHashMap, Entry};
|
||||
use hash_db::Hasher;
|
||||
use sp_runtime::traits::{Block as BlockT, Header, HasherFor, NumberFor};
|
||||
use sp_runtime::traits::{Block as BlockT, Header, HashFor, NumberFor};
|
||||
use sp_core::hexdisplay::HexDisplay;
|
||||
use sp_core::storage::ChildInfo;
|
||||
use sp_state_machine::{
|
||||
@@ -281,7 +281,7 @@ pub struct CacheChanges<B: BlockT> {
|
||||
/// Shared canonical state cache.
|
||||
shared_cache: SharedCache<B>,
|
||||
/// Local cache of values for this state.
|
||||
local_cache: RwLock<LocalCache<HasherFor<B>>>,
|
||||
local_cache: RwLock<LocalCache<HashFor<B>>>,
|
||||
/// Hash of the block on top of which this instance was created or
|
||||
/// `None` if cache is disabled
|
||||
pub parent_hash: Option<B::Hash>,
|
||||
@@ -296,7 +296,7 @@ pub struct CacheChanges<B: BlockT> {
|
||||
/// For canonical instances local cache is accumulated and applied
|
||||
/// in `sync_cache` along with the change overlay.
|
||||
/// For non-canonical clones local cache and changes are dropped.
|
||||
pub struct CachingState<S: StateBackend<HasherFor<B>>, B: BlockT> {
|
||||
pub struct CachingState<S: StateBackend<HashFor<B>>, B: BlockT> {
|
||||
/// Usage statistics
|
||||
usage: StateUsageStats,
|
||||
/// Backing state.
|
||||
@@ -305,7 +305,7 @@ pub struct CachingState<S: StateBackend<HasherFor<B>>, B: BlockT> {
|
||||
pub cache: CacheChanges<B>,
|
||||
}
|
||||
|
||||
impl<S: StateBackend<HasherFor<B>>, B: BlockT> std::fmt::Debug for CachingState<S, B> {
|
||||
impl<S: StateBackend<HashFor<B>>, B: BlockT> std::fmt::Debug for CachingState<S, B> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Block {:?}", self.cache.parent_hash)
|
||||
}
|
||||
@@ -420,7 +420,7 @@ impl<B: BlockT> CacheChanges<B> {
|
||||
|
||||
}
|
||||
|
||||
impl<S: StateBackend<HasherFor<B>>, B: BlockT> CachingState<S, B> {
|
||||
impl<S: StateBackend<HashFor<B>>, B: BlockT> CachingState<S, B> {
|
||||
/// Create a new instance wrapping generic State and shared cache.
|
||||
pub fn new(state: S, shared_cache: SharedCache<B>, parent_hash: Option<B::Hash>) -> Self {
|
||||
CachingState {
|
||||
@@ -489,7 +489,7 @@ impl<S: StateBackend<HasherFor<B>>, B: BlockT> CachingState<S, B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: StateBackend<HasherFor<B>>, B: BlockT> StateBackend<HasherFor<B>> for CachingState<S, B> {
|
||||
impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for CachingState<S, B> {
|
||||
type Error = S::Error;
|
||||
type Transaction = S::Transaction;
|
||||
type TrieBackendStorage = S::TrieBackendStorage;
|
||||
@@ -659,7 +659,7 @@ impl<S: StateBackend<HasherFor<B>>, B: BlockT> StateBackend<HasherFor<B>> for Ca
|
||||
self.state.child_keys(storage_key, child_info, prefix)
|
||||
}
|
||||
|
||||
fn as_trie_backend(&mut self) -> Option<&TrieBackend<Self::TrieBackendStorage, HasherFor<B>>> {
|
||||
fn as_trie_backend(&mut self) -> Option<&TrieBackend<Self::TrieBackendStorage, HashFor<B>>> {
|
||||
self.state.as_trie_backend()
|
||||
}
|
||||
|
||||
@@ -671,9 +671,11 @@ impl<S: StateBackend<HasherFor<B>>, B: BlockT> StateBackend<HasherFor<B>> for Ca
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_runtime::testing::{H256, Block as RawBlock, ExtrinsicWrapper};
|
||||
use sp_runtime::{
|
||||
traits::BlakeTwo256,
|
||||
testing::{H256, Block as RawBlock, ExtrinsicWrapper},
|
||||
};
|
||||
use sp_state_machine::InMemoryBackend;
|
||||
use sp_core::Blake2Hasher;
|
||||
|
||||
type Block = RawBlock<ExtrinsicWrapper<u32>>;
|
||||
|
||||
@@ -695,7 +697,7 @@ mod tests {
|
||||
// blocks [ 3a(c) 2a(c) 2b 1b 1a(c) 0 ]
|
||||
// state [ 5 5 4 3 2 2 ]
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(root_parent),
|
||||
);
|
||||
@@ -710,14 +712,14 @@ mod tests {
|
||||
);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h0),
|
||||
);
|
||||
s.cache.sync_cache(&[], &[], vec![], vec![], Some(h1a), Some(1), true);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h0),
|
||||
);
|
||||
@@ -732,7 +734,7 @@ mod tests {
|
||||
);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h1b),
|
||||
);
|
||||
@@ -747,7 +749,7 @@ mod tests {
|
||||
);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h1a),
|
||||
);
|
||||
@@ -762,35 +764,35 @@ mod tests {
|
||||
);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h2a),
|
||||
);
|
||||
s.cache.sync_cache(&[], &[], vec![], vec![], Some(h3a), Some(3), true);
|
||||
|
||||
let s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h3a),
|
||||
);
|
||||
assert_eq!(s.storage(&key).unwrap().unwrap(), vec![5]);
|
||||
|
||||
let s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h1a),
|
||||
);
|
||||
assert!(s.storage(&key).unwrap().is_none());
|
||||
|
||||
let s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h2b),
|
||||
);
|
||||
assert!(s.storage(&key).unwrap().is_none());
|
||||
|
||||
let s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h1b),
|
||||
);
|
||||
@@ -799,7 +801,7 @@ mod tests {
|
||||
// reorg to 3b
|
||||
// blocks [ 3b(c) 3a 2a 2b(c) 1b 1a 0 ]
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h2b),
|
||||
);
|
||||
@@ -813,7 +815,7 @@ mod tests {
|
||||
true,
|
||||
);
|
||||
let s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h3a),
|
||||
);
|
||||
@@ -834,7 +836,7 @@ mod tests {
|
||||
let shared = new_shared_cache::<Block>(256*1024, (0,1));
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(root_parent),
|
||||
);
|
||||
@@ -849,14 +851,14 @@ mod tests {
|
||||
);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h1),
|
||||
);
|
||||
s.cache.sync_cache(&[], &[], vec![], vec![], Some(h2a), Some(2), true);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h1),
|
||||
);
|
||||
@@ -871,7 +873,7 @@ mod tests {
|
||||
);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h2b),
|
||||
);
|
||||
@@ -886,7 +888,7 @@ mod tests {
|
||||
);
|
||||
|
||||
let s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h2a),
|
||||
);
|
||||
@@ -906,21 +908,21 @@ mod tests {
|
||||
let shared = new_shared_cache::<Block>(256*1024, (0,1));
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(root_parent),
|
||||
);
|
||||
s.cache.sync_cache(&[], &[], vec![], vec![], Some(h1), Some(1), true);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h1),
|
||||
);
|
||||
s.cache.sync_cache(&[], &[], vec![], vec![], Some(h2a), Some(2), true);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h2a),
|
||||
);
|
||||
@@ -935,14 +937,14 @@ mod tests {
|
||||
);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h1),
|
||||
);
|
||||
s.cache.sync_cache(&[], &[], vec![], vec![], Some(h2b), Some(2), false);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h2b),
|
||||
);
|
||||
@@ -957,7 +959,7 @@ mod tests {
|
||||
);
|
||||
|
||||
let s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h3a),
|
||||
);
|
||||
@@ -971,7 +973,7 @@ mod tests {
|
||||
let h0 = H256::random();
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(), shared.clone(), Some(root_parent.clone()),
|
||||
InMemoryBackend::<BlakeTwo256>::default(), shared.clone(), Some(root_parent.clone()),
|
||||
);
|
||||
|
||||
let key = H256::random()[..].to_vec();
|
||||
@@ -1009,7 +1011,7 @@ mod tests {
|
||||
let h0 = H256::random();
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(root_parent),
|
||||
);
|
||||
@@ -1053,7 +1055,7 @@ mod tests {
|
||||
|
||||
let shared = new_shared_cache::<Block>(256 * 1024, (0, 1));
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(root_parent.clone()),
|
||||
);
|
||||
@@ -1068,7 +1070,7 @@ mod tests {
|
||||
);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h0),
|
||||
);
|
||||
@@ -1083,7 +1085,7 @@ mod tests {
|
||||
);
|
||||
|
||||
let mut s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h1),
|
||||
);
|
||||
@@ -1106,7 +1108,7 @@ mod tests {
|
||||
s.cache.sync_cache(&[], &[], vec![], vec![], None, None, true);
|
||||
|
||||
let s = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
shared.clone(),
|
||||
Some(h1),
|
||||
);
|
||||
@@ -1121,9 +1123,11 @@ mod qc {
|
||||
use quickcheck::{quickcheck, TestResult, Arbitrary};
|
||||
|
||||
use super::*;
|
||||
use sp_runtime::testing::{H256, Block as RawBlock, ExtrinsicWrapper};
|
||||
use sp_runtime::{
|
||||
traits::BlakeTwo256,
|
||||
testing::{H256, Block as RawBlock, ExtrinsicWrapper},
|
||||
};
|
||||
use sp_state_machine::InMemoryBackend;
|
||||
use sp_core::Blake2Hasher;
|
||||
|
||||
type Block = RawBlock<ExtrinsicWrapper<u32>>;
|
||||
|
||||
@@ -1250,22 +1254,22 @@ mod qc {
|
||||
}
|
||||
}
|
||||
|
||||
fn head_state(&self, hash: H256) -> CachingState<InMemoryBackend<Blake2Hasher>, Block> {
|
||||
fn head_state(&self, hash: H256) -> CachingState<InMemoryBackend<BlakeTwo256>, Block> {
|
||||
CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
self.shared.clone(),
|
||||
Some(hash)
|
||||
)
|
||||
}
|
||||
|
||||
fn canon_head_state(&self) -> CachingState<InMemoryBackend<Blake2Hasher>, Block> {
|
||||
fn canon_head_state(&self) -> CachingState<InMemoryBackend<BlakeTwo256>, Block> {
|
||||
self.head_state(self.canon.last().expect("Expected to be one commit").hash)
|
||||
}
|
||||
|
||||
fn mutate_static(
|
||||
&mut self,
|
||||
action: Action,
|
||||
) -> CachingState<InMemoryBackend<Blake2Hasher>, Block> {
|
||||
) -> CachingState<InMemoryBackend<BlakeTwo256>, Block> {
|
||||
self.mutate(action).expect("Expected to provide only valid actions to the mutate_static")
|
||||
}
|
||||
|
||||
@@ -1284,7 +1288,7 @@ mod qc {
|
||||
fn mutate(
|
||||
&mut self,
|
||||
action: Action,
|
||||
) -> Result<CachingState<InMemoryBackend<Blake2Hasher>, Block>, ()> {
|
||||
) -> Result<CachingState<InMemoryBackend<BlakeTwo256>, Block>, ()> {
|
||||
let state = match action {
|
||||
Action::Fork { depth, hash, changes } => {
|
||||
let pos = self.canon.len() as isize - depth as isize;
|
||||
@@ -1321,7 +1325,7 @@ mod qc {
|
||||
};
|
||||
|
||||
let mut state = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
self.shared.clone(),
|
||||
Some(parent)
|
||||
);
|
||||
@@ -1360,7 +1364,7 @@ mod qc {
|
||||
}
|
||||
|
||||
let mut state = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
self.shared.clone(),
|
||||
Some(parent_hash)
|
||||
);
|
||||
@@ -1407,7 +1411,7 @@ mod qc {
|
||||
self.canon.push(node);
|
||||
|
||||
let mut state = CachingState::new(
|
||||
InMemoryBackend::<Blake2Hasher>::default(),
|
||||
InMemoryBackend::<BlakeTwo256>::default(),
|
||||
self.shared.clone(),
|
||||
Some(fork_at)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user