Skip to content

Commit 408840b

Browse files
authored
Add some telemetry for eth_sendBundle (#176)
* measure eth_sendBundle latency * rename bundles_received metric to valid_bundles * log error messages * refactor to get rid of unnecessary mut * count all requests sent to eth_sendBundle * add failed_bundles counter
1 parent 4f1931b commit 408840b

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

crates/op-rbuilder/src/metrics.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,16 @@ pub struct OpRBuilderMetrics {
8282
pub flashblock_time_drift: Histogram,
8383
/// Time offset we used for first flashblock
8484
pub first_flashblock_time_offset: Histogram,
85+
/// Number of requests sent to the eth_sendBundle endpoint
86+
pub bundle_requests: Counter,
8587
/// Number of valid bundles received at the eth_sendBundle endpoint
86-
pub bundles_received: Counter,
88+
pub valid_bundles: Counter,
89+
/// Number of bundles that failed to execute
90+
pub failed_bundles: Counter,
8791
/// Number of reverted bundles
8892
pub bundles_reverted: Histogram,
93+
/// Time taken to respond to a request to the eth_sendBundle endpoint
94+
pub bundle_receive_duration: Histogram,
8995
}
9096

9197
/// Contains version information for the application.

crates/op-rbuilder/src/revert_protection.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::{sync::Arc, time::Instant};
22

33
use crate::{
44
metrics::OpRBuilderMetrics,
@@ -17,6 +17,7 @@ use reth_optimism_txpool::{conditional::MaybeConditionalTransaction, OpPooledTra
1717
use reth_provider::StateProviderFactory;
1818
use reth_rpc_eth_types::{utils::recover_raw_transaction, EthApiError};
1919
use reth_transaction_pool::{PoolTransaction, TransactionOrigin, TransactionPool};
20+
use tracing::error;
2021

2122
// We have to split the RPC modules in two sets because we have methods that both
2223
// replace an existing method and add a new one.
@@ -88,6 +89,34 @@ where
8889
Provider: StateProviderFactory + Send + Sync + Clone + 'static,
8990
{
9091
async fn send_bundle(&self, bundle: Bundle) -> RpcResult<BundleResult> {
92+
let request_start_time = Instant::now();
93+
self.metrics.bundle_requests.increment(1);
94+
95+
let bundle_result = self
96+
.send_bundle_inner(bundle)
97+
.await
98+
.inspect_err(|err| error!("eth_sendBundle request failed: {err:?}"));
99+
100+
if bundle_result.is_ok() {
101+
self.metrics.valid_bundles.increment(1);
102+
} else {
103+
self.metrics.failed_bundles.increment(1);
104+
}
105+
106+
self.metrics
107+
.bundle_receive_duration
108+
.record(request_start_time.elapsed());
109+
110+
bundle_result
111+
}
112+
}
113+
114+
impl<Pool, Provider> RevertProtectionBundleAPI<Pool, Provider>
115+
where
116+
Pool: TransactionPool<Transaction = FBPooledTransaction> + Clone + 'static,
117+
Provider: StateProviderFactory + Send + Sync + Clone + 'static,
118+
{
119+
async fn send_bundle_inner(&self, bundle: Bundle) -> RpcResult<BundleResult> {
91120
let last_block_number = self
92121
.provider
93122
.best_block_number()
@@ -115,20 +144,17 @@ where
115144
.map_err(EthApiError::from)?;
116145

117146
let recovered = recover_raw_transaction(&bundle_transaction)?;
118-
let mut pool_transaction: FBPooledTransaction =
119-
OpPooledTransaction::from_pooled(recovered).into();
120-
121-
pool_transaction.set_reverted_hashes(bundle.reverting_hashes.clone().unwrap_or_default());
122-
pool_transaction.set_conditional(conditional);
147+
let pool_transaction =
148+
FBPooledTransaction::from(OpPooledTransaction::from_pooled(recovered))
149+
.with_reverted_hashes(bundle.reverting_hashes.clone().unwrap_or_default())
150+
.with_conditional(conditional);
123151

124152
let hash = self
125153
.pool
126154
.add_transaction(TransactionOrigin::Local, pool_transaction)
127155
.await
128156
.map_err(EthApiError::from)?;
129157

130-
self.metrics.bundles_received.increment(1);
131-
132158
let result = BundleResult { bundle_hash: hash };
133159
Ok(result)
134160
}

crates/op-rbuilder/src/tx.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ impl OpPooledTx for FBPooledTransaction {
3434
}
3535

3636
pub trait MaybeRevertingTransaction {
37-
fn set_reverted_hashes(&mut self, reverted_hashes: Vec<B256>);
37+
fn with_reverted_hashes(self, reverted_hashes: Vec<B256>) -> Self;
3838
fn reverted_hashes(&self) -> Option<Vec<B256>>;
3939
}
4040

4141
impl MaybeRevertingTransaction for FBPooledTransaction {
42-
fn set_reverted_hashes(&mut self, reverted_hashes: Vec<B256>) {
42+
fn with_reverted_hashes(mut self, reverted_hashes: Vec<B256>) -> Self {
4343
self.reverted_hashes = Some(reverted_hashes);
44+
self
4445
}
4546

4647
fn reverted_hashes(&self) -> Option<Vec<B256>> {

0 commit comments

Comments
 (0)