-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generating metadata from runtime wasm files (#1720)
closes #1660
- Loading branch information
Showing
14 changed files
with
1,082 additions
and
80 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2024 Parity Technologies (UK) Ltd. | ||
// This file is dual-licensed as Apache-2.0 or GPL-3.0. | ||
// see LICENSE for license details. | ||
|
||
use std::{borrow::Cow, path::Path}; | ||
|
||
use codec::Decode; | ||
use sc_executor::{WasmExecutionMethod, WasmExecutor}; | ||
use sc_executor_common::runtime_blob::RuntimeBlob; | ||
use sp_maybe_compressed_blob::CODE_BLOB_BOMB_LIMIT; | ||
use subxt_codegen::{fetch_metadata::fetch_metadata_from_file_blocking, CodegenError, Metadata}; | ||
|
||
/// Result type shorthand | ||
pub type WasmMetadataResult<A> = Result<A, CodegenError>; | ||
|
||
/// Uses wasm artifact produced by compiling the runtime to generate metadata | ||
pub fn from_wasm_file(wasm_file_path: &Path) -> WasmMetadataResult<Metadata> { | ||
let wasm_file = fetch_metadata_from_file_blocking(wasm_file_path) | ||
.map_err(Into::<CodegenError>::into) | ||
.and_then(maybe_decompress)?; | ||
call_and_decode(wasm_file) | ||
} | ||
|
||
fn call_and_decode(wasm_file: Vec<u8>) -> WasmMetadataResult<Metadata> { | ||
let mut ext: sp_state_machine::BasicExternalities = Default::default(); | ||
|
||
let executor: WasmExecutor<sp_io::SubstrateHostFunctions> = WasmExecutor::builder() | ||
.with_execution_method(WasmExecutionMethod::default()) | ||
.with_offchain_heap_alloc_strategy(sc_executor::HeapAllocStrategy::Dynamic { | ||
maximum_pages: Some(64), | ||
}) | ||
.with_max_runtime_instances(1) | ||
.with_runtime_cache_size(1) | ||
.build(); | ||
|
||
let runtime_blob = | ||
RuntimeBlob::new(&wasm_file).map_err(|e| CodegenError::Wasm(e.to_string()))?; | ||
let metadata_encoded = executor | ||
.uncached_call(runtime_blob, &mut ext, true, "Metadata_metadata", &[]) | ||
.map_err(|_| CodegenError::Wasm("method \"Metadata_metadata\" doesnt exist".to_owned()))?; | ||
|
||
let metadata = <Vec<u8>>::decode(&mut &metadata_encoded[..]).map_err(CodegenError::Decode)?; | ||
|
||
decode(metadata) | ||
} | ||
|
||
fn decode(encoded_metadata: Vec<u8>) -> WasmMetadataResult<Metadata> { | ||
Metadata::decode(&mut encoded_metadata.as_ref()).map_err(Into::into) | ||
} | ||
|
||
fn maybe_decompress(file_contents: Vec<u8>) -> WasmMetadataResult<Vec<u8>> { | ||
sp_maybe_compressed_blob::decompress(file_contents.as_ref(), CODE_BLOB_BOMB_LIMIT) | ||
.map_err(|e| CodegenError::Wasm(e.to_string())) | ||
.map(Cow::into_owned) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#[subxt::subxt(runtime_path = "../../../../artifacts/westend_runtime.wasm")] | ||
mod runtime {} | ||
|
||
#[subxt::subxt(runtime_path = "../../../../artifacts/westend_runtime.compact.compressed.wasm")] | ||
mod runtime_compressed {} | ||
|
||
fn main() { | ||
use runtime; | ||
use runtime_compressed; | ||
|
||
let _ = runtime::system::events::CodeUpdated; | ||
let _ = runtime_compressed::system::events::CodeUpdated; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters