Skip to content

Commit

Permalink
update to recent reth commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Oct 19, 2023
1 parent 782bc63 commit 4cf26a7
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 230 deletions.
528 changes: 364 additions & 164 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 9 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@ version = "0.3.0"
ethereum-consensus = { git = "https://github.com/ralexstokes/ethereum-consensus", rev = "c84f44ee7ec04613adf5f2be845df35e05de1efc" }
beacon-api-client = { git = "https://github.com/ralexstokes/ethereum-consensus", rev = "c84f44ee7ec04613adf5f2be845df35e05de1efc" }

reth-payload-builder = { git = "https://github.com/paradigmxyz/reth", rev = "1b16d804ef01f4ec3c25e7986381c22739c105b9" }
reth-primitives = { git = "https://github.com/paradigmxyz/reth", rev = "1b16d804ef01f4ec3c25e7986381c22739c105b9" }
reth-provider = { git = "https://github.com/paradigmxyz/reth", rev = "1b16d804ef01f4ec3c25e7986381c22739c105b9" }
reth-rpc-types = { git = "https://github.com/paradigmxyz/reth", rev = "1b16d804ef01f4ec3c25e7986381c22739c105b9" }
reth-transaction-pool = { git = "https://github.com/paradigmxyz/reth", rev = "1b16d804ef01f4ec3c25e7986381c22739c105b9" }
reth-interfaces = { git = "https://github.com/paradigmxyz/reth", rev = "1b16d804ef01f4ec3c25e7986381c22739c105b9" }
reth-revm = { git = "https://github.com/paradigmxyz/reth", rev = "1b16d804ef01f4ec3c25e7986381c22739c105b9" }

reth = { git = "https://github.com/paradigmxyz/reth", rev = "1b16d804ef01f4ec3c25e7986381c22739c105b9" }
revm = { git = "https://github.com/bluealloy/revm/", rev = "516f62cc" }

[patch.crates-io]
revm = { git = "https://github.com/bluealloy/revm/", rev = "516f62cc" }
revm-primitives = { git = "https://github.com/bluealloy/revm/", rev = "516f62cc" }
reth-payload-builder = { git = "https://github.com/paradigmxyz/reth", rev = "5dd5555c5c7d8e43420e273e7005b8af63a847a5" }
reth-primitives = { git = "https://github.com/paradigmxyz/reth", rev = "5dd5555c5c7d8e43420e273e7005b8af63a847a5" }
reth-provider = { git = "https://github.com/paradigmxyz/reth", rev = "5dd5555c5c7d8e43420e273e7005b8af63a847a5" }
reth-rpc-types = { git = "https://github.com/paradigmxyz/reth", rev = "5dd5555c5c7d8e43420e273e7005b8af63a847a5" }
reth-transaction-pool = { git = "https://github.com/paradigmxyz/reth", rev = "5dd5555c5c7d8e43420e273e7005b8af63a847a5" }
reth-interfaces = { git = "https://github.com/paradigmxyz/reth", rev = "5dd5555c5c7d8e43420e273e7005b8af63a847a5" }
reth-revm = { git = "https://github.com/paradigmxyz/reth", rev = "5dd5555c5c7d8e43420e273e7005b8af63a847a5" }
reth = { git = "https://github.com/paradigmxyz/reth", rev = "5dd5555c5c7d8e43420e273e7005b8af63a847a5" }
revm = "3.5.0"
8 changes: 4 additions & 4 deletions mev-build-rs/src/reth_builder/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use mev_rs::{
signing::sign_builder_message,
types::{BidTrace, SignedBidSubmission},
};
use reth_primitives::{Bytes, ChainSpec, SealedBlock, Withdrawal, H256, U256};
use reth_primitives::{Bytes, ChainSpec, SealedBlock, Withdrawal, B256, U256};
use revm::primitives::{BlockEnv, CfgEnv};
use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -48,11 +48,11 @@ fn make_submission(
#[derive(Debug, Clone)]
pub struct BuildContext {
pub slot: Slot,
pub parent_hash: H256,
pub parent_hash: B256,
pub proposer: BlsPublicKey,
pub timestamp: u64,
pub proposer_fee_recipient: ExecutionAddress,
pub prev_randao: H256,
pub prev_randao: B256,
pub withdrawals: Vec<Withdrawal>,
pub relays: Vec<RelayIndex>,
pub chain_spec: Arc<ChainSpec>,
Expand All @@ -69,7 +69,7 @@ pub struct BuildContext {
pub subsidy: U256,
}

pub fn compute_build_id(slot: Slot, parent_hash: H256, proposer: &BlsPublicKey) -> BuildIdentifier {
pub fn compute_build_id(slot: Slot, parent_hash: B256, proposer: &BlsPublicKey) -> BuildIdentifier {
let mut data = Vec::with_capacity(88);
slot.serialize(&mut data).expect("can serialize");
parent_hash.serialize(&mut data).expect("can serialize");
Expand Down
6 changes: 3 additions & 3 deletions mev-build-rs/src/reth_builder/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ethereum_consensus::{
use ethers::signers::{LocalWallet, Signer};
use mev_rs::{blinded_block_relayer::BlindedBlockRelayer, types::ProposerSchedule, Relay};
use reth_payload_builder::PayloadBuilderAttributes;
use reth_primitives::{BlockNumberOrTag, Bytes, ChainSpec, H256, U256};
use reth_primitives::{Address, BlockNumberOrTag, Bytes, ChainSpec, B256, U256};
use reth_provider::{BlockReaderIdExt, BlockSource, StateProviderFactory};
use reth_transaction_pool::TransactionPool;
use std::{
Expand Down Expand Up @@ -313,7 +313,7 @@ impl<Pool: TransactionPool, Client: StateProviderFactory + BlockReaderIdExt> Bui
fn construct_build_context(
&self,
slot: Slot,
parent_hash: H256,
parent_hash: B256,
proposer: &BlsPublicKey,
payload_attributes: PayloadBuilderAttributes,
validator_preferences: &ValidatorPreferences,
Expand Down Expand Up @@ -345,7 +345,7 @@ impl<Pool: TransactionPool, Client: StateProviderFactory + BlockReaderIdExt> Bui

// TODO: configurable "fee collection strategy"
// fee collection strategy: drive all fees to builder
block_env.coinbase = self.builder_wallet.address().into();
block_env.coinbase = Address::from(self.builder_wallet.address().to_fixed_bytes());

let subsidy = U256::from(self.subsidy_gwei);
let subsidy_in_wei = subsidy * U256::from(10u64.pow(9));
Expand Down
4 changes: 2 additions & 2 deletions mev-build-rs/src/reth_builder/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::reth_builder::build::BuildIdentifier;
use ethereum_consensus::{primitives::Slot, Error as ConsensusError};
use reth_interfaces::RethError;
use reth_primitives::H256;
use reth_primitives::B256;
use revm::primitives::EVMError;
use thiserror::Error;

Expand All @@ -12,7 +12,7 @@ pub enum Error {
#[error("missing a build request with identifier {0}")]
MissingBuild(BuildIdentifier),
#[error("missing parent block {0}")]
MissingParentBlock(H256),
MissingParentBlock(B256),
#[error("payload requested but build {0} has not produced one yet")]
PayloadNotPrepared(BuildIdentifier),
#[error("{0}")]
Expand Down
31 changes: 17 additions & 14 deletions mev-build-rs/src/reth_builder/payload_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ use crate::reth_builder::{
use ethers::{
signers::Signer,
types::{
transaction::eip2718::TypedTransaction, Eip1559TransactionRequest, H160 as ethers_H160,
transaction::eip2718::TypedTransaction, Eip1559TransactionRequest, H160,
U256 as ethers_U256,
},
};
use reth_interfaces::RethError;
use reth_primitives::{
constants::{BEACON_NONCE, EMPTY_OMMER_ROOT},
proofs, Block, Bytes, ChainSpec, Header, IntoRecoveredTransaction, Receipt, TransactionSigned,
TransactionSignedEcRecovered, Withdrawal, H256, U256,
proofs, Address, Block, Bytes, ChainSpec, Header, IntoRecoveredTransaction, Receipt, Receipts,
TransactionSigned, TransactionSignedEcRecovered, Withdrawal, B256, U256,
};
use reth_provider::{BundleStateWithReceipts, StateProvider, StateProviderFactory};
use reth_revm::{
Expand All @@ -34,7 +35,7 @@ fn process_withdrawals<DB: Database<Error = RethError>>(
chain_spec: &ChainSpec,
db: &mut State<DB>,
timestamp: u64,
) -> Result<H256, Error> {
) -> Result<B256, Error> {
let balance_increments =
post_block_withdrawals_balance_increments(chain_spec, timestamp, withdrawals);
db.increment_balances(balance_increments)?;
Expand Down Expand Up @@ -111,10 +112,10 @@ fn assemble_payload_with_payments(
let transactions_root = proofs::calculate_transaction_root(&context.executed_txs);

let state_root = client.latest()?.state_root(&bundle_state.clone())?;
let receipts = bundle_state.receipts_by_block(block_number);
let receipts = bundle_state.receipts_by_block(block_number).to_vec();
let bundle = BundleStateWithReceipts::new(
context.db.take_bundle(),
vec![receipts.to_vec()],
Receipts::from_vec(vec![receipts]),
block_number,
);
let receipts_root = bundle.receipts_root_slow(block_number).expect("Number is in range");
Expand All @@ -130,7 +131,7 @@ fn assemble_payload_with_payments(
receipts_root,
logs_bloom,
timestamp: context.build.timestamp,
mix_hash: H256::from_slice(context.build.prev_randao.as_ref()),
mix_hash: B256::from_slice(context.build.prev_randao.as_ref()),
nonce: BEACON_NONCE,
base_fee_per_gas: Some(base_fee),
number: block_number,
Expand Down Expand Up @@ -162,19 +163,21 @@ fn construct_payment_tx(
context: &mut ExecutionContext,
) -> Result<TransactionSignedEcRecovered, Error> {
let sender = context.build.builder_wallet.address();
let signer_account = context.db.load_cache_account(sender.into())?;
let reth_sender = Address::from(sender.to_fixed_bytes());
let signer_account = context.db.load_cache_account(reth_sender)?;
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());
let fee_recipient = H160::from_slice(context.build.proposer_fee_recipient.as_ref());
let value = ethers_U256::from_big_endian(&context.total_payment.to_be_bytes::<32>());
let tx = Eip1559TransactionRequest::new()
.from(sender)
.to(fee_recipient)
// TODO: support smart contract payments
.gas(21000)
.max_fee_per_gas(context.build.base_fee())
.max_priority_fee_per_gas(0)
.value(context.total_payment)
.value(value)
.data(ethers::types::Bytes::default())
.access_list(ethers::types::transaction::eip2930::AccessList::default())
.nonce(nonce)
Expand All @@ -185,10 +188,10 @@ fn construct_payment_tx(
let signature = wallet.sign_transaction_sync(&tx).expect("can make transaction");
let tx_encoded = tx.rlp_signed(&signature);

let payment_tx = TransactionSigned::decode_enveloped(Bytes::from(tx_encoded.as_ref()))
.expect("can decode valid txn");
let tx_encoded = Bytes::from(tx_encoded.0);
let payment_tx = TransactionSigned::decode_enveloped(tx_encoded).expect("can decode valid txn");

Ok(TransactionSignedEcRecovered::from_signed_transaction(payment_tx, sender.into()))
Ok(TransactionSignedEcRecovered::from_signed_transaction(payment_tx, reth_sender))
}

struct ExecutionContext<'a> {
Expand Down Expand Up @@ -226,7 +229,7 @@ impl<'a> ExecutionContext<'a> {
revm::State::builder().with_database_ref(StateProviderDatabase::new(state)).build();
let bundle_state = BundleStateWithReceipts::new(
db.take_bundle(),
vec![],
Receipts::default(),
u64::from_le_bytes(context.block_env.number.to_le_bytes()),
);
Ok(ExecutionContext {
Expand Down
12 changes: 6 additions & 6 deletions mev-build-rs/src/reth_builder/reth_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ use ethereum_consensus::{
},
};
use mev_rs::types::ExecutionPayload;
use reth_primitives::{Bloom, SealedBlock, H160, H256, U256};
use reth_primitives::{Address, Bloom, SealedBlock, B256, U256};

pub(crate) fn to_bytes32(value: H256) -> Bytes32 {
Bytes32::try_from(value.as_bytes()).unwrap()
pub(crate) fn to_bytes32(value: B256) -> Bytes32 {
Bytes32::try_from(value.as_ref()).unwrap()
}

fn to_bytes20(value: H160) -> ExecutionAddress {
ExecutionAddress::try_from(value.as_bytes()).unwrap()
fn to_bytes20(value: Address) -> ExecutionAddress {
ExecutionAddress::try_from(value.as_ref()).unwrap()
}

fn to_byte_vector(value: Bloom) -> ByteVector<256> {
ByteVector::<256>::try_from(value.as_bytes()).unwrap()
ByteVector::<256>::try_from(value.as_ref()).unwrap()
}

pub(crate) fn to_u256(value: &U256) -> ssz_rs::U256 {
Expand Down
2 changes: 1 addition & 1 deletion mev-build-rs/src/reth_builder/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<
pool,
client,
chain_spec,
Bytes::from(config.extra_data.as_bytes()),
Bytes::from(config.extra_data),
builder_wallet,
config.bid_percent.unwrap_or(DEFAULT_BID_PERCENT),
config.subsidy_gwei.unwrap_or_default(),
Expand Down
41 changes: 19 additions & 22 deletions mev-build-rs/src/reth_builder/service_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ use ethereum_consensus::{
state_transition::Context,
};
use reth::{
cli::ext::{RethCliExt, RethNodeCommandConfig},
cli::{
components::RethNodeComponents,
config::PayloadBuilderConfig,
ext::{RethCliExt, RethNodeCommandConfig},
},
node::NodeCommand,
runner::CliContext,
tasks::TaskManager,
tasks::{TaskManager, TaskSpawner},
};
use reth_payload_builder::PayloadBuilderService;
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use std::{sync::Arc, time::Duration};
use tracing::warn;

Expand Down Expand Up @@ -56,23 +60,14 @@ impl RethCliExt for ServiceExt {
}

impl RethNodeCommandConfig for ServiceExt {
fn spawn_payload_builder_service<Conf, Provider, Pool, Tasks>(
fn spawn_payload_builder_service<Conf, Reth>(
&mut self,
_conf: &Conf,
provider: Provider,
pool: Pool,
executor: Tasks,
chain_spec: std::sync::Arc<reth_primitives::ChainSpec>,
) -> eyre::Result<reth_payload_builder::PayloadBuilderHandle>
components: &Reth,
) -> eyre::Result<PayloadBuilderHandle>
where
Conf: reth::cli::config::PayloadBuilderConfig,
Provider: reth::providers::StateProviderFactory
+ reth::providers::BlockReaderIdExt
+ Clone
+ Unpin
+ 'static,
Pool: reth::transaction_pool::TransactionPool + Unpin + 'static,
Tasks: reth::tasks::TaskSpawner + Clone + Unpin + 'static,
Conf: PayloadBuilderConfig,
Reth: RethNodeComponents,
{
let build_config = self.config.clone();
let network = &self.network;
Expand All @@ -87,10 +82,10 @@ impl RethNodeCommandConfig for ServiceExt {
build_config,
context,
clock,
pool.clone(),
provider.clone(),
components.pool(),
components.provider(),
bidder,
chain_spec.clone(),
components.chain_spec(),
)
.unwrap();

Expand All @@ -110,8 +105,10 @@ impl RethNodeCommandConfig for ServiceExt {
}
});

executor.spawn_critical("boost builder", fut);
executor.spawn_critical("payload builder service", Box::pin(payload_service));
components.task_executor().spawn_critical("boost builder", fut);
components
.task_executor()
.spawn_critical("payload builder service", Box::pin(payload_service));

Ok(payload_builder)
}
Expand Down

0 comments on commit 4cf26a7

Please sign in to comment.