Skip to content

Commit

Permalink
Check runtime dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
wtdcode committed Feb 29, 2024
1 parent a0e45a5 commit 63250ab
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 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};

#[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),
Err(_) => Builder::new_current_thread()
.enable_all()
.build()
.expect("Fail to build current_thread runtime")
.block_on(f),
}
}
}
Expand Down Expand Up @@ -134,8 +131,8 @@ mod tests {
use super::*;
use ethers_providers::{Http, Provider};

//#[test]
fn _can_get_basic() {
#[test]
fn test_can_get_basic() {
let client = Provider::<Http>::try_from(
"https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27",
)
Expand Down

0 comments on commit 63250ab

Please sign in to comment.