Skip to content

Commit

Permalink
more patches-or-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Keszey Dániel authored and Keszey Dániel committed Nov 8, 2024
1 parent 3e2fc0d commit a775147
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 147 deletions.
7 changes: 6 additions & 1 deletion crates/rbuilder/src/backtest/backtest_build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,17 @@ where
.builders
.iter()
.filter_map(|builder_name: &String| {
// HashMap with a provider factory
let mut providers = HashMap::default();
// Use ctx.parent_chain_id as the key - one is OK for testing
providers.insert(ctx.parent_chain_id, provider_factory.clone());

let input = BacktestSimulateBlockInput {
ctx: ctx.clone(),
builder_name: builder_name.clone(),
sbundle_mergeabe_signers: sbundle_mergeabe_signers.clone(),
sim_orders: &sim_orders,
provider_factory: provider_factory.clone(),
provider_factory: providers,
cached_reads: None,
};
let build_res = config.build_backtest_block(builder_name, input);
Expand Down
16 changes: 10 additions & 6 deletions crates/rbuilder/src/backtest/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
},
live_builder::cli::LiveBuilderConfig,
primitives::{OrderId, SimulatedOrder},
utils::{clean_extradata, Signer},
utils::{clean_extradata, Signer, provider_factory_reopen::ConsistencyReopener},
};
use ahash::{HashMap, HashSet};
use alloy_primitives::{Address, U256};
Expand Down Expand Up @@ -55,7 +55,7 @@ pub struct BacktestBlockInput {
pub sim_errors: Vec<OrderErr>,
}

pub fn backtest_prepare_ctx_for_block<P>(
pub fn backtest_prepare_ctx_for_block<P, DB>(
block_data: BlockData,
provider_factory: P,
chain_spec: Arc<ChainSpec>,
Expand All @@ -64,7 +64,7 @@ pub fn backtest_prepare_ctx_for_block<P>(
builder_signer: Signer,
) -> eyre::Result<BacktestBlockInput>
where
P: StateProviderFactory + Clone + 'static,
P: StateProviderFactory + ConsistencyReopener<DB> + Clone + 'static,
{
let orders = block_data
.available_orders
Expand Down Expand Up @@ -101,7 +101,7 @@ where
#[allow(clippy::too_many_arguments)]
pub fn backtest_simulate_block<P, DB, ConfigType>(
block_data: BlockData,
provider_factory: P,
provider_factory: P, // Keep this as is for now
chain_spec: Arc<ChainSpec>,
build_block_lag_ms: i64,
builders_names: Vec<String>,
Expand All @@ -111,7 +111,7 @@ pub fn backtest_simulate_block<P, DB, ConfigType>(
) -> eyre::Result<BlockBacktestValue>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + ConsistencyReopener<DB> + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let BacktestBlockInput {
Expand Down Expand Up @@ -161,12 +161,16 @@ where

let mut cached_reads = Some(CachedReads::default());
for building_algorithm_name in builders_names {
// Create HashMap for the provider
let mut providers = HashMap::default();
providers.insert(chain_spec.chain.id(), provider_factory.clone());

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_factory: provider_factory.clone(),
provider_factory: providers, // Pass the HashMap here
cached_reads,
};

Expand Down
4 changes: 2 additions & 2 deletions crates/rbuilder/src/backtest/redistribute/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
redistribute::{calc_redistributions, RedistributionBlockOutput},
BlockData, HistoricalDataStorage,
},
live_builder::{base_config::load_config_toml_and_env, cli::LiveBuilderConfig},
live_builder::{base_config::load_config_toml_and_env, cli::LiveBuilderConfig}, utils::provider_factory_reopen::ConsistencyReopener,
};
use alloy_primitives::utils::format_ether;
use clap::Parser;
Expand Down Expand Up @@ -117,7 +117,7 @@ fn process_redisribution<P, DB, ConfigType>(
) -> eyre::Result<()>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + ConsistencyReopener<DB> + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let block_number = block_data.block_number;
Expand Down
12 changes: 6 additions & 6 deletions crates/rbuilder/src/backtest/redistribute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
},
live_builder::cli::LiveBuilderConfig,
primitives::{Order, OrderId},
utils::{signed_uint_delta, u256decimal_serde_helper},
utils::{signed_uint_delta, u256decimal_serde_helper, provider_factory_reopen::ConsistencyReopener},
};
use ahash::{HashMap, HashSet};
use alloy_primitives::{utils::format_ether, Address, B256, I256, U256};
Expand Down Expand Up @@ -126,7 +126,7 @@ pub fn calc_redistributions<P, DB, ConfigType>(
) -> eyre::Result<RedistributionBlockOutput>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + ConsistencyReopener<DB> + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let _block_span = info_span!("block", block = block_data.block_number).entered();
Expand Down Expand Up @@ -484,7 +484,7 @@ fn calculate_backtest_without_exclusion<P, DB, ConfigType>(
) -> eyre::Result<ResultsWithoutExclusion>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + ConsistencyReopener<DB> + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let ExclusionResult {
Expand Down Expand Up @@ -550,7 +550,7 @@ fn calculate_backtest_identity_and_order_exclusion<P, DB, ConfigType>(
) -> eyre::Result<ExclusionResults>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + ConsistencyReopener<DB> + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let included_orders_exclusion = {
Expand Down Expand Up @@ -621,7 +621,7 @@ fn calc_joint_exclusion_results<P, DB, ConfigType>(
) -> eyre::Result<ExclusionResults>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + ConsistencyReopener<DB> + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
// calculate identities that are possibly connected
Expand Down Expand Up @@ -955,7 +955,7 @@ fn calc_profit_after_exclusion<P, DB, ConfigType>(
) -> eyre::Result<ExclusionResult>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + HeaderProvider + ConsistencyReopener<DB> + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let block_data_with_excluded = {
Expand Down
4 changes: 2 additions & 2 deletions crates/rbuilder/src/building/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ pub trait UnfinishedBlockBuildingSinkFactory: Debug + Send + Sync {
) -> Arc<dyn UnfinishedBlockBuildingSink>;
}

/// Basic configuration to run a single block building with a BlockBuildingAlgorithm
/// Basic configuration to run a single block building with a BlockBuildingAlgorithmpub
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_factory: P,
pub provider_factory: HashMap<u64, P>,
pub cached_reads: Option<CachedReads>,
}

Expand Down
19 changes: 15 additions & 4 deletions crates/rbuilder/src/building/builders/ordering_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,21 @@ where
let mut ctxs = HashMap::default();
ctxs.insert(input.ctx.parent_chain_id, input.ctx.clone());
let use_suggested_fee_recipient_as_coinbase = ordering_config.coinbase_payment;
let state_provider = input
.provider_factory
.history_by_block_number(input.ctx.chains[&input.ctx.parent_chain_id].block_env.number.to::<u64>() - 1)?;

// Get the provider factory for parent chain first
let provider_factory = input.provider_factory.get(&input.ctx.parent_chain_id)
.ok_or_else(|| eyre::eyre!("No provider factory found for parent chain {}", input.ctx.parent_chain_id))?;

let state_provider = provider_factory
.history_by_block_number(input.ctx.chains[&input.ctx.parent_chain_id].block_env.number.to::<u64>() - 1)?;

let mut state_for_sim: HashMap<u64, Arc<dyn StateProvider>> = HashMap::default();

// Iterate through chains and set up state providers
for (chain_id, provider) in input.provider_factory.iter() {
let state = provider.history_by_block_hash(input.ctx.chains[chain_id].attributes.parent)?;
state_for_sim.insert(*chain_id, Arc::from(state));
}
todo!()

// let mut state_for_sim: HashMap<u64, Arc<dyn StateProvider>> = HashMap::default();
Expand Down Expand Up @@ -401,7 +412,7 @@ impl OrderingBuildingAlgorithm {
impl<P, DB> BlockBuildingAlgorithm<P, DB> for OrderingBuildingAlgorithm
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + Clone + 'static,
P: DatabaseProviderFactory<DB> + StateProviderFactory + ProviderFactoryUnchecked<DB> + Clone + 'static,
{
fn name(&self) -> String {
self.name.clone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ where
orders_closed_at: OffsetDateTime,
) -> eyre::Result<Box<dyn BlockBuildingHelper>> {
let mut block_building_helper = BlockBuildingHelperFromProvider::new(
self.provider.clone(),
self.provider_factory.clone(),
self.root_hash_task_pool.clone(),
self.root_hash_config.clone(), // Adjust as needed for backtest
self.ctx.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use eyre::Result;
use itertools::Itertools;
use rand::{seq::SliceRandom, SeedableRng};
use reth::providers::StateProvider;
use reth_payload_builder::database::SyncCachedReads as CachedReads;
use reth_payload_builder::database::{SyncCachedReads, CachedReads, to_sync_cached_reads};
use reth_provider::StateProviderFactory;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
Expand All @@ -20,7 +20,7 @@ use crate::primitives::{OrderId, SimulatedOrder};
/// Context for resolving conflicts in merging tasks.
#[derive(Debug)]
pub struct ResolverContext<P> {
pub provider: P,
pub providers: HashMap<u64, P>,
pub ctx: BlockBuildingContext,
pub cancellation_token: CancellationToken,
pub cache: Option<CachedReads>,
Expand All @@ -41,14 +41,14 @@ where
/// * `cache` - Optional cached reads for optimization.
/// * `simulation_cache` - Shared cache for simulation results.
pub fn new(
provider: P,
providers: HashMap<u64, P>, // Changed parameter type
ctx: BlockBuildingContext,
cancellation_token: CancellationToken,
cache: Option<CachedReads>,
simulation_cache: Arc<SharedSimulationCache>,
) -> Self {
ResolverContext {
provider,
providers,
ctx,
cancellation_token,
cache,
Expand All @@ -71,33 +71,52 @@ where
task.group.id,
task.algorithm
);
let state_provider = self
.provider
.history_by_block_hash(self.ctx.attributes.parent)?;
let state_provider: Arc<dyn StateProvider> = Arc::from(state_provider);

let sequence_to_try = generate_sequences_of_orders_to_try(&task);


// Create a vector of the necessary data from chains to avoid borrowing issues
let chain_data: Vec<_> = self.ctx.chains
.iter()
.map(|(chain_id, chain_context)| {
(*chain_id, chain_context.attributes.parent)
})
.collect();

let mut best_resolution_result = ResolutionResult {
total_profit: U256::ZERO,
sequence_of_orders: vec![],
};

for sequence_of_orders in sequence_to_try {
let (resolution_result, state) =
self.process_sequence_of_orders(sequence_of_orders, &task, &state_provider)?;
self.update_best_result(resolution_result, &mut best_resolution_result);

let (new_cached_reads, _, _) = state.into_parts();
self.cache = Some(new_cached_reads);

// Process each chain using the collected data
for (chain_id, parent_hash) in chain_data {
trace!("Processing chain: {}", chain_id);

// Get the provider for this chain
let provider = self.providers.get(&chain_id)
.ok_or_else(|| eyre::eyre!("No provider found for chain {}", chain_id))?;

let state_provider = provider
.history_by_block_hash(parent_hash)?;
let state_provider: Arc<dyn StateProvider> = Arc::from(state_provider);

let sequence_to_try = generate_sequences_of_orders_to_try(&task);

for sequence_of_orders in sequence_to_try {
let (resolution_result, state) =
self.process_sequence_of_orders(sequence_of_orders.clone(), &task, &state_provider)?;

self.update_best_result(resolution_result, &mut best_resolution_result);

let (new_cached_reads, ..) = state.into_parts();
self.cache = Some(CachedReads::from(new_cached_reads));
}
}

trace!(
"Resolved conflict task {:?} with profit: {:?} and algorithm: {:?}",
task.group.id,
best_resolution_result.total_profit,
task.algorithm
);

Ok(best_resolution_result)
}

Expand Down Expand Up @@ -286,18 +305,38 @@ where
cached_state_option: &Option<Arc<CachedSimulationState>>,
state_provider: &Arc<dyn StateProvider>,
) -> BlockState {
if let Some(cached_state) = &cached_state_option {
// Use cached state
BlockState::new_arc(state_provider.clone())
.with_cached_reads(cached_state.cached_reads.clone())
.with_bundle_state(cached_state.bundle_state.clone())
} else {
// If we don't have a cached state from the simulation cache, we use the cached reads from the block state in some cases
if let Some(cache) = &self.cache {
BlockState::new_arc(state_provider.clone()).with_cached_reads(cache.clone())
} else {
BlockState::new_arc(state_provider.clone())
let mut state_providers: HashMap<u64, Arc<dyn StateProvider>> = HashMap::default();

// Populate state providers for each chain
for (chain_id, _) in &self.ctx.chains {
state_providers.insert(*chain_id, state_provider.clone());
}

let block_state = BlockState::new_arc(state_providers);

// Apply caching in order of precedence
match (cached_state_option, &self.cache) {
(Some(cached_state), _) => {
// Use the to_sync_cached_reads helper
let sync_cached_reads = to_sync_cached_reads(
cached_state.cached_reads.clone(),
self.ctx.parent_chain_id, // or appropriate chain_id
);

block_state
.with_cached_reads(sync_cached_reads)
.with_bundle_state(cached_state.bundle_state.clone())
}
(None, Some(cache)) => {
// Convert CachedReads to SyncCachedReads
let sync_cached_reads = to_sync_cached_reads(
cache.clone(),
self.ctx.parent_chain_id, // or appropriate chain_id
);

block_state.with_cached_reads(sync_cached_reads)
}
(None, None) => block_state,
}
}

Expand All @@ -311,7 +350,7 @@ where
) {
let (cached_reads, bundle_state, _) = state.clone().into_parts();
let cached_simulation_state = CachedSimulationState {
cached_reads,
cached_reads: CachedReads::from(cached_reads),
bundle_state,
total_profit,
per_order_profits: per_order_profits.to_owned(),
Expand Down
Loading

0 comments on commit a775147

Please sign in to comment.