Skip to content

Commit

Permalink
Merge pull request #191 from ralexstokes/reth-payload-builder
Browse files Browse the repository at this point in the history
integrate basic reth payload builder to `mev-build-rs`
  • Loading branch information
ralexstokes authored Apr 19, 2024
2 parents 6275cef + 99ef526 commit 7f66dfb
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 127 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ethereum-consensus = { git = "https://github.com/ralexstokes/ethereum-consensus"
beacon-api-client = { git = "https://github.com/ralexstokes/ethereum-consensus", rev = "fc049504a200926c8bd5f0fbd3f9696c6c6f699d" }

reth-payload-builder = { git = "https://github.com/paradigmxyz/reth", rev = "0a3884ba81579a775a0305be3a6621cd6782176a" }
reth-basic-payload-builder = { git = "https://github.com/paradigmxyz/reth", rev = "0a3884ba81579a775a0305be3a6621cd6782176a" }
reth-primitives = { git = "https://github.com/paradigmxyz/reth", rev = "0a3884ba81579a775a0305be3a6621cd6782176a" }
reth-provider = { git = "https://github.com/paradigmxyz/reth", rev = "0a3884ba81579a775a0305be3a6621cd6782176a" }
reth-rpc-types = { git = "https://github.com/paradigmxyz/reth", rev = "0a3884ba81579a775a0305be3a6621cd6782176a" }
Expand Down
6 changes: 1 addition & 5 deletions bin/mev/src/cmd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use ethereum_consensus::networks::Network;
use eyre::WrapErr;
#[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;
Expand All @@ -17,9 +15,7 @@ pub struct Config {
pub network: Network,
#[cfg(feature = "boost")]
pub boost: Option<BoostConfig>,
#[cfg(feature = "build")]
#[serde(rename = "builder")]
pub build: Option<BuildConfig>,
// NOTE: builder config is handled in reth cli extension
#[cfg(feature = "relay")]
pub relay: Option<RelayConfig>,
}
Expand Down
1 change: 1 addition & 0 deletions mev-build-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mev-rs = { path = "../mev-rs" }

revm = { workspace = true }
reth-payload-builder = { workspace = true }
reth-basic-payload-builder = { workspace = true }
reth-primitives = { workspace = true }
reth-transaction-pool = { workspace = true }
reth-provider = { workspace = true }
Expand Down
21 changes: 16 additions & 5 deletions mev-build-rs/src/reth_builder/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use mev_rs::{
types::{BidTrace, SignedBidSubmission},
Relay,
};
use reth_payload_builder::{BuiltPayload, PayloadBuilderAttributes};
use reth_primitives::{Bytes, ChainSpec, SealedBlock, Withdrawal, B256, U256};
use revm::primitives::{BlockEnv, CfgEnv};
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -62,11 +63,14 @@ pub struct BuildContext {
pub builder_wallet: LocalWallet,
// Amount of gas to reserve after building a payload
// e.g. used for end-of-block proposer payments
pub gas_reserve: u64,
pub _gas_reserve: u64,
// Amount of the block's value to bid to the proposer
pub bid_percent: f64,
// Amount to add to the block's value to bid to the proposer
pub subsidy: U256,
// TODO: refactor these w/ the above fields
pub parent_block: Arc<SealedBlock>,
pub payload_attributes: PayloadBuilderAttributes,
}

pub fn compute_build_id(slot: Slot, parent_hash: B256, proposer: &BlsPublicKey) -> BuildIdentifier {
Expand Down Expand Up @@ -113,9 +117,9 @@ impl Build {
Self { context, state: Mutex::new(Default::default()) }
}

pub fn value(&self) -> U256 {
pub fn payload(&self) -> Option<Arc<BuiltPayload>> {
let state = self.state.lock().unwrap();
state.payload_with_payments.proposer_payment
state.payload_with_payments.payload.clone()
}

pub fn prepare_bid(
Expand All @@ -134,15 +138,22 @@ impl Build {
let payment = &payload_with_payments.proposer_payment;
let builder_payment = payload_with_payments.builder_payment;
Ok((
make_submission(secret_key, public_key, context, build_context, payload, payment)?,
make_submission(
secret_key,
public_key,
context,
build_context,
payload.block(),
payment,
)?,
builder_payment,
))
}
}

#[derive(Debug, Default)]
pub struct PayloadWithPayments {
pub payload: Option<SealedBlock>,
pub payload: Option<Arc<BuiltPayload>>,
pub proposer_payment: U256,
pub builder_payment: U256,
}
31 changes: 20 additions & 11 deletions mev-build-rs/src/reth_builder/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::reth_builder::{
auction_schedule::AuctionSchedule, build::*, cancelled::Cancelled, error::Error,
payload_builder::*,
auction_schedule::AuctionSchedule, build::*, error::Error, payload_builder::*,
};
use ethereum_consensus::{
clock::SystemClock,
Expand All @@ -10,6 +9,7 @@ use ethereum_consensus::{
};
use ethers::signers::{LocalWallet, Signer};
use mev_rs::{blinded_block_relayer::BlindedBlockRelayer, compute_preferred_gas_limit, Relay};
use reth_basic_payload_builder::Cancelled;
use reth_payload_builder::PayloadBuilderAttributes;
use reth_primitives::{Address, BlockNumberOrTag, Bytes, ChainSpec, B256, U256};
use reth_provider::{BlockReaderIdExt, BlockSource, StateProviderFactory};
Expand All @@ -22,6 +22,7 @@ use std::{
};
use tokio::sync::mpsc;
use tokio_stream::{wrappers::ReceiverStream, Stream};
use tracing::debug;

// The amount to let the build progress into its target slot.
// The build will stop early if stopped by an outside process.
Expand Down Expand Up @@ -231,7 +232,9 @@ pub enum PayloadAttributesProcessingOutcome {
Duplicate(PayloadBuilderAttributes),
}

impl<Pool: TransactionPool, Client: StateProviderFactory + BlockReaderIdExt> Builder<Pool, Client> {
impl<Pool: TransactionPool, Client: StateProviderFactory + BlockReaderIdExt + Clone>
Builder<Pool, Client>
{
// TODO: clean up argument set
#[allow(clippy::too_many_arguments)]
// NOTE: this is held inside a lock currently, minimize work here
Expand Down Expand Up @@ -289,9 +292,11 @@ impl<Pool: TransactionPool, Client: StateProviderFactory + BlockReaderIdExt> Bui
extra_data: self.extra_data.clone(),
builder_wallet: self.builder_wallet.clone(),
// TODO: handle smart contract payments to fee recipient
gas_reserve: 21000,
_gas_reserve: 21000,
bid_percent: self.bid_percent,
subsidy: subsidy_in_wei,
parent_block: Arc::new(parent_block),
payload_attributes: payload_attributes.clone(),
};
Ok(context)
}
Expand Down Expand Up @@ -334,11 +339,14 @@ impl<Pool: TransactionPool, Client: StateProviderFactory + BlockReaderIdExt> Bui
let build = Arc::new(Build::new(context));

// TODO: encapsulate these details
let current_value = build.value();
let cancel = Cancelled::default();
if let Ok(BuildOutcome::BetterOrEqual(payload_with_payments)) =
build_payload(&build.context, current_value, &self.client, &self.pool, &cancel)
{
if let Ok(BuildOutcome::BetterOrEqual(payload_with_payments)) = build_payload(
&build.context,
None,
self.client.clone(),
self.pool.clone(),
cancel.clone(),
) {
let mut state = build.state.lock().unwrap();
state.payload_with_payments = payload_with_payments;
}
Expand Down Expand Up @@ -379,13 +387,14 @@ impl<Pool: TransactionPool, Client: StateProviderFactory + BlockReaderIdExt> Bui
return Ok(())
}
_ = interval.tick() => {
let current_value = build.value();
match build_payload(&build.context, current_value, &self.client, &self.pool, &cancel) {
match build_payload(&build.context, build.payload(), self.client.clone(), self.pool.clone(), cancel.clone()) {
Ok(BuildOutcome::BetterOrEqual(payload_with_payments)) => {
let mut state = build.state.lock().unwrap();
state.payload_with_payments = payload_with_payments;
}
Ok(BuildOutcome::Worse { .. }) => continue,
Ok(BuildOutcome::Worse { threshold, provided }) => {
debug!(%threshold, %provided, "did not build a better payload");
}
Ok(BuildOutcome::Cancelled) => {
tracing::trace!(%id, "build cancelled");
return Ok(())
Expand Down
18 changes: 0 additions & 18 deletions mev-build-rs/src/reth_builder/cancelled.rs

This file was deleted.

3 changes: 3 additions & 0 deletions mev-build-rs/src/reth_builder/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::reth_builder::build::BuildIdentifier;
use ethereum_consensus::{primitives::Slot, Error as ConsensusError};
use reth_interfaces::RethError;
use reth_payload_builder::error::PayloadBuilderError;
use reth_primitives::B256;
use revm::primitives::EVMError;
use thiserror::Error;
Expand All @@ -19,6 +20,8 @@ pub enum Error {
Consensus(#[from] ConsensusError),
#[error(transparent)]
Reth(#[from] RethError),
#[error(transparent)]
RethPayloadBuilder(#[from] PayloadBuilderError),
#[error("evm execution error: {0:?}")]
Execution(#[from] EVMError<RethError>),
#[error("{0}")]
Expand Down
1 change: 0 additions & 1 deletion mev-build-rs/src/reth_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod auction_schedule;
mod bidder;
mod build;
mod builder;
mod cancelled;
mod error;
mod payload_builder;
mod reth_compat;
Expand Down
Loading

0 comments on commit 7f66dfb

Please sign in to comment.