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
@@ -237,7 +237,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
pub struct RuntimeApiImpl<Block: #crate_::BlockT, C: #crate_::CallApiAt<Block> + 'static>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
C::StateBackend: #crate_::StateBackend<#crate_::HashFor<Block>>,
{
call: &'static C,
commit_on_success: std::cell::RefCell<bool>,
@@ -257,7 +257,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
for RuntimeApiImpl<Block, C>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
C::StateBackend: #crate_::StateBackend<#crate_::HashFor<Block>>,
{}
#[cfg(any(feature = "std", test))]
@@ -265,7 +265,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
for RuntimeApiImpl<Block, C>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
C::StateBackend: #crate_::StateBackend<#crate_::HashFor<Block>>,
{}
#[cfg(any(feature = "std", test))]
@@ -273,7 +273,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
for RuntimeApiImpl<Block, C>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
C::StateBackend: #crate_::StateBackend<#crate_::HashFor<Block>>,
{
type Error = C::Error;
}
@@ -283,7 +283,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
RuntimeApiImpl<Block, C>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
C::StateBackend: #crate_::StateBackend<#crate_::HashFor<Block>>,
{
type StateBackend = C::StateBackend;
@@ -327,7 +327,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
&self,
backend: &Self::StateBackend,
changes_trie_state: Option<&#crate_::ChangesTrieState<
#crate_::HasherFor<Block>,
#crate_::HashFor<Block>,
#crate_::NumberFor<Block>,
>>,
parent_hash: Block::Hash,
@@ -351,7 +351,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
where
C: #crate_::CallApiAt<Block> + 'static,
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
C::StateBackend: #crate_::StateBackend<#crate_::HashFor<Block>>,
{
type RuntimeApi = RuntimeApiImpl<Block, C>;
@@ -373,7 +373,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
impl<Block: #crate_::BlockT, C: #crate_::CallApiAt<Block>> RuntimeApiImpl<Block, C>
where
// Rust bug: https://github.com/rust-lang/rust/issues/24159
C::StateBackend: #crate_::StateBackend<#crate_::HasherFor<Block>>,
C::StateBackend: #crate_::StateBackend<#crate_::HashFor<Block>>,
{
fn call_api_at<
R: #crate_::Encode + #crate_::Decode + PartialEq,
@@ -603,7 +603,7 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> {
where_clause.predicates.push(
parse_quote! {
RuntimeApiImplCall::StateBackend:
#crate_::StateBackend<#crate_::HasherFor<__SR_API_BLOCK__>>
#crate_::StateBackend<#crate_::HashFor<__SR_API_BLOCK__>>
}
);
+10 -12
View File
@@ -50,7 +50,7 @@ pub use sp_core::to_substrate_wasm_fn_return_value;
#[doc(hidden)]
pub use sp_runtime::{
traits::{
Block as BlockT, GetNodeBlockType, GetRuntimeBlockType, HasherFor, NumberFor,
Block as BlockT, GetNodeBlockType, GetRuntimeBlockType, HashFor, NumberFor,
Header as HeaderT, Hash as HashT,
},
generic::BlockId, transaction_validity::TransactionValidity,
@@ -228,22 +228,20 @@ pub use sp_api_proc_macro::impl_runtime_apis;
/// A type that records all accessed trie nodes and generates a proof out of it.
#[cfg(feature = "std")]
pub type ProofRecorder<B> = sp_state_machine::ProofRecorder<
<<<B as BlockT>::Header as HeaderT>::Hashing as HashT>::Hasher
>;
pub type ProofRecorder<B> = sp_state_machine::ProofRecorder<HashFor<B>>;
/// A type that is used as cache for the storage transactions.
#[cfg(feature = "std")]
pub type StorageTransactionCache<Block, Backend> =
sp_state_machine::StorageTransactionCache<
<Backend as StateBackend<HasherFor<Block>>>::Transaction, HasherFor<Block>, NumberFor<Block>
<Backend as StateBackend<HashFor<Block>>>::Transaction, HashFor<Block>, NumberFor<Block>
>;
#[cfg(feature = "std")]
pub type StorageChanges<SBackend, Block> =
sp_state_machine::StorageChanges<
<SBackend as StateBackend<HasherFor<Block>>>::Transaction,
HasherFor<Block>,
<SBackend as StateBackend<HashFor<Block>>>::Transaction,
HashFor<Block>,
NumberFor<Block>
>;
@@ -255,7 +253,7 @@ pub type StateBackendFor<P, Block> =
/// Extract the state backend transaction type for a type that implements `ProvideRuntimeApi`.
#[cfg(feature = "std")]
pub type TransactionFor<P, Block> =
<StateBackendFor<P, Block> as StateBackend<HasherFor<Block>>>::Transaction;
<StateBackendFor<P, Block> as StateBackend<HashFor<Block>>>::Transaction;
/// Something that can be constructed to a runtime api.
#[cfg(feature = "std")]
@@ -279,7 +277,7 @@ pub trait ApiErrorExt {
#[cfg(feature = "std")]
pub trait ApiExt<Block: BlockT>: ApiErrorExt {
/// The state backend that is used to store the block states.
type StateBackend: StateBackend<HasherFor<Block>>;
type StateBackend: StateBackend<HashFor<Block>>;
/// The given closure will be called with api instance. Inside the closure any api call is
/// allowed. After doing the api call, the closure is allowed to map the `Result` to a
@@ -328,7 +326,7 @@ pub trait ApiExt<Block: BlockT>: ApiErrorExt {
fn into_storage_changes(
&self,
backend: &Self::StateBackend,
changes_trie_state: Option<&ChangesTrieState<HasherFor<Block>, NumberFor<Block>>>,
changes_trie_state: Option<&ChangesTrieState<HashFor<Block>, NumberFor<Block>>>,
parent_hash: Block::Hash,
) -> Result<StorageChanges<Self::StateBackend, Block>, String> where Self: Sized;
}
@@ -355,7 +353,7 @@ pub enum InitializeBlock<'a, Block: BlockT> {
/// Parameters for [`CallApiAt::call_api_at`].
#[cfg(feature = "std")]
pub struct CallApiAtParams<'a, Block: BlockT, C, NC, Backend: StateBackend<HasherFor<Block>>> {
pub struct CallApiAtParams<'a, Block: BlockT, C, NC, Backend: StateBackend<HashFor<Block>>> {
/// A reference to something that implements the [`Core`] api.
pub core_api: &'a C,
/// The block id that determines the state that should be setup when calling the function.
@@ -389,7 +387,7 @@ pub trait CallApiAt<Block: BlockT> {
type Error: std::fmt::Debug + From<String>;
/// The state backend that is used to store the block states.
type StateBackend: StateBackend<HasherFor<Block>>;
type StateBackend: StateBackend<HashFor<Block>>;
/// Calls the given api function with the given encoded arguments at the given block and returns
/// the encoded result.
@@ -18,9 +18,9 @@ use sp_api::ProvideRuntimeApi;
use substrate_test_runtime_client::{
prelude::*,
DefaultTestClientBuilderExt, TestClientBuilder,
runtime::{TestAPI, DecodeFails, Transfer, Header},
runtime::{TestAPI, DecodeFails, Transfer, Block},
};
use sp_runtime::{generic::BlockId, traits::{Header as HeaderT, Hash as HashT}};
use sp_runtime::{generic::BlockId, traits::{Header as HeaderT, HashFor}};
use sp_state_machine::{
ExecutionStrategy, create_proof_check_backend,
execution_proof_check_on_trie_backend,
@@ -184,7 +184,7 @@ fn record_proof_works() {
builder.push(transaction.clone()).unwrap();
let (block, _, proof) = builder.build().expect("Bake block").into_inner();
let backend = create_proof_check_backend::<<<Header as HeaderT>::Hashing as HashT>::Hasher>(
let backend = create_proof_check_backend::<HashFor<Block>>(
storage_root,
proof.expect("Proof was generated"),
).expect("Creates proof backend.");