From 350c823459c172dbc84b1b685952d73d2037f460 Mon Sep 17 00:00:00 2001 From: crystalin Date: Thu, 28 Sep 2023 13:39:30 +0200 Subject: [PATCH 1/3] Fix allow_missing_func_imports --- Cargo.lock | 1 + .../cli/src/commands/precompile_wasm_cmd.rs | 2 - substrate/client/executor/Cargo.toml | 1 + substrate/client/executor/src/wasm_runtime.rs | 42 ++++++++++++++----- .../client/executor/wasmtime/src/runtime.rs | 4 +- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 44cc8fe9d37f..939fdac50297 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15117,6 +15117,7 @@ dependencies = [ "assert_matches", "criterion 0.4.0", "env_logger 0.9.3", + "log", "num_cpus", "parity-scale-codec", "parking_lot 0.12.1", diff --git a/substrate/client/cli/src/commands/precompile_wasm_cmd.rs b/substrate/client/cli/src/commands/precompile_wasm_cmd.rs index f0366f35f572..a5f35cec3b3b 100644 --- a/substrate/client/cli/src/commands/precompile_wasm_cmd.rs +++ b/substrate/client/cli/src/commands/precompile_wasm_cmd.rs @@ -94,7 +94,6 @@ impl PrecompileWasmCmd { let state = backend.state_at(backend.blockchain().info().finalized_hash)?; precompile_and_serialize_versioned_wasm_runtime( - true, HeapAllocStrategy::Static { extra_pages: heap_pages }, &BackendRuntimeCode::new(&state).runtime_code()?, execution_method_from_cli( @@ -113,7 +112,6 @@ impl PrecompileWasmCmd { heap_pages: Some(heap_pages as u64), }; precompile_and_serialize_versioned_wasm_runtime( - true, HeapAllocStrategy::Static { extra_pages: heap_pages }, &runtime_code, execution_method_from_cli( diff --git a/substrate/client/executor/Cargo.toml b/substrate/client/executor/Cargo.toml index 9f41b7423737..a05e0eda2890 100644 --- a/substrate/client/executor/Cargo.toml +++ b/substrate/client/executor/Cargo.toml @@ -17,6 +17,7 @@ targets = ["x86_64-unknown-linux-gnu"] parking_lot = "0.12.1" schnellru = "0.2.1" tracing = "0.1.29" +log = "0.4.17" codec = { package = "parity-scale-codec", version = "3.6.1" } sc-executor-common = { path = "common" } diff --git a/substrate/client/executor/src/wasm_runtime.rs b/substrate/client/executor/src/wasm_runtime.rs index 14e78a2cbf6c..f376e2e57ebe 100644 --- a/substrate/client/executor/src/wasm_runtime.rs +++ b/substrate/client/executor/src/wasm_runtime.rs @@ -331,24 +331,35 @@ where )) }; let mut maybe_compiled_artifact = None; + + let artifact_version = compute_artifact_version( + allow_missing_func_imports, + code_hash, + &semantics, + ); + log::debug!( + target: "wasmtime-runtime", + "Searching for wasm hash: {}", + artifact_version.clone() + ); + for entry in std::fs::read_dir(wasmtime_precompiled_dir).map_err(handle_err)? { let entry = entry.map_err(handle_err)?; if let Some(file_name) = entry.file_name().to_str() { - let artifact_version = compute_artifact_version( - allow_missing_func_imports, - code_hash, - &semantics, - ); - // We check that the artifact was generated for this specific artifact // version and with the same wasm interface version and configuration. - if file_name.contains(&artifact_version) { + if file_name.contains(&artifact_version.clone()) { + log::info!( + target: "wasmtime-runtime", + "Found precompiled wasm: {}", + file_name + ); // We change the version check strategy to make sure that the file // content was serialized with the exact same config as well maybe_compiled_artifact = Some(( entry.path(), sc_executor_wasmtime::ModuleVersionStrategy::Custom( - artifact_version, + artifact_version.clone(), ), )); } @@ -410,7 +421,6 @@ where /// Create and serialize a precompiled artifact of a wasm runtime with the given `code`. pub fn precompile_and_serialize_versioned_wasm_runtime<'c>( - allow_missing_func_imports: bool, heap_alloc_strategy: HeapAllocStrategy, runtime_code: &'c RuntimeCode<'c>, wasm_method: WasmExecutionMethod, @@ -434,7 +444,12 @@ pub fn precompile_and_serialize_versioned_wasm_runtime<'c>( let code_hash = &runtime_code.hash; let artifact_version = - compute_artifact_version(allow_missing_func_imports, code_hash, &semantics); + compute_artifact_version(false, code_hash, &semantics); + log::debug!( + target: "wasmtime-runtime", + "Generated precompiled wasm hash: {}", + artifact_version.clone() + ); let code = runtime_code.fetch_runtime_code().ok_or(WasmError::CodeNotFound)?; @@ -450,7 +465,7 @@ pub fn precompile_and_serialize_versioned_wasm_runtime<'c>( // Write in a file let mut file = std::fs::File::create( - wasmtime_precompiled_path.join(format!("precompiled_wasm_0x{}", &artifact_version)), + wasmtime_precompiled_path.join(format!("precompiled_wasm_{}", &artifact_version)), ) .map_err(|e| { WasmError::Other(format!( @@ -472,6 +487,11 @@ fn compute_artifact_version( code_hash: &[u8], semantics: &sc_executor_wasmtime::Semantics, ) -> String { + log::trace!( + target: "wasmtime-runtime", + "Computing wasm runtime hash [allow_missing_func_imports: {}, code_hash: {:?}, semantics: {:?}]", + allow_missing_func_imports, code_hash, semantics + ); let mut buffer = Vec::new(); buffer.extend_from_slice(code_hash); buffer.extend_from_slice(sp_wasm_interface::VERSION.as_bytes()); diff --git a/substrate/client/executor/wasmtime/src/runtime.rs b/substrate/client/executor/wasmtime/src/runtime.rs index 2ad83c09cd37..2fba36cf5819 100644 --- a/substrate/client/executor/wasmtime/src/runtime.rs +++ b/substrate/client/executor/wasmtime/src/runtime.rs @@ -412,7 +412,7 @@ fn common_config(semantics: &Semantics) -> std::result::Result Date: Thu, 28 Sep 2023 14:55:45 +0200 Subject: [PATCH 2/3] Changes genesis to use blake2hash for wasm hash --- substrate/client/chain-spec/src/genesis.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/substrate/client/chain-spec/src/genesis.rs b/substrate/client/chain-spec/src/genesis.rs index 6aa156a620a7..119e586fdb3c 100644 --- a/substrate/client/chain-spec/src/genesis.rs +++ b/substrate/client/chain-spec/src/genesis.rs @@ -18,11 +18,17 @@ //! Tool for creating the genesis block. -use std::{collections::hash_map::DefaultHasher, marker::PhantomData, sync::Arc}; +use std::{marker::PhantomData, sync::Arc}; use sc_client_api::{backend::Backend, BlockImportOperation}; use sc_executor::RuntimeVersionOf; -use sp_core::storage::{well_known_keys, StateVersion, Storage}; +use sp_core::{ + storage::{ + well_known_keys, StateVersion, Storage + }, + Blake2Hasher +}; + use sp_runtime::{ traits::{Block as BlockT, Hash as HashT, Header as HeaderT, Zero}, BuildStorage, @@ -44,10 +50,8 @@ where code_fetcher: &code_fetcher, heap_pages: None, hash: { - use std::hash::{Hash, Hasher}; - let mut state = DefaultHasher::new(); - wasm.hash(&mut state); - state.finish().to_le_bytes().to_vec() + use sp_core::Hasher; + Blake2Hasher::hash(&wasm.to_vec()).as_ref().to_vec() }, }; let runtime_version = RuntimeVersionOf::runtime_version(executor, &mut ext, &runtime_code) From a9953bda82beedb43041e3212d2c0a8861eeccb2 Mon Sep 17 00:00:00 2001 From: crystalin Date: Thu, 28 Sep 2023 18:25:16 +0200 Subject: [PATCH 3/3] fmt --- substrate/client/chain-spec/src/genesis.rs | 6 +-- .../cli/src/commands/precompile_wasm_cmd.rs | 13 +++--- substrate/client/executor/src/wasm_runtime.rs | 41 +++++++++---------- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/substrate/client/chain-spec/src/genesis.rs b/substrate/client/chain-spec/src/genesis.rs index 119e586fdb3c..be62b640901f 100644 --- a/substrate/client/chain-spec/src/genesis.rs +++ b/substrate/client/chain-spec/src/genesis.rs @@ -23,10 +23,8 @@ use std::{marker::PhantomData, sync::Arc}; use sc_client_api::{backend::Backend, BlockImportOperation}; use sc_executor::RuntimeVersionOf; use sp_core::{ - storage::{ - well_known_keys, StateVersion, Storage - }, - Blake2Hasher + storage::{well_known_keys, StateVersion, Storage}, + Blake2Hasher, }; use sp_runtime::{ diff --git a/substrate/client/cli/src/commands/precompile_wasm_cmd.rs b/substrate/client/cli/src/commands/precompile_wasm_cmd.rs index a5f35cec3b3b..1ab0a876d26a 100644 --- a/substrate/client/cli/src/commands/precompile_wasm_cmd.rs +++ b/substrate/client/cli/src/commands/precompile_wasm_cmd.rs @@ -28,12 +28,12 @@ use crate::{ use clap::Parser; use sc_client_api::{Backend, HeaderBackend}; -use sp_core::traits::RuntimeCode; use sc_executor::{ precompile_and_serialize_versioned_wasm_runtime, HeapAllocStrategy, DEFAULT_HEAP_ALLOC_PAGES, }; -use sp_runtime::traits::Block as BlockT; use sc_service::ChainSpec; +use sp_core::traits::RuntimeCode; +use sp_runtime::traits::Block as BlockT; use sp_state_machine::backend::BackendRuntimeCode; use std::{fmt::Debug, path::PathBuf, sync::Arc}; @@ -81,7 +81,7 @@ pub struct PrecompileWasmCmd { impl PrecompileWasmCmd { /// Run the precompile-wasm command - pub async fn run(&self, backend: Arc, spec: Box,) -> error::Result<()> + pub async fn run(&self, backend: Arc, spec: Box) -> error::Result<()> where B: BlockT, BA: Backend, @@ -92,7 +92,7 @@ impl PrecompileWasmCmd { if backend.have_state_at(blockchain_info.finalized_hash, blockchain_info.finalized_number) { let state = backend.state_at(backend.blockchain().info().finalized_hash)?; - + precompile_and_serialize_versioned_wasm_runtime( HeapAllocStrategy::Static { extra_pages: heap_pages }, &BackendRuntimeCode::new(&state).runtime_code()?, @@ -107,7 +107,9 @@ impl PrecompileWasmCmd { let storage = spec.as_storage_builder().build_storage()?; if let Some(wasm_bytecode) = storage.top.get(sp_storage::well_known_keys::CODE) { let runtime_code = RuntimeCode { - code_fetcher: &sp_core::traits::WrappedRuntimeCode(wasm_bytecode.as_slice().into()), + code_fetcher: &sp_core::traits::WrappedRuntimeCode( + wasm_bytecode.as_slice().into(), + ), hash: sp_core::blake2_256(&wasm_bytecode).to_vec(), heap_pages: Some(heap_pages as u64), }; @@ -124,7 +126,6 @@ impl PrecompileWasmCmd { } } - Ok(()) } } diff --git a/substrate/client/executor/src/wasm_runtime.rs b/substrate/client/executor/src/wasm_runtime.rs index f376e2e57ebe..8a85f9eb58ea 100644 --- a/substrate/client/executor/src/wasm_runtime.rs +++ b/substrate/client/executor/src/wasm_runtime.rs @@ -321,25 +321,22 @@ where return Err(WasmError::Instantiation(format!( "--wasmtime-precompiled is not a directory: {}", wasmtime_precompiled_dir.display() - ))) + ))); } let handle_err = |e: std::io::Error| -> WasmError { return WasmError::Instantiation(format!( "Io error when loading wasmtime precompiled folder ({}): {}", wasmtime_precompiled_dir.display(), e - )) + )); }; let mut maybe_compiled_artifact = None; - - let artifact_version = compute_artifact_version( - allow_missing_func_imports, - code_hash, - &semantics, - ); + + let artifact_version = + compute_artifact_version(allow_missing_func_imports, code_hash, &semantics); log::debug!( - target: "wasmtime-runtime", - "Searching for wasm hash: {}", + target: "wasmtime-runtime", + "Searching for wasm hash: {}", artifact_version.clone() ); @@ -350,8 +347,8 @@ where // version and with the same wasm interface version and configuration. if file_name.contains(&artifact_version.clone()) { log::info!( - target: "wasmtime-runtime", - "Found precompiled wasm: {}", + target: "wasmtime-runtime", + "Found precompiled wasm: {}", file_name ); // We change the version check strategy to make sure that the file @@ -367,7 +364,7 @@ where return Err(WasmError::Instantiation( "wasmtime precompiled folder contain a file with invalid utf8 name" .to_owned(), - )) + )); } } @@ -427,7 +424,7 @@ pub fn precompile_and_serialize_versioned_wasm_runtime<'c>( wasmtime_precompiled_path: &Path, ) -> Result<(), WasmError> { let semantics = match wasm_method { - WasmExecutionMethod::Compiled { instantiation_strategy } => + WasmExecutionMethod::Compiled { instantiation_strategy } => { sc_executor_wasmtime::Semantics { heap_alloc_strategy, instantiation_strategy, @@ -438,16 +435,16 @@ pub fn precompile_and_serialize_versioned_wasm_runtime<'c>( wasm_bulk_memory: false, wasm_reference_types: false, wasm_simd: false, - }, + } + }, }; let code_hash = &runtime_code.hash; - let artifact_version = - compute_artifact_version(false, code_hash, &semantics); + let artifact_version = compute_artifact_version(false, code_hash, &semantics); log::debug!( - target: "wasmtime-runtime", - "Generated precompiled wasm hash: {}", + target: "wasmtime-runtime", + "Generated precompiled wasm hash: {}", artifact_version.clone() ); @@ -488,9 +485,9 @@ fn compute_artifact_version( semantics: &sc_executor_wasmtime::Semantics, ) -> String { log::trace!( - target: "wasmtime-runtime", - "Computing wasm runtime hash [allow_missing_func_imports: {}, code_hash: {:?}, semantics: {:?}]", - allow_missing_func_imports, code_hash, semantics + target: "wasmtime-runtime", + "Computing wasm runtime hash [allow_missing_func_imports: {}, code_hash: {}, semantics: {:?}]", + allow_missing_func_imports, sp_core::bytes::to_hex(&code_hash, false), semantics ); let mut buffer = Vec::new(); buffer.extend_from_slice(code_hash);