Skip to content

Commit

Permalink
refactor to expose full reth cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Oct 29, 2023
1 parent 0ad5eb4 commit 39fd78a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 81 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/mev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0"
[features]
default = ["boost", "build", "relay"]
boost = ["mev-boost-rs"]
build = ["mev-build-rs"]
build = ["mev-build-rs", "reth"]
relay = ["mev-relay-rs"]

[dependencies]
Expand Down
31 changes: 3 additions & 28 deletions bin/mev/src/cmd/build.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
use crate::cmd::config::Config;
use clap::Args;
use mev_build_rs::reth_builder::Service;
use tracing::info;
use mev_build_rs::reth_builder::ServiceExt;
use reth::cli::Cli;

#[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) -> eyre::Result<()> {
let config_file = &self.config_file;

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

let network = config.network;
info!("configured for `{network}`");

if let Some(config) = config.build {
Service::from(network, config).spawn().await;
Ok(())
} else {
Err(eyre::eyre!("missing build config from file provided"))
}
}
}
pub type Command = Cli<ServiceExt>;
6 changes: 3 additions & 3 deletions bin/mev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ fn setup_logging() {
}

async fn run_task_until_signal(task: impl Future<Output = eyre::Result<()>>) -> eyre::Result<()> {
setup_logging();

tokio::select! {
task = task => task,
_ = signal::ctrl_c() => {
Expand All @@ -46,13 +48,11 @@ async fn run_task_until_signal(task: impl Future<Output = eyre::Result<()>>) ->
async fn main() -> eyre::Result<()> {
let cli = Cli::parse();

setup_logging();

match cli.command {
#[cfg(feature = "boost")]
Commands::Boost(cmd) => run_task_until_signal(cmd.execute()).await,
#[cfg(feature = "build")]
Commands::Build(cmd) => run_task_until_signal(cmd.execute()).await,
Commands::Build(cmd) => tokio::task::block_in_place(|| cmd.run()),
#[cfg(feature = "relay")]
Commands::Relay(cmd) => run_task_until_signal(cmd.execute()).await,
Commands::Config(cmd) => run_task_until_signal(cmd.execute()).await,
Expand Down
2 changes: 1 addition & 1 deletion mev-build-rs/src/reth_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ mod types;

pub use bidder::DeadlineBidder;
pub use service::Config;
pub use service_ext::ServiceExt as Service;
pub use service_ext::ServiceExt;
10 changes: 5 additions & 5 deletions mev-build-rs/src/reth_builder/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const DEFAULT_BID_PERCENT: f64 = 0.9;
pub struct Config {
pub secret_key: SecretKey,
pub relays: Vec<String>,
pub extra_data: String,
pub extra_data: Bytes,
pub execution_mnemonic: String,
// amount in milliseconds
pub bidding_deadline_ms: u64,
Expand Down Expand Up @@ -67,15 +67,15 @@ impl<
> Service<Pool, Client, B>
{
pub fn from(
config: Config,
config: &Config,
context: Arc<Context>,
clock: Clock<SystemTimeProvider>,
pool: Pool,
client: Client,
bidder: Arc<B>,
chain_spec: Arc<ChainSpec>,
) -> Result<(Self, Builder<Pool, Client>), Error> {
let secret_key = config.secret_key;
let secret_key = &config.secret_key;
let relays = parse_relays(&config.relays);

let mut derivation_index = 0;
Expand All @@ -94,14 +94,14 @@ impl<
let builder_wallet = wallet.with_chain_id(chain_spec.chain.id());

let builder = Builder::new(
secret_key,
secret_key.clone(),
context.clone(),
clock.clone(),
relays,
pool,
client,
chain_spec,
Bytes::from(config.extra_data),
config.extra_data.clone(),
builder_wallet,
config.bid_percent.unwrap_or(DEFAULT_BID_PERCENT),
config.subsidy_gwei.unwrap_or_default(),
Expand Down
74 changes: 31 additions & 43 deletions mev-build-rs/src/reth_builder/service_ext.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,57 @@
use crate::reth_builder::{service::Service, Config, DeadlineBidder};
use clap::{Args, Parser};
use crate::reth_builder::{service::Service, Config as BuildConfig, DeadlineBidder};
use clap::Args;
use ethereum_consensus::{
networks::{self, Network},
state_transition::Context,
};
use mev_rs::config::from_toml_file;
use reth::{
cli::{
components::RethNodeComponents,
config::PayloadBuilderConfig,
ext::{RethCliExt, RethNodeCommandConfig},
},
node::NodeCommand,
runner::CliContext,
tasks::{TaskManager, TaskSpawner},
tasks::TaskSpawner,
};
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use std::{sync::Arc, time::Duration};
use tracing::warn;
use tracing::info;

#[derive(Debug, Args)]
pub struct ServiceExt {
#[clap(env, long = "mev-builder-config", default_value = "config.toml")]
config_file: String,
#[clap(skip)]
network: Network,
#[clap(skip)]
config: Config,
config: Option<Config>,
}

impl ServiceExt {
pub fn from(network: Network, config: Config) -> Self {
Self { network, 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.network;
let network_name = format!("{0}", network);

let mut params = vec![
"".into(),
"--chain".into(),
network_name.to_string(),
"--full".into(),
"--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:?}");
}
}
// NOTE: this is duplicated here to avoid circular import b/t `mev` bin and `mev-rs` crate
#[derive(Debug, serde::Deserialize)]
struct Config {
pub network: Network,
#[serde(rename = "builder")]
pub build: BuildConfig,
}

impl RethCliExt for ServiceExt {
type Node = ServiceExt;
}

impl RethNodeCommandConfig for ServiceExt {
fn on_components_initialized<Reth: RethNodeComponents>(
&mut self,
_components: &Reth,
) -> eyre::Result<()> {
let config_file = &self.config_file;

let config = from_toml_file::<_, Config>(config_file)?;
let network = &config.network;
info!("configured for `{network}`");

self.config = Some(config);
Ok(())
}

fn spawn_payload_builder_service<Conf, Reth>(
&mut self,
_conf: &Conf,
Expand All @@ -74,12 +61,13 @@ impl RethNodeCommandConfig for ServiceExt {
Conf: PayloadBuilderConfig,
Reth: RethNodeComponents,
{
let build_config = self.config.clone();
let context = Arc::new(Context::try_from(self.network.clone())?);
let config = self.config.as_ref().expect("already loaded config");
let context = Arc::new(Context::try_from(config.network.clone())?);
let clock = context.clock().unwrap_or_else(|| {
let genesis_time = networks::typical_genesis_time(&context);
context.clock_at(genesis_time)
});
let build_config = &config.build;
let deadline = Duration::from_millis(build_config.bidding_deadline_ms);
let bidder = Arc::new(DeadlineBidder::new(clock.clone(), deadline));
let (service, builder) = Service::from(
Expand Down

0 comments on commit 39fd78a

Please sign in to comment.