From 2ae6e6efafe4df83110f5067eec602755994b22e Mon Sep 17 00:00:00 2001 From: Ash Kunda <18058966+akundaz@users.noreply.github.com> Date: Fri, 8 Aug 2025 12:05:35 -0400 Subject: [PATCH] combine eth api modifications --- crates/op-rbuilder/src/launcher.rs | 15 +-- crates/op-rbuilder/src/revert_protection.rs | 96 +++++++------------ .../src/tests/framework/instance.rs | 16 ++-- 3 files changed, 49 insertions(+), 78 deletions(-) diff --git a/crates/op-rbuilder/src/launcher.rs b/crates/op-rbuilder/src/launcher.rs index 6ada7e29c..7379c91d8 100644 --- a/crates/op-rbuilder/src/launcher.rs +++ b/crates/op-rbuilder/src/launcher.rs @@ -7,7 +7,7 @@ use crate::{ metrics::{record_flag_gauge_metrics, VERSION}, monitor_tx_pool::monitor_tx_pool, primitives::reth::engine_api_builder::OpEngineApiBuilder, - revert_protection::{EthApiExtServer, EthApiOverrideServer, RevertProtectionExt}, + revert_protection::{EthApiExtServer, RevertProtectionExt}, tx::FBPooledTransaction, }; use core::fmt::Debug; @@ -149,14 +149,15 @@ where let pool = ctx.pool().clone(); let provider = ctx.provider().clone(); - let revert_protection_ext = - RevertProtectionExt::new(pool, provider, ctx.registry.eth_api().clone()); + let revert_protection_ext = RevertProtectionExt::new( + pool, + provider, + ctx.registry.eth_api().clone(), + reverted_cache, + ); ctx.modules - .merge_configured(revert_protection_ext.bundle_api().into_rpc())?; - ctx.modules.replace_configured( - revert_protection_ext.eth_api(reverted_cache).into_rpc(), - )?; + .add_or_replace_configured(revert_protection_ext.into_rpc())?; } Ok(()) diff --git a/crates/op-rbuilder/src/revert_protection.rs b/crates/op-rbuilder/src/revert_protection.rs index 13b887e89..0d497017e 100644 --- a/crates/op-rbuilder/src/revert_protection.rs +++ b/crates/op-rbuilder/src/revert_protection.rs @@ -19,21 +19,13 @@ use reth_rpc_eth_types::{utils::recover_raw_transaction, EthApiError}; use reth_transaction_pool::{PoolTransaction, TransactionOrigin, TransactionPool}; use tracing::error; -// We have to split the RPC modules in two sets because we have methods that both -// replace an existing method and add a new one. -// Tracking change in Reth here to have a single method for both: -// https://github.com/paradigmxyz/reth/issues/16502 - // Namespace overrides for revert protection support #[cfg_attr(not(test), rpc(server, namespace = "eth"))] #[cfg_attr(test, rpc(server, client, namespace = "eth"))] -pub trait EthApiExt { +pub trait EthApiExt { #[method(name = "sendBundle")] async fn send_bundle(&self, tx: Bundle) -> RpcResult; -} -#[rpc(server, client, namespace = "eth")] -pub trait EthApiOverride { #[method(name = "getTransactionReceipt")] async fn transaction_receipt(&self, hash: B256) -> RpcResult>; } @@ -43,6 +35,7 @@ pub struct RevertProtectionExt { provider: Provider, eth_api: Eth, metrics: Arc, + reverted_cache: Cache, } impl RevertProtectionExt @@ -51,42 +44,29 @@ where Provider: Clone, Eth: Clone, { - pub fn new(pool: Pool, provider: Provider, eth_api: Eth) -> Self { + pub fn new( + pool: Pool, + provider: Provider, + eth_api: Eth, + reverted_cache: Cache, + ) -> Self { Self { pool, provider, eth_api, metrics: Arc::new(OpRBuilderMetrics::default()), - } - } - - pub fn bundle_api(&self) -> RevertProtectionBundleAPI { - RevertProtectionBundleAPI { - pool: self.pool.clone(), - provider: self.provider.clone(), - metrics: self.metrics.clone(), - } - } - - pub fn eth_api(&self, reverted_cache: Cache) -> RevertProtectionEthAPI { - RevertProtectionEthAPI { - eth_api: self.eth_api.clone(), reverted_cache, } } } -pub struct RevertProtectionBundleAPI { - pool: Pool, - provider: Provider, - metrics: Arc, -} - #[async_trait] -impl EthApiExtServer for RevertProtectionBundleAPI +impl EthApiExtServer> + for RevertProtectionExt where Pool: TransactionPool + Clone + 'static, Provider: StateProviderFactory + Send + Sync + Clone + 'static, + Eth: FullEthApi + Send + Sync + Clone + 'static, { async fn send_bundle(&self, bundle: Bundle) -> RpcResult { let request_start_time = Instant::now(); @@ -109,12 +89,33 @@ where bundle_result } + + async fn transaction_receipt( + &self, + hash: B256, + ) -> RpcResult>> { + match self.eth_api.transaction_receipt(hash).await.unwrap() { + Some(receipt) => Ok(Some(receipt)), + None => { + // Try to find the transaction in the reverted cache + if self.reverted_cache.get(&hash).await.is_some() { + return Err(EthApiError::InvalidParams( + "the transaction was dropped from the pool".into(), + ) + .into()); + } else { + return Ok(None); + } + } + } + } } -impl RevertProtectionBundleAPI +impl RevertProtectionExt where Pool: TransactionPool + Clone + 'static, Provider: StateProviderFactory + Send + Sync + Clone + 'static, + Eth: FullEthApi + Send + Sync + Clone + 'static, { async fn send_bundle_inner(&self, bundle: Bundle) -> RpcResult { let last_block_number = self @@ -161,34 +162,3 @@ where Ok(result) } } - -pub struct RevertProtectionEthAPI { - eth_api: Eth, - reverted_cache: Cache, -} - -#[async_trait] -impl EthApiOverrideServer> for RevertProtectionEthAPI -where - Eth: FullEthApi + Send + Sync + Clone + 'static, -{ - async fn transaction_receipt( - &self, - hash: B256, - ) -> RpcResult>> { - match self.eth_api.transaction_receipt(hash).await.unwrap() { - Some(receipt) => Ok(Some(receipt)), - None => { - // Try to find the transaction in the reverted cache - if self.reverted_cache.get(&hash).await.is_some() { - return Err(EthApiError::InvalidParams( - "the transaction was dropped from the pool".into(), - ) - .into()); - } else { - return Ok(None); - } - } - } - } -} diff --git a/crates/op-rbuilder/src/tests/framework/instance.rs b/crates/op-rbuilder/src/tests/framework/instance.rs index eb2c8cdb4..2b2b486fd 100644 --- a/crates/op-rbuilder/src/tests/framework/instance.rs +++ b/crates/op-rbuilder/src/tests/framework/instance.rs @@ -2,7 +2,7 @@ use crate::{ args::OpRbuilderArgs, builders::{BuilderConfig, FlashblocksBuilder, PayloadBuilder, StandardBuilder}, primitives::reth::engine_api_builder::OpEngineApiBuilder, - revert_protection::{EthApiExtServer, EthApiOverrideServer, RevertProtectionExt}, + revert_protection::{EthApiExtServer, RevertProtectionExt}, tests::{ create_test_db, framework::{driver::ChainDriver, BUILDER_PRIVATE_KEY}, @@ -126,15 +126,15 @@ impl LocalInstance { let pool = ctx.pool().clone(); let provider = ctx.provider().clone(); - let revert_protection_ext = - RevertProtectionExt::new(pool, provider, ctx.registry.eth_api().clone()); + let revert_protection_ext = RevertProtectionExt::new( + pool, + provider, + ctx.registry.eth_api().clone(), + reverted_cache, + ); ctx.modules - .merge_configured(revert_protection_ext.bundle_api().into_rpc())?; - - ctx.modules.replace_configured( - revert_protection_ext.eth_api(reverted_cache).into_rpc(), - )?; + .add_or_replace_configured(revert_protection_ext.into_rpc())?; } Ok(())