Skip to content

Commit

Permalink
move reth integration into mev-build-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Oct 19, 2023
1 parent a4753c8 commit a1dbecb
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 61 deletions.
7 changes: 3 additions & 4 deletions Cargo.lock

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

5 changes: 0 additions & 5 deletions bin/mev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,4 @@ toml = "0.8.2"
clap = { version = "4.1.4", features = ["derive", "env"] }
anyhow = "1.0.57"

reth-payload-builder = { workspace = true }
reth-primitives = { workspace = true }
reth = { workspace = true }
eyre = "0.6.8"

ethereum-consensus = { workspace = true }
27 changes: 27 additions & 0 deletions bin/mev/src/cmd/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::cmd::config::Config;
use anyhow::{anyhow, Result};
use clap::Args;
use ethereum_consensus::networks::Network;
use mev_build_rs::reth_builder::Service;

#[derive(Debug, Args)]
#[clap(about = "🛠️ building blocks since 2023")]
pub struct Command {
#[clap(env, default_value = "config.toml")]
config_file: String,
}

impl Command {
pub async fn execute(&self, network: Network) -> Result<()> {
let config_file = &self.config_file;

let config = Config::from_toml_file(config_file)?;

if let Some(mut config) = config.build {
config.network = network;
Ok(Service::from(config).spawn().await)
} else {
Err(anyhow!("missing boost config from file provided"))
}
}
}
3 changes: 3 additions & 0 deletions mev-build-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ reth-transaction-pool = { workspace = true }
reth-provider = { workspace = true }
reth-interfaces = { workspace = true }
reth-revm = { workspace = true }
reth = { workspace = true }

ethers = "2.0"
eyre = "0.6.8"
clap = { version = "4.1.4", features = ["derive", "env"] }
4 changes: 3 additions & 1 deletion mev-build-rs/src/reth_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ mod payload_builder;
mod reth_compat;
mod reth_ext;
mod service;
mod service_ext;
mod types;

pub use bidder::DeadlineBidder;
pub use service::{Config, Service};
pub use service::Config;
pub use service_ext::ServiceExt as Service;
3 changes: 3 additions & 0 deletions mev-build-rs/src/reth_builder/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::reth_builder::{
use ethereum_consensus::{
clock::{Clock, SystemTimeProvider},
crypto::SecretKey,
networks::Network,
state_transition::Context,
};
use ethers::signers::{coins_bip39::English, MnemonicBuilder, Signer};
Expand All @@ -21,6 +22,8 @@ const DEFAULT_BID_PERCENT: f64 = 0.9;

#[derive(Deserialize, Debug, Default, Clone)]
pub struct Config {
#[serde(default)]
pub network: Network,
pub secret_key: SecretKey,
pub relays: Vec<String>,
pub extra_data: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::cmd::config::Config;
use crate::reth_builder::{service::Service, Config, DeadlineBidder};
use clap::{Args, Parser};
use ethereum_consensus::{
networks::{self, Network},
networks::{self},
state_transition::Context,
};
use mev_build_rs::reth_builder::{Config as BuildConfig, DeadlineBidder, Service};
use reth::{
cli::ext::{RethCliExt, RethNodeCommandConfig},
node::NodeCommand,
Expand All @@ -15,35 +14,46 @@ use reth_payload_builder::PayloadBuilderService;
use std::{sync::Arc, time::Duration};
use tracing::warn;

struct RethExt;

impl RethCliExt for RethExt {
type Node = RethNodeExt;
}

#[derive(Debug, Args)]
pub struct RethNodeExt {
#[clap(skip)]
pub config_file: String,
#[clap(skip)]
pub network: Option<Network>,
pub struct ServiceExt {
#[clap(skip)]
pub config: Option<BuildConfig>,
config: Config,
}

impl RethNodeExt {
pub fn get_build_config(&mut self) -> BuildConfig {
self.config.take().unwrap_or_else(|| {
let config = Config::from_toml_file(&self.config_file).unwrap();
self.network = Some(config.network);
let config = config.build.unwrap();
self.config = Some(config.clone());
config
})
impl ServiceExt {
pub fn from(config: Config) -> Self {
Self { config }
}

pub async fn spawn(self) {
let task_manager = TaskManager::new(tokio::runtime::Handle::current());
let task_executor = task_manager.executor();
let ctx = CliContext { task_executor };

let network = &self.config.network;
let network_name = format!("{0}", network);

let mut params =
vec!["".into(), "--chain".into(), network_name.to_string(), "--http".into()];
if let Some(path) = self.config.jwt_secret_path.as_ref() {
params.push("--authrpc.jwtsecret".into());
params.push(path.clone());
}

let mut node = NodeCommand::<ServiceExt>::parse_from(params);
// NOTE: shim to pass in config
node.ext = self;
if let Err(err) = node.execute(ctx).await {
warn!("{err:?}");
}
}
}

impl RethNodeCommandConfig for RethNodeExt {
impl RethCliExt for ServiceExt {
type Node = ServiceExt;
}

impl RethNodeCommandConfig for ServiceExt {
fn spawn_payload_builder_service<Conf, Provider, Pool, Tasks>(
&mut self,
_conf: &Conf,
Expand All @@ -62,8 +72,8 @@ impl RethNodeCommandConfig for RethNodeExt {
Pool: reth::transaction_pool::TransactionPool + Unpin + 'static,
Tasks: reth::tasks::TaskSpawner + Clone + Unpin + 'static,
{
let build_config = self.get_build_config();
let network = self.network.as_ref().unwrap();
let build_config = self.config.clone();
let network = &build_config.network;
let context = Arc::new(Context::try_from(network)?);
let clock = context.clock().unwrap_or_else(|| {
let genesis_time = networks::typical_genesis_time(&context);
Expand Down Expand Up @@ -104,27 +114,3 @@ impl RethNodeCommandConfig for RethNodeExt {
Ok(payload_builder)
}
}

pub(crate) async fn launch_reth_with(mut ext: RethNodeExt) {
let task_manager = TaskManager::new(tokio::runtime::Handle::current());
let task_executor = task_manager.executor();
let ctx = CliContext { task_executor };

let config = ext.get_build_config();

let network = ext.network.as_ref().unwrap();
let network_name = format!("{network}");

let mut params = vec!["".into(), "--chain".into(), network_name.to_string(), "--http".into()];
if let Some(path) = config.jwt_secret_path {
params.push("--authrpc.jwtsecret".into());
params.push(path);
}

let mut node = NodeCommand::<RethExt>::parse_from(params);
// NOTE: shim to pass in config
node.ext = ext;
if let Err(err) = node.execute(ctx).await {
warn!("{err:?}");
}
}

0 comments on commit a1dbecb

Please sign in to comment.