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

fix(solc): add RuntimeOrHandle & fix solc blocking installation #1260

Merged
merged 8 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions ethers-solc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ serde_json = "1.0.68"
serde = { version = "1.0.130", features = ["derive"] }
semver = { version = "1.0.9", features = ["serde"] }
walkdir = "2.3.2"
tokio = { version = "1.15.0", default-features = false, features = ["process", "io-util", "fs", "time"], optional = true }
tokio = { version = "1.15.0", default-features = false, features = ["process", "io-util", "fs", "time"] }
rkrasiuk marked this conversation as resolved.
Show resolved Hide resolved
futures-util = { version = "^0.3", optional = true }
once_cell = "1.10.0"
regex = "1.5.5"
Expand Down Expand Up @@ -80,7 +80,7 @@ required-features = ["full", "project-util"]

[features]
default = ["rustls"]
async = ["tokio", "futures-util"]
async = ["futures-util"]
full = ["async", "svm-solc"]
svm-solc = ["svm/blocking", "svm-builds", "sha2"]
# Utilities for creating and testing project workspaces
Expand Down
15 changes: 14 additions & 1 deletion ethers-solc/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,11 @@ impl Solc {
/// Blocking version of `Self::install`
#[cfg(all(feature = "svm-solc"))]
pub fn blocking_install(version: &Version) -> std::result::Result<Self, svm::SolcVmError> {
rkrasiuk marked this conversation as resolved.
Show resolved Hide resolved
use crate::utils::RuntimeOrHandle;

tracing::trace!("blocking installing solc version \"{}\"", version);
crate::report::solc_installation_start(version);
match svm::blocking_install(version) {
match RuntimeOrHandle::new().block_on(svm::install(version)) {
Ok(path) => {
crate::report::solc_installation_success(version);
Ok(Solc::new(path))
Expand Down Expand Up @@ -799,6 +801,17 @@ mod tests {
assert_eq!(res.solc, expected);
}

#[test]
#[cfg(feature = "svm-solc")]
fn can_install_solc_in_tokio_rt() {
let ver = "0.8.6";
let version = Version::from_str(ver).unwrap();
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(async { Solc::blocking_install(&version) });
rkrasiuk marked this conversation as resolved.
Show resolved Hide resolved
assert!(result.is_ok());
}

#[test]
fn does_not_find_not_installed_version() {
let ver = "1.1.1";
Expand Down
30 changes: 30 additions & 0 deletions ethers-solc/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use regex::{Match, Regex};
use semver::Version;
use serde::de::DeserializeOwned;
use tiny_keccak::{Hasher, Keccak};
use tokio::runtime::{Handle, Runtime};
use walkdir::WalkDir;

/// A regex that matches the import path and identifier of a solidity import
Expand Down Expand Up @@ -330,6 +331,35 @@ pub(crate) fn find_fave_or_alt_path(root: impl AsRef<Path>, fave: &str, alt: &st
p
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub enum RuntimeOrHandle {
Runtime(Runtime),
Handle(Handle),
}

impl Default for RuntimeOrHandle {
fn default() -> Self {
Self::new()
}
}

impl RuntimeOrHandle {
pub fn new() -> RuntimeOrHandle {
match Handle::try_current() {
Ok(handle) => RuntimeOrHandle::Handle(handle),
Err(_) => RuntimeOrHandle::Runtime(Runtime::new().expect("Failed to start runtime")),
}
}

pub fn block_on<F: std::future::Future>(&self, f: F) -> F::Output {
match &self {
RuntimeOrHandle::Runtime(runtime) => runtime.block_on(f),
RuntimeOrHandle::Handle(handle) => tokio::task::block_in_place(|| handle.block_on(f)),
}
}
}

/// Creates a new named tempdir
#[cfg(any(test, feature = "project-util"))]
pub(crate) fn tempdir(name: &str) -> Result<tempfile::TempDir, SolcIoError> {
Expand Down