Skip to content

Commit

Permalink
improve precompile-wasm subcommand: load the bytecode from chain spec
Browse files Browse the repository at this point in the history
  • Loading branch information
librelois committed Sep 27, 2023
1 parent d04c00f commit 1ed54ff
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion substrate/bin/node-template/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub fn run() -> sc_cli::Result<()> {
runner.async_run(|config| {
let PartialComponents { task_manager, backend, .. } =
service::new_partial(&config)?;
Ok((cmd.run(backend), task_manager))
Ok((cmd.run(backend, config.chain_spec), task_manager))
})
},
None => {
Expand Down
1 change: 1 addition & 0 deletions substrate/client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ sp-keystore = { path = "../../primitives/keystore" }
sp-panic-handler = { path = "../../primitives/panic-handler" }
sp-runtime = { path = "../../primitives/runtime" }
sp-state-machine = { path = "../../primitives/state-machine" }
sp-storage = { path = "../../primitives/storage" }
sp-version = { path = "../../primitives/version" }

[dev-dependencies]
Expand Down
63 changes: 44 additions & 19 deletions substrate/client/cli/src/commands/precompile_wasm_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +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_STRATEGY,
precompile_and_serialize_versioned_wasm_runtime, HeapAllocStrategy, DEFAULT_HEAP_ALLOC_PAGES,
};
use sp_runtime::traits::Block as BlockT;
use sc_service::ChainSpec;
use sp_state_machine::backend::BackendRuntimeCode;
use std::{fmt::Debug, path::PathBuf, sync::Arc};

Expand Down Expand Up @@ -79,28 +81,51 @@ pub struct PrecompileWasmCmd {

impl PrecompileWasmCmd {
/// Run the precompile-wasm command
pub async fn run<B, BA>(&self, backend: Arc<BA>) -> error::Result<()>
pub async fn run<B, BA>(&self, backend: Arc<BA>, spec: Box<dyn ChainSpec>,) -> error::Result<()>
where
B: BlockT,
BA: Backend<B>,
{
let state = backend.state_at(backend.blockchain().info().finalized_hash)?;

let heap_pages = self.default_heap_pages.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |h| {
HeapAllocStrategy::Static { extra_pages: h as _ }
});

precompile_and_serialize_versioned_wasm_runtime(
true,
heap_pages,
&BackendRuntimeCode::new(&state).runtime_code()?,
execution_method_from_cli(
WasmExecutionMethod::Compiled,
self.wasmtime_instantiation_strategy,
),
&self.output_dir,
)
.map_err(|e| Error::Application(Box::new(e)))?;
let heap_pages = self.default_heap_pages.unwrap_or(DEFAULT_HEAP_ALLOC_PAGES);

let blockchain_info = backend.blockchain().info();

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(
true,
HeapAllocStrategy::Static { extra_pages: heap_pages },
&BackendRuntimeCode::new(&state).runtime_code()?,
execution_method_from_cli(
WasmExecutionMethod::Compiled,
self.wasmtime_instantiation_strategy,
),
&self.output_dir,
)
.map_err(|e| Error::Application(Box::new(e)))?;
} else {
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()),
hash: sp_core::blake2_256(&wasm_bytecode).to_vec(),
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(
WasmExecutionMethod::Compiled,
self.wasmtime_instantiation_strategy,
),
&self.output_dir,
)
.map_err(|e| Error::Application(Box::new(e)))?;
}
}


Ok(())
}
Expand Down

0 comments on commit 1ed54ff

Please sign in to comment.