chore: add spawn helpers (#7396)

This commit is contained in:
Matthias Seitz
2024-03-30 15:03:36 +01:00
committed by GitHub
parent 87c4ae744a
commit 16456ccc86
2 changed files with 37 additions and 0 deletions

View File

@ -166,6 +166,8 @@ where
///
/// This accepts a closure that creates a new future using a clone of this type and spawns the
/// future onto a new task that is allowed to block.
///
/// Note: This is expected for futures that are dominated by blocking IO operations.
pub(crate) async fn on_blocking_task<C, F, R>(&self, c: C) -> EthResult<R>
where
C: FnOnce(Self) -> F,

View File

@ -47,6 +47,7 @@ use revm::{
},
Inspector,
};
use std::future::Future;
#[cfg(feature = "optimism")]
use crate::eth::api::optimism::OptimismTxMeta;
@ -88,6 +89,24 @@ pub trait EthTransactions: Send + Sync {
/// Returns default gas limit to use for `eth_call` and tracing RPC methods.
fn call_gas_limit(&self) -> u64;
/// Executes the future on a new blocking task.
///
/// Note: This is expected for futures that are dominated by blocking IO operations, for tracing
/// or CPU bound operations in general use [Self::spawn_blocking].
async fn spawn_blocking_future<F, R>(&self, c: F) -> EthResult<R>
where
F: Future<Output = EthResult<R>> + Send + 'static,
R: Send + 'static;
/// Executes a blocking on the tracing pol.
///
/// Note: This is expected for futures that are predominantly CPU bound, for blocking IO futures
/// use [Self::spawn_blocking_future].
async fn spawn_blocking<F, R>(&self, c: F) -> EthResult<R>
where
F: FnOnce() -> EthResult<R> + Send + 'static,
R: Send + 'static;
/// Returns the state at the given [BlockId]
fn state_at(&self, at: BlockId) -> EthResult<StateProviderBox>;
@ -464,6 +483,22 @@ where
self.inner.gas_cap
}
async fn spawn_blocking_future<F, R>(&self, c: F) -> EthResult<R>
where
F: Future<Output = EthResult<R>> + Send + 'static,
R: Send + 'static,
{
self.on_blocking_task(|_| c).await
}
async fn spawn_blocking<F, R>(&self, c: F) -> EthResult<R>
where
F: FnOnce() -> EthResult<R> + Send + 'static,
R: Send + 'static,
{
self.spawn_tracing_task_with(move |_| c()).await
}
fn state_at(&self, at: BlockId) -> EthResult<StateProviderBox> {
self.state_at_block_id(at)
}