Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(bin): Init Subcommand and Refactor #201

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

3 changes: 1 addition & 2 deletions bin/mev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ version.workspace = true
edition = "2021"
license = "MIT OR Apache-2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["boost", "build", "relay"]
boost = ["mev-boost-rs"]
Expand All @@ -16,6 +14,7 @@ relay = ["mev-relay-rs"]
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
toml = "0.8.2"

mev-boost-rs = { path = "../../mev-boost-rs", optional = true }
mev-relay-rs = { path = "../../mev-relay-rs", optional = true }
Expand Down
4 changes: 3 additions & 1 deletion bin/mev/src/cmd/mod.rs → bin/mev/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod debug;
pub mod init;

#[cfg(feature = "boost")]
pub mod boost;
#[cfg(feature = "build")]
pub mod build;
pub mod config;
#[cfg(feature = "relay")]
pub mod relay;
2 changes: 1 addition & 1 deletion bin/mev/src/cmd/boost.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cmd::config::Config;
use crate::config::Config;
use clap::Args;
use mev_boost_rs::Service;
use tracing::info;
Expand Down
21 changes: 21 additions & 0 deletions bin/mev/src/cmd/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use clap::Args;

use crate::config::Config;

#[derive(Debug, Args)]
#[clap(about = "🔬 (debug) utility to verify configuration")]
pub struct Command {
#[clap(env)]
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)?;
tracing::info!("{config:#?}");

Ok(())
}
}
34 changes: 34 additions & 0 deletions bin/mev/src/cmd/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use clap::Args;

use crate::config::Config;

/// Include the `example.config.toml` file as a string constant
/// in the binary. The [`include_str`] macro will read the file
/// at compile time and include the contents in the binary.
const EXAMPLE_CONFIG: &str = std::include_str!("../../../../example.config.toml");

#[derive(Debug, Args)]
#[clap(about = "✨ (init) initializes a new config file")]
pub struct Command {
/// The path to write the config file
#[clap(env, default_value = "config.toml")]
dest: String,
}

impl Command {
pub fn execute(self) -> eyre::Result<()> {
// Deserialize EXAMPLE_CONFIG into a Config struct
let config: Config = toml::from_str(EXAMPLE_CONFIG)?;
tracing::debug!("Loaded config template.");

// Serialize the Config struct into a TOML string
let contents = toml::to_string_pretty(&config)?;
tracing::debug!("Serialized config to TOML.");

// Write the TOML string to a file
std::fs::write(&self.dest, contents)?;
tracing::debug!("Wrote config to `{}`.", self.dest);

Ok(())
}
}
2 changes: 1 addition & 1 deletion bin/mev/src/cmd/relay.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cmd::config::Config;
use crate::config::Config;
use clap::{Args, Subcommand};
use mev_relay_rs::Service;
use tracing::info;
Expand Down
29 changes: 5 additions & 24 deletions bin/mev/src/cmd/config.rs → bin/mev/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use clap::Args;
use ethereum_consensus::networks::Network;
use eyre::WrapErr;
use mev_rs::config::from_toml_file;
use serde::{Deserialize, Serialize};
use std::{fmt, path::Path};

#[cfg(feature = "boost")]
use mev_boost_rs::Config as BoostConfig;
#[cfg(feature = "build")]
use mev_build_rs::reth_builder::Config as BuildConfig;
#[cfg(feature = "relay")]
use mev_relay_rs::Config as RelayConfig;
use mev_rs::config::from_toml_file;
use serde::Deserialize;
use std::{fmt, path::Path};
use tracing::info;

#[derive(Debug, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub network: Network,
#[cfg(feature = "boost")]
Expand All @@ -31,21 +30,3 @@ impl Config {
from_toml_file::<_, Self>(path.as_ref()).wrap_err("could not parse TOML")
}
}

#[derive(Debug, Args)]
#[clap(about = "🔬 (debug) utility to verify configuration")]
pub struct Command {
#[clap(env)]
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)?;
info!("{config:#?}");

Ok(())
}
}
2 changes: 2 additions & 0 deletions bin/mev/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod cmd;
pub mod config;
9 changes: 5 additions & 4 deletions bin/mev/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod cmd;

use clap::{Parser, Subcommand};
use mev::cmd;
use std::future::Future;
use tokio::signal;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
Expand All @@ -14,13 +13,14 @@ struct Cli {

#[derive(Debug, Subcommand)]
enum Commands {
Config(cmd::debug::Command),
Init(cmd::init::Command),
#[cfg(feature = "boost")]
Boost(cmd::boost::Command),
#[cfg(feature = "build")]
Build(cmd::build::Command),
#[cfg(feature = "relay")]
Relay(cmd::relay::Command),
Config(cmd::config::Command),
}

fn setup_logging() {
Expand Down Expand Up @@ -49,12 +49,13 @@ async fn main() -> eyre::Result<()> {
let cli = Cli::parse();

match cli.command {
Commands::Config(cmd) => run_task_until_signal(cmd.execute()).await,
Commands::Init(cmd) => cmd.execute(),
#[cfg(feature = "boost")]
Commands::Boost(cmd) => run_task_until_signal(cmd.execute()).await,
#[cfg(feature = "build")]
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,
}
}
4 changes: 2 additions & 2 deletions mev-boost-rs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use mev_rs::{
relay::{parse_relay_endpoints, Relay, RelayEndpoint},
Error,
};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::{future::Future, net::Ipv4Addr, pin::Pin, task::Poll};
use tokio::task::{JoinError, JoinHandle};
use tracing::{error, info};

#[derive(Debug, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub host: Ipv4Addr,
pub port: u16,
Expand Down
12 changes: 10 additions & 2 deletions mev-build-rs/src/reth_builder/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ use ethers::signers::{coins_bip39::English, MnemonicBuilder, Signer};
use futures::StreamExt;
use mev_rs::{relay::parse_relay_endpoints, Error, Relay};
use reth_primitives::{Bytes, ChainSpec};
use serde::Deserialize;
use serde::{Deserialize, Serialize, Serializer};
use std::{future::Future, pin::Pin, sync::Arc, task::Poll};
use tokio::task::{JoinError, JoinHandle};
use tracing::{error, info};

const DEFAULT_BID_PERCENT: f64 = 0.9;

#[derive(Deserialize, Debug, Default, Clone)]
fn serialize_secret_key<S>(x: &SecretKey, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_str(format!("{:?}", x).as_str())
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Config {
#[serde(serialize_with = "serialize_secret_key")]
pub secret_key: SecretKey,
pub relays: Vec<String>,
pub extra_data: Bytes,
Expand Down
2 changes: 1 addition & 1 deletion mev-build-rs/src/reth_builder/service_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct ServiceExt {
}

// NOTE: this is duplicated here to avoid circular import b/t `mev` bin and `mev-rs` crate
#[derive(Debug, serde::Deserialize)]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Config {
pub network: Network,
#[serde(rename = "builder")]
Expand Down
12 changes: 10 additions & 2 deletions mev-relay-rs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,25 @@ use ethereum_consensus::{
};
use futures::StreamExt;
use mev_rs::{blinded_block_relayer::Server as BlindedBlockRelayerServer, Error};
use serde::Deserialize;
use serde::{Deserialize, Serialize, Serializer};
use std::{future::Future, net::Ipv4Addr, pin::Pin, task::Poll};
use tokio::task::{JoinError, JoinHandle};
use tracing::{error, warn};
use url::Url;

#[derive(Deserialize, Debug)]
fn serialize_secret_key<S>(x: &SecretKey, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_str(format!("{:?}", x).as_str())
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub host: Ipv4Addr,
pub port: u16,
pub beacon_node_url: String,
#[serde(serialize_with = "serialize_secret_key")]
pub secret_key: SecretKey,
pub accepted_builders: Vec<BlsPublicKey>,
}
Expand Down