Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move bundle merger #298

Merged
merged 5 commits into from
Dec 19, 2024
Merged
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
3 changes: 1 addition & 2 deletions crates/rbuilder/src/backtest/backtest_build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ where

let provider_factory = config.base_config().create_provider_factory()?;
let chain_spec = config.base_config().chain_spec()?;
let sbundle_mergeabe_signers = config.base_config().sbundle_mergeabe_signers();

if cli.sim_landed_block {
let tx_sim_results = sim_historical_block(
Expand All @@ -109,6 +108,7 @@ where
chain_spec.clone(),
cli.block_building_time_ms,
config.base_config().blocklist()?,
&config.base_config().sbundle_mergeabe_signers(),
config.base_config().coinbase_signer()?,
)?;

Expand All @@ -124,7 +124,6 @@ where
let input = BacktestSimulateBlockInput {
ctx: ctx.clone(),
builder_name: builder_name.clone(),
sbundle_mergeabe_signers: sbundle_mergeabe_signers.clone(),
sim_orders: &sim_orders,
provider: provider_factory.clone(),
cached_reads: None,
Expand Down
18 changes: 14 additions & 4 deletions crates/rbuilder/src/backtest/execute.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{
backtest::BlockData,
building::{
builders::BacktestSimulateBlockInput, sim::simulate_all_orders_with_sim_tree,
BlockBuildingContext, BundleErr, OrderErr, TransactionErr,
builders::BacktestSimulateBlockInput, multi_share_bundle_merger::MultiShareBundleMerger,
sim::simulate_all_orders_with_sim_tree, BlockBuildingContext, BundleErr, OrderErr,
SimulatedOrderSink, SimulatedOrderStore, TransactionErr,
},
live_builder::cli::LiveBuilderConfig,
primitives::{OrderId, SimulatedOrder},
Expand All @@ -15,7 +16,7 @@ use reth_chainspec::ChainSpec;
use reth_db::Database;
use reth_provider::{BlockReader, DatabaseProviderFactory, HeaderProvider, StateProviderFactory};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::{cell::RefCell, rc::Rc, sync::Arc};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct BacktestBuilderOutput {
Expand Down Expand Up @@ -61,6 +62,7 @@ pub fn backtest_prepare_ctx_for_block<P>(
chain_spec: Arc<ChainSpec>,
build_block_lag_ms: i64,
blocklist: HashSet<Address>,
sbundle_mergeabe_signers: &[Address],
builder_signer: Signer,
) -> eyre::Result<BacktestBlockInput>
where
Expand Down Expand Up @@ -89,6 +91,14 @@ where
);
let (sim_orders, sim_errors) =
simulate_all_orders_with_sim_tree(provider.clone(), &ctx, &orders, false)?;

// Apply bundle merging as in live building.
let order_store = Rc::new(RefCell::new(SimulatedOrderStore::new()));
let mut merger = MultiShareBundleMerger::new(sbundle_mergeabe_signers, order_store.clone());
for sim_order in sim_orders {
merger.insert_order(sim_order);
}
let sim_orders = order_store.borrow().get_orders();
Ok(BacktestBlockInput {
ctx,
sim_orders,
Expand Down Expand Up @@ -126,6 +136,7 @@ where
chain_spec.clone(),
build_block_lag_ms,
blocklist,
sbundle_mergeabe_signers,
config.base_config().coinbase_signer()?,
)?;

Expand Down Expand Up @@ -166,7 +177,6 @@ where
let input = BacktestSimulateBlockInput {
ctx: ctx.clone(),
builder_name: building_algorithm_name.clone(),
sbundle_mergeabe_signers: sbundle_mergeabe_signers.to_vec(),
sim_orders: &sim_orders,
provider: provider.clone(),
cached_reads,
Expand Down
1 change: 1 addition & 0 deletions crates/rbuilder/src/bin/dummy-builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ async fn main() -> eyre::Result<()> {
run_sparse_trie_prefetcher: false,
orderpool_sender,
orderpool_receiver,
sbundle_merger_selected_signers: Default::default(),
};

let ctrlc = tokio::spawn(async move {
Expand Down
87 changes: 28 additions & 59 deletions crates/rbuilder/src/building/block_orders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ mod order_dumper;
#[cfg(test)]
mod test_context;
mod test_data_generator;

use std::{cell::RefCell, rc::Rc};

use crate::{
building::Sorting,
live_builder::simulation::SimulatedOrderCommand,
primitives::{AccountNonce, OrderId, SimulatedOrder},
};
use ahash::HashMap;
use alloy_primitives::Address;
use multi_share_bundle_merger::MultiShareBundleMerger;
use reth_errors::ProviderResult;
use reth_provider::StateProviderBox;

Expand All @@ -39,30 +35,24 @@ pub trait SimulatedOrderSink {
}
}

/// Chained composition of [`ShareBundleMerger`] -> [`PrioritizedOrderStore`] allowing merged orders in an prioritized store
/// IMPORTANT: Read comments for PrioritizedOrderStore to see how to use (add_order here is insert_order) since nonces are a little tricky
#[derive(Debug)]
pub struct BlockOrders {
prioritized_order_store: Rc<RefCell<PrioritizedOrderStore>>,
share_bundle_merger: Box<MultiShareBundleMerger<PrioritizedOrderStore>>,
/// Sends command to sink calling the proper function.
pub fn simulated_order_command_to_sink<SinkType: SimulatedOrderSink>(
command: SimulatedOrderCommand,
sink: &mut SinkType,
) {
match command {
SimulatedOrderCommand::Simulation(sim_order) => sink.insert_order(sim_order),
SimulatedOrderCommand::Cancellation(id) => {
let _ = sink.remove_order(id);
}
};
}

impl Clone for BlockOrders {
/// This cloning is tricky since we don't want to clone the Rcs we want to clone the real objects.
fn clone(&self) -> Self {
let prioritized_order_store =
Rc::new(RefCell::new(self.prioritized_order_store.borrow().clone()));

let share_bundle_merger = Box::new(
self.share_bundle_merger
.clone_with_sink(prioritized_order_store.clone()),
);

Self {
prioritized_order_store,
share_bundle_merger,
}
}
/// 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.
Expand Down Expand Up @@ -117,33 +107,18 @@ impl BlockOrders {
pub fn new(
priority: Sorting,
initial_onchain_nonces: impl IntoIterator<Item = AccountNonce>,
sbundle_merger_selected_signers: &[Address],
) -> 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 = Rc::new(RefCell::new(PrioritizedOrderStore::new(
priority,
onchain_nonces,
)));

let share_bundle_merger = Box::new(MultiShareBundleMerger::new(
sbundle_merger_selected_signers,
prioritized_order_store.clone(),
));

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

fn input_order_store(&mut self) -> &mut MultiShareBundleMerger<PrioritizedOrderStore> {
&mut self.share_bundle_merger
}

pub fn add_order(&mut self, order: SimulatedOrder) {
self.insert_order(order);
}
Expand All @@ -152,41 +127,37 @@ impl BlockOrders {
/// 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
.borrow_mut()
.insert_order(order);
self.prioritized_order_store.insert_order(order);
}

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

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

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

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

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

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

Expand All @@ -195,7 +166,6 @@ pub fn block_orders_from_sim_orders(
sim_orders: &[SimulatedOrder],
sorting: Sorting,
state_provider: &StateProviderBox,
sbundle_merger_selected_signers: &[Address],
) -> ProviderResult<BlockOrders> {
let mut onchain_nonces = vec![];
for order in sim_orders {
Expand All @@ -209,8 +179,7 @@ pub fn block_orders_from_sim_orders(
});
}
}
let mut block_orders =
BlockOrders::new(sorting, onchain_nonces, sbundle_merger_selected_signers);
let mut block_orders = BlockOrders::new(sorting, onchain_nonces);

for order in sim_orders.iter().cloned() {
block_orders.add_order(order);
Expand Down Expand Up @@ -240,7 +209,7 @@ mod test {
nonce.clone(),
TestContext {
data_gen,
order_pool: BlockOrders::new(Sorting::MaxProfit, vec![nonce], &[]),
order_pool: BlockOrders::new(Sorting::MaxProfit, vec![nonce]),
},
)
}
Expand All @@ -258,7 +227,7 @@ mod test {
nonce_2.clone(),
TestContext {
data_gen,
order_pool: BlockOrders::new(Sorting::MaxProfit, vec![nonce_1, nonce_2], &[]),
order_pool: BlockOrders::new(Sorting::MaxProfit, vec![nonce_1, nonce_2]),
},
)
}
Expand Down
14 changes: 4 additions & 10 deletions crates/rbuilder/src/building/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use tokio::sync::{broadcast, broadcast::error::TryRecvError};
use tokio_util::sync::CancellationToken;
use tracing::{info, warn};

use super::simulated_order_command_to_sink;

/// Block we built
#[derive(Debug, Clone)]
pub struct Block {
Expand All @@ -47,7 +49,6 @@ pub struct LiveBuilderInput<P, DB> {
pub sink: Arc<dyn UnfinishedBlockBuildingSink>,
pub builder_name: String,
pub cancel: CancellationToken,
pub sbundle_mergeabe_signers: Vec<Address>,
phantom: PhantomData<DB>,
}

Expand Down Expand Up @@ -98,12 +99,7 @@ impl OrderConsumer {
// Apply insertions and sbundle cancellations on sink
pub fn apply_new_commands<SinkType: SimulatedOrderSink>(&mut self, sink: &mut SinkType) {
for order_command in self.new_commands.drain(..) {
match order_command {
SimulatedOrderCommand::Simulation(sim_order) => sink.insert_order(sim_order),
SimulatedOrderCommand::Cancellation(id) => {
let _ = sink.remove_order(id);
}
};
simulated_order_command_to_sink(order_command, sink);
}
}
}
Expand All @@ -128,13 +124,12 @@ where
orders: broadcast::Receiver<SimulatedOrderCommand>,
parent_block: B256,
sorting: Sorting,
sbundle_merger_selected_signers: &[Address],
) -> Self {
let nonce_cache = NonceCache::new(provider, parent_block);

Self {
nonce_cache,
block_orders: BlockOrders::new(sorting, vec![], sbundle_merger_selected_signers),
block_orders: BlockOrders::new(sorting, vec![]),
onchain_nonces_updated: HashSet::default(),
order_consumer: OrderConsumer::new(orders),
}
Expand Down Expand Up @@ -245,7 +240,6 @@ pub trait UnfinishedBlockBuildingSinkFactory: Debug + Send + Sync {
pub struct BacktestSimulateBlockInput<'a, P> {
pub ctx: BlockBuildingContext,
pub builder_name: String,
pub sbundle_mergeabe_signers: Vec<Address>,
pub sim_orders: &'a Vec<SimulatedOrder>,
pub provider: P,
pub cached_reads: Option<CachedReads>,
Expand Down
14 changes: 2 additions & 12 deletions crates/rbuilder/src/building/builders/ordering_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::{
roothash::RootHashConfig,
};
use ahash::{HashMap, HashSet};
use alloy_primitives::Address;
use reth::revm::cached::CachedReads;
use reth_db::database::Database;
use reth_provider::{BlockReader, DatabaseProviderFactory, StateProviderFactory};
Expand Down Expand Up @@ -75,7 +74,6 @@ where
input.input,
input.ctx.attributes.parent,
config.sorting,
&input.sbundle_mergeabe_signers,
);

let mut builder = OrderingBuilderContext::new(
Expand Down Expand Up @@ -147,12 +145,8 @@ where
let state_provider = input
.provider
.history_by_block_number(input.ctx.block_env.number.to::<u64>() - 1)?;
let block_orders = block_orders_from_sim_orders(
input.sim_orders,
ordering_config.sorting,
&state_provider,
&input.sbundle_mergeabe_signers,
)?;
let block_orders =
block_orders_from_sim_orders(input.sim_orders, ordering_config.sorting, &state_provider)?;
let mut builder = OrderingBuilderContext::new(
input.provider.clone(),
input.builder_name,
Expand Down Expand Up @@ -346,21 +340,18 @@ where
#[derive(Debug)]
pub struct OrderingBuildingAlgorithm {
root_hash_config: RootHashConfig,
sbundle_mergeabe_signers: Vec<Address>,
config: OrderingBuilderConfig,
name: String,
}

impl OrderingBuildingAlgorithm {
pub fn new(
root_hash_config: RootHashConfig,
sbundle_mergeabe_signers: Vec<Address>,
config: OrderingBuilderConfig,
name: String,
) -> Self {
Self {
root_hash_config,
sbundle_mergeabe_signers,
config,
name,
}
Expand Down Expand Up @@ -388,7 +379,6 @@ where
sink: input.sink,
builder_name: self.name.clone(),
cancel: input.cancel,
sbundle_mergeabe_signers: self.sbundle_mergeabe_signers.clone(),
phantom: Default::default(),
};
run_ordering_builder(live_input, &self.config);
Expand Down
Loading
Loading