Skip to content

Commit 9879c92

Browse files
committed
comments + add meterbundleres to bundlewmetadata
1 parent 595d857 commit 9879c92

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

crates/core/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ pub mod types;
55
#[cfg(any(test, feature = "test-utils"))]
66
pub mod test_utils;
77

8-
pub use types::{Bundle, BundleHash, BundleWithMetadata, CancelBundle, MeterBundleResponse};
8+
pub use types::{
9+
BLOCK_TIME, Bundle, BundleHash, BundleWithMetadata, CancelBundle, MeterBundleResponse,
10+
};

crates/core/src/types.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use op_alloy_flz::tx_estimated_size_fjord_bytes;
77
use serde::{Deserialize, Serialize};
88
use uuid::Uuid;
99

10+
/// Block time in microseconds
11+
pub const BLOCK_TIME: u128 = 2_000_000;
12+
1013
#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
1114
#[serde(rename_all = "camelCase")]
1215
pub struct Bundle {
@@ -70,6 +73,7 @@ pub struct BundleWithMetadata {
7073
bundle: Bundle,
7174
uuid: Uuid,
7275
transactions: Vec<OpTxEnvelope>,
76+
meter_bundle_response: Option<MeterBundleResponse>,
7377
}
7478

7579
impl BundleWithMetadata {
@@ -96,6 +100,7 @@ impl BundleWithMetadata {
96100
bundle,
97101
transactions,
98102
uuid,
103+
meter_bundle_response: None,
99104
})
100105
}
101106

@@ -140,6 +145,10 @@ impl BundleWithMetadata {
140145
.map(|t| tx_estimated_size_fjord_bytes(&t.encoded_2718()))
141146
.sum()
142147
}
148+
149+
pub fn set_meter_bundle_response(&mut self, meter_bundle_response: MeterBundleResponse) {
150+
self.meter_bundle_response = Some(meter_bundle_response);
151+
}
143152
}
144153

145154
#[derive(Debug, Clone, Serialize, Deserialize)]

crates/ingress-rpc/src/service.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use op_alloy_network::Optimism;
1111
use reth_rpc_eth_types::EthApiError;
1212
use std::time::{SystemTime, UNIX_EPOCH};
1313
use tips_audit::{BundleEvent, BundleEventPublisher};
14-
use tips_core::{Bundle, BundleHash, BundleWithMetadata, CancelBundle, MeterBundleResponse};
14+
use tips_core::{
15+
BLOCK_TIME, Bundle, BundleHash, BundleWithMetadata, CancelBundle, MeterBundleResponse,
16+
};
1517
use tracing::{info, warn};
1618

1719
use crate::queue::QueuePublisher;
@@ -65,8 +67,8 @@ where
6567
Audit: BundleEventPublisher + Sync + Send + 'static,
6668
{
6769
async fn send_bundle(&self, bundle: Bundle) -> RpcResult<BundleHash> {
68-
let bundle_with_metadata = self.validate_bundle(&bundle).await?;
69-
self.meter_bundle(&bundle).await?;
70+
let mut bundle_with_metadata = self.validate_bundle(&bundle).await?;
71+
bundle_with_metadata.set_meter_bundle_response(self.meter_bundle(&bundle).await?);
7072

7173
let bundle_hash = bundle_with_metadata.bundle_hash();
7274
if let Err(e) = self
@@ -118,10 +120,11 @@ where
118120
reverting_tx_hashes: vec![transaction.tx_hash()],
119121
..Default::default()
120122
};
121-
self.meter_bundle(&bundle).await?;
123+
let meter_bundle_response = self.meter_bundle(&bundle).await?;
122124

123-
let bundle_with_metadata = BundleWithMetadata::load(bundle)
125+
let mut bundle_with_metadata = BundleWithMetadata::load(bundle)
124126
.map_err(|e| EthApiError::InvalidParams(e.to_string()).into_rpc_err())?;
127+
bundle_with_metadata.set_meter_bundle_response(meter_bundle_response);
125128
let bundle_hash = bundle_with_metadata.bundle_hash();
126129

127130
if let Err(e) = self
@@ -215,20 +218,24 @@ where
215218
Ok(bundle_with_metadata)
216219
}
217220

218-
async fn meter_bundle(&self, bundle: &Bundle) -> RpcResult<()> {
221+
/// `meter_bundle` is used to determine how long a bundle will take to execute. A bundle that
222+
/// is within `BLOCK_TIME` will return the `MeterBundleResponse` that can be passed along
223+
/// to the builder.
224+
async fn meter_bundle(&self, bundle: &Bundle) -> RpcResult<MeterBundleResponse> {
219225
let res: MeterBundleResponse = self
220226
.provider
221227
.client()
222228
.request("base_meterBundle", (bundle,))
223229
.await
224230
.map_err(|e| EthApiError::InvalidParams(e.to_string()).into_rpc_err())?;
225231

226-
// if simulation takes longer than 2s, we don't include and just error to user
227-
if res.total_execution_time_us > 2000000 {
232+
// we can save some builder payload building computation by not including bundles
233+
// that we know will take longer than the block time to execute
234+
if res.total_execution_time_us > BLOCK_TIME {
228235
return Err(
229236
EthApiError::InvalidParams("Bundle simulation took too long".into()).into_rpc_err(),
230237
);
231238
}
232-
Ok(())
239+
Ok(res)
233240
}
234241
}

0 commit comments

Comments
 (0)