Skip to content

Commit

Permalink
Initial version for the chain state abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Jul 6, 2024
1 parent 882ce81 commit 76bdb11
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 90 deletions.
5 changes: 3 additions & 2 deletions crates/rbuilder/src/backtest/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
},
live_builder::cli::LiveBuilderConfig,
primitives::SimulatedOrder,
provider::StateProviderFactory,
utils::clean_extradata,
};
use ahash::HashSet;
Expand Down Expand Up @@ -49,9 +50,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
24 changes: 12 additions & 12 deletions crates/rbuilder/src/building/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ use crate::{
simulation::SimulatedOrderCommand,
},
primitives::{AccountNonce, OrderId, SimulatedOrder},
provider::StateProviderFactory,
utils::NonceCache,
};
use ahash::HashSet;
use alloy_primitives::{Address, B256, U256};
use reth::{
primitives::{BlobTransactionSidecar, SealedBlock},
providers::ProviderFactory,
tasks::pool::BlockingTaskPool,
};
use reth_db::database::Database;
use reth_payload_builder::database::CachedReads;
use std::{
cmp::max,
Expand Down Expand Up @@ -80,8 +79,8 @@ impl BestBlockCell {
}

#[derive(Debug)]
pub struct LiveBuilderInput<DB: Database, SinkType: BlockBuildingSink> {
pub provider_factory: ProviderFactory<DB>,
pub struct LiveBuilderInput<Provider, SinkType: BlockBuildingSink> {
pub provider_factory: Provider,
pub root_hash_task_pool: BlockingTaskPool,
pub ctx: BlockBuildingContext,
pub input: broadcast::Receiver<SimulatedOrderCommand>,
Expand Down Expand Up @@ -159,10 +158,10 @@ pub struct OrderIntakeConsumer<DB> {
order_consumer: OrderConsumer,
}

impl<DB: Database + Clone> OrderIntakeConsumer<DB> {
impl<Provider: StateProviderFactory + Clone> OrderIntakeConsumer<Provider> {
/// See [`ShareBundleMerger`] for sbundle_merger_selected_signers
pub fn new(
provider_factory: ProviderFactory<DB>,
provider_factory: Provider,
orders: broadcast::Receiver<SimulatedOrderCommand>,
parent_block: B256,
sorting: Sorting,
Expand Down Expand Up @@ -264,8 +263,9 @@ pub trait BlockBuildingSink: std::fmt::Debug + Clone + Send + Sync {
}

#[derive(Debug)]
pub struct BlockBuildingAlgorithmInput<DB: Database, SinkType: BlockBuildingSink> {
pub provider_factory: ProviderFactory<DB>,
pub struct BlockBuildingAlgorithmInput<Provider: StateProviderFactory, SinkType: BlockBuildingSink>
{
pub provider_factory: Provider,
pub ctx: BlockBuildingContext,
pub input: broadcast::Receiver<SimulatedOrderCommand>,
/// output for the blocks
Expand All @@ -278,11 +278,11 @@ pub struct BlockBuildingAlgorithmInput<DB: Database, SinkType: BlockBuildingSink
/// 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, SinkType: BlockBuildingSink>:
pub trait BlockBuildingAlgorithm<Provider: StateProviderFactory, SinkType: BlockBuildingSink>:
std::fmt::Debug + Send + Sync
{
fn name(&self) -> String;
fn build_blocks(&self, input: BlockBuildingAlgorithmInput<DB, SinkType>);
fn build_blocks(&self, input: BlockBuildingAlgorithmInput<Provider, SinkType>);
}

/// Factory used to create BlockBuildingSink for builders when we are targeting blocks for slots.
Expand All @@ -299,11 +299,11 @@ pub trait BuilderSinkFactory {
}

/// Basic configuration to run a single block building with a BlockBuildingAlgorithm
pub struct BacktestSimulateBlockInput<'a, DB> {
pub struct BacktestSimulateBlockInput<'a, Provider> {
pub ctx: BlockBuildingContext,
pub builder_name: String,
pub sbundle_mergeabe_signers: Vec<Address>,
pub sim_orders: &'a Vec<SimulatedOrder>,
pub provider_factory: ProviderFactory<DB>,
pub provider_factory: Provider,
pub cached_reads: Option<CachedReads>,
}
29 changes: 16 additions & 13 deletions crates/rbuilder/src/building/builders/ordering_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use crate::{
ExecutionError, PartialBlock, Sorting,
},
primitives::{AccountNonce, OrderId},
provider::StateProviderFactory,
telemetry,
utils::is_provider_factory_health_error,
};
use ahash::{HashMap, HashSet};
use alloy_primitives::{utils::format_ether, Address};
use reth::providers::{BlockNumReader, ProviderFactory};
use reth_db::database::Database;
use reth::providers::BlockNumReader;
use reth_provider::StateProvider;

use crate::{
Expand Down Expand Up @@ -69,8 +69,11 @@ impl OrderingBuilderConfig {
}
}

pub fn run_ordering_builder<DB: Database + Clone + 'static, SinkType: BlockBuildingSink>(
input: LiveBuilderInput<DB, SinkType>,
pub fn run_ordering_builder<
Provider: StateProviderFactory + Clone + 'static,
SinkType: BlockBuildingSink,
>(
input: LiveBuilderInput<Provider, SinkType>,
config: &OrderingBuilderConfig,
) {
let block_number = input.ctx.block_env.number.to::<u64>();
Expand Down Expand Up @@ -152,9 +155,9 @@ pub fn run_ordering_builder<DB: Database + Clone + 'static, SinkType: BlockBuild
}
}

pub fn backtest_simulate_block<DB: Database + Clone + 'static>(
pub fn backtest_simulate_block<Provider: StateProviderFactory + Clone + 'static>(
ordering_config: OrderingBuilderConfig,
input: BacktestSimulateBlockInput<'_, DB>,
input: BacktestSimulateBlockInput<'_, Provider>,
) -> eyre::Result<(Block, CachedReads)> {
let use_suggested_fee_recipient_as_coinbase = ordering_config.coinbase_payment;
let state_provider = input
Expand Down Expand Up @@ -183,8 +186,8 @@ pub fn backtest_simulate_block<DB: Database + Clone + 'static>(
}

#[derive(Debug)]
pub struct OrderingBuilderContext<DB> {
provider_factory: ProviderFactory<DB>,
pub struct OrderingBuilderContext<Provider> {
provider_factory: Provider,
root_hash_task_pool: BlockingTaskPool,
builder_name: String,
ctx: BlockBuildingContext,
Expand All @@ -200,9 +203,9 @@ pub struct OrderingBuilderContext<DB> {
order_attempts: HashMap<OrderId, usize>,
}

impl<DB: Database + Clone + 'static> OrderingBuilderContext<DB> {
impl<Provider: StateProviderFactory + Clone + 'static> OrderingBuilderContext<Provider> {
pub fn new(
provider_factory: ProviderFactory<DB>,
provider_factory: Provider,
slot_bidder: Arc<dyn SlotBidder>,
root_hash_task_pool: BlockingTaskPool,
builder_name: String,
Expand Down Expand Up @@ -479,14 +482,14 @@ impl OrderingBuildingAlgorithm {
}
}

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

fn build_blocks(&self, input: BlockBuildingAlgorithmInput<DB, SinkType>) {
fn build_blocks(&self, input: BlockBuildingAlgorithmInput<Provider, SinkType>) {
let live_input = LiveBuilderInput {
provider_factory: input.provider_factory,
root_hash_task_pool: self.root_hash_task_pool.clone(),
Expand Down
15 changes: 7 additions & 8 deletions crates/rbuilder/src/building/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ use super::{
use crate::{
building::{BlockBuildingContext, BlockState, CriticalCommitOrderError},
primitives::{Order, OrderId, SimValue, SimulatedOrder},
provider::StateProviderFactory,
utils::{NonceCache, NonceCacheRef},
};
use ahash::{HashMap, HashSet};
use alloy_primitives::{Address, B256};
use rand::seq::SliceRandom;
use reth::providers::ProviderFactory;
use reth_db::database::Database;
use reth_interfaces::provider::ProviderError;
use reth_payload_builder::database::CachedReads;
use std::{
Expand Down Expand Up @@ -66,9 +65,9 @@ pub struct SimulatedResult {

// @Feat replaceable orders
#[derive(Debug)]
pub struct SimTree<DB> {
pub struct SimTree<Provider> {
// fields for nonce management
nonce_cache: NonceCache<DB>,
nonce_cache: NonceCache<Provider>,

sims: HashMap<SimulationId, SimulatedResult>,
sims_that_update_one_nonce: HashMap<NonceKey, SimulationId>,
Expand All @@ -86,8 +85,8 @@ enum OrderNonceState {
Ready(Vec<Order>),
}

impl<DB: Database> SimTree<DB> {
pub fn new(provider_factory: ProviderFactory<DB>, parent_block: B256) -> Self {
impl<Provider: StateProviderFactory> SimTree<Provider> {
pub fn new(provider_factory: Provider, parent_block: B256) -> Self {
let nonce_cache = NonceCache::new(provider_factory, parent_block);
Self {
nonce_cache,
Expand Down Expand Up @@ -304,8 +303,8 @@ impl<DB: Database> SimTree<DB> {
/// Non-interactive usage of sim tree that will simply simulate all orders.
/// `randomize_insertion` is used to debug if sim tree works correctly when orders are inserted in a different order
/// outputs should be independent of this arg.
pub fn simulate_all_orders_with_sim_tree<DB: Database + Clone>(
factory: ProviderFactory<DB>,
pub fn simulate_all_orders_with_sim_tree<Provider: StateProviderFactory + Clone>(
factory: Provider,
ctx: &BlockBuildingContext,
orders: &[Order],
randomize_insertion: bool,
Expand Down
1 change: 1 addition & 0 deletions crates/rbuilder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod flashbots;
pub mod live_builder;
pub mod mev_boost;
pub mod primitives;
pub mod provider;
pub mod roothash;
pub mod telemetry;
pub mod utils;
Expand Down
12 changes: 8 additions & 4 deletions crates/rbuilder/src/live_builder/base_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
},
mev_boost::BLSBlockSigner,
primitives::mev_boost::MevBoostRelay,
provider::StateProviderFactory,
telemetry::{setup_reloadable_tracing_subscriber, LoggerConfig},
utils::{http_provider, BoxedProvider, ProviderFactoryReopener, Signer},
validation_api_client::ValidationAPIClient,
Expand Down Expand Up @@ -179,12 +180,15 @@ impl BaseConfig {
}

/// WARN: opens reth db
pub async fn create_builder(
pub async fn create_builder<Provider>(
&self,
cancellation_token: tokio_util::sync::CancellationToken,
) -> eyre::Result<
super::LiveBuilder<Arc<DatabaseEnv>, super::building::relay_submit::RelaySubmitSinkFactory>,
> {
super::LiveBuilder<Provider, super::building::relay_submit::RelaySubmitSinkFactory>,
>
where
Provider: StateProviderFactory,
{
let submission_config = self.submission_config()?;
info!(
"Builder mev boost normal relay pubkey: {:?}",
Expand All @@ -206,7 +210,7 @@ impl BaseConfig {
let relays = self.relays()?;
let sink_factory = RelaySubmitSinkFactory::new(self.submission_config()?, relays.clone());

Ok(LiveBuilder::<Arc<DatabaseEnv>, RelaySubmitSinkFactory> {
Ok(LiveBuilder {
cl_urls: self.cl_node_url.clone(),
relays,
watchdog_timeout: self.watchdog_timeout(),
Expand Down
25 changes: 13 additions & 12 deletions crates/rbuilder/src/live_builder/building/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use crate::{
BlockBuildingContext,
},
live_builder::{payload_events::MevBoostSlotData, simulation::SlotOrderSimResults},
utils::ProviderFactoryReopener,
provider::StateProviderFactory,
};
use reth_db::database::Database;
use tokio::sync::{broadcast, mpsc};
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, trace};
Expand All @@ -27,27 +26,29 @@ use super::{
};

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

impl<DB: Database + Clone + 'static, BuilderSinkFactoryType: BuilderSinkFactory>
BlockBuildingPool<DB, BuilderSinkFactoryType>
impl<
Provider: StateProviderFactory + Clone + 'static,
BuilderSinkFactoryType: BuilderSinkFactory,
> BlockBuildingPool<Provider, BuilderSinkFactoryType>
where
<BuilderSinkFactoryType as BuilderSinkFactory>::SinkType: 'static,
{
pub fn new(
provider_factory: ProviderFactoryReopener<DB>,
builders: Vec<Arc<dyn BlockBuildingAlgorithm<DB, BuilderSinkFactoryType::SinkType>>>,
provider_factory: Provider,
builders: Vec<Arc<dyn BlockBuildingAlgorithm<Provider, BuilderSinkFactoryType::SinkType>>>,
sink_factory: BuilderSinkFactoryType,
bidding_service: Box<dyn BiddingService>,
orderpool_subscriber: order_input::OrderPoolSubscriber,
order_simulation_pool: OrderSimulationPool<DB>,
order_simulation_pool: OrderSimulationPool<Provider>,
) -> Self {
BlockBuildingPool {
provider_factory,
Expand Down Expand Up @@ -132,7 +133,7 @@ where
for builder in self.builders.iter() {
let builder_name = builder.name();
debug!(block = block_number, builder_name, "Spawning builder job");
let input = BlockBuildingAlgorithmInput::<DB, BuilderSinkFactoryType::SinkType> {
let input = BlockBuildingAlgorithmInput::<Provider, BuilderSinkFactoryType::SinkType> {
provider_factory: provider_factory.clone(),
ctx: ctx.clone(),
input: broadcast_input.subscribe(),
Expand Down
6 changes: 4 additions & 2 deletions crates/rbuilder/src/live_builder/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
building::builders::{BacktestSimulateBlockInput, Block},
live_builder::base_config::load_config_toml_and_env,
telemetry::spawn_telemetry_server,
utils::build_info::Version,
utils::{build_info::Version, ProviderFactoryReopener},
};

use super::{base_config::BaseConfig, building::relay_submit::RelaySubmitSinkFactory, LiveBuilder};
Expand Down Expand Up @@ -41,7 +41,9 @@ pub trait LiveBuilderConfig: std::fmt::Debug + serde::de::DeserializeOwned {
&self,
cancellation_token: CancellationToken,
) -> impl std::future::Future<
Output = eyre::Result<LiveBuilder<Arc<DatabaseEnv>, RelaySubmitSinkFactory>>,
Output = eyre::Result<
LiveBuilder<ProviderFactoryReopener<Arc<DatabaseEnv>>, RelaySubmitSinkFactory>,
>,
> + Send;

/// Patch until we have a unified way of backtesting using the exact algorithms we use on the LiveBuilder.
Expand Down
5 changes: 4 additions & 1 deletion crates/rbuilder/src/live_builder/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ impl LiveBuilderConfig for Config {
&self,
cancellation_token: tokio_util::sync::CancellationToken,
) -> eyre::Result<
super::LiveBuilder<Arc<DatabaseEnv>, super::building::relay_submit::RelaySubmitSinkFactory>,
super::LiveBuilder<
ProviderFactoryReopener<Arc<DatabaseEnv>>,
super::building::relay_submit::RelaySubmitSinkFactory,
>,
> {
let live_builder = self.base_config.create_builder(cancellation_token).await?;
let root_hash_task_pool = self.base_config.root_hash_task_pool()?;
Expand Down
Loading

0 comments on commit 76bdb11

Please sign in to comment.