Skip to content

Commit

Permalink
BlockOrders->PrioritizedOrderStore
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanCorDX committed Dec 19, 2024
1 parent b2a21b1 commit 6003015
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 89 deletions.
91 changes: 14 additions & 77 deletions crates/rbuilder/src/building/block_orders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ahash::HashMap;
use reth_errors::ProviderResult;
use reth_provider::StateProviderBox;

use prioritized_order_store::PrioritizedOrderStore;
pub use prioritized_order_store::PrioritizedOrderStore;
pub use test_data_generator::TestDataGenerator;

/// Generic SimulatedOrder sink to add and remove orders.
Expand Down Expand Up @@ -48,13 +48,6 @@ pub fn simulated_order_command_to_sink<SinkType: SimulatedOrderSink>(
};
}

/// Wrapper on [`PrioritizedOrderStore`] soon will die.
/// IMPORTANT: Read comments for PrioritizedOrderStore to see how to use (add_order here is insert_order) since nonces are a little tricky
#[derive(Debug, Clone)]
pub struct BlockOrders {
prioritized_order_store: PrioritizedOrderStore,
}

/// SimulatedOrderSink that stores all orders + all the adds from last drain_new_orders ONLY if we didn't see removes.
pub struct SimulatedOrderStore {
/// Id -> order for all orders we manage. Carefully maintained by remove/insert
Expand Down Expand Up @@ -102,71 +95,12 @@ impl SimulatedOrderSink for SimulatedOrderStore {
}
}

impl BlockOrders {
/// sbundle_merger_selected_signers see [`ShareBundleMerger`]
pub fn new(
priority: Sorting,
initial_onchain_nonces: impl IntoIterator<Item = AccountNonce>,
) -> Self {
let mut onchain_nonces = HashMap::default();
for onchain_nonce in initial_onchain_nonces {
onchain_nonces.insert(onchain_nonce.account, onchain_nonce.nonce);
}

let prioritized_order_store = PrioritizedOrderStore::new(priority, onchain_nonces);
Self {
prioritized_order_store,
}
}

pub fn add_order(&mut self, order: SimulatedOrder) {
self.insert_order(order);
}

/// Readds a poped order to the priority queue bypassing other stages
/// Use ONLY if you are using BlockOrders as an static priority with no new add_order/remove_order etc.
/// @Pending For this cases it would probably be better to have a PrioritizedOrderStore instead of a BlockOrders
pub fn readd_order(&mut self, order: SimulatedOrder) {
self.prioritized_order_store.insert_order(order);
}

pub fn remove_orders(
&mut self,
orders: impl IntoIterator<Item = OrderId>,
) -> Vec<SimulatedOrder> {
self.prioritized_order_store.remove_orders(orders)
}

pub fn pop_order(&mut self) -> Option<SimulatedOrder> {
self.prioritized_order_store.pop_order()
}

pub fn update_onchain_nonces(&mut self, new_nonces: &[AccountNonce]) {
self.prioritized_order_store
.update_onchain_nonces(new_nonces);
}

pub fn get_all_orders(&self) -> Vec<SimulatedOrder> {
self.prioritized_order_store.get_all_orders()
}
}

impl SimulatedOrderSink for BlockOrders {
fn insert_order(&mut self, order: SimulatedOrder) {
self.prioritized_order_store.insert_order(order);
}

fn remove_order(&mut self, id: OrderId) -> Option<SimulatedOrder> {
self.prioritized_order_store.remove_order(id)
}
}

/// Create block orders struct from simulated orders. Used in the backtest, not practical while live.
pub fn block_orders_from_sim_orders(
sim_orders: &[SimulatedOrder],
sorting: Sorting,
state_provider: &StateProviderBox,
) -> ProviderResult<BlockOrders> {
) -> ProviderResult<PrioritizedOrderStore> {
let mut onchain_nonces = vec![];
for order in sim_orders {
for nonce in order.order.nonces() {
Expand All @@ -179,10 +113,10 @@ pub fn block_orders_from_sim_orders(
});
}
}
let mut block_orders = BlockOrders::new(sorting, onchain_nonces);
let mut block_orders = PrioritizedOrderStore::new(sorting, onchain_nonces);

for order in sim_orders.iter().cloned() {
block_orders.add_order(order);
block_orders.insert_order(order);
}

Ok(block_orders)
Expand All @@ -193,11 +127,11 @@ mod test {
use crate::primitives::BundledTxInfo;

use super::*;
/// Helper struct for common BlockOrders test operations
/// Helper struct for common PrioritizedOrderStore test operations
/// Works hardcoded on Sorting::MaxProfit since it changes nothing on internal logic
struct TestContext {
pub data_gen: TestDataGenerator,
pub order_pool: BlockOrders,
pub order_pool: PrioritizedOrderStore,
}

impl TestContext {
Expand All @@ -209,7 +143,7 @@ mod test {
nonce.clone(),
TestContext {
data_gen,
order_pool: BlockOrders::new(Sorting::MaxProfit, vec![nonce]),
order_pool: PrioritizedOrderStore::new(Sorting::MaxProfit, vec![nonce]),
},
)
}
Expand All @@ -227,7 +161,10 @@ mod test {
nonce_2.clone(),
TestContext {
data_gen,
order_pool: BlockOrders::new(Sorting::MaxProfit, vec![nonce_1, nonce_2]),
order_pool: PrioritizedOrderStore::new(
Sorting::MaxProfit,
vec![nonce_1, nonce_2],
),
},
)
}
Expand All @@ -240,7 +177,7 @@ mod test {
) -> SimulatedOrder {
let order = self.data_gen.base.create_tx_order(tx_nonce.clone());
let order = self.data_gen.create_sim_order(order, tx_profit, tx_profit);
self.order_pool.add_order(order.clone());
self.order_pool.insert_order(order.clone());
order
}

Expand All @@ -251,13 +188,13 @@ mod test {
bundle_profit: u64,
) -> SimulatedOrder {
let order = self.data_gen.base.create_bundle_multi_tx_order(
0, // in the context of BlockOrders we don't care about the block (it's prefiltered)
0, // in the context of PrioritizedOrderStore we don't care about the block (it's prefiltered)
txs_info, None,
);
let order = self
.data_gen
.create_sim_order(order, bundle_profit, bundle_profit);
self.order_pool.add_order(order.clone());
self.order_pool.insert_order(order.clone());
order
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ pub struct PrioritizedOrderStore {
}

impl PrioritizedOrderStore {
pub fn new(priority: Sorting, onchain_nonces: HashMap<Address, u64>) -> Self {
pub fn new(
priority: Sorting,
initial_onchain_nonces: impl IntoIterator<Item = AccountNonce>,
) -> Self {
let mut onchain_nonces = HashMap::default();
for onchain_nonce in initial_onchain_nonces {
onchain_nonces.insert(onchain_nonce.account, onchain_nonce.nonce);
}
Self {
main_queue: PriorityQueue::new(),
main_queue_nonces: HashMap::default(),
Expand Down
10 changes: 5 additions & 5 deletions crates/rbuilder/src/building/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod ordering_builder;
pub mod parallel_builder;

use crate::{
building::{BlockBuildingContext, BlockOrders, BuiltBlockTrace, SimulatedOrderSink, Sorting},
building::{BlockBuildingContext, BuiltBlockTrace, SimulatedOrderSink, Sorting},
live_builder::{payload_events::MevBoostSlotData, simulation::SimulatedOrderCommand},
primitives::{AccountNonce, OrderId, SimulatedOrder},
roothash::RootHashConfig,
Expand All @@ -24,7 +24,7 @@ use tokio::sync::{broadcast, broadcast::error::TryRecvError};
use tokio_util::sync::CancellationToken;
use tracing::{info, warn};

use super::simulated_order_command_to_sink;
use super::{simulated_order_command_to_sink, PrioritizedOrderStore};

/// Block we built
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -106,7 +106,7 @@ impl OrderConsumer {
pub struct OrderIntakeConsumer<P> {
nonce_cache: NonceCache<P>,

block_orders: BlockOrders,
block_orders: PrioritizedOrderStore,
onchain_nonces_updated: HashSet<Address>,

order_consumer: OrderConsumer,
Expand All @@ -127,7 +127,7 @@ where

Self {
nonce_cache,
block_orders: BlockOrders::new(sorting, vec![]),
block_orders: PrioritizedOrderStore::new(sorting, vec![]),
onchain_nonces_updated: HashSet::default(),
order_consumer: OrderConsumer::new(orders),
}
Expand Down Expand Up @@ -180,7 +180,7 @@ where
Ok(true)
}

pub fn current_block_orders(&self) -> BlockOrders {
pub fn current_block_orders(&self) -> PrioritizedOrderStore {
self.block_orders.clone()
}

Expand Down
10 changes: 5 additions & 5 deletions crates/rbuilder/src/building/builders/ordering_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
builders::{
block_building_helper::BlockBuildingHelper, LiveBuilderInput, OrderIntakeConsumer,
},
BlockBuildingContext, BlockOrders, ExecutionError, Sorting,
BlockBuildingContext, ExecutionError, PrioritizedOrderStore, SimulatedOrderSink, Sorting,
},
primitives::{AccountNonce, OrderId},
roothash::RootHashConfig,
Expand Down Expand Up @@ -42,7 +42,7 @@ pub struct OrderingBuilderConfig {
pub discard_txs: bool,
pub sorting: Sorting,
/// Only when a tx fails because the profit was worst than expected: Number of time an order can fail during a single block building iteration.
/// When thi happens it gets reinserted in the BlockStore with the new simulated profit (the one that failed).
/// When thi happens it gets reinserted in the PrioritizedOrderStore with the new simulated profit (the one that failed).
pub failed_order_retries: usize,
/// if a tx fails in a block building iteration it's dropped so next iterations will not use it.
pub drop_failed_orders: bool,
Expand Down Expand Up @@ -235,7 +235,7 @@ where
/// !use_suggested_fee_recipient_as_coinbase: all the mev profit goes to the builder and at the end of the block we pay to the suggested_fee_recipient.
pub fn build_block(
&mut self,
block_orders: BlockOrders,
block_orders: PrioritizedOrderStore,
use_suggested_fee_recipient_as_coinbase: bool,
cancel_block: CancellationToken,
) -> eyre::Result<Box<dyn BlockBuildingHelper>> {
Expand Down Expand Up @@ -273,7 +273,7 @@ where
fn fill_orders(
&mut self,
block_building_helper: &mut dyn BlockBuildingHelper,
mut block_orders: BlockOrders,
mut block_orders: PrioritizedOrderStore,
build_start: Instant,
) -> eyre::Result<()> {
let mut order_attempts: HashMap<OrderId, usize> = HashMap::default();
Expand Down Expand Up @@ -312,7 +312,7 @@ where
if *order_attempts < self.config.failed_order_retries {
let mut new_order = sim_order.clone();
new_order.sim_value = inplace.clone();
block_orders.readd_order(new_order);
block_orders.insert_order(new_order);
*order_attempts += 1;
reinserted = true;
}
Expand Down
1 change: 0 additions & 1 deletion crates/rbuilder/src/building/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub mod testing;
pub mod tracers;
use alloy_consensus::{Header, EMPTY_OMMER_ROOT_HASH};
use alloy_primitives::{Address, Bytes, Sealable, U256};
pub use block_orders::BlockOrders;
use eth_sparse_mpt::SparseTrieSharedCache;
use reth_db::Database;
use reth_primitives::BlockBody;
Expand Down

0 comments on commit 6003015

Please sign in to comment.