Skip to content

Commit

Permalink
Check runtime dynamically (#1135)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wtdcode authored Mar 1, 2024
1 parent 48e26fb commit ef64e4d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

2 changes: 0 additions & 2 deletions crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -78,7 +77,6 @@ negate-optimism-default-handler = [
ethersdb = [
"std",
"tokio",
"futures",
"ethers-providers",
"ethers-core",
] # Negate optimism default handler
Expand Down
42 changes: 29 additions & 13 deletions crates/revm/src/db/ethersdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<M: Middleware> {
client: Arc<M>,
runtime: Option<Runtime>,
block_number: Option<BlockId>,
}

impl<M: Middleware> EthersDB<M> {
/// create ethers db connector inputs are url and block on what we are basing our database (None for latest)
pub fn new(client: Arc<M>, block_number: Option<BlockId>) -> Option<Self> {
let runtime = Handle::try_current()
.is_err()
.then(|| Runtime::new().unwrap());

let client = client;

let mut out = Self {
client,
runtime,
block_number: None,
};

Expand All @@ -39,10 +32,33 @@ impl<M: Middleware> EthersDB<M> {
}

/// internal utility function to call tokio feature and wait for output
fn block_on<F: core::future::Future>(&self, f: F) -> F::Output {
match &self.runtime {
Some(runtime) => runtime.block_on(f),
None => futures::executor::block_on(f),
fn block_on<F>(&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),
}
}
}
Expand Down

0 comments on commit ef64e4d

Please sign in to comment.