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

Add a trait to abstract the access to the state #37

Closed
wants to merge 16 commits into from
15 changes: 6 additions & 9 deletions crates/rbuilder/src/backtest/backtest_build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
use ahash::HashMap;
use alloy_primitives::utils::format_ether;

use crate::backtest::restore_landed_orders::{
restore_landed_orders, sim_historical_block, ExecutedBlockTx, ExecutedTxs, SimplifiedOrder,
};
use crate::backtest::OrdersWithTimestamp;
use crate::{
backtest::{
execute::{backtest_prepare_ctx_for_block, BacktestBlockInput},
BlockData, HistoricalDataStorage,
restore_landed_orders::{
restore_landed_orders, sim_historical_block, ExecutedBlockTx, ExecutedTxs,
SimplifiedOrder,
},
BlockData, HistoricalDataStorage, OrdersWithTimestamp,
},
building::builders::BacktestSimulateBlockInput,
live_builder::{base_config::load_config_toml_and_env, cli::LiveBuilderConfig},
Expand Down Expand Up @@ -85,10 +85,7 @@ pub async fn run_backtest_build_block<ConfigType: LiveBuilderConfig>() -> eyre::
print_order_and_timestamp(&block_data.available_orders, &block_data);
}

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

Expand Down
5 changes: 1 addition & 4 deletions crates/rbuilder/src/backtest/backtest_build_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ pub async fn run_backtest_build_range<ConfigType: LiveBuilderConfig + Send + Syn
result
};

let provider_factory = config
.base_config()
.provider_factory()?
.provider_factory_unchecked();
let provider_factory = config.base_config().provider_factory()?;
let chain_spec = config.base_config().chain_spec()?;

let mut profits = Vec::new();
Expand Down
20 changes: 10 additions & 10 deletions crates/rbuilder/src/backtest/execute.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use crate::primitives::OrderId;
use crate::utils::Signer;
use crate::{
backtest::BlockData,
building::{
builders::BacktestSimulateBlockInput, sim::simulate_all_orders_with_sim_tree,
BlockBuildingContext, BundleErr, OrderErr, TransactionErr,
},
live_builder::cli::LiveBuilderConfig,
primitives::SimulatedOrder,
utils::clean_extradata,
primitives::{OrderId, SimulatedOrder},
provider::StateProviderFactory,
utils::{clean_extradata, Signer},
};
use ahash::HashSet;
use alloy_primitives::{Address, U256};
use reth::providers::ProviderFactory;
use reth_chainspec::ChainSpec;
use reth_db::{database::Database, DatabaseEnv};
use reth_payload_builder::database::CachedReads;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
Expand Down Expand Up @@ -57,9 +54,9 @@ pub struct BacktestBlockInput {
pub sim_errors: Vec<OrderErr>,
}

pub fn backtest_prepare_ctx_for_block<DB: Database + Clone>(
pub fn backtest_prepare_ctx_for_block<Provider: StateProviderFactory + Clone>(
block_data: BlockData,
provider_factory: ProviderFactory<DB>,
provider_factory: Provider,
chain_spec: Arc<ChainSpec>,
build_block_lag_ms: i64,
blocklist: HashSet<Address>,
Expand Down Expand Up @@ -96,9 +93,12 @@ pub fn backtest_prepare_ctx_for_block<DB: Database + Clone>(
}

#[allow(clippy::too_many_arguments)]
pub fn backtest_simulate_block<ConfigType: LiveBuilderConfig>(
pub fn backtest_simulate_block<
ConfigType: LiveBuilderConfig,
Provider: StateProviderFactory + Clone + 'static,
>(
block_data: BlockData,
provider_factory: ProviderFactory<Arc<DatabaseEnv>>,
provider_factory: Provider,
chain_spec: Arc<ChainSpec>,
build_block_lag_ms: i64,
builders_names: Vec<String>,
Expand Down
32 changes: 16 additions & 16 deletions crates/rbuilder/src/backtest/redistribute/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
mod csv_output;

use crate::backtest::redistribute::{calc_redistributions, RedistributionBlockOutput};
use crate::backtest::BlockData;
use crate::live_builder::base_config::load_config_toml_and_env;
use crate::live_builder::cli::LiveBuilderConfig;
use crate::{backtest::HistoricalDataStorage, live_builder::config::Config};
use crate::{
backtest::{
redistribute::{calc_redistributions, RedistributionBlockOutput},
BlockData, HistoricalDataStorage,
},
live_builder::{base_config::load_config_toml_and_env, cli::LiveBuilderConfig, config::Config},
provider::StateProviderFactory,
};
use alloy_primitives::utils::format_ether;
use clap::Parser;
use csv_output::{CSVOutputRow, CSVResultWriter};
use reth_db::DatabaseEnv;
use reth_provider::ProviderFactory;
use std::io;
use std::path::PathBuf;
use std::sync::Arc;
use std::{io, path::PathBuf};
use tracing::info;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -54,10 +53,8 @@ pub async fn run_backtest_redistribute<ConfigType: LiveBuilderConfig>() -> eyre:
let mut historical_data_storage =
HistoricalDataStorage::new_from_path(&config.base_config.backtest_fetch_output_file)
.await?;
let provider_factory = config
.base_config
.provider_factory()?
.provider_factory_unchecked();
let provider_factory = config.base_config.provider_factory()?;

let mut csv_writer = cli
.csv
.map(|path| -> io::Result<_> { CSVResultWriter::new(path) })
Expand Down Expand Up @@ -107,11 +104,14 @@ pub async fn run_backtest_redistribute<ConfigType: LiveBuilderConfig>() -> eyre:
Ok(())
}

fn process_redisribution<ConfigType: LiveBuilderConfig + Send + Sync>(
fn process_redisribution<
ConfigType: LiveBuilderConfig + Send + Sync,
Provider: StateProviderFactory + Clone + 'static,
>(
block_data: BlockData,
csv_writer: Option<&mut CSVResultWriter>,
json_accum: Option<&mut Vec<RedistributionBlockOutput>>,
provider_factory: ProviderFactory<Arc<DatabaseEnv>>,
provider_factory: Provider,
config: &ConfigType,
distribute_to_mempool_txs: bool,
) -> eyre::Result<()> {
Expand Down
78 changes: 49 additions & 29 deletions crates/rbuilder/src/backtest/redistribute/mod.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
mod cli;
mod redistribution_algo;

use crate::backtest::execute::backtest_simulate_block;
use crate::backtest::redistribute::redistribution_algo::{
IncludedOrderData, RedistributionCalculator, RedistributionIdentityData, RedistributionResult,
};
use crate::backtest::restore_landed_orders::{
restore_landed_orders, sim_historical_block, ExecutedBlockTx, LandedOrderData, SimplifiedOrder,
use crate::{
backtest::{
execute::backtest_simulate_block,
redistribute::redistribution_algo::{
IncludedOrderData, RedistributionCalculator, RedistributionIdentityData,
RedistributionResult,
},
restore_landed_orders::{
restore_landed_orders, sim_historical_block, ExecutedBlockTx, LandedOrderData,
SimplifiedOrder,
},
BlockData, BuiltBlockData, OrdersWithTimestamp,
},
live_builder::cli::LiveBuilderConfig,
primitives::{Order, OrderId},
provider::StateProviderFactory,
utils::{signed_uint_delta, u256decimal_serde_helper},
};
use crate::backtest::{BlockData, BuiltBlockData, OrdersWithTimestamp};
use crate::live_builder::cli::LiveBuilderConfig;
use crate::primitives::{Order, OrderId};
use crate::utils::signed_uint_delta;
use crate::utils::u256decimal_serde_helper;
use ahash::{HashMap, HashSet};
use alloy_primitives::utils::format_ether;
use alloy_primitives::{Address, B256, I256, U256};
use alloy_primitives::{utils::format_ether, Address, B256, I256, U256};
pub use cli::run_backtest_redistribute;
use jsonrpsee::core::Serialize;
use rayon::prelude::*;
use reth_chainspec::ChainSpec;
use reth_db::DatabaseEnv;
use reth_provider::ProviderFactory;
use std::cmp::{max, min};
use std::sync::Arc;
use std::{
cmp::{max, min},
sync::Arc,
};
use tracing::{debug, info, info_span, trace, warn};

#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -87,8 +92,11 @@ pub struct RedistributionBlockOutput {
pub joint_contribution: Vec<JointContributionData>,
}

pub fn calc_redistributions<ConfigType: LiveBuilderConfig + Send + Sync>(
provider_factory: ProviderFactory<Arc<DatabaseEnv>>,
pub fn calc_redistributions<
ConfigType: LiveBuilderConfig + Send + Sync,
Provider: StateProviderFactory + Clone + 'static,
>(
provider_factory: Provider,
config: &ConfigType,
block_data: BlockData,
distribute_to_mempool_txs: bool,
Expand Down Expand Up @@ -233,8 +241,8 @@ fn get_available_orders(
included_orders_available
}

fn restore_available_landed_orders(
provider_factory: ProviderFactory<Arc<DatabaseEnv>>,
fn restore_available_landed_orders<Provider: StateProviderFactory + Clone>(
provider_factory: Provider,
chain_spec: Arc<ChainSpec>,
block_data: &BlockData,
included_orders_available: &[OrdersWithTimestamp],
Expand Down Expand Up @@ -427,8 +435,11 @@ impl ResultsWithoutExclusion {
}
}

fn calculate_backtest_without_exclusion<ConfigType: LiveBuilderConfig>(
provider_factory: ProviderFactory<Arc<DatabaseEnv>>,
fn calculate_backtest_without_exclusion<
ConfigType: LiveBuilderConfig,
Provider: StateProviderFactory + Clone + 'static,
>(
provider_factory: Provider,
config: &ConfigType,
block_data: BlockData,
) -> eyre::Result<ResultsWithoutExclusion> {
Expand Down Expand Up @@ -486,8 +497,11 @@ impl ExclusionResults {
}
}

fn calculate_backtest_identity_and_order_exclusion<ConfigType: LiveBuilderConfig + Sync>(
provider_factory: ProviderFactory<Arc<DatabaseEnv>>,
fn calculate_backtest_identity_and_order_exclusion<
ConfigType: LiveBuilderConfig + Sync,
Provider: StateProviderFactory + Clone + 'static,
>(
provider_factory: Provider,
config: &ConfigType,
block_data: BlockData,
available_orders: &AvailableOrders,
Expand Down Expand Up @@ -550,8 +564,11 @@ fn calculate_backtest_identity_and_order_exclusion<ConfigType: LiveBuilderConfig
})
}

fn calc_joint_exclusion_results<ConfigType: LiveBuilderConfig + Sync>(
provider_factory: ProviderFactory<Arc<DatabaseEnv>>,
fn calc_joint_exclusion_results<
ConfigType: LiveBuilderConfig + Sync,
Provider: StateProviderFactory + Clone + 'static,
>(
provider_factory: Provider,
config: &ConfigType,
block_data: BlockData,
available_orders: &AvailableOrders,
Expand Down Expand Up @@ -864,8 +881,11 @@ struct ExclusionResult {
}

/// calculate block profit excluding some orders
fn calc_profit_after_exclusion<ConfigType: LiveBuilderConfig>(
provider_factory: ProviderFactory<Arc<DatabaseEnv>>,
fn calc_profit_after_exclusion<
ConfigType: LiveBuilderConfig,
Provider: StateProviderFactory + Clone + 'static,
>(
provider_factory: Provider,
config: &ConfigType,
block_data: &BlockData,
exclusion_input: ExclusionInput,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use crate::building::evm_inspector::SlotKey;
use crate::building::tracers::AccumulatorSimulationTracer;
use crate::building::{BlockBuildingContext, BlockState, PartialBlock, PartialBlockFork};
use crate::primitives::serialize::{RawTx, TxEncoding};
use crate::primitives::TransactionSignedEcRecoveredWithBlobs;
use crate::utils::signed_uint_delta;
use crate::{
building::{
evm_inspector::SlotKey, tracers::AccumulatorSimulationTracer, BlockBuildingContext,
BlockState, PartialBlock, PartialBlockFork,
},
primitives::{
serialize::{RawTx, TxEncoding},
TransactionSignedEcRecoveredWithBlobs,
},
provider::StateProviderFactory,
utils::signed_uint_delta,
};
use ahash::{HashMap, HashSet};
use alloy_consensus::TxEnvelope;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{Address, B256, I256};
use eyre::Context;
use reth_chainspec::ChainSpec;
use reth_db::DatabaseEnv;
use reth_primitives::{Receipt, TransactionSignedEcRecovered};
use reth_provider::ProviderFactory;
use std::sync::Arc;

#[derive(Debug)]
Expand All @@ -23,8 +27,8 @@ pub struct ExecutedTxs {
pub conflicting_txs: Vec<(B256, Vec<SlotKey>)>,
}

pub fn sim_historical_block(
provider_factory: ProviderFactory<Arc<DatabaseEnv>>,
pub fn sim_historical_block<Provider: StateProviderFactory>(
provider_factory: Provider,
chain_spec: Arc<ChainSpec>,
onchain_block: alloy_rpc_types::Block,
) -> eyre::Result<Vec<ExecutedTxs>> {
Expand Down
12 changes: 3 additions & 9 deletions crates/rbuilder/src/bin/debug-bench-machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ use rbuilder::{
},
live_builder::{base_config::load_config_toml_and_env, cli::LiveBuilderConfig, config::Config},
primitives::{MempoolTx, Order, TransactionSignedEcRecoveredWithBlobs},
provider::StateProviderFactory,
roothash::RootHashMode,
utils::{default_cfg_env, Signer},
};
use reth::{
payload::PayloadId,
providers::{BlockNumReader, BlockReader},
};
use reth::payload::PayloadId;
use reth_payload_builder::{database::CachedReads, EthPayloadBuilderAttributes};
use reth_provider::StateProvider;
use revm_primitives::{BlobExcessGasAndPrice, BlockEnv, SpecId};
Expand All @@ -38,10 +36,7 @@ async fn main() -> eyre::Result<()> {

let chain = config.base_config().chain_spec()?;

let factory = config
.base_config()
.provider_factory()?
.provider_factory_unchecked();
let factory = config.base_config().provider_factory()?;

let last_block = factory.last_block_number()?;
let block_data = factory
Expand Down Expand Up @@ -129,7 +124,6 @@ async fn main() -> eyre::Result<()> {
&ctx,
factory.clone(),
RootHashMode::IgnoreParentHash,
config.base_config().root_hash_task_pool()?,
)?;
let finalize_time = finalize_time.elapsed();

Expand Down
Loading
Loading