Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/rbuilder-operator/src/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl BidObserver for BuiltBlocksWriter {
let mut used_bundle_hashes = Vec::new();
let mut used_bundle_uuids = Vec::new();
for res in &built_block_trace.included_orders {
if let Order::Bundle(bundle) = &res.order {
if let Order::Bundle(bundle) = res.order.as_ref() {
used_bundle_hashes
.push(bundle.external_hash.unwrap_or(bundle.hash).to_string());
used_bundle_uuids.push(bundle.uuid.to_string());
Expand Down
14 changes: 13 additions & 1 deletion crates/rbuilder-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,13 +1011,25 @@ impl SimValue {
/// Order simulated (usually on top of block) + SimValue
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimulatedOrder {
pub order: Order,
pub order: Arc<Order>,
pub sim_value: SimValue,
/// Info about read/write slots during the simulation to help figure out what the Order is doing.
pub used_state_trace: Option<UsedStateTrace>,
}

impl SimulatedOrder {
pub fn new(
order: Arc<Order>,
sim_value: SimValue,
used_state_trace: Option<UsedStateTrace>,
) -> Self {
Self {
order,
sim_value,
used_state_trace,
}
}

pub fn id(&self) -> OrderId {
self.order.id()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn print_orders_with_tx_hash(
.iter()
.map(|order_with_timestamp| &order_with_timestamp.order)
.filter(|order| order.list_txs().iter().any(|(tx, _)| tx.hash() == tx_hash))
.for_each(print_order);
.for_each(|order| print_order(order));
println!("\nSIM ORDERS:");
sim_orders
.iter()
Expand All @@ -241,7 +241,7 @@ fn print_order_execution_result(order_result: &ExecutionResult) {
order_result.space_used.gas,
format_ether(order_result.coinbase_profit),
);
if let Order::Bundle(_) = order_result.order {
if let Order::Bundle(_) = order_result.order.as_ref() {
for tx in order_result.tx_infos.iter().map(|info| &info.tx) {
println!(" ↳ {:?}", tx.hash());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rbuilder_primitives::{
Bundle, MempoolTx, Metadata, Order, TransactionSignedEcRecoveredWithBlobs, LAST_BUNDLE_VERSION,
};
use reth_provider::test_utils::MockNodeTypesWithDB;
use std::sync::Arc;
use uuid::Uuid;

use super::backtest_build_block::{run_backtest_build_block, BuildBlockCfg, OrdersSource};
Expand Down Expand Up @@ -75,7 +76,7 @@ impl<ConfigType: LiveBuilderConfig> SyntheticOrdersSource<ConfigType> {
)));
orders.push(OrdersWithTimestamp {
timestamp_ms: 0,
order,
order: Arc::new(order),
});
}

Expand Down Expand Up @@ -106,7 +107,7 @@ impl<ConfigType: LiveBuilderConfig> SyntheticOrdersSource<ConfigType> {
bundle.hash_slow();
orders.push(OrdersWithTimestamp {
timestamp_ms: 0,
order: Order::Bundle(bundle),
order: Arc::new(Order::Bundle(bundle)),
});
}

Expand Down
8 changes: 4 additions & 4 deletions crates/rbuilder/src/backtest/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};
use alloy_eips::BlockNumHash;
use alloy_primitives::U256;
use rbuilder_primitives::{OrderId, SimulatedOrder};
use rbuilder_primitives::{Order, OrderId, SimulatedOrder};
use reth_chainspec::ChainSpec;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
Expand Down Expand Up @@ -95,10 +95,10 @@ pub fn backtest_prepare_orders_from_building_context<P>(
where
P: StateProviderFactory + Clone + 'static,
{
let orders = available_orders
let orders: Vec<Arc<Order>> = available_orders
.iter()
.map(|order| order.order.clone())
.collect::<Vec<_>>();
.map(|order| Arc::clone(&order.order))
.collect();
for order in &orders {
ctx.mempool_tx_detector.add_tx(order);
}
Expand Down
3 changes: 2 additions & 1 deletion crates/rbuilder/src/backtest/fetch/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
};
use alloy_primitives::B256;
use async_trait::async_trait;
use std::sync::Arc;

#[derive(Debug, Clone)]
pub struct DatasourceData {
Expand Down Expand Up @@ -45,7 +46,7 @@ pub async fn get_full_slot_data_from_data(
.into_iter()
.map(|o| ReplaceableOrderPoolCommandWithTimestamp {
timestamp_ms: o.timestamp_ms,
command: ReplaceableOrderPoolCommand::Order(o.order),
command: ReplaceableOrderPoolCommand::Order(Arc::clone(&o.order)),
})
.collect(),
built_block_data: data.built_block_data,
Expand Down
3 changes: 2 additions & 1 deletion crates/rbuilder/src/backtest/fetch/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rbuilder_primitives::{
Order,
};
use sqlx::types::chrono::DateTime;
use std::sync::Arc;
use std::{
fs::create_dir_all,
path::{Path, PathBuf},
Expand Down Expand Up @@ -52,7 +53,7 @@ pub fn get_mempool_transactions(

Some(OrdersWithTimestamp {
timestamp_ms,
order,
order: Arc::new(order),
})
})
.collect())
Expand Down
8 changes: 4 additions & 4 deletions crates/rbuilder/src/backtest/full_slot_block_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl From<OrdersWithTimestamp> for ReplaceableOrderPoolCommandWithTimestamp {
fn from(order: OrdersWithTimestamp) -> Self {
ReplaceableOrderPoolCommandWithTimestamp {
timestamp_ms: order.timestamp_ms,
command: ReplaceableOrderPoolCommand::Order(order.order),
command: ReplaceableOrderPoolCommand::Order(Arc::clone(&order.order)),
}
}
}
Expand Down Expand Up @@ -116,7 +116,7 @@ impl FullSlotBlockData {
if included_orders_ids.remove(&order.id()) {
Some(OrdersWithTimestamp {
timestamp_ms: command_ts.timestamp_ms,
order: order.clone(),
order: Arc::clone(order),
})
} else {
None
Expand Down Expand Up @@ -167,7 +167,7 @@ impl FullSlotBlockData {
match command_ts.command {
ReplaceableOrderPoolCommand::Order(order) => {
order_id_to_timestamp.insert(order.id(), command_ts.timestamp_ms);
order_manager.insert_order(order);
order_manager.insert_order(Arc::clone(&order));
}
ReplaceableOrderPoolCommand::CancelBundle(replacement_data) => {
order_manager.remove_bundle(replacement_data);
Expand All @@ -181,7 +181,7 @@ impl FullSlotBlockData {
.iter()
.map(|o| OrdersWithTimestamp {
timestamp_ms: *order_id_to_timestamp.get(&o.id()).unwrap(),
order: o.clone(),
order: Arc::clone(o),
})
.collect();

Expand Down
7 changes: 4 additions & 3 deletions crates/rbuilder/src/backtest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod store;
use ahash::HashMap;
pub use backtest_build_range::run_backtest_build_range;
use std::collections::HashSet;
use std::sync::Arc;

use crate::{mev_boost::BuilderBlockReceived, utils::offset_datetime_to_timestamp_ms};
use alloy_consensus::Transaction as TransactionTrait;
Expand All @@ -38,15 +39,15 @@ impl From<OrdersWithTimestamp> for RawOrdersWithTimestamp {
fn from(orders: OrdersWithTimestamp) -> Self {
Self {
timestamp_ms: orders.timestamp_ms,
order: orders.order.into(),
order: (*orders.order).clone().into(),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OrdersWithTimestamp {
pub timestamp_ms: u64,
pub order: Order,
pub order: Arc<Order>,
}

/// Historic data for a block.
Expand Down Expand Up @@ -171,7 +172,7 @@ impl BlockData {
let mempool_txs = self
.available_orders
.iter()
.filter_map(|o| match &o.order {
.filter_map(|o| match o.order.as_ref() {
Order::Tx(tx) => Some(tx.tx_with_blobs.hash()),
_ => None,
})
Expand Down
8 changes: 4 additions & 4 deletions crates/rbuilder/src/backtest/redistribute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ where
let mut txs = 0;
let mut bundles = 0;
for ts_order in &block_data.available_orders {
match &ts_order.order {
match ts_order.order.as_ref() {
Order::Bundle(_) => bundles += 1,
Order::Tx(_) => txs += 1,
}
Expand Down Expand Up @@ -449,7 +449,7 @@ struct AvailableOrders {
included_orders_by_address: Vec<(Address, Vec<OrderId>)>,
all_orders_by_address: HashMap<Address, Vec<OrderId>>,
orders_id_to_address: HashMap<OrderId, Address>,
all_orders_by_id: HashMap<OrderId, Order>,
all_orders_by_id: HashMap<OrderId, Arc<Order>>,
bundle_hash_by_id: HashMap<OrderId, B256>,
order_sender_by_id: HashMap<OrderId, Address>,
}
Expand Down Expand Up @@ -550,7 +550,7 @@ fn split_orders_by_identities(

for order in &block_data.available_orders {
let id = order.order.id();
if let Order::Bundle(bundle) = &order.order {
if let Order::Bundle(bundle) = order.order.as_ref() {
bundle_hash_by_id.insert(id, bundle.external_hash.unwrap_or(bundle.hash));
};
order_sender_by_id.insert(id, order_sender(&order.order));
Expand Down Expand Up @@ -603,7 +603,7 @@ fn split_orders_by_identities(
all_orders_by_id: block_data
.available_orders
.iter()
.map(|order| (order.order.id(), order.order.clone()))
.map(|order| (order.order.id(), Arc::clone(&order.order)))
.collect(),
bundle_hash_by_id,
order_sender_by_id,
Expand Down
5 changes: 3 additions & 2 deletions crates/rbuilder/src/backtest/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use std::{
ffi::OsString,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
};

/// Version of the data/format on the DB.
Expand Down Expand Up @@ -638,7 +639,7 @@ impl From<ReplaceableOrderPoolCommand> for RawReplaceableOrderPoolCommand {
fn from(command: ReplaceableOrderPoolCommand) -> Self {
match command {
ReplaceableOrderPoolCommand::Order(order) => {
RawReplaceableOrderPoolCommand::Order(order.into())
RawReplaceableOrderPoolCommand::Order((*order).clone().into())
}
ReplaceableOrderPoolCommand::CancelBundle(replacement_data) => {
RawReplaceableOrderPoolCommand::CancelBundle(replacement_data)
Expand Down Expand Up @@ -673,7 +674,7 @@ impl RawReplaceableOrderPoolCommandWithTimestamp {
timestamp_ms: self.timestamp_ms,
command: match self.command {
RawReplaceableOrderPoolCommand::Order(raw_order) => {
ReplaceableOrderPoolCommand::Order(raw_order.decode(encoding)?)
ReplaceableOrderPoolCommand::Order(Arc::new(raw_order.decode(encoding)?))
}
RawReplaceableOrderPoolCommand::CancelBundle(replacement_data) => {
ReplaceableOrderPoolCommand::CancelBundle(replacement_data)
Expand Down
17 changes: 7 additions & 10 deletions crates/rbuilder/src/bin/run-bundle-on-prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,8 @@ async fn main() -> eyre::Result<()> {
break;
}
let order = Order::Tx(MempoolTx::new(tx.clone()));
let sim_order = SimulatedOrder {
order,
sim_value: Default::default(),
used_state_trace: Default::default(),
};
let sim_order =
SimulatedOrder::new(Arc::new(order), Default::default(), Default::default());
let res = builder.commit_order(&mut block_info.local_ctx, &sim_order, &|_| Ok(()))?;
println!("{:?} {:?}", tx.hash(), res.is_ok());
}
Expand Down Expand Up @@ -301,11 +298,11 @@ fn execute_orders_on_tob(
) -> eyre::Result<()> {
for order_ts in target_orders {
let mut builder = block_info.create_building_helper(false)?;
let sim_order = SimulatedOrder {
order: order_ts.order.clone(),
sim_value: Default::default(),
used_state_trace: Default::default(),
};
let sim_order = SimulatedOrder::new(
Arc::clone(&order_ts.order),
Default::default(),
Default::default(),
);
let res = builder.commit_order(&mut block_info.local_ctx, &sim_order, &|_| Ok(()))?;
let profit = res
.as_ref()
Expand Down
14 changes: 5 additions & 9 deletions crates/rbuilder/src/building/block_orders/order_priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,11 @@ mod test {
gas: u64,
order_type: OrderType,
) -> Arc<SimulatedOrder> {
Arc::new(SimulatedOrder {
order: self.create_order(order_type),
sim_value: SimValue::new_test(
U256::from(full_profit),
U256::from(non_mempool_profit),
gas,
),
used_state_trace: None,
})
Arc::new(SimulatedOrder::new(
Arc::new(self.create_order(order_type)),
SimValue::new_test(U256::from(full_profit), U256::from(non_mempool_profit), gas),
None,
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ impl TestDataGenerator {
let sim_value =
SimValue::new_test_no_gas(U256::from(coinbase_profit), U256::from(mev_gas_price));

Arc::new(SimulatedOrder {
order,
sim_value,
used_state_trace: None,
})
Arc::new(SimulatedOrder::new(Arc::new(order), sim_value, None))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,11 @@ mod tests {
external_hash: None,
};

Arc::new(SimulatedOrder {
order: Order::Bundle(bundle),
used_state_trace: None,
Arc::new(SimulatedOrder::new(
Arc::new(Order::Bundle(bundle)),
sim_value,
})
None,
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,16 +487,16 @@ mod tests {

let sim_value = SimValue::new_test_no_gas(coinbase_profit, U256::ZERO);

Arc::new(SimulatedOrder {
order: Order::Tx(MempoolTx {
Arc::new(SimulatedOrder::new(
Arc::new(Order::Tx(MempoolTx {
tx_with_blobs: TransactionSignedEcRecoveredWithBlobs::new_no_blobs(
self.create_tx(),
)
.unwrap(),
}),
})),
sim_value,
used_state_trace: Some(trace),
})
Some(trace),
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,16 @@ mod tests {
trace.destructed_contracts.push(*contract_address);
}

Arc::new(SimulatedOrder {
order: Order::Tx(MempoolTx {
Arc::new(SimulatedOrder::new(
Arc::new(Order::Tx(MempoolTx {
tx_with_blobs: TransactionSignedEcRecoveredWithBlobs::new_no_blobs(
self.create_tx(),
)
.unwrap(),
}),
used_state_trace: Some(trace),
sim_value: SimValue::default(),
})
})),
SimValue::default(),
Some(trace),
))
}
}

Expand Down
Loading
Loading