Remove deprecated host functions (#5038)

Sadly we need to keep one function `ext_blake2_256`. This function is
manually defined in `sp-core`.
This commit is contained in:
Bastian Köcher
2020-02-27 13:38:55 +01:00
committed by GitHub
parent 5907b0d902
commit e5123166d4
8 changed files with 72 additions and 1139 deletions
+51
View File
@@ -915,6 +915,56 @@ 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.
@@ -929,6 +979,7 @@ pub type SubstrateHostFunctions = (
logging::HostFunctions,
sandbox::HostFunctions,
crate::trie::HostFunctions,
ext_blake2_256::ExtBlake2_256,
);
#[cfg(test)]
@@ -105,23 +105,6 @@ pub trait TestApi {
}
}
/// Two random external functions from the old runtime interface.
/// This ensures that we still inherently export these functions from the host and that we are still
/// compatible with old wasm runtimes.
#[cfg(not(feature = "std"))]
extern "C" {
pub fn ext_clear_storage(key_data: *const u8, key_len: u32);
pub fn ext_keccak_256(data: *const u8, len: u32, out: *mut u8);
}
/// Make sure the old runtime interface needs to be imported.
#[no_mangle]
#[cfg(not(feature = "std"))]
pub fn force_old_runtime_interface_import() {
unsafe { ext_clear_storage(sp_std::ptr::null(), 0); }
unsafe { ext_keccak_256(sp_std::ptr::null(), 0, sp_std::ptr::null_mut()); }
}
/// This function is not used, but we require it for the compiler to include `sp-io`.
/// `sp-io` is required for its panic and oom handler.
#[no_mangle]
@@ -248,4 +231,14 @@ 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());
}
}
@@ -33,7 +33,6 @@ fn call_wasm_method<HF: HostFunctionsT>(method: &str) -> TestExternalities {
(
HF,
sp_io::SubstrateHostFunctions,
sc_executor::deprecated_host_interface::SubstrateExternals
)
>(
method,
@@ -128,3 +127,8 @@ 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");
}
@@ -170,6 +170,12 @@ impl<T: PointerType> Pointer<T> {
}
}
impl<T: PointerType> From<u32> for Pointer<T> {
fn from(ptr: u32) -> Self {
Pointer::new(ptr)
}
}
impl<T: PointerType> From<Pointer<T>> for u32 {
fn from(ptr: Pointer<T>) -> Self {
ptr.ptr