Skip to content

Commit

Permalink
updates for latest reth updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Oct 24, 2024
1 parent 2c8b75c commit 41e79a4
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 208 deletions.
220 changes: 90 additions & 130 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ reth-basic-payload-builder = { git = "https://github.com/paradigmxyz/reth", rev
reth-db = { git = "https://github.com/paradigmxyz/reth", rev = "8a40d5c6aabd8f232b9aaacce0ca474a480a9b64" }
reth-evm = { git = "https://github.com/paradigmxyz/reth", rev = "8a40d5c6aabd8f232b9aaacce0ca474a480a9b64" }
reth-node-ethereum = { git = "https://github.com/paradigmxyz/reth", rev = "8a40d5c6aabd8f232b9aaacce0ca474a480a9b64" }
reth-chain-state = { git = "https://github.com/paradigmxyz/reth", rev = "8a40d5c6aabd8f232b9aaacce0ca474a480a9b64" }
reth-evm-ethereum = { git = "https://github.com/paradigmxyz/reth", rev = "8a40d5c6aabd8f232b9aaacce0ca474a480a9b64" }
reth-errors = { git = "https://github.com/paradigmxyz/reth", rev = "8a40d5c6aabd8f232b9aaacce0ca474a480a9b64" }
alloy = { version = "0.5.4", features = [
# "consensus",
# "eips",
"consensus",
"eips",
"signers",
"signer-local",
"signer-mnemonic",
] }
# TODO: use `alloy` crate with `eips` feature, should be possible in next reth release
alloy-eips = "0.4.2"
# TODO: use `alloy` crate with `eips` feature, should be possible in next reth release
alloy-consensus = "0.4.2"

futures = "0.3.21"
tokio = "1.0"
Expand Down
5 changes: 3 additions & 2 deletions mev-build-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ reth-basic-payload-builder = { workspace = true }
reth-db = { workspace = true }
reth-evm = { workspace = true }
reth-node-ethereum = { workspace = true }
reth-chain-state = { workspace = true }
reth-evm-ethereum = { workspace = true }
reth-errors = { workspace = true }
alloy = { workspace = true }
alloy-eips = { workspace = true }
alloy-consensus = { workspace = true }

sha2 = { workspace = true }
eyre = { workspace = true }
Expand Down
18 changes: 14 additions & 4 deletions mev-build-rs/src/auctioneer/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ use mev_rs::{
BlindedBlockRelayer, Relay,
};
use reth::{
api::{EngineTypes, PayloadBuilderAttributes},
payload::{EthBuiltPayload, Events, PayloadBuilder, PayloadBuilderHandle, PayloadId},
api::{BuiltPayload, EngineTypes, PayloadBuilderAttributes},
payload::{
EthBuiltPayload, Events, PayloadBuilder, PayloadBuilderError, PayloadBuilderHandle,
PayloadId,
},
};
use serde::Deserialize;
use std::{
Expand Down Expand Up @@ -63,7 +66,6 @@ fn prepare_submission(
};
let fork = context.fork_for(auction_context.slot);
let execution_payload = to_execution_payload(payload.block(), fork)?;
let execution_requests = to_execution_requests(payload.block(), fork)?;
let signature = sign_builder_message(&message, signing_key, context)?;
let submission = match fork {
Fork::Bellatrix => {
Expand All @@ -87,6 +89,14 @@ fn prepare_submission(
signature,
}),
Fork::Electra => {
let executed_block = payload
.executed_block()
.ok_or_else(|| Error::PayloadBuilderError(PayloadBuilderError::MissingPayload))?;

let execution_output = executed_block.execution_output.as_ref();
// NOTE: assume the target requests we want are the first entry;
let requests = execution_output.requests.first();
let execution_requests = to_execution_requests(requests, fork)?;
SignedBidSubmission::Electra(block_submission::electra::SignedBidSubmission {
message,
execution_payload,
Expand Down Expand Up @@ -246,7 +256,7 @@ impl<
// TODO: work out cancellation discipline
let auction = self.store_auction(auction);

if let Err(err) = self.builder.new_payload(auction.attributes.clone()).await {
if let Err(err) = self.builder.send_new_payload(auction.attributes.clone()).await {
warn!(%err, "could not start build with payload builder");
return None
}
Expand Down
59 changes: 20 additions & 39 deletions mev-build-rs/src/compat.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::Error;
use alloy_eips::eip2718::Encodable2718;
use alloy::eips::{eip2718::Encodable2718, eip7685::Requests};
use ethereum_consensus::{
crypto::{self, KzgCommitment, KzgProof},
crypto::{KzgCommitment, KzgProof},
primitives::{Bytes32, ExecutionAddress},
ssz::prelude::{ByteList, ByteVector, DeserializeError, SimpleSerializeError, U256},
ssz::prelude::{deserialize, ByteList, ByteVector, SimpleSerializeError, U256},
Fork,
};
use mev_rs::types::{BlobsBundle, ExecutionPayload};
use reth::primitives::{
revm_primitives::{alloy_primitives::Bloom, Address, B256},
BlobTransactionSidecar, Request, SealedBlock,
BlobTransactionSidecar, SealedBlock,
};

#[cfg(not(feature = "minimal-preset"))]
Expand All @@ -30,46 +30,27 @@ fn to_byte_vector(value: Bloom) -> ByteVector<256> {
}

pub fn to_execution_requests(
block: &SealedBlock,
payload_requests: Option<&Requests>,
fork: Fork,
) -> Result<electra::ExecutionRequests, Error> {
let input = block.body.requests.as_ref();
let mut requests = electra::ExecutionRequests::default();
if let Some(input) = input {
for request in &input.0 {
match request {
Request::DepositRequest(request) => {
requests.deposits.push(electra::DepositRequest {
public_key: TryFrom::try_from(request.pubkey.as_ref())
.map_err(|err: crypto::BlsError| Error::Consensus(err.into()))?,
withdrawal_credentials: TryFrom::try_from(
request.withdrawal_credentials.as_ref(),
)
.map_err(|err: DeserializeError| {
Error::Consensus(SimpleSerializeError::Deserialize(err).into())
})?,
amount: request.amount,
signature: TryFrom::try_from(request.signature.as_ref())
.map_err(|err: crypto::BlsError| Error::Consensus(err.into()))?,
index: request.index,
});
if let Some(payload_requests) = payload_requests {
for (index, request_data) in payload_requests.iter().enumerate() {
match index {
0 => {
requests.deposits = deserialize(&request_data).map_err(|err| {
Error::Consensus(SimpleSerializeError::Deserialize(err).into())
})?;
}
Request::WithdrawalRequest(request) => {
requests.withdrawals.push(electra::WithdrawalRequest {
source_address: to_bytes20(request.source_address),
validator_public_key: TryFrom::try_from(request.validator_pubkey.as_ref())
.map_err(|err: crypto::BlsError| Error::Consensus(err.into()))?,
amount: request.amount,
});
1 => {
requests.withdrawals = deserialize(&request_data).map_err(|err| {
Error::Consensus(SimpleSerializeError::Deserialize(err).into())
})?;
}
Request::ConsolidationRequest(request) => {
requests.consolidations.push(electra::ConsolidationRequest {
source_address: to_bytes20(request.source_address),
source_public_key: TryFrom::try_from(request.source_pubkey.as_ref())
.map_err(|err: crypto::BlsError| Error::Consensus(err.into()))?,
target_public_key: TryFrom::try_from(request.target_pubkey.as_ref())
.map_err(|err: crypto::BlsError| Error::Consensus(err.into()))?,
});
2 => {
requests.consolidations = deserialize(&request_data).map_err(|err| {
Error::Consensus(SimpleSerializeError::Deserialize(err).into())
})?;
}
_ => return Err(Error::UnsupportedFork(fork)),
}
Expand Down
11 changes: 4 additions & 7 deletions mev-build-rs/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use reth::{
},
};
use reth_node_ethereum::node::{
EthereumConsensusBuilder, EthereumEngineValidatorBuilder, EthereumExecutorBuilder,
EthereumNetworkBuilder, EthereumPoolBuilder,
EthereumConsensusBuilder, EthereumExecutorBuilder, EthereumNetworkBuilder, EthereumPoolBuilder,
};

#[derive(Debug, Default, Clone, Copy)]
Expand All @@ -32,7 +31,6 @@ impl BuilderNode {
EthereumNetworkBuilder,
EthereumExecutorBuilder,
EthereumConsensusBuilder,
EthereumEngineValidatorBuilder,
>
where
Node: FullNodeTypes<
Expand All @@ -46,7 +44,6 @@ impl BuilderNode {
.network(EthereumNetworkBuilder::default())
.executor(EthereumExecutorBuilder::default())
.consensus(EthereumConsensusBuilder::default())
.engine_validator(EthereumEngineValidatorBuilder::default())
}
}

Expand All @@ -70,7 +67,7 @@ impl PayloadTypes for BuilderEngineTypes {

impl EngineTypes for BuilderEngineTypes {
type ExecutionPayloadV1 = ExecutionPayloadV1;
type ExecutionPayloadV2 = ExecutionPayloadEnvelopeV2;
type ExecutionPayloadV3 = ExecutionPayloadEnvelopeV3;
type ExecutionPayloadV4 = ExecutionPayloadEnvelopeV4;
type ExecutionPayloadEnvelopeV2 = ExecutionPayloadEnvelopeV2;
type ExecutionPayloadEnvelopeV3 = ExecutionPayloadEnvelopeV3;
type ExecutionPayloadEnvelopeV4 = ExecutionPayloadEnvelopeV4;
}
72 changes: 53 additions & 19 deletions mev-build-rs/src/payload/builder.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
use crate::payload::{attributes::BuilderPayloadBuilderAttributes, job::PayloadFinalizerConfig};
use alloy::signers::{local::PrivateKeySigner, SignerSync};
use alloy_consensus::TxEip1559;
use alloy::{
consensus::{
constants::{EMPTY_RECEIPTS, EMPTY_TRANSACTIONS},
TxEip1559, EMPTY_OMMER_ROOT_HASH,
},
eips::eip7685::{Requests, EMPTY_REQUESTS_HASH},
signers::{local::PrivateKeySigner, SignerSync},
};
use mev_rs::compute_preferred_gas_limit;
use reth::{
api::PayloadBuilderAttributes,
chainspec::{ChainSpec, EthereumHardforks},
payload::{EthBuiltPayload, PayloadBuilderError, PayloadId},
primitives::{
constants::{
eip4844::MAX_DATA_GAS_PER_BLOCK, BEACON_NONCE, EMPTY_RECEIPTS, EMPTY_TRANSACTIONS,
},
constants::{eip4844::MAX_DATA_GAS_PER_BLOCK, BEACON_NONCE},
proofs,
revm_primitives::{
alloy_primitives::{ChainId, Parity},
calc_excess_blob_gas, Address, BlockEnv, CfgEnvWithHandlerCfg, TxEnv, TxKind, U256,
},
transaction::FillTxEnv,
Block, BlockBody, Header, Receipt, Receipts, SealedBlock, Signature, Transaction,
TransactionSigned, TransactionSignedEcRecovered, EMPTY_OMMER_ROOT_HASH,
TransactionSigned, TransactionSignedEcRecovered,
},
providers::{ExecutionOutcome, StateProviderFactory},
revm::{
Expand All @@ -33,7 +37,10 @@ use reth_basic_payload_builder::{
commit_withdrawals, is_better_payload, BuildArguments, BuildOutcome, PayloadConfig,
WithdrawalsOutcome,
};
use reth_chain_state::ExecutedBlock;
use reth_errors::RethError;
use reth_evm::{system_calls::SystemCaller, ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes};
use reth_evm_ethereum::eip6110::parse_deposits_from_receipts;
use reth_node_ethereum::EthEvmConfig;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -374,7 +381,7 @@ where
let block_number = block_env.number.to::<u64>();
let block_gas_limit: u64 = block_env.gas_limit.try_into().unwrap_or(u64::MAX);

let mut system_caller = SystemCaller::new(&self.evm_config, chain_spec.clone());
let mut system_caller = SystemCaller::new(self.evm_config.clone(), chain_spec.clone());

// apply eip-4788 pre block contract call
system_caller.pre_block_beacon_root_contract_call(
Expand Down Expand Up @@ -426,7 +433,7 @@ where
blob_gas_used = Some(0);
}

let header = Header {
let mut header = Header {
parent_hash: parent_block.hash(),
ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: block_env.coinbase,
Expand All @@ -447,10 +454,14 @@ where
blob_gas_used,
excess_blob_gas,
parent_beacon_block_root: attributes.parent_beacon_block_root(),
requests_root: None,
requests_hash: None,
};

let body = BlockBody { transactions: vec![], withdrawals, ommers: vec![], requests: None };
if chain_spec.is_prague_active_at_timestamp(header.timestamp) {
header.requests_hash = Some(EMPTY_REQUESTS_HASH);
}

let body = BlockBody { transactions: vec![], withdrawals, ommers: vec![] };
let block = Block { header, body };
let sealed_block = block.seal_slow();

Expand Down Expand Up @@ -501,7 +512,7 @@ where

let block_number = block_env.number.to::<u64>();

let mut system_caller = SystemCaller::new(&evm_config, chain_spec.clone());
let mut system_caller = SystemCaller::new(evm_config.clone(), chain_spec.clone());

// apply eip-4788 pre block contract call
system_caller.pre_block_beacon_root_contract_call(
Expand Down Expand Up @@ -551,7 +562,7 @@ where
let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg_env.clone(),
block_env.clone(),
evm_config.tx_env(&tx),
evm_config.tx_env(&tx, tx.signer()),
);

// Configure the environment for the block.
Expand Down Expand Up @@ -628,6 +639,21 @@ where
return Ok((BuildOutcome::Aborted { fees: total_fees, cached_reads }, None))
}

let requests = if chain_spec.is_prague_active_at_timestamp(attributes.timestamp()) {
let deposit_requests = parse_deposits_from_receipts(&chain_spec, receipts.iter().flatten())
.map_err(|err| PayloadBuilderError::Internal(RethError::Execution(err.into())))?;
let withdrawal_requests = system_caller
.post_block_withdrawal_requests_contract_call(&mut db, &cfg_env, &block_env)
.map_err(|err| PayloadBuilderError::Internal(err.into()))?;
let consolidation_requests = system_caller
.post_block_consolidation_requests_contract_call(&mut db, &cfg_env, &block_env)
.map_err(|err| PayloadBuilderError::Internal(err.into()))?;

Some(Requests::new(vec![deposit_requests, withdrawal_requests, consolidation_requests]))
} else {
None
};

let WithdrawalsOutcome { withdrawals_root, withdrawals } = commit_withdrawals(
&mut db,
chain_spec,
Expand All @@ -637,14 +663,14 @@ where

// merge all transitions into bundle state, this would apply the withdrawal balance changes
// and 4788 contract call
db.merge_transitions(BundleRetention::PlainState);
db.merge_transitions(BundleRetention::Reverts);

// TODO: final parameter is for EIP-7685 requests
let requests_hash = requests.as_ref().map(|requests| requests.requests_hash());
let execution_outcome = ExecutionOutcome::new(
db.take_bundle(),
Receipts::from(vec![receipts]),
block_number,
vec![],
vec![requests.unwrap_or_default()],
);
let receipts_root =
execution_outcome.receipts_root_slow(block_number).expect("Number is in range");
Expand Down Expand Up @@ -703,18 +729,26 @@ where
parent_beacon_block_root: attributes.parent_beacon_block_root(),
blob_gas_used,
excess_blob_gas,
requests_root: None,
requests_hash,
};

// seal the block
let body =
BlockBody { transactions: executed_txs, withdrawals, ommers: vec![], requests: None };
let body = BlockBody { transactions: executed_txs, withdrawals, ommers: vec![] };
let block = Block { header, body };

let sealed_block = block.seal_slow();
debug!(target: "payload_builder", ?sealed_block, "sealed built block");

let mut payload = EthBuiltPayload::new(attributes.payload_id(), sealed_block, total_fees, None);
let executed = ExecutedBlock {
block: Arc::new(sealed_block.clone()),
senders: Arc::new(vec![]),
execution_output: Arc::new(execution_outcome.clone()),
hashed_state: Arc::new(Default::default()),
trie: Arc::new(Default::default()),
};

let mut payload =
EthBuiltPayload::new(attributes.payload_id(), sealed_block, total_fees, Some(executed));

// extend the payload with the blob sidecars from the executed txs
payload.extend_sidecars(blob_sidecars);
Expand Down
5 changes: 4 additions & 1 deletion mev-build-rs/src/payload/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ where
Ok(self.config.attributes.clone())
}

fn resolve(&mut self) -> (Self::ResolvePayloadFuture, KeepPayloadJobAlive) {
fn resolve_kind(
&mut self,
_kind: payload::PayloadKind,
) -> (Self::ResolvePayloadFuture, KeepPayloadJobAlive) {
let best_payload = self.best_payload.take();
let maybe_better = self.pending_block.take();
let mut empty_payload = None;
Expand Down

0 comments on commit 41e79a4

Please sign in to comment.