From c8d8b66d0ad695495bf3ea5d551ccdc1c3933312 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 12 Feb 2022 17:40:09 +0100 Subject: [PATCH] feat(solc): use svm blocking feature (#904) * feat(solc): use svm blocking feature * chore: bump ethers * fix: enable svm blocking feature Co-authored-by: Georgios Konstantopoulos --- Cargo.lock | 10 ++--- ethers-solc/Cargo.toml | 7 +-- ethers-solc/src/compile/mod.rs | 78 ++++------------------------------ 3 files changed, 18 insertions(+), 77 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0bded85b..a54e4064 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3475,8 +3475,9 @@ dependencies = [ [[package]] name = "solang-parser" -version = "0.1.1" -source = "git+https://github.com/hyperledger-labs/solang#b286b63d8003a17092e0ae54d89898970df6d6fa" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec12f31165397a3b145b0b06bc47470226ac9bf52b9aa8ea1ab7aa6e74809b39" dependencies = [ "lalrpop", "lalrpop-util", @@ -3536,9 +3537,8 @@ checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" [[package]] name = "svm-rs" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01ebb94785ad8eecc53c119257322f72afd3e3ab5016e74b2b2b143449107135" +version = "0.2.8" +source = "git+https://github.com/roynalnaruto/svm-rs#9b3627cfde2763fdc35afa9cbcea8ebc2926938d" dependencies = [ "anyhow", "cfg-if 1.0.0", diff --git a/ethers-solc/Cargo.toml b/ethers-solc/Cargo.toml index 22d8a566..2425d0ae 100644 --- a/ethers-solc/Cargo.toml +++ b/ethers-solc/Cargo.toml @@ -35,13 +35,14 @@ tempfile = { version = "3.3.0", optional = true } fs_extra = { version = "1.2.0", optional = true } sha2 = { version = "0.9.8", default-features = false } dunce = "1.0.2" -solang-parser = { git = "https://github.com/hyperledger-labs/solang", default-features = false } +solang-parser = { default-features = false, version = "0.1.2" } rayon = "1.5.1" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] home = "0.5.3" # SVM is not WASM compatible yet. -svm = { package = "svm-rs", default-features = false, version = "0.2.6", optional = true } +# svm = { package = "svm-rs", default-features = false, version = "0.2.7", optional = true } +svm = { package = "svm-rs", default-features = false, git = "https://github.com/roynalnaruto/svm-rs", optional = true, features = ["blocking"] } [target.'cfg(target_arch = "wasm32")'.dependencies] # NOTE: this enables wasm compatibility for getrandom indirectly @@ -72,7 +73,7 @@ required-features = ["project-util"] [features] default = ["rustls"] async = ["tokio", "futures-util"] -full = ["async", "svm"] +full = ["async", "svm", "svm/blocking"] # Utilities for creating and testing project workspaces project-util = ["tempfile", "fs_extra"] tests = [] diff --git a/ethers-solc/src/compile/mod.rs b/ethers-solc/src/compile/mod.rs index cf04bd3a..2ced3f05 100644 --- a/ethers-solc/src/compile/mod.rs +++ b/ethers-solc/src/compile/mod.rs @@ -52,32 +52,6 @@ use std::sync::Mutex; #[allow(unused)] static LOCK: Lazy> = Lazy::new(|| Mutex::new(())); -#[cfg(all(feature = "svm", feature = "async"))] -#[allow(clippy::large_enum_variant)] -pub enum RuntimeOrHandle { - Runtime(tokio::runtime::Runtime), - Handle(tokio::runtime::Handle), -} - -#[cfg(all(feature = "svm", feature = "async"))] -impl Default for RuntimeOrHandle { - fn default() -> Self { - Self::new() - } -} - -#[cfg(all(feature = "svm", feature = "async"))] -impl RuntimeOrHandle { - pub fn new() -> RuntimeOrHandle { - match tokio::runtime::Handle::try_current() { - Ok(handle) => RuntimeOrHandle::Handle(handle), - Err(_) => RuntimeOrHandle::Runtime( - tokio::runtime::Runtime::new().expect("Failed to start runtime"), - ), - } - } -} - /// take the lock in tests, we use this to enforce that /// a test does not run while a compiler version is being installed /// @@ -90,55 +64,21 @@ pub(crate) fn take_solc_installer_lock() -> std::sync::MutexGuard<'static, ()> { LOCK.lock().unwrap() } -#[cfg(all(feature = "svm", feature = "async"))] /// A list of upstream Solc releases, used to check which version /// we should download. /// The boolean value marks whether there was an error. -pub static RELEASES: Lazy<(svm::Releases, Vec, bool)> = Lazy::new(|| { - // Try to download the releases, if it fails default to empty - let releases_result = match RuntimeOrHandle::new() { - RuntimeOrHandle::Runtime(runtime) => - // we do not degrade startup performance if the consumer has a weak network? - // use a 3 sec timeout for the request which should still be fine for slower connections - { - runtime.block_on(async { - tokio::time::timeout( - std::time::Duration::from_millis(3000), - svm::all_releases(svm::platform()), - ) - .await - }) +#[cfg(all(feature = "svm"))] +pub static RELEASES: once_cell::sync::Lazy<(svm::Releases, Vec, bool)> = + once_cell::sync::Lazy::new(|| match svm::blocking_all_releases(svm::platform()) { + Ok(releases) => { + let sorted_versions = releases.clone().into_versions(); + (releases, sorted_versions, true) } - RuntimeOrHandle::Handle(handle) => - // we do not degrade startup performance if the consumer has a weak network? - // use a 3 sec timeout for the request which should still be fine for slower connections - { - handle.block_on(async { - tokio::time::timeout( - std::time::Duration::from_millis(3000), - svm::all_releases(svm::platform()), - ) - .await - }) - } - }; - - match releases_result { - Ok(Ok(releases)) => { - let mut sorted_releases = releases.releases.keys().cloned().collect::>(); - sorted_releases.sort(); - (releases, sorted_releases, true) - } - Ok(Err(err)) => { + Err(err) => { tracing::error!("{:?}", err); (svm::Releases::default(), Vec::new(), false) } - Err(err) => { - tracing::error!("Releases request timed out: {:?}", err); - (svm::Releases::default(), Vec::new(), false) - } - } -}); + }); /// A `Solc` version is either installed (available locally) or can be downloaded, from the remote /// endpoint @@ -434,7 +374,7 @@ impl Solc { pub fn blocking_install(version: &Version) -> std::result::Result<(), svm::SolcVmError> { tracing::trace!("blocking installing solc version \"{}\"", version); crate::report::solc_installation_start(version); - tokio::runtime::Runtime::new().unwrap().block_on(svm::install(version))?; + svm::blocking_install(version)?; crate::report::solc_installation_success(version); Ok(()) }