Skip to content

Commit

Permalink
chore: add ratelimiting
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Dec 5, 2024
1 parent 21bf045 commit f9d1d08
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
1 change: 0 additions & 1 deletion crates/optimism/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ revm.workspace = true
parking_lot.workspace = true
tokio.workspace = true
reqwest = { workspace = true, features = ["rustls-tls-native-roots"] }
async-trait.workspace = true

# rpc
jsonrpsee-core.workspace = true
Expand Down
28 changes: 13 additions & 15 deletions crates/optimism/rpc/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use alloy_consensus::Header;
use alloy_primitives::B256;
use alloy_rpc_types_debug::ExecutionWitness;
use async_trait::async_trait;
use jsonrpsee_core::RpcResult;
use jsonrpsee_core::{async_trait, RpcResult};
use op_alloy_rpc_types_engine::OpPayloadAttributes;
use reth_chainspec::ChainSpecProvider;
use reth_evm::ConfigureEvm;
Expand All @@ -16,7 +15,7 @@ pub use reth_rpc_api::DebugExecutionWitnessApiServer;
use reth_rpc_server_types::{result::internal_rpc_err, ToRpcResult};
use reth_tasks::TaskSpawner;
use std::{fmt::Debug, sync::Arc};
use tokio::sync::Semaphore;
use tokio::sync::{oneshot, Semaphore};

/// An extension to the `debug_` namespace of the RPC API.
pub struct OpDebugWitnessApi<Provider, EvmConfig> {
Expand Down Expand Up @@ -50,13 +49,14 @@ where
}
}

#[async_trait::async_trait]
#[async_trait]
impl<Provider, EvmConfig> DebugExecutionWitnessApiServer<OpPayloadAttributes>
for OpDebugWitnessApi<Provider, EvmConfig>
where
Provider: BlockReaderIdExt<Header = reth_primitives::Header>
+ StateProviderFactory
+ ChainSpecProvider<ChainSpec = OpChainSpec>
+ Clone
+ 'static,
EvmConfig: ConfigureEvm<Header = Header, Transaction = TransactionSigned> + 'static,
{
Expand All @@ -69,19 +69,17 @@ where

let parent_header = self.parent_header(parent_block_hash).to_rpc_result()?;

let provider = self.inner.provider.clone();
let builder = self.inner.builder.clone();
let (tx, rx) = oneshot::channel();
let this = self.clone();
self.inner.task_spawner.spawn_blocking(Box::pin(async move {
let res =
this.inner.builder.payload_witness(&this.inner.provider, parent_header, attributes);
let _ = tx.send(res);
}));

// Spawn the CPU-intensive work on a blocking task
self.inner
.task_spawner
.spawn_blocking(move || {
builder
.payload_witness(&provider, parent_header, attributes)
.map_err(|err| internal_rpc_err(err.to_string()))
})
.await
rx.await
.map_err(|err| internal_rpc_err(err.to_string()))?
.map_err(|err| internal_rpc_err(err.to_string()))
}
}

Expand Down

0 comments on commit f9d1d08

Please sign in to comment.