mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 14:01:06 +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
@@ -31,7 +31,7 @@ use sp_state_machine::{
|
||||
StorageCollection, ChildStorageCollection,
|
||||
};
|
||||
use sp_runtime::{generic::BlockId, Justification, Storage};
|
||||
use sp_runtime::traits::{Block as BlockT, NumberFor, Zero, Header, HasherFor};
|
||||
use sp_runtime::traits::{Block as BlockT, NumberFor, Zero, Header, HashFor};
|
||||
use crate::in_mem::check_genesis_storage;
|
||||
use sp_blockchain::{Error as ClientError, Result as ClientResult};
|
||||
use sc_client_api::{
|
||||
@@ -65,7 +65,7 @@ pub struct ImportOperation<Block: BlockT, S> {
|
||||
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
|
||||
finalized_blocks: Vec<BlockId<Block>>,
|
||||
set_head: Option<BlockId<Block>>,
|
||||
storage_update: Option<InMemoryBackend<HasherFor<Block>>>,
|
||||
storage_update: Option<InMemoryBackend<HashFor<Block>>>,
|
||||
changes_trie_config_update: Option<Option<ChangesTrieConfiguration>>,
|
||||
_phantom: std::marker::PhantomData<S>,
|
||||
}
|
||||
@@ -111,7 +111,7 @@ impl<S: AuxStore, H: Hasher> AuxStore for Backend<S, H> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, Block> ClientBackend<Block> for Backend<S, HasherFor<Block>>
|
||||
impl<S, Block> ClientBackend<Block> for Backend<S, HashFor<Block>>
|
||||
where
|
||||
Block: BlockT,
|
||||
S: BlockchainStorage<Block>,
|
||||
@@ -119,7 +119,7 @@ impl<S, Block> ClientBackend<Block> for Backend<S, HasherFor<Block>>
|
||||
{
|
||||
type BlockImportOperation = ImportOperation<Block, S>;
|
||||
type Blockchain = Blockchain<S>;
|
||||
type State = GenesisOrUnavailableState<HasherFor<Block>>;
|
||||
type State = GenesisOrUnavailableState<HashFor<Block>>;
|
||||
type OffchainStorage = InMemOffchainStorage;
|
||||
|
||||
fn begin_operation(&self) -> ClientResult<Self::BlockImportOperation> {
|
||||
@@ -238,7 +238,7 @@ impl<S, Block> ClientBackend<Block> for Backend<S, HasherFor<Block>>
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, Block> RemoteBackend<Block> for Backend<S, HasherFor<Block>>
|
||||
impl<S, Block> RemoteBackend<Block> for Backend<S, HashFor<Block>>
|
||||
where
|
||||
Block: BlockT,
|
||||
S: BlockchainStorage<Block> + 'static,
|
||||
@@ -262,7 +262,7 @@ impl<S, Block> BlockImportOperation<Block> for ImportOperation<Block, S>
|
||||
S: BlockchainStorage<Block>,
|
||||
Block::Hash: Ord,
|
||||
{
|
||||
type State = GenesisOrUnavailableState<HasherFor<Block>>;
|
||||
type State = GenesisOrUnavailableState<HashFor<Block>>;
|
||||
|
||||
fn state(&self) -> ClientResult<Option<&Self::State>> {
|
||||
// None means 'locally-stateless' backend
|
||||
@@ -287,7 +287,7 @@ impl<S, Block> BlockImportOperation<Block> for ImportOperation<Block, S>
|
||||
|
||||
fn update_db_storage(
|
||||
&mut self,
|
||||
_update: <Self::State as StateBackend<HasherFor<Block>>>::Transaction,
|
||||
_update: <Self::State as StateBackend<HashFor<Block>>>::Transaction,
|
||||
) -> ClientResult<()> {
|
||||
// we're not storing anything locally => ignore changes
|
||||
Ok(())
|
||||
@@ -295,7 +295,7 @@ impl<S, Block> BlockImportOperation<Block> for ImportOperation<Block, S>
|
||||
|
||||
fn update_changes_trie(
|
||||
&mut self,
|
||||
_update: ChangesTrieTransaction<HasherFor<Block>, NumberFor<Block>>,
|
||||
_update: ChangesTrieTransaction<HashFor<Block>, NumberFor<Block>>,
|
||||
) -> ClientResult<()> {
|
||||
// we're not storing anything locally => ignore changes
|
||||
Ok(())
|
||||
@@ -515,10 +515,10 @@ impl<H: Hasher> StateBackend<H> for GenesisOrUnavailableState<H>
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use sp_core::Blake2Hasher;
|
||||
use substrate_test_runtime_client::{self, runtime::Block};
|
||||
use sc_client_api::backend::NewBlockState;
|
||||
use crate::light::blockchain::tests::{DummyBlockchain, DummyStorage};
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
@@ -526,7 +526,9 @@ mod tests {
|
||||
let def = Default::default();
|
||||
let header0 = substrate_test_runtime_client::runtime::Header::new(0, def, def, def, Default::default());
|
||||
|
||||
let backend: Backend<_, Blake2Hasher> = Backend::new(Arc::new(DummyBlockchain::new(DummyStorage::new())));
|
||||
let backend: Backend<_, BlakeTwo256> = Backend::new(
|
||||
Arc::new(DummyBlockchain::new(DummyStorage::new())),
|
||||
);
|
||||
let mut op = backend.begin_operation().unwrap();
|
||||
op.set_block_data(header0, None, None, NewBlockState::Final).unwrap();
|
||||
op.reset_storage(Default::default()).unwrap();
|
||||
@@ -540,7 +542,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn unavailable_state_is_created_when_genesis_state_is_unavailable() {
|
||||
let backend: Backend<_, Blake2Hasher> = Backend::new(Arc::new(DummyBlockchain::new(DummyStorage::new())));
|
||||
let backend: Backend<_, BlakeTwo256> = Backend::new(
|
||||
Arc::new(DummyBlockchain::new(DummyStorage::new())),
|
||||
);
|
||||
|
||||
match backend.state_at(BlockId::Number(0)).unwrap() {
|
||||
GenesisOrUnavailableState::Unavailable => (),
|
||||
|
||||
@@ -23,7 +23,7 @@ use std::{
|
||||
use codec::{Encode, Decode};
|
||||
use sp_core::{convert_hash, NativeOrEncoded, traits::CodeExecutor};
|
||||
use sp_runtime::{
|
||||
generic::BlockId, traits::{One, Block as BlockT, Header as HeaderT, HasherFor},
|
||||
generic::BlockId, traits::{One, Block as BlockT, Header as HeaderT, HashFor},
|
||||
};
|
||||
use sp_externalities::Extensions;
|
||||
use sp_state_machine::{
|
||||
@@ -152,9 +152,9 @@ impl<Block, B, Local> CallExecutor<Block> for
|
||||
}
|
||||
}
|
||||
|
||||
fn prove_at_trie_state<S: sp_state_machine::TrieBackendStorage<HasherFor<Block>>>(
|
||||
fn prove_at_trie_state<S: sp_state_machine::TrieBackendStorage<HashFor<Block>>>(
|
||||
&self,
|
||||
_state: &sp_state_machine::TrieBackend<S, HasherFor<Block>>,
|
||||
_state: &sp_state_machine::TrieBackend<S, HashFor<Block>>,
|
||||
_changes: &mut OverlayedChanges,
|
||||
_method: &str,
|
||||
_call_data: &[u8],
|
||||
@@ -180,7 +180,7 @@ pub fn prove_execution<Block, S, E>(
|
||||
) -> ClientResult<(Vec<u8>, StorageProof)>
|
||||
where
|
||||
Block: BlockT,
|
||||
S: StateBackend<HasherFor<Block>>,
|
||||
S: StateBackend<HashFor<Block>>,
|
||||
E: CallExecutor<Block>,
|
||||
{
|
||||
let trie_state = state.as_trie_backend()
|
||||
@@ -291,9 +291,10 @@ mod tests {
|
||||
runtime::{Header, Digest, Block}, TestClient, ClientBlockImportExt,
|
||||
};
|
||||
use sc_executor::{NativeExecutor, WasmExecutionMethod};
|
||||
use sp_core::{Blake2Hasher, H256};
|
||||
use sp_core::H256;
|
||||
use sc_client_api::backend::{Backend, NewBlockState};
|
||||
use crate::in_mem::Backend as InMemBackend;
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
|
||||
struct DummyCallExecutor;
|
||||
|
||||
@@ -348,9 +349,9 @@ mod tests {
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
fn prove_at_trie_state<S: sp_state_machine::TrieBackendStorage<HasherFor<Block>>>(
|
||||
fn prove_at_trie_state<S: sp_state_machine::TrieBackendStorage<HashFor<Block>>>(
|
||||
&self,
|
||||
_trie_state: &sp_state_machine::TrieBackend<S, HasherFor<Block>>,
|
||||
_trie_state: &sp_state_machine::TrieBackend<S, HashFor<Block>>,
|
||||
_overlay: &mut OverlayedChanges,
|
||||
_method: &str,
|
||||
_call_data: &[u8]
|
||||
@@ -381,7 +382,7 @@ mod tests {
|
||||
).unwrap();
|
||||
|
||||
// check remote execution proof locally
|
||||
let local_result = check_execution_proof::<_, _, Blake2Hasher>(
|
||||
let local_result = check_execution_proof::<_, _, BlakeTwo256>(
|
||||
&local_executor(),
|
||||
&RemoteCallRequest {
|
||||
block: substrate_test_runtime_client::runtime::Hash::default(),
|
||||
@@ -408,7 +409,7 @@ mod tests {
|
||||
).unwrap();
|
||||
|
||||
// check remote execution proof locally
|
||||
let execution_result = check_execution_proof_with_make_header::<_, _, Blake2Hasher, _>(
|
||||
let execution_result = check_execution_proof_with_make_header::<_, _, BlakeTwo256, _>(
|
||||
&local_executor(),
|
||||
&RemoteCallRequest {
|
||||
block: substrate_test_runtime_client::runtime::Hash::default(),
|
||||
|
||||
@@ -342,12 +342,12 @@ pub mod tests {
|
||||
};
|
||||
use sp_consensus::BlockOrigin;
|
||||
|
||||
use crate::in_mem::{Blockchain as InMemoryBlockchain};
|
||||
use crate::in_mem::Blockchain as InMemoryBlockchain;
|
||||
use crate::light::fetcher::{FetchChecker, LightDataChecker, RemoteHeaderRequest};
|
||||
use crate::light::blockchain::tests::{DummyStorage, DummyBlockchain};
|
||||
use sp_core::{blake2_256, Blake2Hasher, ChangesTrieConfiguration, H256};
|
||||
use sp_core::{blake2_256, ChangesTrieConfiguration, H256};
|
||||
use sp_core::storage::{well_known_keys, StorageKey, ChildInfo};
|
||||
use sp_runtime::generic::BlockId;
|
||||
use sp_runtime::{generic::BlockId, traits::BlakeTwo256};
|
||||
use sp_state_machine::Backend;
|
||||
use super::*;
|
||||
|
||||
@@ -355,7 +355,7 @@ pub mod tests {
|
||||
|
||||
type TestChecker = LightDataChecker<
|
||||
NativeExecutor<substrate_test_runtime_client::LocalExecutor>,
|
||||
Blake2Hasher,
|
||||
BlakeTwo256,
|
||||
Block,
|
||||
DummyStorage,
|
||||
>;
|
||||
@@ -465,7 +465,7 @@ pub mod tests {
|
||||
|
||||
// check remote read proof locally
|
||||
let local_storage = InMemoryBlockchain::<Block>::new();
|
||||
let local_cht_root = cht::compute_root::<Header, Blake2Hasher, _>(4, 0, local_headers_hashes).unwrap();
|
||||
let local_cht_root = cht::compute_root::<Header, BlakeTwo256, _>(4, 0, local_headers_hashes).unwrap();
|
||||
if insert_cht {
|
||||
local_storage.insert_cht_root(1, local_cht_root);
|
||||
}
|
||||
@@ -479,7 +479,7 @@ pub mod tests {
|
||||
fn header_with_computed_extrinsics_root(extrinsics: Vec<Extrinsic>) -> Header {
|
||||
use sp_trie::{TrieConfiguration, trie_types::Layout};
|
||||
let iter = extrinsics.iter().map(Encode::encode);
|
||||
let extrinsics_root = Layout::<Blake2Hasher>::ordered_trie_root(iter);
|
||||
let extrinsics_root = Layout::<BlakeTwo256>::ordered_trie_root(iter);
|
||||
|
||||
// only care about `extrinsics_root`
|
||||
Header::new(0, extrinsics_root, H256::zero(), H256::zero(), Default::default())
|
||||
@@ -625,7 +625,7 @@ pub mod tests {
|
||||
).unwrap();
|
||||
|
||||
// prepare local checker, having a root of changes trie CHT#0
|
||||
let local_cht_root = cht::compute_root::<Header, Blake2Hasher, _>(4, 0, remote_roots.iter().cloned().map(|ct| Ok(Some(ct)))).unwrap();
|
||||
let local_cht_root = cht::compute_root::<Header, BlakeTwo256, _>(4, 0, remote_roots.iter().cloned().map(|ct| Ok(Some(ct)))).unwrap();
|
||||
let mut local_storage = DummyStorage::new();
|
||||
local_storage.changes_tries_cht_roots.insert(0, local_cht_root);
|
||||
let local_checker = TestChecker::new(
|
||||
@@ -732,7 +732,7 @@ pub mod tests {
|
||||
// we're testing this test case here:
|
||||
// (1, 4, dave.clone(), vec![(4, 0), (1, 1), (1, 0)]),
|
||||
let (remote_client, remote_roots, _) = prepare_client_with_key_changes();
|
||||
let local_cht_root = cht::compute_root::<Header, Blake2Hasher, _>(
|
||||
let local_cht_root = cht::compute_root::<Header, BlakeTwo256, _>(
|
||||
4, 0, remote_roots.iter().cloned().map(|ct| Ok(Some(ct)))).unwrap();
|
||||
let dave = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Dave.into())).to_vec();
|
||||
let dave = StorageKey(dave);
|
||||
|
||||
@@ -26,7 +26,7 @@ use std::sync::Arc;
|
||||
use sc_executor::RuntimeInfo;
|
||||
use sp_core::traits::CodeExecutor;
|
||||
use sp_runtime::BuildStorage;
|
||||
use sp_runtime::traits::{Block as BlockT, HasherFor};
|
||||
use sp_runtime::traits::{Block as BlockT, HashFor};
|
||||
use sp_blockchain::Result as ClientResult;
|
||||
|
||||
use crate::call_executor::LocalCallExecutor;
|
||||
@@ -45,7 +45,7 @@ pub fn new_light_blockchain<B: BlockT, S: BlockchainStorage<B>>(storage: S) -> A
|
||||
}
|
||||
|
||||
/// Create an instance of light client backend.
|
||||
pub fn new_light_backend<B, S>(blockchain: Arc<Blockchain<S>>) -> Arc<Backend<S, HasherFor<B>>>
|
||||
pub fn new_light_backend<B, S>(blockchain: Arc<Blockchain<S>>) -> Arc<Backend<S, HashFor<B>>>
|
||||
where
|
||||
B: BlockT,
|
||||
S: BlockchainStorage<B>,
|
||||
@@ -55,15 +55,15 @@ pub fn new_light_backend<B, S>(blockchain: Arc<Blockchain<S>>) -> Arc<Backend<S,
|
||||
|
||||
/// Create an instance of light client.
|
||||
pub fn new_light<B, S, GS, RA, E>(
|
||||
backend: Arc<Backend<S, HasherFor<B>>>,
|
||||
backend: Arc<Backend<S, HashFor<B>>>,
|
||||
genesis_storage: &GS,
|
||||
code_executor: E,
|
||||
) -> ClientResult<
|
||||
Client<
|
||||
Backend<S, HasherFor<B>>,
|
||||
Backend<S, HashFor<B>>,
|
||||
GenesisCallExecutor<
|
||||
Backend<S, HasherFor<B>>,
|
||||
LocalCallExecutor<Backend<S, HasherFor<B>>, E>
|
||||
Backend<S, HashFor<B>>,
|
||||
LocalCallExecutor<Backend<S, HashFor<B>>, E>
|
||||
>,
|
||||
B,
|
||||
RA
|
||||
@@ -91,7 +91,7 @@ pub fn new_light<B, S, GS, RA, E>(
|
||||
pub fn new_fetch_checker<E, B: BlockT, S: BlockchainStorage<B>>(
|
||||
blockchain: Arc<Blockchain<S>>,
|
||||
executor: E,
|
||||
) -> LightDataChecker<E, HasherFor<B>, B, S>
|
||||
) -> LightDataChecker<E, HashFor<B>, B, S>
|
||||
where
|
||||
E: CodeExecutor,
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user