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 StateProviderFactory custom trait #331

Merged
merged 18 commits into from
Jan 9, 2025
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions crates/rbuilder/src/backtest/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ use crate::{
},
live_builder::cli::LiveBuilderConfig,
primitives::{OrderId, SimulatedOrder},
provider::StateProviderFactory,
utils::{clean_extradata, Signer},
};
use ahash::HashSet;
use alloy_primitives::{Address, U256};
use reth::revm::cached::CachedReads;
use reth_chainspec::ChainSpec;
use reth_db::Database;
use reth_provider::{BlockReader, DatabaseProviderFactory, HeaderProvider, StateProviderFactory};
use serde::{Deserialize, Serialize};
use std::{cell::RefCell, rc::Rc, sync::Arc};

Expand Down Expand Up @@ -88,6 +87,7 @@ where
builder_signer.address,
block_data.winning_bid_trace.proposer_fee_recipient,
Some(builder_signer),
Arc::from(provider.root_hasher(block_data.winning_bid_trace.parent_hash)),
);
let (sim_orders, sim_errors) =
simulate_all_orders_with_sim_tree(provider.clone(), &ctx, &orders, false)?;
Expand All @@ -107,7 +107,7 @@ where
}

#[allow(clippy::too_many_arguments)]
pub fn backtest_simulate_block<P, DB, ConfigType>(
pub fn backtest_simulate_block<P, ConfigType>(
block_data: BlockData,
provider: P,
chain_spec: Arc<ChainSpec>,
Expand All @@ -118,12 +118,7 @@ pub fn backtest_simulate_block<P, DB, ConfigType>(
sbundle_mergeabe_signers: &[Address],
) -> eyre::Result<BlockBacktestValue>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB = DB, Provider: BlockReader>
+ StateProviderFactory
+ HeaderProvider
+ Clone
+ 'static,
P: StateProviderFactory + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let BacktestBlockInput {
Expand Down
12 changes: 3 additions & 9 deletions crates/rbuilder/src/backtest/redistribute/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ use crate::{
BlockData, HistoricalDataStorage,
},
live_builder::{base_config::load_config_toml_and_env, cli::LiveBuilderConfig},
provider::StateProviderFactory,
};
use alloy_primitives::utils::format_ether;
use clap::Parser;
use csv_output::{CSVOutputRow, CSVResultWriter};
use reth_db::Database;
use reth_provider::{BlockReader, DatabaseProviderFactory, HeaderProvider, StateProviderFactory};
use std::{io, path::PathBuf};
use tracing::info;

Expand Down Expand Up @@ -107,7 +106,7 @@ where
Ok(())
}

fn process_redisribution<P, DB, ConfigType>(
fn process_redisribution<P, ConfigType>(
block_data: BlockData,
csv_writer: Option<&mut CSVResultWriter>,
json_accum: Option<&mut Vec<RedistributionBlockOutput>>,
Expand All @@ -116,12 +115,7 @@ fn process_redisribution<P, DB, ConfigType>(
distribute_to_mempool_txs: bool,
) -> eyre::Result<()>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB = DB, Provider: BlockReader>
+ StateProviderFactory
+ HeaderProvider
+ Clone
+ 'static,
P: StateProviderFactory + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let block_number = block_data.block_number;
Expand Down
50 changes: 12 additions & 38 deletions crates/rbuilder/src/backtest/redistribute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ use crate::{
},
live_builder::cli::LiveBuilderConfig,
primitives::{Order, OrderId},
provider::StateProviderFactory,
utils::{signed_uint_delta, u256decimal_serde_helper},
};
use ahash::{HashMap, HashSet};
use alloy_primitives::{utils::format_ether, Address, B256, I256, U256};
pub use cli::run_backtest_redistribute;
use rayon::prelude::*;
use reth_chainspec::ChainSpec;
use reth_db::Database;
use reth_provider::{BlockReader, DatabaseProviderFactory, HeaderProvider, StateProviderFactory};
use serde::{Deserialize, Serialize};
use std::{
cmp::{max, min},
Expand Down Expand Up @@ -117,19 +116,14 @@ pub struct RedistributionBlockOutput {
pub joint_contribution: Vec<JointContributionData>,
}

pub fn calc_redistributions<P, DB, ConfigType>(
pub fn calc_redistributions<P, ConfigType>(
provider: P,
config: &ConfigType,
block_data: BlockData,
distribute_to_mempool_txs: bool,
) -> eyre::Result<RedistributionBlockOutput>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB = DB, Provider: BlockReader>
+ StateProviderFactory
+ HeaderProvider
+ Clone
+ 'static,
P: StateProviderFactory + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let _block_span = info_span!("block", block = block_data.block_number).entered();
Expand Down Expand Up @@ -280,7 +274,7 @@ fn restore_available_landed_orders<P>(
included_orders_available: &[OrdersWithTimestamp],
) -> eyre::Result<HashMap<OrderId, LandedOrderData>>
where
P: StateProviderFactory + HeaderProvider + Clone + 'static,
P: StateProviderFactory + Clone + 'static,
{
let block_txs = sim_historical_block(
provider.clone(),
Expand Down Expand Up @@ -480,18 +474,13 @@ impl ResultsWithoutExclusion {
}
}

fn calculate_backtest_without_exclusion<P, DB, ConfigType>(
fn calculate_backtest_without_exclusion<P, ConfigType>(
provider: P,
config: &ConfigType,
block_data: BlockData,
) -> eyre::Result<ResultsWithoutExclusion>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB = DB, Provider: BlockReader>
+ StateProviderFactory
+ HeaderProvider
+ Clone
+ 'static,
P: StateProviderFactory + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let ExclusionResult {
Expand Down Expand Up @@ -548,20 +537,15 @@ impl ExclusionResults {
}
}

fn calculate_backtest_identity_and_order_exclusion<P, DB, ConfigType>(
fn calculate_backtest_identity_and_order_exclusion<P, ConfigType>(
provider: P,
config: &ConfigType,
block_data: BlockData,
available_orders: &AvailableOrders,
results_without_exclusion: &ResultsWithoutExclusion,
) -> eyre::Result<ExclusionResults>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB = DB, Provider: BlockReader>
+ StateProviderFactory
+ HeaderProvider
+ Clone
+ 'static,
P: StateProviderFactory + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let included_orders_exclusion = {
Expand Down Expand Up @@ -621,7 +605,7 @@ where
})
}

fn calc_joint_exclusion_results<P, DB, ConfigType>(
fn calc_joint_exclusion_results<P, ConfigType>(
provider: P,
config: &ConfigType,
block_data: BlockData,
Expand All @@ -631,12 +615,7 @@ fn calc_joint_exclusion_results<P, DB, ConfigType>(
distribute_to_mempool_txs: bool,
) -> eyre::Result<ExclusionResults>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB = DB, Provider: BlockReader>
+ StateProviderFactory
+ HeaderProvider
+ Clone
+ 'static,
P: StateProviderFactory + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
// calculate identities that are possibly connected
Expand Down Expand Up @@ -962,19 +941,14 @@ struct ExclusionResult {
}

/// calculate block profit excluding some orders
fn calc_profit_after_exclusion<P, DB, ConfigType>(
fn calc_profit_after_exclusion<P, ConfigType>(
provider: P,
config: &ConfigType,
block_data: &BlockData,
exclusion_input: ExclusionInput,
) -> eyre::Result<ExclusionResult>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB = DB, Provider: BlockReader>
+ StateProviderFactory
+ HeaderProvider
+ Clone
+ 'static,
P: StateProviderFactory + Clone + 'static,
ConfigType: LiveBuilderConfig,
{
let block_data_with_excluded = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use crate::{
evm_inspector::SlotKey, tracers::AccumulatorSimulationTracer, BlockBuildingContext,
BlockState, PartialBlock, PartialBlockFork,
},
provider::StateProviderFactory,
utils::{extract_onchain_block_txs, find_suggested_fee_recipient, signed_uint_delta},
};
use ahash::{HashMap, HashSet};
use alloy_primitives::{TxHash, B256, I256};
use eyre::Context;
use reth_chainspec::ChainSpec;
use reth_primitives::{Receipt, TransactionSignedEcRecovered};
use reth_provider::StateProviderFactory;
use std::sync::Arc;

#[derive(Debug)]
Expand Down Expand Up @@ -40,7 +40,9 @@ where
let txs = extract_onchain_block_txs(&onchain_block)?;

let suggested_fee_recipient = find_suggested_fee_recipient(&onchain_block, &txs);

let coinbase = onchain_block.header.beneficiary;
let parent_hash = onchain_block.header.parent_hash;

let ctx = BlockBuildingContext::from_onchain_block(
onchain_block,
Expand All @@ -50,6 +52,7 @@ where
coinbase,
suggested_fee_recipient,
None,
Arc::from(provider.root_hasher(parent_hash)),
);

let state_provider = provider.history_by_block_hash(ctx.attributes.parent)?;
Expand Down
15 changes: 5 additions & 10 deletions crates/rbuilder/src/bin/debug-bench-machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use itertools::Itertools;
use rbuilder::{
building::{BlockBuildingContext, BlockState, PartialBlock, PartialBlockFork},
live_builder::{base_config::load_config_toml_and_env, cli::LiveBuilderConfig, config::Config},
provider::StateProviderFactory,
utils::{extract_onchain_block_txs, find_suggested_fee_recipient, http_provider},
};
use reth::{providers::BlockNumReader, revm::cached::CachedReads};
use reth::revm::cached::CachedReads;
use reth_provider::StateProvider;
use std::{path::PathBuf, sync::Arc, time::Instant};
use tracing::{debug, info};
Expand Down Expand Up @@ -61,6 +62,7 @@ async fn main() -> eyre::Result<()> {

let coinbase = onchain_block.header.beneficiary;

let parent_hash = onchain_block.header.parent_hash;
let ctx = BlockBuildingContext::from_onchain_block(
onchain_block,
chain_spec,
Expand All @@ -69,6 +71,7 @@ async fn main() -> eyre::Result<()> {
coinbase,
suggested_fee_recipient,
None,
Arc::from(provider_factory.root_hasher(parent_hash)),
);

let state_provider = Arc::<dyn StateProvider>::from(
Expand All @@ -84,9 +87,6 @@ async fn main() -> eyre::Result<()> {
let ctx = ctx.clone();
let txs = txs.clone();
let state_provider = state_provider.clone();
let factory = provider_factory.clone();
let config = config.clone();
let root_hash_config = config.base_config.live_root_hash_config()?;
let (new_cached_reads, build_time, finalize_time) =
tokio::task::spawn_blocking(move || -> eyre::Result<_> {
let partial_block = PartialBlock::new(true, None);
Expand All @@ -112,12 +112,7 @@ async fn main() -> eyre::Result<()> {
let build_time = build_time.elapsed();

let finalize_time = Instant::now();
let finalized_block = partial_block.finalize(
&mut state,
&ctx,
factory.clone(),
root_hash_config.clone(),
)?;
let finalized_block = partial_block.finalize(&mut state, &ctx)?;
let finalize_time = finalize_time.elapsed();

debug!(
Expand Down
24 changes: 7 additions & 17 deletions crates/rbuilder/src/bin/dummy-builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ use rbuilder::{
mev_boost::{MevBoostRelay, RelayConfig},
SimulatedOrder,
},
roothash::RootHashConfig,
provider::StateProviderFactory,
utils::{ProviderFactoryReopener, Signer},
};
use reth_chainspec::MAINNET;
use reth_db::{database::Database, DatabaseEnv};
use reth_db::DatabaseEnv;
use reth_node_api::NodeTypesWithDBAdapter;
use reth_node_ethereum::EthereumNode;
use reth_provider::{BlockReader, DatabaseProviderFactory, StateProviderFactory};
use tokio::{
signal::ctrl_c,
sync::{broadcast, mpsc},
Expand Down Expand Up @@ -89,7 +88,6 @@ async fn main() -> eyre::Result<()> {
mpsc::channel(order_input_config.input_channel_buffer_size);
let builder = LiveBuilder::<
ProviderFactoryReopener<NodeTypesWithDBAdapter<EthereumNode, Arc<DatabaseEnv>>>,
Arc<DatabaseEnv>,
MevBoostSlotDataGenerator,
> {
watchdog_timeout: Some(Duration::from_secs(10000)),
Expand All @@ -103,6 +101,7 @@ async fn main() -> eyre::Result<()> {
None,
None,
chain_spec.clone(),
None,
)?,
coinbase_signer: Signer::random(),
extra_data: Vec::new(),
Expand Down Expand Up @@ -199,22 +198,17 @@ impl DummyBuildingAlgorithm {
}
}

fn build_block<P, DB>(
fn build_block<P>(
&self,
orders: Vec<SimulatedOrder>,
provider: P,
ctx: &BlockBuildingContext,
) -> eyre::Result<Box<dyn BlockBuildingHelper>>
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB = DB, Provider: BlockReader>
+ StateProviderFactory
+ Clone
+ 'static,
P: StateProviderFactory + Clone + 'static,
{
let mut block_building_helper = BlockBuildingHelperFromProvider::new(
provider.clone(),
RootHashConfig::live_config(false, false),
ctx.clone(),
None,
BUILDER_NAME.to_string(),
Expand All @@ -231,13 +225,9 @@ impl DummyBuildingAlgorithm {
}
}

impl<P, DB> BlockBuildingAlgorithm<P, DB> for DummyBuildingAlgorithm
impl<P> BlockBuildingAlgorithm<P> for DummyBuildingAlgorithm
where
DB: Database + Clone + 'static,
P: DatabaseProviderFactory<DB = DB, Provider: BlockReader>
+ StateProviderFactory
+ Clone
+ 'static,
P: StateProviderFactory + Clone + 'static,
{
fn name(&self) -> String {
BUILDER_NAME.to_string()
Expand Down
Loading
Loading