feat: Add ratelimiting to OpWitness API (#12998)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Steven
2024-12-05 09:42:52 -06:00
committed by GitHub
parent eb4aa2c077
commit d71a4be982
3 changed files with 35 additions and 11 deletions

View File

@ -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

View File

@ -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;
@ -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> {
@ -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) }
}
}
@ -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()))
}
}
@ -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>,
}

View File

@ -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,