Skip to content

Commit

Permalink
Rebase top
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Aug 23, 2024
1 parent cd52d78 commit 765230d
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 95 deletions.
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
8 changes: 3 additions & 5 deletions crates/rbuilder/src/backtest/execute.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
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,
primitives::{OrderId, SimulatedOrder},
provider::StateProviderFactory,
utils::clean_extradata,
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_db::DatabaseEnv;
use reth_payload_builder::database::CachedReads;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
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
14 changes: 8 additions & 6 deletions crates/rbuilder/src/building/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct Block {
}

#[derive(Debug)]
pub struct LiveBuilderInput<DB: Database> {
pub provider_factory: ProviderFactory<DB>,
pub struct LiveBuilderInput<Provider: StateProviderFactory> {
pub provider_factory: Provider,
pub root_hash_task_pool: BlockingTaskPool,
pub ctx: BlockBuildingContext,
pub input: broadcast::Receiver<SimulatedOrderCommand>,
Expand Down Expand Up @@ -191,8 +191,8 @@ pub trait UnfinishedBlockBuildingSink: std::fmt::Debug + Send + Sync {
}

#[derive(Debug)]
pub struct BlockBuildingAlgorithmInput<DB: Database> {
pub provider_factory: ProviderFactory<DB>,
pub struct BlockBuildingAlgorithmInput<Provider: StateProviderFactory> {
pub provider_factory: Provider,
pub ctx: BlockBuildingContext,
pub input: broadcast::Receiver<SimulatedOrderCommand>,
/// output for the blocks
Expand All @@ -203,9 +203,11 @@ pub struct BlockBuildingAlgorithmInput<DB: Database> {
/// Algorithm to build blocks
/// build_blocks should send block to input.sink until input.cancel is cancelled.
/// slot_bidder should be used to decide how much to bid.
pub trait BlockBuildingAlgorithm<DB: Database>: std::fmt::Debug + Send + Sync {
pub trait BlockBuildingAlgorithm<Provider: StateProviderFactory>:
std::fmt::Debug + Send + Sync
{
fn name(&self) -> String;
fn build_blocks(&self, input: BlockBuildingAlgorithmInput<DB>);
fn build_blocks(&self, input: BlockBuildingAlgorithmInput<Provider>);
}

/// Factory used to create UnfinishedBlockBuildingSink for builders.
Expand Down
15 changes: 8 additions & 7 deletions crates/rbuilder/src/building/builders/ordering_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ use crate::{
BlockBuildingContext, BlockOrders, ExecutionError, Sorting,
},
primitives::{AccountNonce, OrderId},
provider::StateProviderFactory,
};
use ahash::{HashMap, HashSet};
use alloy_primitives::Address;
use reth::providers::ProviderFactory;
use reth_db::database::Database;
use tokio_util::sync::CancellationToken;

use crate::{roothash::RootHashMode, utils::check_provider_factory_health};
Expand Down Expand Up @@ -61,8 +60,8 @@ impl OrderingBuilderConfig {
}
}

pub fn run_ordering_builder<DB: Database + Clone + 'static>(
input: LiveBuilderInput<DB>,
pub fn run_ordering_builder<Provider: StateProviderFactory + Clone + 'static>(
input: LiveBuilderInput<Provider>,
config: &OrderingBuilderConfig,
) {
let mut order_intake_consumer = OrderIntakeConsumer::new(
Expand Down Expand Up @@ -187,7 +186,7 @@ pub struct OrderingBuilderContext<Provider> {

impl<Provider: StateProviderFactory + Clone + 'static> OrderingBuilderContext<Provider> {
pub fn new(
provider_factory: ProviderFactory<DB>,
provider_factory: Provider,
root_hash_task_pool: BlockingTaskPool,
builder_name: String,
ctx: BlockBuildingContext,
Expand Down Expand Up @@ -359,12 +358,14 @@ impl OrderingBuildingAlgorithm {
}
}

impl<DB: Database + Clone + 'static> BlockBuildingAlgorithm<DB> for OrderingBuildingAlgorithm {
impl<Provider: StateProviderFactory + Clone + 'static> BlockBuildingAlgorithm<Provider>
for OrderingBuildingAlgorithm
{
fn name(&self) -> String {
self.name.clone()
}

fn build_blocks(&self, input: BlockBuildingAlgorithmInput<DB>) {
fn build_blocks(&self, input: BlockBuildingAlgorithmInput<Provider>) {
let live_input = LiveBuilderInput {
provider_factory: input.provider_factory,
root_hash_task_pool: self.root_hash_task_pool.clone(),
Expand Down
2 changes: 0 additions & 2 deletions crates/rbuilder/src/building/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use crate::{
use ahash::{HashMap, HashSet};
use alloy_primitives::{Address, B256};
use rand::seq::SliceRandom;
use reth::providers::ProviderFactory;
use reth_db::database::Database;
use reth_errors::ProviderError;
use reth_payload_builder::database::CachedReads;
use reth_provider::StateProvider;
Expand Down
44 changes: 23 additions & 21 deletions crates/rbuilder/src/live_builder/base_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl BaseConfig {
cancellation_token: tokio_util::sync::CancellationToken,
sink_factory: Box<dyn UnfinishedBlockBuildingSinkFactory>,
slot_source: SlotSourceType,
) -> eyre::Result<super::LiveBuilder<Arc<DatabaseEnv>, SlotSourceType>>
) -> eyre::Result<super::LiveBuilder<ProviderFactoryReopener<Arc<DatabaseEnv>>, SlotSourceType>>
where
SlotSourceType: SlotSource,
{
Expand All @@ -172,29 +172,31 @@ impl BaseConfig {
sink_factory: Box<dyn UnfinishedBlockBuildingSinkFactory>,
slot_source: SlotSourceType,
provider_factory: ProviderFactoryReopener<Arc<DatabaseEnv>>,
) -> eyre::Result<super::LiveBuilder<Arc<DatabaseEnv>, SlotSourceType>>
) -> eyre::Result<super::LiveBuilder<ProviderFactoryReopener<Arc<DatabaseEnv>>, SlotSourceType>>
where
SlotSourceType: SlotSource,
{
Ok(LiveBuilder::<Arc<DatabaseEnv>, SlotSourceType> {
watchdog_timeout: self.watchdog_timeout(),
error_storage_path: self.error_storage_path.clone(),
simulation_threads: self.simulation_threads,
order_input_config: OrderInputConfig::from_config(self),
blocks_source: slot_source,
chain_chain_spec: self.chain_spec()?,
provider_factory,

coinbase_signer: self.coinbase_signer()?,
extra_data: self.extra_data()?,
blocklist: self.blocklist()?,

global_cancellation: cancellation_token,

extra_rpc: RpcModule::new(()),
sink_factory,
builders: Vec::new(),
})
Ok(
LiveBuilder::<ProviderFactoryReopener<Arc<DatabaseEnv>>, SlotSourceType> {
watchdog_timeout: self.watchdog_timeout(),
error_storage_path: self.error_storage_path.clone(),
simulation_threads: self.simulation_threads,
order_input_config: OrderInputConfig::from_config(self),
blocks_source: slot_source,
chain_chain_spec: self.chain_spec()?,
provider_factory,

coinbase_signer: self.coinbase_signer()?,
extra_data: self.extra_data()?,
blocklist: self.blocklist()?,

global_cancellation: cancellation_token,

extra_rpc: RpcModule::new(()),
sink_factory,
builders: Vec::new(),
},
)
}

pub fn jsonrpc_server_ip(&self) -> Ipv4Addr {
Expand Down
14 changes: 7 additions & 7 deletions crates/rbuilder/src/live_builder/building/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ use super::{
};

#[derive(Debug)]
pub struct BlockBuildingPool<DB> {
provider_factory: ProviderFactoryReopener<DB>,
builders: Vec<Arc<dyn BlockBuildingAlgorithm<DB>>>,
pub struct BlockBuildingPool<Provider> {
provider_factory: Provider,
builders: Vec<Arc<dyn BlockBuildingAlgorithm<Provider>>>,
sink_factory: Box<dyn UnfinishedBlockBuildingSinkFactory>,
orderpool_subscriber: order_input::OrderPoolSubscriber,
order_simulation_pool: OrderSimulationPool<Provider>,
}

impl<DB: Database + Clone + 'static> BlockBuildingPool<DB> {
impl<Provider: StateProviderFactory + Clone + 'static> BlockBuildingPool<Provider> {
pub fn new(
provider_factory: ProviderFactoryReopener<DB>,
builders: Vec<Arc<dyn BlockBuildingAlgorithm<DB>>>,
provider_factory: Provider,
builders: Vec<Arc<dyn BlockBuildingAlgorithm<Provider>>>,
sink_factory: Box<dyn UnfinishedBlockBuildingSinkFactory>,
orderpool_subscriber: order_input::OrderPoolSubscriber,
order_simulation_pool: OrderSimulationPool<Provider>,
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<DB: Database + Clone + 'static> BlockBuildingPool<DB> {
for builder in self.builders.iter() {
let builder_name = builder.name();
debug!(block = block_number, builder_name, "Spawning builder job");
let input = BlockBuildingAlgorithmInput::<DB> {
let input = BlockBuildingAlgorithmInput::<Provider> {
provider_factory: provider_factory.clone(),
ctx: ctx.clone(),
input: broadcast_input.subscribe(),
Expand Down
4 changes: 3 additions & 1 deletion crates/rbuilder/src/live_builder/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ pub trait LiveBuilderConfig: std::fmt::Debug + serde::de::DeserializeOwned {
&self,
cancellation_token: CancellationToken,
) -> impl std::future::Future<
Output = eyre::Result<LiveBuilder<Arc<DatabaseEnv>, MevBoostSlotDataGenerator>>,
Output = eyre::Result<
LiveBuilder<ProviderFactoryReopener<Arc<DatabaseEnv>>, MevBoostSlotDataGenerator>,
>,
> + Send;

/// Patch until we have a unified way of backtesting using the exact algorithms we use on the LiveBuilder.
Expand Down
13 changes: 8 additions & 5 deletions crates/rbuilder/src/live_builder/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
},
mev_boost::BLSBlockSigner,
primitives::mev_boost::{MevBoostRelay, RelayConfig},
provider::StateProviderFactory,
utils::{build_info::rbuilder_version, ProviderFactoryReopener, Signer},
validation_api_client::ValidationAPIClient,
};
Expand Down Expand Up @@ -285,7 +286,9 @@ impl LiveBuilderConfig for Config {
async fn create_builder(
&self,
cancellation_token: tokio_util::sync::CancellationToken,
) -> eyre::Result<super::LiveBuilder<Arc<DatabaseEnv>, MevBoostSlotDataGenerator>> {
) -> eyre::Result<
super::LiveBuilder<ProviderFactoryReopener<Arc<DatabaseEnv>>, MevBoostSlotDataGenerator>,
> {
let (sink_factory, relays) = self.l1_config.create_relays_sink_factory(
self.base_config.chain_spec()?,
Box::new(DummyBiddingService {}),
Expand Down Expand Up @@ -427,22 +430,22 @@ pub fn coinbase_signer_from_secret_key(secret_key: &str) -> eyre::Result<Signer>
Ok(Signer::try_from_secret(secret_key)?)
}

fn create_builders(
fn create_builders<Provider: StateProviderFactory>(
configs: Vec<BuilderConfig>,
root_hash_task_pool: BlockingTaskPool,
sbundle_mergeabe_signers: Vec<Address>,
) -> Vec<Arc<dyn BlockBuildingAlgorithm<Arc<DatabaseEnv>>>> {
) -> Vec<Arc<dyn BlockBuildingAlgorithm<Provider>>> {
configs
.into_iter()
.map(|cfg| create_builder(cfg, &root_hash_task_pool, &sbundle_mergeabe_signers))
.collect()
}

fn create_builder(
fn create_builder<Provider: StateProviderFactory>(
cfg: BuilderConfig,
root_hash_task_pool: &BlockingTaskPool,
sbundle_mergeabe_signers: &[Address],
) -> Arc<dyn BlockBuildingAlgorithm<Arc<DatabaseEnv>>> {
) -> Arc<dyn BlockBuildingAlgorithm<Provider>> {
match cfg.builder {
SpecificBuilderConfig::OrderingBuilder(order_cfg) => {
Arc::new(OrderingBuildingAlgorithm::new(
Expand Down
15 changes: 7 additions & 8 deletions crates/rbuilder/src/live_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::{
simulation::OrderSimulationPool,
watchdog::spawn_watchdog_thread,
},
primitives::mev_boost::MevBoostRelay,
provider::StateProviderFactory,
telemetry::inc_active_slots,
utils::{error_storage::spawn_error_storage_writer, Signer},
Expand All @@ -37,9 +36,9 @@ use reth_chainspec::ChainSpec;
use reth_db::database::Database;
use std::{cmp::min, path::PathBuf, sync::Arc, time::Duration};
use time::OffsetDateTime;
use tokio::{sync::mpsc, task::spawn_blocking};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, warn};
use tracing::{debug, info, warn};

/// Time the proposer have to propose a block from the beginning of the slot (https://www.paradigm.xyz/2023/04/mev-boost-ethereum-consensus Slot anatomy)
const SLOT_PROPOSAL_DURATION: std::time::Duration = Duration::from_secs(4);
Expand All @@ -59,7 +58,7 @@ pub trait SlotSource {
/// # Usage
/// Create and run()
#[derive(Debug)]
pub struct LiveBuilder<DB, BlocksSourceType: SlotSource> {
pub struct LiveBuilder<Provider, BlocksSourceType: SlotSource> {
pub watchdog_timeout: Duration,
pub error_storage_path: PathBuf,
pub simulation_threads: usize,
Expand All @@ -76,18 +75,18 @@ pub struct LiveBuilder<DB, BlocksSourceType: SlotSource> {
pub global_cancellation: CancellationToken,

pub sink_factory: Box<dyn UnfinishedBlockBuildingSinkFactory>,
pub builders: Vec<Arc<dyn BlockBuildingAlgorithm<DB>>>,
pub builders: Vec<Arc<dyn BlockBuildingAlgorithm<Provider>>>,
pub extra_rpc: RpcModule<()>,
}

impl<DB: Database + Clone + 'static, BuilderSourceType: SlotSource>
LiveBuilder<DB, BuilderSourceType>
impl<Provider: StateProviderFactory + Clone + 'static, BuilderSourceType: SlotSource>
LiveBuilder<Provider, BuilderSourceType>
{
pub fn with_extra_rpc(self, extra_rpc: RpcModule<()>) -> Self {
Self { extra_rpc, ..self }
}

pub fn with_builders(self, builders: Vec<Arc<dyn BlockBuildingAlgorithm<DB>>>) -> Self {
pub fn with_builders(self, builders: Vec<Arc<dyn BlockBuildingAlgorithm<Provider>>>) -> Self {
Self { builders, ..self }
}

Expand Down
Loading

0 comments on commit 765230d

Please sign in to comment.