Skip to content

Commit

Permalink
move config from command line to config file
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Oct 18, 2023
1 parent 2044b8a commit 82da77e
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 88 deletions.
29 changes: 27 additions & 2 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 @@ -17,7 +17,7 @@ mev-build-rs = { path = "../../mev-build-rs" }
mev-rs = { path = "../../mev-rs" }

serde = { version = "1.0", features = ["derive"] }
toml = "0.5.9"
toml = "0.8.2"

clap = { version = "4.1.4", features = ["derive", "env"] }
anyhow = "1.0.57"
Expand Down
12 changes: 7 additions & 5 deletions bin/mev/src/cmd/boost.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::cmd::config::Config;
use anyhow::{anyhow, Result};
use clap::Args;
use ethereum_consensus::networks::Network;
use mev_boost_rs::Service;
use tracing::info;

#[derive(Debug, Args)]
#[clap(about = "🚀 connecting proposers to the external builder network")]
Expand All @@ -12,14 +12,16 @@ pub struct Command {
}

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

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

if let Some(mut config) = config.boost {
config.network = network;
Ok(Service::from(config).spawn(None)?.await?)
let network = config.network;
info!("configured for {network}");

if let Some(config) = config.boost {
Ok(Service::from(network, config).spawn(None)?.await?)
} else {
Err(anyhow!("missing boost config from file provided"))
}
Expand Down
6 changes: 3 additions & 3 deletions bin/mev/src/cmd/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod reth_ext;

use anyhow::Result;
use clap::Args;
use ethereum_consensus::networks::Network;
use reth_ext::{launch_reth_with, RethNodeExt};

#[derive(Debug, Args)]
Expand All @@ -13,8 +12,9 @@ pub struct Command {
}

impl Command {
pub async fn execute(&self, network: Network) -> Result<()> {
let ext = RethNodeExt { config_file: self.config_file.clone(), network, config: None };
pub async fn execute(self) -> Result<()> {
let ext =
RethNodeExt { config_file: self.config_file.clone(), network: None, config: None };
launch_reth_with(ext).await;
Ok(())
}
Expand Down
11 changes: 7 additions & 4 deletions bin/mev/src/cmd/build/reth_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use reth::{
};
use reth_payload_builder::PayloadBuilderService;
use std::{sync::Arc, time::Duration};
use tracing::warn;

struct RethExt;

Expand All @@ -25,7 +26,7 @@ pub struct RethNodeExt {
#[clap(skip)]
pub config_file: String,
#[clap(skip)]
pub network: Network,
pub network: Option<Network>,
#[clap(skip)]
pub config: Option<BuildConfig>,
}
Expand All @@ -34,6 +35,7 @@ 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
Expand Down Expand Up @@ -61,7 +63,7 @@ impl RethNodeCommandConfig for RethNodeExt {
Tasks: reth::tasks::TaskSpawner + Clone + Unpin + 'static,
{
let build_config = self.get_build_config();
let network = &self.network;
let network = self.network.as_ref().unwrap();
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 @@ -110,7 +112,8 @@ pub(crate) async fn launch_reth_with(mut ext: RethNodeExt) {

let config = ext.get_build_config();

let network_name = format!("{0}", ext.network);
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 {
Expand All @@ -122,6 +125,6 @@ pub(crate) async fn launch_reth_with(mut ext: RethNodeExt) {
// NOTE: shim to pass in config
node.ext = ext;
if let Err(err) = node.execute(ctx).await {
tracing::warn!("{err:?}");
warn!("{err:?}");
}
}
13 changes: 7 additions & 6 deletions bin/mev/src/cmd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use mev_build_rs::reth_builder::Config as BuildConfig;
use mev_relay_rs::Config as RelayConfig;
use serde::Deserialize;
use std::{fmt, path::Path};
use tracing::info;

#[derive(Debug, Deserialize)]
pub struct Config {
pub network: Network,
pub boost: Option<BoostConfig>,
#[serde(rename = "builder")]
pub build: Option<BuildConfig>,
Expand All @@ -19,10 +21,10 @@ impl Config {
pub fn from_toml_file<P: AsRef<Path> + fmt::Display + Clone>(path: P) -> Result<Config> {
tracing::info!("loading config from `{path}`...");

let config_data = std::fs::read(path.as_ref())
let config_data = std::fs::read_to_string(path.as_ref())
.with_context(|| format!("could not read config from `{path}`"))?;

toml::from_slice(&config_data).context("could not parse TOML")
toml::from_str(&config_data).context("could not parse TOML")
}
}

Expand All @@ -34,12 +36,11 @@ pub struct Command {
}

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

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

tracing::info!("configured for network `{}` with configuration {:#?}", network, config);
info!("{config:#?}");

Ok(())
}
Expand Down
12 changes: 7 additions & 5 deletions bin/mev/src/cmd/relay.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::cmd::config::Config;
use anyhow::{anyhow, Result};
use clap::{Args, Subcommand};
use ethereum_consensus::networks::Network;
use mev_relay_rs::Service;
use tracing::info;

#[derive(Debug, Args)]
#[clap(about = "🏗 connecting builders to proposers", subcommand_negates_reqs = true)]
Expand All @@ -20,7 +20,7 @@ pub enum Commands {
}

impl Command {
pub async fn execute(&self, network: Network) -> Result<()> {
pub async fn execute(self) -> Result<()> {
let (config_file, _mock) = if let Some(subcommand) = self.command.as_ref() {
match subcommand {
Commands::Mock { config_file } => (config_file, true),
Expand All @@ -31,9 +31,11 @@ impl Command {

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

if let Some(mut config) = config.relay {
config.network = network;
let service = Service::from(config).spawn(None).await?;
let network = config.network;
info!("configured for {network}");

if let Some(config) = config.relay {
let service = Service::from(network, config).spawn(None).await?;
Ok(service.await?)
} else {
Err(anyhow!("missing relay config from file provided"))
Expand Down
51 changes: 5 additions & 46 deletions bin/mev/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,14 @@
mod cmd;

use anyhow::Result;
use clap::{ArgGroup, Parser, Subcommand};
use ethereum_consensus::networks::Network;
use clap::{Parser, Subcommand};
use std::future::Future;
use tokio::signal;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[derive(Default, Debug, Clone, clap::ValueEnum)]
pub enum NetworkArg {
#[default]
Mainnet,
Sepolia,
Goerli,
Holesky,
}

// NOTE: define this mapping so only this crate needs the `clap` dependency while still being able
// to use the `clap::ValueEnum` machinery
impl From<NetworkArg> for Network {
fn from(arg: NetworkArg) -> Self {
match arg {
NetworkArg::Mainnet => Self::Mainnet,
NetworkArg::Sepolia => Self::Sepolia,
NetworkArg::Goerli => Self::Goerli,
NetworkArg::Holesky => Self::Holesky,
}
}
}

#[derive(Debug, Parser)]
#[clap(author, version, about = "utilities for block space", long_about = None)]
#[clap(group(ArgGroup::new("network-config").args(&["network", "network_config"])))]
struct Cli {
#[clap(long, value_enum, value_name = "NETWORK")]
network: Option<NetworkArg>,
#[clap(long, value_name = "FILE")]
network_config: Option<String>,
#[clap(subcommand)]
command: Commands,
}
Expand Down Expand Up @@ -72,25 +44,12 @@ async fn run_task_until_signal(task: impl Future<Output = Result<()>>) -> Result
async fn main() -> Result<()> {
let cli = Cli::parse();

let network = if let Some(network) = cli.network {
network.into()
} else if let Some(network_config) = cli.network_config {
// use custom config if provided
Network::Custom(network_config)
} else {
// default to `mainnet` if no network configuration is provided
let network = NetworkArg::Mainnet;
network.into()
};

setup_logging();

tracing::info!("configured for {network}");

match cli.command {
Commands::Boost(cmd) => run_task_until_signal(cmd.execute(network)).await,
Commands::Build(cmd) => run_task_until_signal(cmd.execute(network)).await,
Commands::Relay(cmd) => run_task_until_signal(cmd.execute(network)).await,
Commands::Config(cmd) => run_task_until_signal(cmd.execute(network)).await,
Commands::Boost(cmd) => run_task_until_signal(cmd.execute()).await,
Commands::Build(cmd) => run_task_until_signal(cmd.execute()).await,
Commands::Relay(cmd) => run_task_until_signal(cmd.execute()).await,
Commands::Config(cmd) => run_task_until_signal(cmd.execute()).await,
}
}
2 changes: 2 additions & 0 deletions example.config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
network = "holesky"

[boost]
host = "0.0.0.0"
port = 18550
Expand Down
13 changes: 3 additions & 10 deletions mev-boost-rs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,11 @@ pub struct Config {
pub host: Ipv4Addr,
pub port: u16,
pub relays: Vec<String>,
#[serde(default)]
pub network: Network,
}

impl Default for Config {
fn default() -> Self {
Self {
host: Ipv4Addr::UNSPECIFIED,
port: 18550,
relays: vec![],
network: Network::default(),
}
Self { host: Ipv4Addr::UNSPECIFIED, port: 18550, relays: vec![] }
}
}

Expand All @@ -58,10 +51,10 @@ pub struct Service {
}

impl Service {
pub fn from(config: Config) -> Self {
pub fn from(network: Network, config: Config) -> Self {
let relays = parse_relay_endpoints(&config.relays);

Self { host: config.host, port: config.port, relays, network: config.network }
Self { host: config.host, port: config.port, relays, network }
}

/// Spawns a new [`RelayMux`] and [`BlindedBlockProviderServer`] task
Expand Down
4 changes: 3 additions & 1 deletion mev-boost-rs/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ethereum_consensus::{
builder::{SignedValidatorRegistration, ValidatorRegistration},
capella::mainnet as capella,
crypto::SecretKey,
networks::Network,
phase0::mainnet::{compute_domain, Validator},
primitives::{DomainType, ExecutionAddress, Hash32, Root},
signing::sign_with_domain,
Expand Down Expand Up @@ -96,7 +97,8 @@ async fn test_end_to_end() {
config.relays.push(format!("http://{relay_public_key}@127.0.0.1:{port}"));

let mux_port = config.port;
let service = Service::from(config);
let network = Network::Sepolia;
let service = Service::from(network, config);
service.spawn(Some(context.clone())).unwrap();

let beacon_node = RelayClient::new(ApiClient::new(
Expand Down
Loading

0 comments on commit 82da77e

Please sign in to comment.