From edb413f95d4030b5167178cecf435cc131be66d9 Mon Sep 17 00:00:00 2001 From: lazymio Date: Fri, 1 Mar 2024 09:22:15 +0800 Subject: [PATCH] Check runtime dynamically (#1135) * Check runtime dynamically * Drop futures as clippy suggests * Fix panic * Format * Support current_thread runtime * Revert unnecessary changes * Add a few documents * derive Clone thanks to removing runtime field --- Cargo.lock | 1 - crates/revm/Cargo.toml | 2 -- crates/revm/src/db/ethersdb.rs | 42 +++++++++++++++++++++++----------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fb382c4fe8..ec7ba04563 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2324,7 +2324,6 @@ dependencies = [ "ethers-contract", "ethers-core", "ethers-providers", - "futures", "indicatif", "revm-interpreter", "revm-precompile", diff --git a/crates/revm/Cargo.toml b/crates/revm/Cargo.toml index 3855d4d5d9..ddb4757343 100644 --- a/crates/revm/Cargo.toml +++ b/crates/revm/Cargo.toml @@ -38,7 +38,6 @@ tokio = { version = "1.36", features = [ ], optional = true } ethers-providers = { version = "2.0", optional = true } ethers-core = { version = "2.0", optional = true } -futures = { version = "0.3.30", optional = true } [dev-dependencies] ethers-contract = { version = "2.0.13", default-features = false } @@ -78,7 +77,6 @@ negate-optimism-default-handler = [ ethersdb = [ "std", "tokio", - "futures", "ethers-providers", "ethers-core", ] # Negate optimism default handler diff --git a/crates/revm/src/db/ethersdb.rs b/crates/revm/src/db/ethersdb.rs index 866aab8abd..ce8a6c89e1 100644 --- a/crates/revm/src/db/ethersdb.rs +++ b/crates/revm/src/db/ethersdb.rs @@ -3,27 +3,20 @@ use crate::{Database, DatabaseRef}; use ethers_core::types::{BlockId, H160 as eH160, H256, U64 as eU64}; use ethers_providers::Middleware; use std::sync::Arc; -use tokio::runtime::{Handle, Runtime}; +use tokio::runtime::{Builder, Handle, RuntimeFlavor}; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct EthersDB { client: Arc, - runtime: Option, block_number: Option, } impl EthersDB { /// create ethers db connector inputs are url and block on what we are basing our database (None for latest) pub fn new(client: Arc, block_number: Option) -> Option { - let runtime = Handle::try_current() - .is_err() - .then(|| Runtime::new().unwrap()); - let client = client; - let mut out = Self { client, - runtime, block_number: None, }; @@ -39,10 +32,33 @@ impl EthersDB { } /// internal utility function to call tokio feature and wait for output - fn block_on(&self, f: F) -> F::Output { - match &self.runtime { - Some(runtime) => runtime.block_on(f), - None => futures::executor::block_on(f), + fn block_on(&self, f: F) -> F::Output + where + F: core::future::Future + Send, + F::Output: Send, + { + match Handle::try_current() { + Ok(handle) => match handle.runtime_flavor() { + // This essentially equals to tokio::task::spawn_blocking because tokio doesn't + // allow current_thread runtime to block_in_place + RuntimeFlavor::CurrentThread => std::thread::scope(move |s| { + s.spawn(move || { + Builder::new_current_thread() + .enable_all() + .build() + .unwrap() + .block_on(f) + }) + .join() + .unwrap() + }), + _ => tokio::task::block_in_place(move || handle.block_on(f)), + }, + Err(_) => Builder::new_current_thread() + .enable_all() + .build() + .unwrap() + .block_on(f), } } }