mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-24 12:31:17 +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
@@ -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__>>
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -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.");
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
//! Block import helpers.
|
||||
|
||||
use sp_runtime::traits::{Block as BlockT, DigestItemFor, Header as HeaderT, NumberFor, HasherFor};
|
||||
use sp_runtime::traits::{Block as BlockT, DigestItemFor, Header as HeaderT, NumberFor, HashFor};
|
||||
use sp_runtime::Justification;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::borrow::Cow;
|
||||
@@ -139,7 +139,7 @@ pub struct BlockImportParams<Block: BlockT, Transaction> {
|
||||
/// The changes to the storage to create the state for the block. If this is `Some(_)`,
|
||||
/// the block import will not need to re-execute the block for importing it.
|
||||
pub storage_changes: Option<
|
||||
sp_state_machine::StorageChanges<Transaction, HasherFor<Block>, NumberFor<Block>>
|
||||
sp_state_machine::StorageChanges<Transaction, HashFor<Block>, NumberFor<Block>>
|
||||
>,
|
||||
/// Is this block finalized already?
|
||||
/// `true` implies instant finality.
|
||||
|
||||
@@ -32,7 +32,7 @@ use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use sp_runtime::{
|
||||
generic::BlockId, traits::{Block as BlockT, DigestFor, NumberFor, HasherFor},
|
||||
generic::BlockId, traits::{Block as BlockT, DigestFor, NumberFor, HashFor},
|
||||
};
|
||||
use futures::prelude::*;
|
||||
pub use sp_inherents::InherentData;
|
||||
@@ -93,7 +93,7 @@ pub struct Proposal<Block: BlockT, Transaction> {
|
||||
/// Optional proof that was recorded while building the block.
|
||||
pub proof: Option<sp_state_machine::StorageProof>,
|
||||
/// The storage changes while building this block.
|
||||
pub storage_changes: sp_state_machine::StorageChanges<Transaction, HasherFor<Block>, NumberFor<Block>>,
|
||||
pub storage_changes: sp_state_machine::StorageChanges<Transaction, HashFor<Block>, NumberFor<Block>>,
|
||||
}
|
||||
|
||||
/// Used as parameter to [`Proposer`] to tell the requirement on recording a proof.
|
||||
|
||||
@@ -16,27 +16,10 @@
|
||||
|
||||
//! Substrate Blake2b Hasher implementation
|
||||
|
||||
use hash_db::Hasher;
|
||||
use hash256_std_hasher::Hash256StdHasher;
|
||||
use crate::hash::H256;
|
||||
|
||||
pub mod blake2 {
|
||||
use super::{Hasher, Hash256StdHasher, H256};
|
||||
#[cfg(feature = "std")]
|
||||
use crate::hashing::blake2_256;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
extern "C" {
|
||||
fn ext_blake2_256(data: *const u8, len: u32, out: *mut u8);
|
||||
}
|
||||
#[cfg(not(feature = "std"))]
|
||||
fn blake2_256(data: &[u8]) -> [u8; 32] {
|
||||
let mut result: [u8; 32] = Default::default();
|
||||
unsafe {
|
||||
ext_blake2_256(data.as_ptr(), data.len() as u32, result.as_mut_ptr());
|
||||
}
|
||||
result
|
||||
}
|
||||
use hash_db::Hasher;
|
||||
use hash256_std_hasher::Hash256StdHasher;
|
||||
use crate::hash::H256;
|
||||
|
||||
/// Concrete implementation of Hasher using Blake2b 256-bit hashes
|
||||
#[derive(Debug)]
|
||||
@@ -46,8 +29,9 @@ pub mod blake2 {
|
||||
type Out = H256;
|
||||
type StdHasher = Hash256StdHasher;
|
||||
const LENGTH: usize = 32;
|
||||
|
||||
fn hash(x: &[u8]) -> Self::Out {
|
||||
blake2_256(x).into()
|
||||
crate::hashing::blake2_256(x).into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ pub mod ed25519;
|
||||
pub mod sr25519;
|
||||
pub mod ecdsa;
|
||||
pub mod hash;
|
||||
#[cfg(feature = "std")]
|
||||
mod hasher;
|
||||
pub mod offchain;
|
||||
pub mod sandbox;
|
||||
@@ -77,8 +78,7 @@ pub use changes_trie::{ChangesTrieConfiguration, ChangesTrieConfigurationRange};
|
||||
pub use crypto::{DeriveJunction, Pair, Public};
|
||||
|
||||
pub use hash_db::Hasher;
|
||||
// Switch back to Blake after PoC-3 is out
|
||||
// pub use self::hasher::blake::BlakeHasher;
|
||||
#[cfg(feature = "std")]
|
||||
pub use self::hasher::blake2::Blake2Hasher;
|
||||
|
||||
pub use sp_storage as storage;
|
||||
|
||||
@@ -915,56 +915,6 @@ pub fn oom(_: core::alloc::Layout) -> ! {
|
||||
#[cfg(feature = "std")]
|
||||
pub type TestExternalities = sp_state_machine::TestExternalities<sp_core::Blake2Hasher, u64>;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod ext_blake2_256 {
|
||||
use sp_wasm_interface::{Signature, Function, HostFunctions, ValueType, Value, FunctionContext};
|
||||
|
||||
/// There is a custom `extern function` in `sp_core::hasher` for `ext_blake2_256` hasher. This
|
||||
/// custom extern was missed to remove and requires us to support this now. This type is a custom
|
||||
/// implementation for the wasm function in native.
|
||||
pub struct ExtBlake2_256;
|
||||
|
||||
impl HostFunctions for ExtBlake2_256 {
|
||||
fn host_functions() -> Vec<&'static dyn Function> {
|
||||
vec![&ExtBlake2_256]
|
||||
}
|
||||
}
|
||||
|
||||
impl Function for ExtBlake2_256 {
|
||||
fn name(&self) -> &str {
|
||||
"ext_blake2_256"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::new_with_args(&[ValueType::I32, ValueType::I32, ValueType::I32][..])
|
||||
}
|
||||
|
||||
fn execute(
|
||||
&self,
|
||||
context: &mut dyn FunctionContext,
|
||||
args: &mut dyn Iterator<Item = Value>,
|
||||
) -> sp_wasm_interface::Result<Option<Value>> {
|
||||
let data = args.next().and_then(|v| v.as_i32())
|
||||
.ok_or_else(|| "`data` not present or not an `i32`")? as u32;
|
||||
let len = args.next().and_then(|v| v.as_i32())
|
||||
.ok_or_else(|| "`len` not present or not an `i32`")? as u32;
|
||||
let out = args.next().and_then(|v| v.as_i32())
|
||||
.ok_or_else(|| "`out` not present or not an `i32`")? as u32;
|
||||
|
||||
let result: [u8; 32] = if len == 0 {
|
||||
sp_core::hashing::blake2_256(&[0u8; 0])
|
||||
} else {
|
||||
let mem = context.read_memory(data.into(), len)
|
||||
.map_err(|_| "Invalid attempt to get data in ext_blake2_256")?;
|
||||
sp_core::hashing::blake2_256(&mem)
|
||||
};
|
||||
context.write_memory(out.into(), &result)
|
||||
.map_err(|_| "Invalid attempt to set result in ext_blake2_256")?;
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The host functions Substrate provides for the Wasm runtime environment.
|
||||
///
|
||||
/// All these host functions will be callable from inside the Wasm environment.
|
||||
@@ -979,7 +929,6 @@ pub type SubstrateHostFunctions = (
|
||||
logging::HostFunctions,
|
||||
sandbox::HostFunctions,
|
||||
crate::trie::HostFunctions,
|
||||
ext_blake2_256::ExtBlake2_256,
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -231,14 +231,4 @@ wasm_export_functions! {
|
||||
}
|
||||
assert_eq!(0, len);
|
||||
}
|
||||
|
||||
fn test_ext_blake2_256() {
|
||||
use sp_core::Hasher;
|
||||
|
||||
let data = "hey, hash me please!";
|
||||
let hash = sp_core::Blake2Hasher::hash(data.as_bytes());
|
||||
|
||||
let expected = sp_io::hashing::blake2_256(data.as_bytes());
|
||||
assert_eq!(&expected, hash.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,5 +13,5 @@ sp-runtime-interface = { version = "2.0.0-alpha.2", path = "../" }
|
||||
sc-executor = { version = "0.8.0-alpha.2", path = "../../../client/executor" }
|
||||
sp-runtime-interface-test-wasm = { version = "2.0.0-dev", path = "../test-wasm" }
|
||||
sp-state-machine = { version = "0.8.0-alpha.2", path = "../../../primitives/state-machine" }
|
||||
sp-core = { version = "2.0.0-alpha.2", path = "../../core" }
|
||||
sp-runtime = { version = "2.0.0-alpha.2", path = "../../runtime" }
|
||||
sp-io = { version = "2.0.0-alpha.2", path = "../../io" }
|
||||
|
||||
@@ -23,7 +23,7 @@ use sp_runtime_interface::*;
|
||||
use sp_runtime_interface_test_wasm::{WASM_BINARY, test_api::HostFunctions};
|
||||
use sp_wasm_interface::HostFunctions as HostFunctionsT;
|
||||
|
||||
type TestExternalities = sp_state_machine::TestExternalities<sp_core::Blake2Hasher, u64>;
|
||||
type TestExternalities = sp_state_machine::TestExternalities<sp_runtime::traits::BlakeTwo256, u64>;
|
||||
|
||||
fn call_wasm_method<HF: HostFunctionsT>(method: &str) -> TestExternalities {
|
||||
let mut ext = TestExternalities::default();
|
||||
@@ -127,8 +127,3 @@ fn test_encoded_return_value_memory_is_freed() {
|
||||
fn test_array_return_value_memory_is_freed() {
|
||||
call_wasm_method::<HostFunctions>("test_array_return_value_memory_is_freed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ext_blake2_256() {
|
||||
call_wasm_method::<HostFunctions>("test_ext_blake2_256");
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ rand = { version = "0.7.2", optional = true }
|
||||
impl-trait-for-tuples = "0.1.3"
|
||||
sp-inherents = { version = "2.0.0-alpha.2", default-features = false, path = "../inherents" }
|
||||
parity-util-mem = { version = "0.5.1", default-features = false, features = ["primitive-types"] }
|
||||
hash256-std-hasher = { version = "0.15.2", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_json = "1.0.41"
|
||||
@@ -44,4 +45,5 @@ std = [
|
||||
"serde",
|
||||
"sp-inherents/std",
|
||||
"parity-util-mem/std",
|
||||
"hash256-std-hasher/std",
|
||||
]
|
||||
|
||||
@@ -66,7 +66,7 @@ impl<Hashing: Hash> RandomNumberGenerator<Hashing> {
|
||||
loop {
|
||||
if self.offset() + needed > self.current.as_ref().len() {
|
||||
// rehash
|
||||
self.current = Hashing::hash(self.current.as_ref());
|
||||
self.current = <Hashing as Hash>::hash(self.current.as_ref());
|
||||
self.offset = 0;
|
||||
}
|
||||
let data = &self.current.as_ref()[self.offset()..self.offset() + needed];
|
||||
|
||||
@@ -25,7 +25,7 @@ use std::fmt::Display;
|
||||
use std::str::FromStr;
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Serialize, Deserialize, de::DeserializeOwned};
|
||||
use sp_core::{self, Hasher, Blake2Hasher, TypeId, RuntimeDebug};
|
||||
use sp_core::{self, Hasher, TypeId, RuntimeDebug};
|
||||
use crate::codec::{Codec, Encode, Decode};
|
||||
use crate::transaction_validity::{
|
||||
ValidTransaction, TransactionValidity, TransactionValidityError, UnknownTransaction,
|
||||
@@ -369,20 +369,19 @@ pub trait OffchainWorker<BlockNumber> {
|
||||
/// Abstraction around hashing
|
||||
// Stupid bug in the Rust compiler believes derived
|
||||
// traits must be fulfilled by all type parameters.
|
||||
pub trait Hash: 'static + MaybeSerializeDeserialize + Debug + Clone + Eq + PartialEq {
|
||||
pub trait Hash: 'static + MaybeSerializeDeserialize + Debug + Clone + Eq + PartialEq + Hasher<Out = <Self as Hash>::Output> {
|
||||
/// The hash type produced.
|
||||
type Output: Member + MaybeSerializeDeserialize + Debug + sp_std::hash::Hash
|
||||
+ AsRef<[u8]> + AsMut<[u8]> + Copy + Default + Encode + Decode;
|
||||
|
||||
/// The associated hash_db Hasher type.
|
||||
type Hasher: Hasher<Out=Self::Output>;
|
||||
|
||||
/// Produce the hash of some byte-slice.
|
||||
fn hash(s: &[u8]) -> Self::Output;
|
||||
fn hash(s: &[u8]) -> Self::Output {
|
||||
<Self as Hasher>::hash(s)
|
||||
}
|
||||
|
||||
/// Produce the hash of some codec-encodable value.
|
||||
fn hash_of<S: Encode>(s: &S) -> Self::Output {
|
||||
Encode::using_encoded(s, Self::hash)
|
||||
Encode::using_encoded(s, <Self as Hasher>::hash)
|
||||
}
|
||||
|
||||
/// The ordered Patricia tree root of the given `input`.
|
||||
@@ -397,12 +396,18 @@ pub trait Hash: 'static + MaybeSerializeDeserialize + Debug + Clone + Eq + Parti
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
||||
pub struct BlakeTwo256;
|
||||
|
||||
impl Hash for BlakeTwo256 {
|
||||
type Output = sp_core::H256;
|
||||
type Hasher = Blake2Hasher;
|
||||
fn hash(s: &[u8]) -> Self::Output {
|
||||
impl Hasher for BlakeTwo256 {
|
||||
type Out = sp_core::H256;
|
||||
type StdHasher = hash256_std_hasher::Hash256StdHasher;
|
||||
const LENGTH: usize = 32;
|
||||
|
||||
fn hash(s: &[u8]) -> Self::Out {
|
||||
sp_io::hashing::blake2_256(s).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for BlakeTwo256 {
|
||||
type Output = sp_core::H256;
|
||||
|
||||
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> Self::Output {
|
||||
sp_io::trie::blake2_256_root(input)
|
||||
@@ -616,8 +621,6 @@ pub trait ExtrinsicMetadata {
|
||||
type SignedExtensions: SignedExtension;
|
||||
}
|
||||
|
||||
/// Extract the hasher type for a block.
|
||||
pub type HasherFor<B> = <HashFor<B> as Hash>::Hasher;
|
||||
/// Extract the hashing type for a block.
|
||||
pub type HashFor<B> = <<B as Block>::Header as Header>::Hashing;
|
||||
/// Extract the number type for a block.
|
||||
|
||||
@@ -25,6 +25,7 @@ sp-externalities = { version = "0.8.0-alpha.2", path = "../externalities" }
|
||||
|
||||
[dev-dependencies]
|
||||
hex-literal = "0.2.1"
|
||||
sp-runtime = { version = "2.0.0-alpha.2", path = "../runtime" }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
@@ -350,7 +350,7 @@ mod tests {
|
||||
top: Default::default(),
|
||||
children: map![
|
||||
child_storage.clone() => StorageChild {
|
||||
data: map![ b"doe".to_vec() => b"reindeer".to_vec() ],
|
||||
data: map![ b"doe".to_vec() => b"reindeer".to_vec() ],
|
||||
child_info: CHILD_INFO_1.to_owned(),
|
||||
}
|
||||
]
|
||||
|
||||
@@ -376,13 +376,13 @@ impl<'a, H, Number> Iterator for ProvingDrilldownIterator<'a, H, Number>
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::iter::FromIterator;
|
||||
use sp_core::Blake2Hasher;
|
||||
use crate::changes_trie::Configuration;
|
||||
use crate::changes_trie::input::InputPair;
|
||||
use crate::changes_trie::storage::InMemoryStorage;
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
use super::*;
|
||||
|
||||
fn prepare_for_drilldown() -> (Configuration, InMemoryStorage<Blake2Hasher, u64>) {
|
||||
fn prepare_for_drilldown() -> (Configuration, InMemoryStorage<BlakeTwo256, u64>) {
|
||||
let config = Configuration { digest_interval: 4, digest_levels: 2 };
|
||||
let backend = InMemoryStorage::with_inputs(vec![
|
||||
// digest: 1..4 => [(3, 0)]
|
||||
@@ -447,7 +447,7 @@ mod tests {
|
||||
#[test]
|
||||
fn drilldown_iterator_works() {
|
||||
let (config, storage) = prepare_for_drilldown();
|
||||
let drilldown_result = key_changes::<Blake2Hasher, u64>(
|
||||
let drilldown_result = key_changes::<BlakeTwo256, u64>(
|
||||
configuration_range(&config, 0),
|
||||
&storage,
|
||||
1,
|
||||
@@ -458,7 +458,7 @@ mod tests {
|
||||
).and_then(Result::from_iter);
|
||||
assert_eq!(drilldown_result, Ok(vec![(8, 2), (8, 1), (6, 3), (3, 0)]));
|
||||
|
||||
let drilldown_result = key_changes::<Blake2Hasher, u64>(
|
||||
let drilldown_result = key_changes::<BlakeTwo256, u64>(
|
||||
configuration_range(&config, 0),
|
||||
&storage,
|
||||
1,
|
||||
@@ -469,7 +469,7 @@ mod tests {
|
||||
).and_then(Result::from_iter);
|
||||
assert_eq!(drilldown_result, Ok(vec![]));
|
||||
|
||||
let drilldown_result = key_changes::<Blake2Hasher, u64>(
|
||||
let drilldown_result = key_changes::<BlakeTwo256, u64>(
|
||||
configuration_range(&config, 0),
|
||||
&storage,
|
||||
1,
|
||||
@@ -480,7 +480,7 @@ mod tests {
|
||||
).and_then(Result::from_iter);
|
||||
assert_eq!(drilldown_result, Ok(vec![(3, 0)]));
|
||||
|
||||
let drilldown_result = key_changes::<Blake2Hasher, u64>(
|
||||
let drilldown_result = key_changes::<BlakeTwo256, u64>(
|
||||
configuration_range(&config, 0),
|
||||
&storage,
|
||||
1,
|
||||
@@ -491,7 +491,7 @@ mod tests {
|
||||
).and_then(Result::from_iter);
|
||||
assert_eq!(drilldown_result, Ok(vec![(6, 3), (3, 0)]));
|
||||
|
||||
let drilldown_result = key_changes::<Blake2Hasher, u64>(
|
||||
let drilldown_result = key_changes::<BlakeTwo256, u64>(
|
||||
configuration_range(&config, 0),
|
||||
&storage,
|
||||
7,
|
||||
@@ -502,7 +502,7 @@ mod tests {
|
||||
).and_then(Result::from_iter);
|
||||
assert_eq!(drilldown_result, Ok(vec![(8, 2), (8, 1)]));
|
||||
|
||||
let drilldown_result = key_changes::<Blake2Hasher, u64>(
|
||||
let drilldown_result = key_changes::<BlakeTwo256, u64>(
|
||||
configuration_range(&config, 0),
|
||||
&storage,
|
||||
5,
|
||||
@@ -519,7 +519,7 @@ mod tests {
|
||||
let (config, storage) = prepare_for_drilldown();
|
||||
storage.clear_storage();
|
||||
|
||||
assert!(key_changes::<Blake2Hasher, u64>(
|
||||
assert!(key_changes::<BlakeTwo256, u64>(
|
||||
configuration_range(&config, 0),
|
||||
&storage,
|
||||
1,
|
||||
@@ -529,7 +529,7 @@ mod tests {
|
||||
&[42],
|
||||
).and_then(|i| i.collect::<Result<Vec<_>, _>>()).is_err());
|
||||
|
||||
assert!(key_changes::<Blake2Hasher, u64>(
|
||||
assert!(key_changes::<BlakeTwo256, u64>(
|
||||
configuration_range(&config, 0),
|
||||
&storage,
|
||||
1,
|
||||
@@ -543,7 +543,7 @@ mod tests {
|
||||
#[test]
|
||||
fn drilldown_iterator_fails_when_range_is_invalid() {
|
||||
let (config, storage) = prepare_for_drilldown();
|
||||
assert!(key_changes::<Blake2Hasher, u64>(
|
||||
assert!(key_changes::<BlakeTwo256, u64>(
|
||||
configuration_range(&config, 0),
|
||||
&storage,
|
||||
1,
|
||||
@@ -552,7 +552,7 @@ mod tests {
|
||||
None,
|
||||
&[42],
|
||||
).is_err());
|
||||
assert!(key_changes::<Blake2Hasher, u64>(
|
||||
assert!(key_changes::<BlakeTwo256, u64>(
|
||||
configuration_range(&config, 0),
|
||||
&storage,
|
||||
20,
|
||||
@@ -570,12 +570,12 @@ mod tests {
|
||||
|
||||
// create drilldown iterator that records all trie nodes during drilldown
|
||||
let (remote_config, remote_storage) = prepare_for_drilldown();
|
||||
let remote_proof = key_changes_proof::<Blake2Hasher, u64>(
|
||||
let remote_proof = key_changes_proof::<BlakeTwo256, u64>(
|
||||
configuration_range(&remote_config, 0), &remote_storage, 1,
|
||||
&AnchorBlockId { hash: Default::default(), number: 16 }, 16, None, &[42]).unwrap();
|
||||
|
||||
let (remote_config, remote_storage) = prepare_for_drilldown();
|
||||
let remote_proof_child = key_changes_proof::<Blake2Hasher, u64>(
|
||||
let remote_proof_child = key_changes_proof::<BlakeTwo256, u64>(
|
||||
configuration_range(&remote_config, 0), &remote_storage, 1,
|
||||
&AnchorBlockId { hash: Default::default(), number: 16 }, 16, Some(&b"1"[..]), &[42]).unwrap();
|
||||
|
||||
@@ -584,13 +584,13 @@ mod tests {
|
||||
// create drilldown iterator that works the same, but only depends on trie
|
||||
let (local_config, local_storage) = prepare_for_drilldown();
|
||||
local_storage.clear_storage();
|
||||
let local_result = key_changes_proof_check::<Blake2Hasher, u64>(
|
||||
let local_result = key_changes_proof_check::<BlakeTwo256, u64>(
|
||||
configuration_range(&local_config, 0), &local_storage, remote_proof, 1,
|
||||
&AnchorBlockId { hash: Default::default(), number: 16 }, 16, None, &[42]);
|
||||
|
||||
let (local_config, local_storage) = prepare_for_drilldown();
|
||||
local_storage.clear_storage();
|
||||
let local_result_child = key_changes_proof_check::<Blake2Hasher, u64>(
|
||||
let local_result_child = key_changes_proof_check::<BlakeTwo256, u64>(
|
||||
configuration_range(&local_config, 0), &local_storage, remote_proof_child, 1,
|
||||
&AnchorBlockId { hash: Default::default(), number: 16 }, 16, Some(&b"1"[..]), &[42]);
|
||||
|
||||
@@ -621,7 +621,7 @@ mod tests {
|
||||
input[91 - 1].1.push(InputPair::DigestIndex(DigestIndex { block: 91, key: vec![42] }, vec![80]));
|
||||
let storage = InMemoryStorage::with_inputs(input, vec![]);
|
||||
|
||||
let drilldown_result = key_changes::<Blake2Hasher, u64>(
|
||||
let drilldown_result = key_changes::<BlakeTwo256, u64>(
|
||||
config_range,
|
||||
&storage,
|
||||
1,
|
||||
|
||||
@@ -114,14 +114,15 @@ fn prune_trie<H: Hasher, Number: BlockNumber, F: FnMut(H::Out)>(
|
||||
mod tests {
|
||||
use std::collections::HashSet;
|
||||
use sp_trie::MemoryDB;
|
||||
use sp_core::{H256, Blake2Hasher};
|
||||
use sp_core::H256;
|
||||
use crate::backend::insert_into_memory_db;
|
||||
use crate::changes_trie::storage::InMemoryStorage;
|
||||
use codec::Encode;
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
use super::*;
|
||||
|
||||
fn prune_by_collect(
|
||||
storage: &dyn Storage<Blake2Hasher, u64>,
|
||||
storage: &dyn Storage<BlakeTwo256, u64>,
|
||||
first: u64,
|
||||
last: u64,
|
||||
current_block: u64,
|
||||
@@ -135,27 +136,26 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn prune_works() {
|
||||
fn prepare_storage() -> InMemoryStorage<Blake2Hasher, u64> {
|
||||
|
||||
fn prepare_storage() -> InMemoryStorage<BlakeTwo256, u64> {
|
||||
let child_key = ChildIndex { block: 67u64, storage_key: b"1".to_vec() }.encode();
|
||||
let mut mdb1 = MemoryDB::<Blake2Hasher>::default();
|
||||
let root1 = insert_into_memory_db::<Blake2Hasher, _>(
|
||||
let mut mdb1 = MemoryDB::<BlakeTwo256>::default();
|
||||
let root1 = insert_into_memory_db::<BlakeTwo256, _>(
|
||||
&mut mdb1, vec![(vec![10], vec![20])]).unwrap();
|
||||
let mut mdb2 = MemoryDB::<Blake2Hasher>::default();
|
||||
let root2 = insert_into_memory_db::<Blake2Hasher, _>(
|
||||
let mut mdb2 = MemoryDB::<BlakeTwo256>::default();
|
||||
let root2 = insert_into_memory_db::<BlakeTwo256, _>(
|
||||
&mut mdb2,
|
||||
vec![(vec![11], vec![21]), (vec![12], vec![22])],
|
||||
).unwrap();
|
||||
let mut mdb3 = MemoryDB::<Blake2Hasher>::default();
|
||||
let ch_root3 = insert_into_memory_db::<Blake2Hasher, _>(
|
||||
let mut mdb3 = MemoryDB::<BlakeTwo256>::default();
|
||||
let ch_root3 = insert_into_memory_db::<BlakeTwo256, _>(
|
||||
&mut mdb3, vec![(vec![110], vec![120])]).unwrap();
|
||||
let root3 = insert_into_memory_db::<Blake2Hasher, _>(&mut mdb3, vec![
|
||||
let root3 = insert_into_memory_db::<BlakeTwo256, _>(&mut mdb3, vec![
|
||||
(vec![13], vec![23]),
|
||||
(vec![14], vec![24]),
|
||||
(child_key, ch_root3.as_ref().encode()),
|
||||
]).unwrap();
|
||||
let mut mdb4 = MemoryDB::<Blake2Hasher>::default();
|
||||
let root4 = insert_into_memory_db::<Blake2Hasher, _>(
|
||||
let mut mdb4 = MemoryDB::<BlakeTwo256>::default();
|
||||
let root4 = insert_into_memory_db::<BlakeTwo256, _>(
|
||||
&mut mdb4,
|
||||
vec![(vec![15], vec![25])],
|
||||
).unwrap();
|
||||
|
||||
@@ -362,11 +362,12 @@ impl<H: Hasher> Backend<H> for InMemory<H> where H::Out: Codec {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
|
||||
/// Assert in memory backend with only child trie keys works as trie backend.
|
||||
#[test]
|
||||
fn in_memory_with_child_trie_only() {
|
||||
let storage = InMemory::<sp_core::Blake2Hasher>::default();
|
||||
let storage = InMemory::<BlakeTwo256>::default();
|
||||
let child_info = OwnedChildInfo::new_default(b"unique_id_1".to_vec());
|
||||
let mut storage = storage.update(
|
||||
vec![(
|
||||
|
||||
@@ -727,9 +727,8 @@ mod tests {
|
||||
use super::*;
|
||||
use super::ext::Ext;
|
||||
use super::changes_trie::Configuration as ChangesTrieConfig;
|
||||
use sp_core::{
|
||||
Blake2Hasher, map, traits::{Externalities, RuntimeCode}, storage::ChildStorageKey,
|
||||
};
|
||||
use sp_core::{map, traits::{Externalities, RuntimeCode}, storage::ChildStorageKey};
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct DummyCodeExecutor {
|
||||
@@ -912,7 +911,7 @@ mod tests {
|
||||
).unwrap();
|
||||
|
||||
// check proof locally
|
||||
let local_result = execution_proof_check::<Blake2Hasher, u64, _>(
|
||||
let local_result = execution_proof_check::<BlakeTwo256, u64, _>(
|
||||
remote_root,
|
||||
remote_proof,
|
||||
&mut Default::default(),
|
||||
@@ -935,7 +934,7 @@ mod tests {
|
||||
b"abc".to_vec() => b"2".to_vec(),
|
||||
b"bbb".to_vec() => b"3".to_vec()
|
||||
];
|
||||
let mut state = InMemoryBackend::<Blake2Hasher>::from(initial);
|
||||
let mut state = InMemoryBackend::<BlakeTwo256>::from(initial);
|
||||
let backend = state.as_trie_backend().unwrap();
|
||||
let mut overlay = OverlayedChanges {
|
||||
committed: map![
|
||||
@@ -978,7 +977,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn set_child_storage_works() {
|
||||
let mut state = InMemoryBackend::<Blake2Hasher>::default();
|
||||
let mut state = InMemoryBackend::<BlakeTwo256>::default();
|
||||
let backend = state.as_trie_backend().unwrap();
|
||||
let mut overlay = OverlayedChanges::default();
|
||||
let mut cache = StorageTransactionCache::default();
|
||||
@@ -1025,12 +1024,12 @@ mod tests {
|
||||
let remote_root = remote_backend.storage_root(::std::iter::empty()).0;
|
||||
let remote_proof = prove_read(remote_backend, &[b"value2"]).unwrap();
|
||||
// check proof locally
|
||||
let local_result1 = read_proof_check::<Blake2Hasher, _>(
|
||||
let local_result1 = read_proof_check::<BlakeTwo256, _>(
|
||||
remote_root,
|
||||
remote_proof.clone(),
|
||||
&[b"value2"],
|
||||
).unwrap();
|
||||
let local_result2 = read_proof_check::<Blake2Hasher, _>(
|
||||
let local_result2 = read_proof_check::<BlakeTwo256, _>(
|
||||
remote_root,
|
||||
remote_proof.clone(),
|
||||
&[&[0xff]],
|
||||
@@ -1050,13 +1049,13 @@ mod tests {
|
||||
CHILD_INFO_1,
|
||||
&[b"value3"],
|
||||
).unwrap();
|
||||
let local_result1 = read_child_proof_check::<Blake2Hasher, _>(
|
||||
let local_result1 = read_child_proof_check::<BlakeTwo256, _>(
|
||||
remote_root,
|
||||
remote_proof.clone(),
|
||||
b":child_storage:default:sub1",
|
||||
&[b"value3"],
|
||||
).unwrap();
|
||||
let local_result2 = read_child_proof_check::<Blake2Hasher, _>(
|
||||
let local_result2 = read_child_proof_check::<BlakeTwo256, _>(
|
||||
remote_root,
|
||||
remote_proof.clone(),
|
||||
b":child_storage:default:sub1",
|
||||
|
||||
@@ -308,16 +308,17 @@ mod tests {
|
||||
use crate::InMemoryBackend;
|
||||
use crate::trie_backend::tests::test_trie;
|
||||
use super::*;
|
||||
use sp_core::{Blake2Hasher, storage::ChildStorageKey};
|
||||
use sp_core::storage::ChildStorageKey;
|
||||
use crate::proving_backend::create_proof_check_backend;
|
||||
use sp_trie::PrefixedMemoryDB;
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
|
||||
const CHILD_INFO_1: ChildInfo<'static> = ChildInfo::new_default(b"unique_id_1");
|
||||
const CHILD_INFO_2: ChildInfo<'static> = ChildInfo::new_default(b"unique_id_2");
|
||||
|
||||
fn test_proving<'a>(
|
||||
trie_backend: &'a TrieBackend<PrefixedMemoryDB<Blake2Hasher>,Blake2Hasher>,
|
||||
) -> ProvingBackend<'a, PrefixedMemoryDB<Blake2Hasher>, Blake2Hasher> {
|
||||
trie_backend: &'a TrieBackend<PrefixedMemoryDB<BlakeTwo256>,BlakeTwo256>,
|
||||
) -> ProvingBackend<'a, PrefixedMemoryDB<BlakeTwo256>, BlakeTwo256> {
|
||||
ProvingBackend::new(trie_backend)
|
||||
}
|
||||
|
||||
@@ -338,7 +339,7 @@ mod tests {
|
||||
#[test]
|
||||
fn proof_is_invalid_when_does_not_contains_root() {
|
||||
use sp_core::H256;
|
||||
let result = create_proof_check_backend::<Blake2Hasher>(
|
||||
let result = create_proof_check_backend::<BlakeTwo256>(
|
||||
H256::from_low_u64_be(1),
|
||||
StorageProof::empty()
|
||||
);
|
||||
@@ -361,7 +362,7 @@ mod tests {
|
||||
#[test]
|
||||
fn proof_recorded_and_checked() {
|
||||
let contents = (0..64).map(|i| (vec![i], Some(vec![i]))).collect::<Vec<_>>();
|
||||
let in_memory = InMemoryBackend::<Blake2Hasher>::default();
|
||||
let in_memory = InMemoryBackend::<BlakeTwo256>::default();
|
||||
let mut in_memory = in_memory.update(vec![(None, contents)]);
|
||||
let in_memory_root = in_memory.storage_root(::std::iter::empty()).0;
|
||||
(0..64).for_each(|i| assert_eq!(in_memory.storage(&[i]).unwrap().unwrap(), vec![i]));
|
||||
@@ -376,7 +377,7 @@ mod tests {
|
||||
|
||||
let proof = proving.extract_proof();
|
||||
|
||||
let proof_check = create_proof_check_backend::<Blake2Hasher>(in_memory_root.into(), proof).unwrap();
|
||||
let proof_check = create_proof_check_backend::<BlakeTwo256>(in_memory_root.into(), proof).unwrap();
|
||||
assert_eq!(proof_check.storage(&[42]).unwrap().unwrap(), vec![42]);
|
||||
}
|
||||
|
||||
@@ -393,7 +394,7 @@ mod tests {
|
||||
(Some((own2.clone(), CHILD_INFO_2.to_owned())),
|
||||
(10..15).map(|i| (vec![i], Some(vec![i]))).collect()),
|
||||
];
|
||||
let in_memory = InMemoryBackend::<Blake2Hasher>::default();
|
||||
let in_memory = InMemoryBackend::<BlakeTwo256>::default();
|
||||
let mut in_memory = in_memory.update(contents);
|
||||
let in_memory_root = in_memory.full_storage_root::<_, Vec<_>, _>(
|
||||
::std::iter::empty(),
|
||||
@@ -425,7 +426,7 @@ mod tests {
|
||||
|
||||
let proof = proving.extract_proof();
|
||||
|
||||
let proof_check = create_proof_check_backend::<Blake2Hasher>(
|
||||
let proof_check = create_proof_check_backend::<BlakeTwo256>(
|
||||
in_memory_root.into(),
|
||||
proof
|
||||
).unwrap();
|
||||
@@ -439,7 +440,7 @@ mod tests {
|
||||
assert_eq!(proving.child_storage(&own1[..], CHILD_INFO_1, &[64]), Ok(Some(vec![64])));
|
||||
|
||||
let proof = proving.extract_proof();
|
||||
let proof_check = create_proof_check_backend::<Blake2Hasher>(
|
||||
let proof_check = create_proof_check_backend::<BlakeTwo256>(
|
||||
in_memory_root.into(),
|
||||
proof
|
||||
).unwrap();
|
||||
|
||||
@@ -34,13 +34,12 @@ use sp_core::{
|
||||
well_known_keys::{CHANGES_TRIE_CONFIG, CODE, HEAP_PAGES, is_child_storage_key},
|
||||
Storage,
|
||||
},
|
||||
Blake2Hasher,
|
||||
};
|
||||
use codec::Encode;
|
||||
use sp_externalities::{Extensions, Extension};
|
||||
|
||||
/// Simple HashMap-based Externalities impl.
|
||||
pub struct TestExternalities<H: Hasher = Blake2Hasher, N: ChangesTrieBlockNumber = u64>
|
||||
pub struct TestExternalities<H: Hasher, N: ChangesTrieBlockNumber = u64>
|
||||
where
|
||||
H::Out: codec::Codec,
|
||||
{
|
||||
@@ -198,11 +197,12 @@ impl<H, N> sp_externalities::ExtensionStore for TestExternalities<H, N> where
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_core::traits::Externalities;
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
use hex_literal::hex;
|
||||
|
||||
#[test]
|
||||
fn commit_should_work() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher, u64>::default();
|
||||
let mut ext = TestExternalities::<BlakeTwo256, u64>::default();
|
||||
let mut ext = ext.ext();
|
||||
ext.set_storage(b"doe".to_vec(), b"reindeer".to_vec());
|
||||
ext.set_storage(b"dog".to_vec(), b"puppy".to_vec());
|
||||
@@ -213,7 +213,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn set_and_retrieve_code() {
|
||||
let mut ext = TestExternalities::<Blake2Hasher, u64>::default();
|
||||
let mut ext = TestExternalities::<BlakeTwo256, u64>::default();
|
||||
let mut ext = ext.ext();
|
||||
|
||||
let code = vec![1, 2, 3];
|
||||
@@ -225,6 +225,6 @@ mod tests {
|
||||
#[test]
|
||||
fn check_send() {
|
||||
fn assert_send<T: Send>() {}
|
||||
assert_send::<TestExternalities::<Blake2Hasher, u64>>();
|
||||
assert_send::<TestExternalities::<BlakeTwo256, u64>>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,9 +245,10 @@ impl<S: TrieBackendStorage<H>, H: Hasher> Backend<H> for TrieBackend<S, H> where
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use std::collections::HashSet;
|
||||
use sp_core::{Blake2Hasher, H256};
|
||||
use sp_core::H256;
|
||||
use codec::Encode;
|
||||
use sp_trie::{TrieMut, PrefixedMemoryDB, trie_types::TrieDBMut, KeySpacedDBMut};
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
use super::*;
|
||||
|
||||
const CHILD_KEY_1: &[u8] = b":child_storage:default:sub1";
|
||||
@@ -255,9 +256,9 @@ pub mod tests {
|
||||
const CHILD_UUID_1: &[u8] = b"unique_id_1";
|
||||
const CHILD_INFO_1: ChildInfo<'static> = ChildInfo::new_default(CHILD_UUID_1);
|
||||
|
||||
fn test_db() -> (PrefixedMemoryDB<Blake2Hasher>, H256) {
|
||||
fn test_db() -> (PrefixedMemoryDB<BlakeTwo256>, H256) {
|
||||
let mut root = H256::default();
|
||||
let mut mdb = PrefixedMemoryDB::<Blake2Hasher>::default();
|
||||
let mut mdb = PrefixedMemoryDB::<BlakeTwo256>::default();
|
||||
{
|
||||
let mut mdb = KeySpacedDBMut::new(&mut mdb, CHILD_UUID_1);
|
||||
let mut trie = TrieDBMut::new(&mut mdb, &mut root);
|
||||
@@ -281,7 +282,7 @@ pub mod tests {
|
||||
(mdb, root)
|
||||
}
|
||||
|
||||
pub(crate) fn test_trie() -> TrieBackend<PrefixedMemoryDB<Blake2Hasher>, Blake2Hasher> {
|
||||
pub(crate) fn test_trie() -> TrieBackend<PrefixedMemoryDB<BlakeTwo256>, BlakeTwo256> {
|
||||
let (mdb, root) = test_db();
|
||||
TrieBackend::new(mdb, root)
|
||||
}
|
||||
@@ -312,7 +313,7 @@ pub mod tests {
|
||||
|
||||
#[test]
|
||||
fn pairs_are_empty_on_empty_storage() {
|
||||
assert!(TrieBackend::<PrefixedMemoryDB<Blake2Hasher>, Blake2Hasher>::new(
|
||||
assert!(TrieBackend::<PrefixedMemoryDB<BlakeTwo256>, BlakeTwo256>::new(
|
||||
PrefixedMemoryDB::default(),
|
||||
Default::default(),
|
||||
).pairs().is_empty());
|
||||
|
||||
@@ -27,6 +27,7 @@ trie-bench = "0.20.0"
|
||||
trie-standardmap = "0.15.2"
|
||||
criterion = "0.2.11"
|
||||
hex-literal = "0.2.1"
|
||||
sp-runtime = { version = "2.0.0-alpha.2", path = "../runtime" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
|
||||
@@ -20,11 +20,11 @@ criterion_main!(benches);
|
||||
|
||||
fn benchmark(c: &mut Criterion) {
|
||||
trie_bench::standard_benchmark::<
|
||||
sp_trie::Layout<sp_core::Blake2Hasher>,
|
||||
sp_trie::Layout<sp_runtime::traits::BlakeTwo256>,
|
||||
sp_trie::TrieStream,
|
||||
>(c, "substrate-blake2");
|
||||
trie_bench::standard_benchmark::<
|
||||
sp_trie::Layout<sp_core::Blake2Hasher>,
|
||||
sp_trie::Layout<sp_runtime::traits::BlakeTwo256>,
|
||||
sp_trie::TrieStream,
|
||||
>(c, "substrate-keccak");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user