Skip to content

Commit

Permalink
Merge pull request #1 from open-web3-stack/refactor
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
ermalkaleci authored Aug 31, 2023
2 parents 9cf8d88 + 4ddfa9a commit ef5baa2
Show file tree
Hide file tree
Showing 15 changed files with 1,115 additions and 843 deletions.
995 changes: 572 additions & 423 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@ resolver = "2"

[profile.dev]
split-debuginfo = "unpacked"

[workspace.dependencies]
sp-core = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sp-std = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sp-io = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sp-runtime-interface = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sp-runtime = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sp-state-machine = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sc-executor = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sc-executor-common = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sc-client-db = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sp-maybe-compressed-blob = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
frame-support = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
frame-system = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sp-externalities = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
sp-storage = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
pallet-balances = { git="https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", default-features = false }
36 changes: 21 additions & 15 deletions bencher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ edition = "2021"
[dependencies]
paste = "^1.0"
build-helper = { version = "^0.1", optional = true }
cargo_metadata = { version = "^0.15", optional = true }
cargo_metadata = { version = "^0.17", optional = true }
strum = { version = "^0.25", features = ["derive"], optional = true }
filetime = { version = "^0.2", optional = true }
wasm-opt = { version = "^0.112", optional = true }
parity-wasm = { version = "0.45", optional = true }
tempfile = { version = "^3.5", optional = true }
toml = { version = "^0.7", optional = true }
walkdir = { version = "^2.3", optional = true }
Expand All @@ -22,29 +26,32 @@ parking_lot = { version = "^0.12", optional = true }
thiserror = { version = "^1.0", optional = true }
serde = { version = "^1.0", optional = true, features = ['derive'] }
serde_json = {version = "^1.0", optional = true }
hash-db = { version = "^0.16", default-features = false, optional = true }
wasm-bencher-procedural = { path = "bencher-procedural", version = "*", default-features = false }
codec = { package = "parity-scale-codec", version = "^3.0", features = ["derive"], default-features = false }

sp-core = {version = "20.0.0", default-features = false }
sp-std = { version = "7.0.0", default-features = false }
sp-io = { version = "22.0.0", default-features = false }
sp-runtime-interface = { version = "16.0.0", default-features = false }
sp-state-machine = { version = "0.27.0", default-features = false, optional = true }
sc-executor = { version = "0.25.0", default-features = false, optional = true }
sc-executor-common = { version = "0.22.0", default-features = false, optional = true }
sc-client-db = { version = "0.28.0", default-features = false, features = ["rocksdb"], optional = true }
sp-maybe-compressed-blob = { version = "5.0.0", default-features = false, optional = true }
frame-support = { version = "21.0.0", default-features = false }
sp-externalities = { version = "0.18.0", default-features = false }
sp-storage = { version = "12.0.0", default-features = false, optional = true }
sp-core = { workspace = true, default-features = false }
sp-std = { workspace = true, default-features = false }
sp-io = { workspace = true, default-features = false }
sp-runtime-interface = { workspace = true, default-features = false }
sp-state-machine = { workspace = true, default-features = false, optional = true }
sc-executor = { workspace = true, default-features = false, optional = true }
sc-executor-common = { workspace = true, default-features = false, optional = true }
sc-client-db = { workspace = true, default-features = false, features = ["rocksdb"], optional = true }
sp-maybe-compressed-blob = { workspace = true, default-features = false, optional = true }
frame-support = { workspace = true, default-features = false }
sp-externalities = { workspace = true, default-features = false }
sp-storage = { workspace = true, default-features = false, optional = true }

[features]
default = ["std"]
std = [
"wasm-bencher-procedural/std",
"build-helper",
"cargo_metadata",
"strum",
"filetime",
"wasm-opt",
"parity-wasm",
"tempfile",
"toml",
"walkdir",
Expand All @@ -56,7 +63,6 @@ std = [
"thiserror",
"serde/std",
"serde_json/std",
"hash-db/std",
"codec/std",
"sp-core/std",
"sp-std/std",
Expand Down
2 changes: 1 addition & 1 deletion bencher/bencher-procedural/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "^1.0"
quote = "^1.0"
syn = { version = "^1.0", features = ["full"] }
syn = { version = "^2.0", features = ["full"] }

[features]
default = ["std"]
Expand Down
15 changes: 5 additions & 10 deletions bencher/src/bench_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{
bench_ext::BenchExt,
tracker::{BenchTracker, BenchTrackerExt},
};
use sc_executor::{WasmExecutionMethod, WasmExecutor, WasmtimeInstantiationStrategy};
use sc_executor::WasmExecutor;
use sc_executor_common::runtime_blob::RuntimeBlob;
use sp_externalities::Extensions;
use sp_state_machine::{Ext, OverlayedChanges, StorageTransactionCache};
Expand All @@ -19,15 +19,10 @@ type ComposeHostFunctions = (
);

fn executor() -> WasmExecutor<ComposeHostFunctions> {
WasmExecutor::<ComposeHostFunctions>::new(
WasmExecutionMethod::Compiled {
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
},
Default::default(),
1,
None,
0,
)
WasmExecutor::<ComposeHostFunctions>::builder()
.with_max_runtime_instances(1)
.with_runtime_cache_size(0)
.build()
}

/// Run benches
Expand Down
107 changes: 99 additions & 8 deletions bencher/src/build_wasm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,103 @@
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{
fs,
path::{Path, PathBuf},
process::Command,
};

pub mod prerequisites;
pub mod wasm_project;
mod prerequisites;
mod wasm_project;

/// Environment variable to disable color output of the wasm build.
const WASM_BUILD_NO_COLOR: &str = "WASM_BUILD_NO_COLOR";
/// Environment variable that tells us to skip building the wasm binary.
const SKIP_BUILD_ENV: &str = "SKIP_WASM_BUILD";

/// Returns `true` when color output is enabled.
pub fn color_output_enabled() -> bool {
std::env::var(WASM_BUILD_NO_COLOR).is_err()
/// Environment variable that tells us whether we should avoid network requests
const OFFLINE: &str = "CARGO_NET_OFFLINE";

/// Environment variable to force a certain build type when building the wasm binary.
/// Expects "debug", "release" or "production" as value.
///
/// When unset the WASM binary uses the same build type as the main cargo build with
/// the exception of a debug build: In this case the wasm build defaults to `release` in
/// order to avoid a slowdown when not explicitly requested.
const WASM_BUILD_TYPE_ENV: &str = "WASM_BUILD_TYPE";

/// Environment variable to extend the `RUSTFLAGS` variable given to the wasm build.
const WASM_BUILD_RUSTFLAGS_ENV: &str = "WASM_BUILD_RUSTFLAGS";

/// Environment variable to set the target directory to copy the final wasm binary.
///
/// The directory needs to be an absolute path.
const WASM_TARGET_DIRECTORY: &str = "WASM_TARGET_DIRECTORY";

/// Environment variable that hints the workspace we are building.
const WASM_BUILD_WORKSPACE_HINT: &str = "WASM_BUILD_WORKSPACE_HINT";

/// Write to the given `file` if the `content` is different.
fn write_file_if_changed(file: impl AsRef<Path>, content: impl AsRef<str>) {
if fs::read_to_string(file.as_ref()).ok().as_deref() != Some(content.as_ref()) {
fs::write(file.as_ref(), content.as_ref())
.unwrap_or_else(|_| panic!("Writing `{}` can not fail!", file.as_ref().display()));
}
}

/// Copy `src` to `dst` if the `dst` does not exist or is different.
fn copy_file_if_changed(src: PathBuf, dst: PathBuf) {
let src_file = fs::read_to_string(&src).ok();
let dst_file = fs::read_to_string(&dst).ok();

if src_file != dst_file {
fs::copy(&src, &dst).unwrap_or_else(|_| {
panic!(
"Copying `{}` to `{}` can not fail; qed",
src.display(),
dst.display()
)
});
}
}

/// Wraps a specific command which represents a cargo invocation.
#[derive(Debug)]
pub struct CargoCommand {
program: String,
}

impl CargoCommand {
fn new(program: &str) -> Self {
CargoCommand {
program: program.into(),
}
}

fn command(&self) -> Command {
Command::new(&self.program)
}
}

/// Wraps a [`CargoCommand`] and the version of `rustc` the cargo command uses.
pub struct CargoCommandVersioned {
command: CargoCommand,
version: String,
}

impl CargoCommandVersioned {
pub fn new(command: CargoCommand, version: String) -> Self {
Self { command, version }
}

/// Returns the `rustc` version.
pub fn rustc_version(&self) -> &str {
&self.version
}
}

impl std::ops::Deref for CargoCommandVersioned {
type Target = CargoCommand;

fn deref(&self) -> &CargoCommand {
&self.command
}
}

pub fn build() -> std::io::Result<Vec<u8>> {
Expand All @@ -21,8 +110,9 @@ pub fn build() -> std::io::Result<Vec<u8>> {
.map(char::from)
.collect::<String>();

let profile = wasm_project::Profile::detect(&std::env::current_dir()?).name();
let mut out_dir = std::path::PathBuf::from(manifest_dir);
out_dir.push(format!("target/release/build/{}-{}/out", pkg_name, random));
out_dir.push(format!("target/{profile}/build/{pkg_name}-{random}/out"));

std::env::set_var("OUT_DIR", out_dir.display().to_string());

Expand All @@ -44,6 +134,7 @@ pub fn build() -> std::io::Result<Vec<u8>> {
cargo_cmd,
vec!["wasm-bench".to_string()],
None,
false,
);

let (wasm_binary, _wasm_binary_bloaty) = if let Some(wasm_binary) = wasm_binary {
Expand Down
Loading

0 comments on commit ef5baa2

Please sign in to comment.