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 2 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
19 changes: 8 additions & 11 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};

#[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 @@ -40,9 +33,13 @@ 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),
match Handle::try_current() {
Ok(handle) => handle.block_on(f),
Copy link
Collaborator

Choose a reason for hiding this comment

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

this will still panic if called from the main Runtime::block_on thread.

what we want here is

tokio::task::block_in_place(move || {
    handle.block_on(f)
});

Copy link
Contributor Author

@wtdcode wtdcode Feb 29, 2024

Choose a reason for hiding this comment

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

Yes, I just noticed the problem. Actually, I think it looks impossible to run a future in a non-async function within a tokio runtime (?.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I tried this works indeed. But block_in_place panic for new_current_thread therefore we have to handle that too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mattsse

I investigated a bit. It looks like we can't support current_thread due to not being able to blocking_in_place without changing the signature of the Database(Ref) traits. I have two options here:

  1. Document clearly that, EthersDB can't be used with current_thread.
  2. Less preferably, we start another thread and create another runtime to do it.

Which one do you prefer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done with 2 so we can support all use cases, could you have a look? @mattsse

Copy link
Member

Choose a reason for hiding this comment

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

There is issue related to this, not sure if you have looked at it: #1056

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I tested this addresses #1056 cleanly.

Err(_) => Builder::new_current_thread()
.enable_all()
.build()
.expect("Fail to build current_thread runtime")
.block_on(f),
}
}
}
Expand Down
Loading