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:
Benjamin Kampmann
2020-03-05 08:51:03 +01:00
committed by GitHub
parent 406fa981bb
commit 5a33228ea9
64 changed files with 372 additions and 451 deletions
+25 -25
View File
@@ -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());
}