From 16456ccc86bbfa4563fbb0d7d396a3733c0eb10b Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 30 Mar 2024 15:03:36 +0100 Subject: [PATCH] chore: add spawn helpers (#7396) --- crates/rpc/rpc/src/eth/api/mod.rs | 2 ++ crates/rpc/rpc/src/eth/api/transactions.rs | 35 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/crates/rpc/rpc/src/eth/api/mod.rs b/crates/rpc/rpc/src/eth/api/mod.rs index 2828cef00..6f643b328 100644 --- a/crates/rpc/rpc/src/eth/api/mod.rs +++ b/crates/rpc/rpc/src/eth/api/mod.rs @@ -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(&self, c: C) -> EthResult where C: FnOnce(Self) -> F, diff --git a/crates/rpc/rpc/src/eth/api/transactions.rs b/crates/rpc/rpc/src/eth/api/transactions.rs index 14681558c..12580a321 100644 --- a/crates/rpc/rpc/src/eth/api/transactions.rs +++ b/crates/rpc/rpc/src/eth/api/transactions.rs @@ -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(&self, c: F) -> EthResult + where + F: Future> + 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(&self, c: F) -> EthResult + where + F: FnOnce() -> EthResult + Send + 'static, + R: Send + 'static; + /// Returns the state at the given [BlockId] fn state_at(&self, at: BlockId) -> EthResult; @@ -464,6 +483,22 @@ where self.inner.gas_cap } + async fn spawn_blocking_future(&self, c: F) -> EthResult + where + F: Future> + Send + 'static, + R: Send + 'static, + { + self.on_blocking_task(|_| c).await + } + + async fn spawn_blocking(&self, c: F) -> EthResult + where + F: FnOnce() -> EthResult + Send + 'static, + R: Send + 'static, + { + self.spawn_tracing_task_with(move |_| c()).await + } + fn state_at(&self, at: BlockId) -> EthResult { self.state_at_block_id(at) }