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
-51
View File
@@ -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)]