-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add cli option --wasmtime-precompiled and subcommand precompile-wasm
#1641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
librelois
wants to merge
29
commits into
paritytech:master
Choose a base branch
from
moonbeam-foundation:elois-wasmtime-precompiled
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
d7b3887
add command PrecompileWasmCmd
librelois 0616813
fmt
librelois f7705b6
Merge branch 'master' into elois-wasmtime-precompiled
librelois 68e7765
fix tests & benches build
librelois 6b122c3
fix rust compilation
librelois c56bffd
typo and rm unused import
librelois e0b1111
fix rust test
librelois 50eac51
Merge branch 'master' into elois-wasmtime-precompiled
librelois 2755221
enable wasm-precompiled feature in node-template
librelois 0b26c2e
improve precompile-wasm subcommand: load the bytecode from chain spec
librelois 5c038e0
Fix allow_missing_func_imports
crystalin 85d5570
Changes genesis to use blake2hash for wasm hash
crystalin 3bedb37
fmt
crystalin 582c404
fmt
crystalin 9f92b3c
more fmt
crystalin ac73de9
Merge branch 'master' into elois-wasmtime-precompiled
crystalin 2da154a
Update substrate/client/executor/src/wasm_runtime.rs
RomarQ 9e4abc7
Update substrate/client/executor/src/wasm_runtime.rs
RomarQ 8061c87
fix: 🩹 Amend wasmtime precompiled PR review (#13)
ffarall fe0b4db
feat: 🔀 Merge upstream into wasmtime-precompile PR (#14)
ffarall 635219c
Revert "feat: 🔀 Merge upstream into wasmtime-precompile PR (#14)"
RomarQ e3405e0
Merge branch 'master' into pr/1641
ffarall d420424
fix: :rotating_light: Compiler fixes after merge
ffarall d844373
feat: :sparkles: Add value parser to `output_dir` parameter in `preco…
ffarall 9dbee39
feat: :sparkles: Add `precompile-wasm` subcommand to parachain template
ffarall 11ede5c
style: :art: Apply `cargo fmt`
ffarall 8e5f8ba
Merge branch 'master' into pr/1641
ffarall d9cc028
Merge branch 'master' into elois-wasmtime-precompiled
librelois c1f1cf8
add prdoc
librelois File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
145 changes: 145 additions & 0 deletions
145
substrate/client/cli/src/commands/precompile_wasm_cmd.rs
This file contains hidden or 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,145 @@ | ||||||||||||||||||||||||||||||||||||
| // This file is part of Substrate. | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||||||||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // This program is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||
| // it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||||||||||
| // the Free Software Foundation, either version 3 of the License, or | ||||||||||||||||||||||||||||||||||||
| // (at your option) any later version. | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||||||||||
| // GNU General Public License for more details. | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||||||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| use crate::{ | ||||||||||||||||||||||||||||||||||||
| arg_enums::{ | ||||||||||||||||||||||||||||||||||||
| execution_method_from_cli, WasmExecutionMethod, WasmtimeInstantiationStrategy, | ||||||||||||||||||||||||||||||||||||
| DEFAULT_WASMTIME_INSTANTIATION_STRATEGY, | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| error::{self, Error}, | ||||||||||||||||||||||||||||||||||||
| params::{DatabaseParams, PruningParams, SharedParams}, | ||||||||||||||||||||||||||||||||||||
| CliConfiguration, | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| use clap::Parser; | ||||||||||||||||||||||||||||||||||||
| use sc_client_api::{Backend, HeaderBackend}; | ||||||||||||||||||||||||||||||||||||
| use sc_executor::{ | ||||||||||||||||||||||||||||||||||||
| precompile_and_serialize_versioned_wasm_runtime, HeapAllocStrategy, DEFAULT_HEAP_ALLOC_PAGES, | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
| 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}; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// The `precompile-wasm` command used to serialize a precompiled WASM module. | ||||||||||||||||||||||||||||||||||||
| #[derive(Debug, Parser)] | ||||||||||||||||||||||||||||||||||||
| pub struct PrecompileWasmCmd { | ||||||||||||||||||||||||||||||||||||
| #[allow(missing_docs)] | ||||||||||||||||||||||||||||||||||||
| #[clap(flatten)] | ||||||||||||||||||||||||||||||||||||
| pub database_params: DatabaseParams, | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// The default number of 64KB pages to ever allocate for Wasm execution. | ||||||||||||||||||||||||||||||||||||
| /// Don't alter this unless you know what you're doing. | ||||||||||||||||||||||||||||||||||||
| #[arg(long, value_name = "COUNT")] | ||||||||||||||||||||||||||||||||||||
| pub default_heap_pages: Option<u32>, | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// path to the directory where precompiled artifact will be written | ||||||||||||||||||||||||||||||||||||
| #[arg()] | ||||||||||||||||||||||||||||||||||||
| pub output_dir: PathBuf, | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+60
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| #[allow(missing_docs)] | ||||||||||||||||||||||||||||||||||||
| #[clap(flatten)] | ||||||||||||||||||||||||||||||||||||
| pub pruning_params: PruningParams, | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| #[allow(missing_docs)] | ||||||||||||||||||||||||||||||||||||
| #[clap(flatten)] | ||||||||||||||||||||||||||||||||||||
| pub shared_params: SharedParams, | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// The WASM instantiation method to use. | ||||||||||||||||||||||||||||||||||||
|
librelois marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||
| /// Only has an effect when `wasm-execution` is set to `compiled`. | ||||||||||||||||||||||||||||||||||||
| /// The copy-on-write strategies are only supported on Linux. | ||||||||||||||||||||||||||||||||||||
| /// If the copy-on-write variant of a strategy is unsupported | ||||||||||||||||||||||||||||||||||||
| /// the executor will fall back to the non-CoW equivalent. | ||||||||||||||||||||||||||||||||||||
| /// The fastest (and the default) strategy available is `pooling-copy-on-write`. | ||||||||||||||||||||||||||||||||||||
| /// The `legacy-instance-reuse` strategy is deprecated and will | ||||||||||||||||||||||||||||||||||||
| /// be removed in the future. It should only be used in case of | ||||||||||||||||||||||||||||||||||||
| /// issues with the default instantiation strategy. | ||||||||||||||||||||||||||||||||||||
| #[arg( | ||||||||||||||||||||||||||||||||||||
| long, | ||||||||||||||||||||||||||||||||||||
| value_name = "STRATEGY", | ||||||||||||||||||||||||||||||||||||
| default_value_t = DEFAULT_WASMTIME_INSTANTIATION_STRATEGY, | ||||||||||||||||||||||||||||||||||||
| value_enum, | ||||||||||||||||||||||||||||||||||||
| )] | ||||||||||||||||||||||||||||||||||||
| pub wasmtime_instantiation_strategy: WasmtimeInstantiationStrategy, | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| impl PrecompileWasmCmd { | ||||||||||||||||||||||||||||||||||||
| /// Run the precompile-wasm command | ||||||||||||||||||||||||||||||||||||
| pub async fn run<B, BA>(&self, backend: Arc<BA>, spec: Box<dyn ChainSpec>) -> error::Result<()> | ||||||||||||||||||||||||||||||||||||
| where | ||||||||||||||||||||||||||||||||||||
| B: BlockT, | ||||||||||||||||||||||||||||||||||||
| BA: Backend<B>, | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| 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( | ||||||||||||||||||||||||||||||||||||
| 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) { | ||||||||||||||||||||||||||||||||||||
|
librelois marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||
| let runtime_code = RuntimeCode { | ||||||||||||||||||||||||||||||||||||
| code_fetcher: &sp_core::traits::WrappedRuntimeCode( | ||||||||||||||||||||||||||||||||||||
| wasm_bytecode.as_slice().into(), | ||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||
| hash: sp_core::blake2_256(&wasm_bytecode).to_vec(), | ||||||||||||||||||||||||||||||||||||
|
librelois marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||
| heap_pages: Some(heap_pages as u64), | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
| precompile_and_serialize_versioned_wasm_runtime( | ||||||||||||||||||||||||||||||||||||
| 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(()) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| impl CliConfiguration for PrecompileWasmCmd { | ||||||||||||||||||||||||||||||||||||
| fn shared_params(&self) -> &SharedParams { | ||||||||||||||||||||||||||||||||||||
| &self.shared_params | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| fn pruning_params(&self) -> Option<&PruningParams> { | ||||||||||||||||||||||||||||||||||||
| Some(&self.pruning_params) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| fn database_params(&self) -> Option<&DatabaseParams> { | ||||||||||||||||||||||||||||||||||||
| Some(&self.database_params) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.