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

feat(solc): use svm blocking feature #904

Merged
merged 3 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions ethers-solc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

driveby

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
Expand Down Expand Up @@ -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 = []
Expand Down
78 changes: 9 additions & 69 deletions ethers-solc/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,6 @@ use std::sync::Mutex;
#[allow(unused)]
static LOCK: Lazy<Mutex<()>> = 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
///
Expand All @@ -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<Version>, 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
})
}
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::<Vec<Version>>();
sorted_releases.sort();
(releases, sorted_releases, true)
}
Ok(Err(err)) => {
tracing::error!("{:?}", err);
(svm::Releases::default(), Vec::new(), false)
#[cfg(all(feature = "svm"))]
pub static RELEASES: once_cell::sync::Lazy<(svm::Releases, Vec<Version>, 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)
}
Err(err) => {
tracing::error!("Releases request timed out: {:?}", err);
tracing::error!("{:?}", err);
(svm::Releases::default(), Vec::new(), false)
}
}
});
});

/// A `Solc` version is either installed (available locally) or can be downloaded, from the remote
/// endpoint
Expand Down Expand Up @@ -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(())
}
Expand Down