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 cbc0514
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 87 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
5 changes: 3 additions & 2 deletions book/mev-build-rs.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ Fields you should change:
This wallet will be used to author payment transactions to the proposer and also is used as the source of funds for any subsidy value you wish to add to the block.
You can select a particular index (following BIP-39) by terminating the seed phrase with a `:N` and integer index `N`. Otherwise the builder will just use the first index from the key tree.
* `subsidy_gwei`: set this value to 0 if your execution layer address has no ETH in it; otherwise, the blocks will be invalid.
* `jwt_secret_path`: this path points to the JWT secret file created previously and is specific to your deployment.

### Launch

Expand All @@ -62,9 +61,11 @@ the tip of the chain, the CL and EL nodes will sync. To expedite syncing times,
1. Run `mev` with config file `config.toml`:
```sh
mev build config.toml
mev build node --mev-builder-config config.toml --authrpc.jwtsecret $JWT_SECRET_FILE_PATH
```

> NOTE: `mev build` exposes the same CLI interface as stock `reth`, so refer to that command's help for further settings you may be interested in.
2. Run `lighthouse`:
```sh
lighthouse --network sepolia \
Expand Down
4 changes: 1 addition & 3 deletions example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ relays = [
"https://0x845bd072b7cd566f02faeb0a4033ce9399e42839ced64e8b2adcfc859ed1e8e1a5a293336a49feac6d9a5edb779be53a@boost-relay-sepolia.flashbots.net",
]
# extra data to write into built execution payload
extra_data = "hello world"
extra_data = "0x68656C6C6F20776F726C640A" # "hello world"
# wallet seed for builder to author payment transactions
execution_mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
# number of milliseconds to submit bids ahead of the target slot
Expand All @@ -36,5 +36,3 @@ bid_percent = 0.9
# amount to add to the bid on top of the payload's revenue,
# currently sourced from the builder's wallet authoring the payment transaction
subsidy_gwei = 100000000 # 0.1 eth
# path for the Engine API credentials shared between consensus and execution clients
jwt_secret_path = "/secrets/jwt.hex"
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;
11 changes: 5 additions & 6 deletions mev-build-rs/src/reth_builder/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ 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,
// amount to bid as a fraction of the block's value
pub bid_percent: Option<f64>,
// amount to add from the builder's wallet as a subsidy to the auction bid
pub subsidy_gwei: Option<u64>,
pub jwt_secret_path: Option<String>,
}

pub struct Service<Pool, Client, Bidder> {
Expand Down Expand Up @@ -67,15 +66,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 +93,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 cbc0514

Please sign in to comment.