Skip to content

Commit

Permalink
Remove backtest fb db initialisation from config (#322)
Browse files Browse the repository at this point in the history
## 📝 Summary

Move the FlashbotsDB connection initialization from Config into the
RelayDB struct. This improves separation of concerns by making Config
unaware of any Postgres-specific details, while placing the connection
logic in the appropriate domain.

## 💡 Motivation and Context

<!--- (Optional) Why is this change required? What problem does it
solve? Remove this section if not applicable. -->

---

## ✅ I have completed the following steps:

* [ ] Run `make lint`
* [ ] Run `make test`
* [ ] Added tests (if applicable)
  • Loading branch information
ferranbt authored Jan 2, 2025
1 parent 278de7a commit c67a668
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion config-playground.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ name = "custom"
url = "http://0xac6e77dfe25ecd6110b8e780608cce0dab71fdd5ebea22a16c0205200f2f8e2e3ad3b71d3499c54ad14d6c21b41a37ae@localhost:5555"
priority = 0
use_ssz_for_submit = false
use_gzip_for_submit = false
use_gzip_for_submit = false
5 changes: 5 additions & 0 deletions crates/rbuilder/src/backtest/fetch/flashbots_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ impl RelayDB {
pub fn new(pool: PgPool) -> Self {
Self { pool }
}

pub async fn from_url(url: String) -> eyre::Result<Self> {
let pool = PgPool::connect(&url).await?;
Ok(Self::new(pool))
}
}

impl RelayDB {
Expand Down
11 changes: 4 additions & 7 deletions crates/rbuilder/src/backtest/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use alloy_rpc_types::{Block, BlockId, BlockNumberOrTag, BlockTransactionsKind};
use eyre::WrapErr;
use flashbots_db::RelayDB;
use futures::TryStreamExt;
use sqlx::PgPool;
use std::{
collections::HashMap,
path::PathBuf,
Expand Down Expand Up @@ -62,15 +61,13 @@ impl HistoricalDataFetcher {
pub fn with_default_datasource(
mut self,
mempool_datadir: PathBuf,
flashbots_db: Option<PgPool>,
flashbots_db: Option<RelayDB>,
) -> eyre::Result<Self> {
let mempool = Box::new(mempool::MempoolDumpsterDatasource::new(mempool_datadir)?);
self.data_sources.push(mempool);

if let Some(db_pool) = flashbots_db {
let datasource = Box::new(RelayDB::new(db_pool));
self.data_sources.push(datasource);
}
if let Some(flashbots_db) = flashbots_db {
self.data_sources.push(Box::new(flashbots_db));
};
Ok(self)
}

Expand Down
9 changes: 7 additions & 2 deletions crates/rbuilder/src/bin/backtest-fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use alloy_primitives::utils::format_ether;
use clap::Parser;
use rbuilder::{
backtest::{HistoricalDataFetcher, HistoricalDataStorage},
backtest::{fetch::flashbots_db::RelayDB, HistoricalDataFetcher, HistoricalDataStorage},
live_builder::{base_config::load_config_toml_and_env, cli::LiveBuilderConfig, config::Config},
};
use std::{ffi::OsString, fs, path::PathBuf};
Expand Down Expand Up @@ -62,7 +62,12 @@ async fn main() -> eyre::Result<()> {
let backtest_fetch_mempool_data_dir =
config.base_config().backtest_fetch_mempool_data_dir()?;

let db = config.base_config().flashbots_db().await?;
let db = if let Some(db) = config.base_config().flashbots_db.clone() {
Some(RelayDB::from_url(db.value()?).await?)
} else {
None
};

let provider = config.base_config().eth_rpc_provider()?;
let fetcher = HistoricalDataFetcher::new(
provider,
Expand Down
11 changes: 0 additions & 11 deletions crates/rbuilder/src/live_builder/base_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use reth_provider::{
};
use serde::{Deserialize, Deserializer};
use serde_with::{serde_as, DeserializeAs};
use sqlx::PgPool;
use std::{
env::var,
fs::read_to_string,
Expand Down Expand Up @@ -318,16 +317,6 @@ impl BaseConfig {
Ok(HashSet::default())
}

pub async fn flashbots_db(&self) -> eyre::Result<Option<PgPool>> {
if let Some(url) = &self.flashbots_db {
let url = url.value()?;
let pool = PgPool::connect(&url).await?;
Ok(Some(pool))
} else {
Ok(None)
}
}

pub fn eth_rpc_provider(&self) -> eyre::Result<BoxedProvider> {
Ok(http_provider(self.backtest_fetch_eth_rpc_url.parse()?))
}
Expand Down

0 comments on commit c67a668

Please sign in to comment.