Skip to content

Commit

Permalink
Merge pull request #156 from ralexstokes/reth-refactor
Browse files Browse the repository at this point in the history
Refactoring `reth` integration for `mev-build-rs`
  • Loading branch information
ralexstokes authored Oct 19, 2023
2 parents 9d6314b + cbc2f9d commit ffefbf2
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 197 deletions.
258 changes: 141 additions & 117 deletions Cargo.lock

Large diffs are not rendered by default.

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 }
30 changes: 30 additions & 0 deletions bin/mev/src/cmd/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::cmd::config::Config;
use anyhow::{anyhow, Result};
use clap::Args;
use mev_build_rs::reth_builder::Service;
use tracing::info;

#[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) -> 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(anyhow!("missing boost config from file provided"))
}
}
}
21 changes: 0 additions & 21 deletions bin/mev/src/cmd/build/mod.rs

This file was deleted.

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"] }
2 changes: 1 addition & 1 deletion mev-build-rs/src/reth_builder/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Error {
#[error(transparent)]
Reth(#[from] RethError),
#[error("evm execution error: {0:?}")]
Execution(EVMError<RethError>),
Execution(#[from] EVMError<RethError>),
#[error("{0}")]
Internal(&'static str),
}
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;
6 changes: 3 additions & 3 deletions mev-build-rs/src/reth_builder/payload_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn assemble_payload_with_payments(
let bundle_state = context.bundle_state;
let transactions_root = proofs::calculate_transaction_root(&context.executed_txs);

let state_root = client.latest().unwrap().state_root(&bundle_state.clone()).unwrap();
let state_root = client.latest()?.state_root(&bundle_state.clone())?;
let receipts = bundle_state.receipts_by_block(block_number);
let bundle = BundleStateWithReceipts::new(
context.db.take_bundle(),
Expand Down Expand Up @@ -163,7 +163,7 @@ fn construct_payment_tx(
) -> Result<TransactionSignedEcRecovered, Error> {
let sender = context.build.builder_wallet.address();
let signer_account = context.db.load_cache_account(sender.into())?;
let nonce = signer_account.account_info().unwrap().nonce;
let nonce = signer_account.account_info().expect("account exists").nonce;
let chain_id = context.build.chain_spec.genesis().config.chain_id;

let fee_recipient = ethers_H160::from_slice(context.build.proposer_fee_recipient.as_ref());
Expand Down Expand Up @@ -263,7 +263,7 @@ impl<'a> ExecutionContext<'a> {
let mut evm = revm::EVM::with_env(env);
evm.database(&mut self.db);

let ResultAndState { result, state } = evm.transact().unwrap();
let ResultAndState { result, state } = evm.transact()?;

let block_number = self.build.number();
self.db.commit(state);
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},
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,48 @@ 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 {
pub struct ServiceExt {
#[clap(skip)]
pub config_file: String,
network: Network,
#[clap(skip)]
pub network: Option<Network>,
#[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(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(), "--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 +74,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 = &self.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 +116,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 ffefbf2

Please sign in to comment.