Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ratelimiting to OpWitness API #12998

Merged
merged 10 commits into from
Dec 5, 2024
7 changes: 5 additions & 2 deletions crates/optimism/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,11 @@ where
) -> eyre::Result<Self::Handle> {
let Self { rpc_add_ons, da_config } = self;
// install additional OP specific rpc methods
let debug_ext =
OpDebugWitnessApi::new(ctx.node.provider().clone(), ctx.node.evm_config().clone());
let debug_ext = OpDebugWitnessApi::new(
ctx.node.provider().clone(),
ctx.node.evm_config().clone(),
Box::new(ctx.node.task_executor().clone()),
);
let miner_ext = OpMinerExtApi::new(da_config);

rpc_add_ons
Expand Down
35 changes: 28 additions & 7 deletions crates/optimism/rpc/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use alloy_consensus::Header;
use alloy_primitives::B256;
use alloy_rpc_types_debug::ExecutionWitness;
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 @@ -13,7 +13,9 @@ use reth_primitives::{SealedHeader, TransactionSigned};
use reth_provider::{BlockReaderIdExt, ProviderError, ProviderResult, StateProviderFactory};
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::{oneshot, Semaphore};

/// An extension to the `debug_` namespace of the RPC API.
pub struct OpDebugWitnessApi<Provider, EvmConfig> {
Expand All @@ -22,9 +24,14 @@ pub struct OpDebugWitnessApi<Provider, EvmConfig> {

impl<Provider, EvmConfig> OpDebugWitnessApi<Provider, EvmConfig> {
/// Creates a new instance of the `OpDebugWitnessApi`.
pub fn new(provider: Provider, evm_config: EvmConfig) -> Self {
pub fn new(
provider: Provider,
evm_config: EvmConfig,
task_spawner: Box<dyn TaskSpawner>,
) -> Self {
let builder = OpPayloadBuilder::new(evm_config);
let inner = OpDebugWitnessApiInner { provider, builder };
let semaphore = Arc::new(Semaphore::new(3));
let inner = OpDebugWitnessApiInner { provider, builder, task_spawner, semaphore };
Self { inner: Arc::new(inner) }
}
}
Expand All @@ -42,24 +49,36 @@ where
}
}

#[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,
{
fn execute_payload(
async fn execute_payload(
&self,
parent_block_hash: B256,
attributes: OpPayloadAttributes,
) -> RpcResult<ExecutionWitness> {
let _permit = self.inner.semaphore.acquire().await;

let parent_header = self.parent_header(parent_block_hash).to_rpc_result()?;
self.inner
.builder
.payload_witness(&self.inner.provider, parent_header, attributes)

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);
}));

rx.await
.map_err(|err| internal_rpc_err(err.to_string()))?
.map_err(|err| internal_rpc_err(err.to_string()))
}
}
Expand All @@ -78,4 +97,6 @@ impl<Provider, EvmConfig> Debug for OpDebugWitnessApi<Provider, EvmConfig> {
struct OpDebugWitnessApiInner<Provider, EvmConfig> {
provider: Provider,
builder: OpPayloadBuilder<EvmConfig>,
task_spawner: Box<dyn TaskSpawner>,
semaphore: Arc<Semaphore>,
}
4 changes: 2 additions & 2 deletions crates/rpc/rpc-api/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ pub trait DebugExecutionWitnessApi<Attributes> {
///
/// The first argument is the parent block hash. The second argument is the payload
/// attributes for the new block.
#[method(name = "executePayload", blocking)]
fn execute_payload(
#[method(name = "executePayload")]
async fn execute_payload(
&self,
parent_block_hash: B256,
attributes: Attributes,
Expand Down
Loading