Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request paritytech#300 from subspace/bundled-execution-run…
Browse files Browse the repository at this point in the history
…time

Bundled execution runtime
  • Loading branch information
nazar-pc authored Mar 30, 2022
2 parents c451f2b + 98c2f24 commit 87eed02
Show file tree
Hide file tree
Showing 18 changed files with 1,067 additions and 934 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions crates/sp-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use sp_consensus_slots::Slot;
use sp_core::H256;
use sp_runtime::traits::{BlakeTwo256, Hash as HashT};
use sp_runtime::{OpaqueExtrinsic, RuntimeDebug};
use sp_std::borrow::Cow;
use sp_std::vec::Vec;
use sp_trie::StorageProof;
use subspace_core_primitives::{Randomness, Sha256Hash};
Expand Down Expand Up @@ -207,5 +208,8 @@ sp_api::decl_runtime_apis! {

/// Generates a randomness seed for extrinsics shuffling.
fn extrinsics_shuffling_seed(header: Block::Header) -> Randomness;

/// WASM bundle for execution runtime.
fn execution_wasm_bundle() -> Cow<'static, [u8]>;
}
}
5 changes: 5 additions & 0 deletions crates/subspace-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pallet-timestamp = { version = "4.0.0-dev", default-features = false, git = "htt
pallet-transaction-fees = { version = "0.1.0", default-features = false, path = "../pallet-transaction-fees" }
pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "c364008a6c7da8456e17967f55edf51e45146998" }
pallet-utility = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "c364008a6c7da8456e17967f55edf51e45146998" }
parachain-template-runtime = { version = "0.1.0", default-features = false, path = "../../cumulus/parachain-template/runtime" }
scale-info = { version = "2.0.1", default-features = false, features = ["derive"] }
sp-api = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "c364008a6c7da8456e17967f55edf51e45146998" }
sp-block-builder = { git = "https://github.com/paritytech/substrate", rev = "c364008a6c7da8456e17967f55edf51e45146998", default-features = false, version = "4.0.0-dev"}
Expand All @@ -60,6 +61,8 @@ frame-benchmarking = { version = "4.0.0-dev", default-features = false, git = "h
frame-system-benchmarking = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "c364008a6c7da8456e17967f55edf51e45146998", optional = true }

[build-dependencies]
parachain-template-runtime = { version = "0.1.0", path = "../../cumulus/parachain-template/runtime" }
subspace-wasm-tools = { version = "0.1.0", default-features = false, path = "../subspace-wasm-tools" }
substrate-wasm-builder = { version = "5.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "c364008a6c7da8456e17967f55edf51e45146998" }

[features]
Expand All @@ -84,6 +87,7 @@ std = [
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
"pallet-utility/std",
"parachain-template-runtime/std",
"scale-info/std",
"sp-api/std",
"sp-block-builder/std",
Expand Down Expand Up @@ -111,6 +115,7 @@ runtime-benchmarks = [
"pallet-balances/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"parachain-template-runtime/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
do-not-enforce-cost-of-storage = []
11 changes: 10 additions & 1 deletion crates/subspace-runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@
// 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 std::env;
use substrate_wasm_builder::WasmBuilder;

fn main() {
let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Set by cargo; qed");
subspace_wasm_tools::create_runtime_bundle_inclusion_file(
&(cargo_manifest_dir + "/../../target"),
"parachain-template-runtime",
"EXECUTION_WASM_BUNDLE",
"execution_wasm_bundle.rs",
);

WasmBuilder::new()
.with_current_project()
.export_heap_base()
.import_memory()
.build()
.build();
}
8 changes: 8 additions & 0 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit = "256"]

// Make execution WASM runtime available.
include!(concat!(env!("OUT_DIR"), "/execution_wasm_bundle.rs"));

// Make the WASM binary available.
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
Expand Down Expand Up @@ -50,6 +53,7 @@ use sp_runtime::transaction_validity::{
};
use sp_runtime::OpaqueExtrinsic;
use sp_runtime::{create_runtime_str, generic, ApplyExtrinsicResult, Perbill};
use sp_std::borrow::Cow;
use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
Expand Down Expand Up @@ -888,6 +892,10 @@ impl_runtime_apis! {
fn extrinsics_shuffling_seed(header: <Block as BlockT>::Header) -> Randomness {
extrinsics_shuffling_seed::<Block>(header)
}

fn execution_wasm_bundle() -> Cow<'static, [u8]> {
EXECUTION_WASM_BUNDLE.into()
}
}

impl sp_session::SessionKeys<Block> for Runtime {
Expand Down
11 changes: 11 additions & 0 deletions crates/subspace-wasm-tools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "subspace-wasm-tools"
description = "Utilities for building WASM bundles"
license = "Apache-2.0"
version = "0.1.0"
authors = ["Nazar Mokrynskyi <[email protected]>"]
edition = "2021"
include = [
"/src",
"/Cargo.toml",
]
43 changes: 43 additions & 0 deletions crates/subspace-wasm-tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::{env, fs};

/// Creates a `target_file_name` in `OUT_DIR` that will contain constant `bundle_const_name` with
/// runtime of `runtime_crate_name` stored in it.
///
/// Must be called before Substrate's WASM builder.
pub fn create_runtime_bundle_inclusion_file(
target_dir: &str,
runtime_crate_name: &str,
bundle_const_name: &str,
target_file_name: &str,
) {
// Make correct profile accessible in child processes.
env::set_var(
"WBUILD_PROFILE",
env::var("WBUILD_PROFILE")
.unwrap_or_else(|_| env::var("PROFILE").expect("Defined by cargo; qed")),
);

// Create a file that will include execution runtime into consensus runtime
let profile = env::var("WBUILD_PROFILE").expect("Set above; qed");
let execution_wasm_bundle_path = format!(
"{target_dir}/{profile}/wbuild/{runtime_crate_name}/{}.compact.wasm",
runtime_crate_name.replace('-', "_")
);

let execution_wasm_bundle_rs_path =
env::var("OUT_DIR").expect("Set by cargo; qed") + "/" + target_file_name;
let execution_wasm_bundle_rs_contents = format!(
r#"
pub const {bundle_const_name}: &[u8] = include_bytes!("{}");
"#,
execution_wasm_bundle_path.escape_default()
);

fs::write(
execution_wasm_bundle_rs_path,
execution_wasm_bundle_rs_contents,
)
.unwrap_or_else(|error| {
panic!("Must be able to write to {target_file_name}: {error}");
});
}
2 changes: 2 additions & 0 deletions cumulus/parachain-template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ std = [
"cirrus-pallet-executive/std",
"cirrus-primitives/std",
]
# Internal implementation detail, enabled during building of wasm blob.
wasm-builder = []
runtime-benchmarks = [
'hex-literal',
"sp-runtime/runtime-benchmarks",
Expand Down
1 change: 1 addition & 0 deletions cumulus/parachain-template/runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use substrate_wasm_builder::WasmBuilder;
fn main() {
WasmBuilder::new()
.with_current_project()
.enable_feature("wasm-builder")
.export_heap_base()
.import_memory()
.build()
Expand Down
Loading

0 comments on commit 87eed02

Please sign in to comment.