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 905badc
Showing 1 changed file with 8 additions and 11 deletions.
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),
Err(_) => Builder::new_current_thread()
.enable_all()
.build()
.expect("Fail to build current_thread runtime")
.block_on(f),
}
}
}
Expand Down

0 comments on commit 905badc

Please sign in to comment.