Skip to content

Commit 6027a50

Browse files
committed
make meter_response not optional
1 parent 9879c92 commit 6027a50

File tree

4 files changed

+57
-33
lines changed

4 files changed

+57
-33
lines changed

crates/core/src/test_utils.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Bundle, BundleWithMetadata};
1+
use crate::{Bundle, BundleWithMetadata, MeterBundleResponse};
22
use alloy_consensus::SignableTransaction;
33
use alloy_primitives::{Address, U256};
44
use alloy_provider::network::TxSignerSync;
@@ -38,6 +38,7 @@ pub fn create_test_bundle(
3838
max_timestamp,
3939
..Default::default()
4040
};
41+
let meter_bundle_response = MeterBundleResponse::default();
4142

42-
BundleWithMetadata::load(bundle).unwrap()
43+
BundleWithMetadata::load(bundle, meter_bundle_response).unwrap()
4344
}

crates/core/src/types.rs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ pub struct BundleWithMetadata {
7373
bundle: Bundle,
7474
uuid: Uuid,
7575
transactions: Vec<OpTxEnvelope>,
76-
meter_bundle_response: Option<MeterBundleResponse>,
76+
meter_bundle_response: MeterBundleResponse,
7777
}
7878

7979
impl BundleWithMetadata {
80-
pub fn load(mut bundle: Bundle) -> Result<Self, String> {
80+
pub fn load(
81+
mut bundle: Bundle,
82+
meter_bundle_response: MeterBundleResponse,
83+
) -> Result<Self, String> {
8184
let uuid = bundle
8285
.replacement_uuid
8386
.clone()
@@ -100,7 +103,7 @@ impl BundleWithMetadata {
100103
bundle,
101104
transactions,
102105
uuid,
103-
meter_bundle_response: None,
106+
meter_bundle_response,
104107
})
105108
}
106109

@@ -145,10 +148,6 @@ impl BundleWithMetadata {
145148
.map(|t| tx_estimated_size_fjord_bytes(&t.encoded_2718()))
146149
.sum()
147150
}
148-
149-
pub fn set_meter_bundle_response(&mut self, meter_bundle_response: MeterBundleResponse) {
150-
self.meter_bundle_response = Some(meter_bundle_response);
151-
}
152151
}
153152

154153
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -187,6 +186,24 @@ pub struct MeterBundleResponse {
187186
pub state_root_time_us: u128,
188187
}
189188

189+
impl Default for MeterBundleResponse {
190+
fn default() -> Self {
191+
Self {
192+
bundle_gas_price: "0".to_string(),
193+
bundle_hash: B256::default(),
194+
coinbase_diff: "0".to_string(),
195+
eth_sent_to_coinbase: "0".to_string(),
196+
gas_fees: "0".to_string(),
197+
results: vec![],
198+
state_block_number: 0,
199+
state_flashblock_index: None,
200+
total_gas_used: 0,
201+
total_execution_time_us: 0,
202+
state_root_time_us: 0,
203+
}
204+
}
205+
}
206+
190207
#[cfg(test)]
191208
mod tests {
192209
use super::*;
@@ -206,12 +223,15 @@ mod tests {
206223
let tx1_bytes = tx1.encoded_2718();
207224
let tx2_bytes = tx2.encoded_2718();
208225

209-
let bundle = BundleWithMetadata::load(Bundle {
210-
replacement_uuid: None,
211-
txs: vec![tx1_bytes.clone().into()],
212-
block_number: 1,
213-
..Default::default()
214-
})
226+
let bundle = BundleWithMetadata::load(
227+
Bundle {
228+
replacement_uuid: None,
229+
txs: vec![tx1_bytes.clone().into()],
230+
block_number: 1,
231+
..Default::default()
232+
},
233+
MeterBundleResponse::default(),
234+
)
215235
.unwrap();
216236

217237
assert!(!bundle.uuid().is_nil());
@@ -234,12 +254,15 @@ mod tests {
234254
assert_eq!(bundle.bundle_hash(), expected_bundle_hash_single);
235255

236256
let uuid = Uuid::new_v4();
237-
let bundle = BundleWithMetadata::load(Bundle {
238-
replacement_uuid: Some(uuid.to_string()),
239-
txs: vec![tx1_bytes.clone().into(), tx2_bytes.clone().into()],
240-
block_number: 1,
241-
..Default::default()
242-
})
257+
let bundle = BundleWithMetadata::load(
258+
Bundle {
259+
replacement_uuid: Some(uuid.to_string()),
260+
txs: vec![tx1_bytes.clone().into(), tx2_bytes.clone().into()],
261+
block_number: 1,
262+
..Default::default()
263+
},
264+
MeterBundleResponse::default(),
265+
)
243266
.unwrap();
244267

245268
assert_eq!(*bundle.uuid(), uuid);

crates/ingress-rpc/src/queue.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl QueuePublisher for KafkaQueuePublisher {
7575
mod tests {
7676
use super::*;
7777
use rdkafka::config::ClientConfig;
78-
use tips_core::{Bundle, BundleWithMetadata};
78+
use tips_core::{Bundle, BundleWithMetadata, MeterBundleResponse};
7979
use tokio::time::{Duration, Instant};
8080

8181
fn create_test_bundle() -> Bundle {
@@ -93,7 +93,8 @@ mod tests {
9393

9494
let publisher = KafkaQueuePublisher::new(producer, "tips-ingress-rpc".to_string());
9595
let bundle = create_test_bundle();
96-
let bundle_with_metadata = BundleWithMetadata::load(bundle.clone()).unwrap();
96+
let bundle_with_metadata =
97+
BundleWithMetadata::load(bundle.clone(), MeterBundleResponse::default()).unwrap();
9798
let bundle_hash = bundle_with_metadata.bundle_hash();
9899

99100
let start = Instant::now();

crates/ingress-rpc/src/service.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ where
6767
Audit: BundleEventPublisher + Sync + Send + 'static,
6868
{
6969
async fn send_bundle(&self, bundle: Bundle) -> RpcResult<BundleHash> {
70-
let mut bundle_with_metadata = self.validate_bundle(&bundle).await?;
71-
bundle_with_metadata.set_meter_bundle_response(self.meter_bundle(&bundle).await?);
70+
self.validate_bundle(&bundle).await?;
71+
let meter_bundle_response = self.meter_bundle(&bundle).await?;
72+
let bundle_with_metadata = BundleWithMetadata::load(bundle, meter_bundle_response)
73+
.map_err(|e| EthApiError::InvalidParams(e.to_string()).into_rpc_err())?;
7274

7375
let bundle_hash = bundle_with_metadata.bundle_hash();
7476
if let Err(e) = self
@@ -122,9 +124,8 @@ where
122124
};
123125
let meter_bundle_response = self.meter_bundle(&bundle).await?;
124126

125-
let mut bundle_with_metadata = BundleWithMetadata::load(bundle)
127+
let bundle_with_metadata = BundleWithMetadata::load(bundle, meter_bundle_response)
126128
.map_err(|e| EthApiError::InvalidParams(e.to_string()).into_rpc_err())?;
127-
bundle_with_metadata.set_meter_bundle_response(meter_bundle_response);
128129
let bundle_hash = bundle_with_metadata.bundle_hash();
129130

130131
if let Err(e) = self
@@ -196,26 +197,24 @@ where
196197
Ok(transaction)
197198
}
198199

199-
async fn validate_bundle(&self, bundle: &Bundle) -> RpcResult<BundleWithMetadata> {
200+
async fn validate_bundle(&self, bundle: &Bundle) -> RpcResult<()> {
200201
if bundle.txs.is_empty() {
201202
return Err(
202203
EthApiError::InvalidParams("Bundle cannot have empty transactions".into())
203204
.into_rpc_err(),
204205
);
205206
}
206207

207-
let bundle_with_metadata = BundleWithMetadata::load(bundle.clone())
208-
.map_err(|e| EthApiError::InvalidParams(e.to_string()).into_rpc_err())?;
209-
let tx_hashes = bundle_with_metadata.txn_hashes();
210-
211208
let mut total_gas = 0u64;
209+
let mut tx_hashes = Vec::new();
212210
for tx_data in &bundle.txs {
213211
let transaction = self.validate_tx(tx_data).await?;
214212
total_gas = total_gas.saturating_add(transaction.gas_limit());
213+
tx_hashes.push(transaction.tx_hash());
215214
}
216215
validate_bundle(bundle, total_gas, tx_hashes)?;
217216

218-
Ok(bundle_with_metadata)
217+
Ok(())
219218
}
220219

221220
/// `meter_bundle` is used to determine how long a bundle will take to execute. A bundle that

0 commit comments

Comments
 (0)