Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check runtime dynamically #1135

Merged
merged 8 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
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
40 changes: 28 additions & 12 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)]
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
Loading