feat: op-reth (#4377)

Co-authored-by: Roberto Bayardo <bayardo@alum.mit.edu>
Co-authored-by: refcell.eth <abigger87@gmail.com>
Co-authored-by: Roman Krasiuk <rokrassyuk@gmail.com>
Co-authored-by: refcell <refcell@oplabs.co>
Co-authored-by: nicolas <48695862+merklefruit@users.noreply.github.com>
This commit is contained in:
clabby
2023-11-05 18:33:42 +01:00
committed by GitHub
parent 390abf3a44
commit 52670a8b24
105 changed files with 33415 additions and 408 deletions

View File

@ -33,3 +33,12 @@ metrics.workspace = true
## misc
tracing.workspace = true
[features]
optimism = [
"reth-primitives/optimism",
"reth-revm/optimism",
"reth-transaction-pool/optimism",
"reth-provider/optimism",
"reth-payload-builder/optimism"
]

View File

@ -57,6 +57,11 @@ use tracing::{debug, trace};
mod metrics;
#[cfg(feature = "optimism")]
mod optimism;
#[cfg(feature = "optimism")]
pub use optimism::OptimismPayloadBuilder;
/// The [`PayloadJobGenerator`] that creates [`BasicPayloadJob`]s.
#[derive(Debug)]
pub struct BasicPayloadJobGenerator<Client, Pool, Tasks, Builder = ()> {
@ -152,6 +157,8 @@ where
self.config.extradata.clone(),
attributes,
Arc::clone(&self.chain_spec),
#[cfg(feature = "optimism")]
self.config.compute_pending_block,
);
let until = tokio::time::Instant::now() + self.config.deadline;
@ -199,6 +206,9 @@ pub struct BasicPayloadJobGeneratorConfig {
deadline: Duration,
/// Maximum number of tasks to spawn for building a payload.
max_payload_tasks: usize,
/// The rollup's compute pending block configuration option.
#[cfg(feature = "optimism")]
compute_pending_block: bool,
}
// === impl BasicPayloadJobGeneratorConfig ===
@ -242,6 +252,15 @@ impl BasicPayloadJobGeneratorConfig {
self.max_gas_limit = max_gas_limit;
self
}
/// Sets the compute pending block configuration option.
///
/// Defaults to `false`.
#[cfg(feature = "optimism")]
pub fn compute_pending_block(mut self, compute_pending_block: bool) -> Self {
self.compute_pending_block = compute_pending_block;
self
}
}
impl Default for BasicPayloadJobGeneratorConfig {
@ -255,6 +274,8 @@ impl Default for BasicPayloadJobGeneratorConfig {
// 12s slot time
deadline: SLOT_DURATION,
max_payload_tasks: 3,
#[cfg(feature = "optimism")]
compute_pending_block: false,
}
}
}
@ -341,6 +362,7 @@ where
let result = builder.try_build(args);
let _ = tx.send(result);
}));
this.pending_block = Some(PendingPayload { _cancel, payload: rx });
}
}
@ -425,6 +447,39 @@ where
let _ = tx.send(res);
}));
// In Optimism, the PayloadAttributes can specify a `no_tx_pool` option that implies we
// should not pull transactions from the tx pool. In this case, we build the payload
// upfront with the list of transactions sent in the attributes without caring about
// the results of the polling job, if a best payload has not already been built.
#[cfg(feature = "optimism")]
if self.config.chain_spec.is_optimism() &&
self.config.attributes.optimism_payload_attributes.no_tx_pool
{
let args = BuildArguments {
client: self.client.clone(),
pool: self.pool.clone(),
cached_reads: self.cached_reads.take().unwrap_or_default(),
config: self.config.clone(),
cancel: Cancelled::default(),
best_payload: None,
};
if let Ok(BuildOutcome::Better { payload, cached_reads }) =
self.builder.try_build(args)
{
self.cached_reads = Some(cached_reads);
trace!(target: "payload_builder", "[OPTIMISM] Forced best payload");
let payload = Arc::new(payload);
return (
ResolveBestPayload {
best_payload: Some(payload),
maybe_better,
empty_payload,
},
KeepPayloadJobAlive::Yes,
)
}
}
empty_payload = Some(rx);
}
@ -539,6 +594,22 @@ pub struct PayloadConfig {
attributes: PayloadBuilderAttributes,
/// The chain spec.
chain_spec: Arc<ChainSpec>,
/// The rollup's compute pending block configuration option.
/// TODO(clabby): Implement this feature.
#[cfg(feature = "optimism")]
#[allow(dead_code)]
compute_pending_block: bool,
}
impl PayloadConfig {
/// Returns an owned instance of the [PayloadConfig]'s extra_data bytes.
pub(crate) fn extra_data(&self) -> reth_primitives::Bytes {
#[cfg(feature = "optimism")]
if self.chain_spec.is_optimism() {
return Default::default()
}
self.extra_data.clone()
}
}
impl PayloadConfig {
@ -548,6 +619,7 @@ impl PayloadConfig {
extra_data: Bytes,
attributes: PayloadBuilderAttributes,
chain_spec: Arc<ChainSpec>,
#[cfg(feature = "optimism")] compute_pending_block: bool,
) -> Self {
// configure evm env based on parent block
let (initialized_cfg, initialized_block_env) =
@ -560,6 +632,8 @@ impl PayloadConfig {
extra_data,
attributes,
chain_spec,
#[cfg(feature = "optimism")]
compute_pending_block,
}
}
}
@ -674,13 +748,14 @@ where
let state = StateProviderDatabase::new(&state_provider);
let mut db =
State::builder().with_database_ref(cached_reads.as_db(&state)).with_bundle_update().build();
let extra_data = config.extra_data();
let PayloadConfig {
initialized_block_env,
initialized_cfg,
parent_block,
extra_data,
attributes,
chain_spec,
..
} = config;
debug!(target: "payload_builder", parent_hash = ?parent_block.hash, parent_number = parent_block.number, "building new payload");
@ -799,6 +874,8 @@ where
success: result.is_success(),
cumulative_gas_used,
logs: result.logs().into_iter().map(into_reth_log).collect(),
#[cfg(feature = "optimism")]
deposit_nonce: None,
}));
// update add to total fees
@ -908,13 +985,14 @@ fn build_empty_payload<Client>(
where
Client: StateProviderFactory,
{
let extra_data = config.extra_data();
let PayloadConfig {
initialized_block_env,
parent_block,
extra_data,
attributes,
chain_spec,
initialized_cfg,
..
} = config;
debug!(target: "payload_builder", parent_hash = ?parent_block.hash, parent_number = parent_block.number, "building empty payload");
@ -968,9 +1046,9 @@ where
gas_limit: block_gas_limit,
difficulty: U256::ZERO,
gas_used: 0,
extra_data,
blob_gas_used: None,
excess_blob_gas: None,
extra_data,
parent_beacon_block_root: attributes.parent_beacon_block_root,
};

View File

@ -0,0 +1,305 @@
//! Optimism's [PayloadBuilder] implementation.
use super::*;
use reth_payload_builder::error::OptimismPayloadBuilderError;
use reth_primitives::Hardfork;
/// Constructs an Ethereum transaction payload from the transactions sent through the
/// Payload attributes by the sequencer. If the `no_tx_pool` argument is passed in
/// the payload attributes, the transaction pool will be ignored and the only transactions
/// included in the payload will be those sent through the attributes.
///
/// Given build arguments including an Ethereum client, transaction pool,
/// and configuration, this function creates a transaction payload. Returns
/// a result indicating success with the payload or an error in case of failure.
#[inline]
pub(crate) fn optimism_payload_builder<Pool, Client>(
args: BuildArguments<Pool, Client>,
) -> Result<BuildOutcome, PayloadBuilderError>
where
Client: StateProviderFactory,
Pool: TransactionPool,
{
let BuildArguments { client, pool, mut cached_reads, config, cancel, best_payload } = args;
let state_provider = client.state_by_block_hash(config.parent_block.hash)?;
let state = StateProviderDatabase::new(&state_provider);
let mut db =
State::builder().with_database_ref(cached_reads.as_db(&state)).with_bundle_update().build();
let extra_data = config.extra_data();
let PayloadConfig {
initialized_block_env,
initialized_cfg,
parent_block,
attributes,
chain_spec,
..
} = config;
debug!(target: "payload_builder", parent_hash = ?parent_block.hash, parent_number = parent_block.number, "building new payload");
let mut cumulative_gas_used = 0;
let block_gas_limit: u64 = attributes
.optimism_payload_attributes
.gas_limit
.unwrap_or(initialized_block_env.gas_limit.try_into().unwrap_or(u64::MAX));
let base_fee = initialized_block_env.basefee.to::<u64>();
let mut executed_txs = Vec::new();
let mut best_txs = pool.best_transactions_with_base_fee(base_fee);
let mut total_fees = U256::ZERO;
let block_number = initialized_block_env.number.to::<u64>();
let is_regolith =
chain_spec.is_fork_active_at_timestamp(Hardfork::Regolith, attributes.timestamp);
let mut receipts = Vec::new();
for sequencer_tx in attributes.optimism_payload_attributes.transactions {
// Check if the job was cancelled, if so we can exit early.
if cancel.is_cancelled() {
return Ok(BuildOutcome::Cancelled)
}
// Convert the transaction to a [TransactionSignedEcRecovered]. This is
// purely for the purposes of utilizing the [tx_env_with_recovered] function.
// Deposit transactions do not have signatures, so if the tx is a deposit, this
// will just pull in its `from` address.
let sequencer_tx = sequencer_tx.clone().try_into_ecrecovered().map_err(|_| {
PayloadBuilderError::Optimism(OptimismPayloadBuilderError::TransactionEcRecoverFailed)
})?;
// Cache the depositor account prior to the state transition for the deposit nonce.
//
// Note that this *only* needs to be done post-regolith hardfork, as deposit nonces
// were not introduced in Bedrock. In addition, regular transactions don't have deposit
// nonces, so we don't need to touch the DB for those.
let depositor = (is_regolith && sequencer_tx.is_deposit())
.then(|| {
db.load_cache_account(sequencer_tx.signer())
.map(|acc| acc.account_info().unwrap_or_default())
})
.transpose()
.map_err(|_| {
PayloadBuilderError::Optimism(OptimismPayloadBuilderError::AccountLoadFailed(
sequencer_tx.signer(),
))
})?;
// Configure the environment for the block.
let env = Env {
cfg: initialized_cfg.clone(),
block: initialized_block_env.clone(),
tx: tx_env_with_recovered(&sequencer_tx),
};
let mut evm = revm::EVM::with_env(env);
evm.database(&mut db);
let ResultAndState { result, state } = match evm.transact() {
Ok(res) => res,
Err(err) => {
match err {
EVMError::Transaction(err) => {
trace!(target: "optimism_payload_builder", ?err, ?sequencer_tx, "Error in sequencer transaction, skipping.");
continue
}
err => {
// this is an error that we should treat as fatal for this attempt
return Err(PayloadBuilderError::EvmExecutionError(err))
}
}
}
};
// commit changes
db.commit(state);
let gas_used = result.gas_used();
// add gas used by the transaction to cumulative gas used, before creating the receipt
cumulative_gas_used += gas_used;
// Push transaction changeset and calculate header bloom filter for receipt.
receipts.push(Some(Receipt {
tx_type: sequencer_tx.tx_type(),
success: result.is_success(),
cumulative_gas_used,
logs: result.logs().into_iter().map(into_reth_log).collect(),
#[cfg(feature = "optimism")]
deposit_nonce: depositor.map(|account| account.nonce),
}));
// append transaction to the list of executed transactions
executed_txs.push(sequencer_tx.into_signed());
}
if !attributes.optimism_payload_attributes.no_tx_pool {
while let Some(pool_tx) = best_txs.next() {
// ensure we still have capacity for this transaction
if cumulative_gas_used + pool_tx.gas_limit() > block_gas_limit {
// we can't fit this transaction into the block, so we need to mark it as invalid
// which also removes all dependent transaction from the iterator before we can
// continue
best_txs.mark_invalid(&pool_tx);
continue
}
// check if the job was cancelled, if so we can exit early
if cancel.is_cancelled() {
return Ok(BuildOutcome::Cancelled)
}
// convert tx to a signed transaction
let tx = pool_tx.to_recovered_transaction();
// Configure the environment for the block.
let env = Env {
cfg: initialized_cfg.clone(),
block: initialized_block_env.clone(),
tx: tx_env_with_recovered(&tx),
};
let mut evm = revm::EVM::with_env(env);
evm.database(&mut db);
let ResultAndState { result, state } = match evm.transact() {
Ok(res) => res,
Err(err) => {
match err {
EVMError::Transaction(err) => {
if matches!(err, InvalidTransaction::NonceTooLow { .. }) {
// if the nonce is too low, we can skip this transaction
trace!(target: "payload_builder", ?err, ?tx, "skipping nonce too low transaction");
} else {
// if the transaction is invalid, we can skip it and all of its
// descendants
trace!(target: "payload_builder", ?err, ?tx, "skipping invalid transaction and its descendants");
best_txs.mark_invalid(&pool_tx);
}
continue
}
err => {
// this is an error that we should treat as fatal for this attempt
return Err(PayloadBuilderError::EvmExecutionError(err))
}
}
}
};
// commit changes
db.commit(state);
let gas_used = result.gas_used();
// add gas used by the transaction to cumulative gas used, before creating the receipt
cumulative_gas_used += gas_used;
// Push transaction changeset and calculate header bloom filter for receipt.
receipts.push(Some(Receipt {
tx_type: tx.tx_type(),
success: result.is_success(),
cumulative_gas_used,
logs: result.logs().into_iter().map(into_reth_log).collect(),
#[cfg(feature = "optimism")]
deposit_nonce: None,
}));
// update add to total fees
let miner_fee = tx
.effective_tip_per_gas(Some(base_fee))
.expect("fee is always valid; execution succeeded");
total_fees += U256::from(miner_fee) * U256::from(gas_used);
// append transaction to the list of executed transactions
executed_txs.push(tx.into_signed());
}
}
// check if we have a better block
if !is_better_payload(best_payload.as_deref(), total_fees) {
// can skip building the block
return Ok(BuildOutcome::Aborted { fees: total_fees, cached_reads })
}
let WithdrawalsOutcome { withdrawals_root, withdrawals } =
commit_withdrawals(&mut db, &chain_spec, attributes.timestamp, attributes.withdrawals)?;
// merge all transitions into bundle state, this would apply the withdrawal balance changes
// and 4788 contract call
db.merge_transitions(BundleRetention::PlainState);
let bundle = BundleStateWithReceipts::new(
db.take_bundle(),
Receipts::from_vec(vec![receipts]),
block_number,
);
let receipts_root = bundle.receipts_root_slow(block_number).expect("Number is in range");
let logs_bloom = bundle.block_logs_bloom(block_number).expect("Number is in range");
// calculate the state root
let state_root = state_provider.state_root(&bundle)?;
// create the block header
let transactions_root = proofs::calculate_transaction_root(&executed_txs);
// Cancun is not yet active on Optimism chains.
let blob_sidecars = Vec::new();
let excess_blob_gas = None;
let blob_gas_used = None;
let header = Header {
parent_hash: parent_block.hash,
ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: initialized_block_env.coinbase,
state_root,
transactions_root,
receipts_root,
withdrawals_root,
logs_bloom,
timestamp: attributes.timestamp,
mix_hash: attributes.prev_randao,
nonce: BEACON_NONCE,
base_fee_per_gas: Some(base_fee),
number: parent_block.number + 1,
gas_limit: block_gas_limit,
difficulty: U256::ZERO,
gas_used: cumulative_gas_used,
extra_data,
parent_beacon_block_root: attributes.parent_beacon_block_root,
blob_gas_used,
excess_blob_gas,
};
// seal the block
let block = Block { header, body: executed_txs, ommers: vec![], withdrawals };
let sealed_block = block.seal_slow();
debug!(target: "payload_builder", ?sealed_block, "sealed built block");
let mut payload = BuiltPayload::new(attributes.id, sealed_block, total_fees);
// extend the payload with the blob sidecars from the executed txs
payload.extend_sidecars(blob_sidecars);
Ok(BuildOutcome::Better { payload, cached_reads })
}
/// Optimism's payload builder
#[derive(Debug, Clone)]
pub struct OptimismPayloadBuilder;
/// Implementation of the [PayloadBuilder] trait for [OptimismPayloadBuilder].
impl<Pool, Client> PayloadBuilder<Pool, Client> for OptimismPayloadBuilder
where
Client: StateProviderFactory,
Pool: TransactionPool,
{
fn try_build(
&self,
args: BuildArguments<Pool, Client>,
) -> Result<BuildOutcome, PayloadBuilderError> {
optimism_payload_builder(args)
}
}

View File

@ -39,3 +39,8 @@ revm.workspace = true
[features]
test-utils = []
optimism = [
"reth-primitives/optimism",
"reth-rpc-types/optimism",
"reth-interfaces/optimism"
]

View File

@ -27,6 +27,29 @@ pub enum PayloadBuilderError {
/// Thrown if the payload requests withdrawals before Shanghai activation.
#[error("withdrawals set before Shanghai activation")]
WithdrawalsBeforeShanghai,
/// Optimism specific payload building errors.
#[cfg(feature = "optimism")]
#[error(transparent)]
Optimism(#[from] OptimismPayloadBuilderError),
}
/// Optimism specific payload building errors.
#[derive(Debug, thiserror::Error)]
pub enum OptimismPayloadBuilderError {
/// Thrown when a transaction fails to convert to a
/// [reth_primitives::TransactionSignedEcRecovered].
#[cfg(feature = "optimism")]
#[error("failed to convert deposit transaction to TransactionSignedEcRecovered")]
TransactionEcRecoverFailed,
/// Thrown when the L1 block info could not be parsed from the calldata of the
/// first transaction supplied in the payload attributes.
#[cfg(feature = "optimism")]
#[error("failed to parse L1 block info from L1 info tx calldata")]
L1BlockInfoParseFailed,
/// Thrown when a database account could not be loaded.
#[error("failed to load account {0:?}")]
#[cfg(feature = "optimism")]
AccountLoadFailed(revm_primitives::Address),
}
impl From<oneshot::error::RecvError> for PayloadBuilderError {

View File

@ -1,6 +1,6 @@
//! Contains types required for building a payload.
use alloy_rlp::Encodable;
use alloy_rlp::{Encodable, Error as DecodeError};
use reth_primitives::{
revm::config::revm_spec_by_timestamp_after_merge, Address, BlobTransactionSidecar, ChainSpec,
Header, SealedBlock, Withdrawal, B256, U256,
@ -9,11 +9,16 @@ use reth_rpc_types::engine::{
ExecutionPayloadEnvelopeV2, ExecutionPayloadEnvelopeV3, ExecutionPayloadV1, PayloadAttributes,
PayloadId,
};
use reth_rpc_types_compat::engine::payload::{
block_to_payload_v3, convert_block_to_payload_field_v2,
convert_standalone_withdraw_to_withdrawal, from_primitive_sidecar, try_block_to_payload_v1,
};
use revm_primitives::{BlobExcessGasAndPrice, BlockEnv, CfgEnv, SpecId};
#[cfg(feature = "optimism")]
use reth_primitives::TransactionSigned;
/// Contains the built payload.
///
/// According to the [engine API specification](https://github.com/ethereum/execution-apis/blob/main/src/engine/README.md) the execution layer should build the initial version of the payload with an empty transaction set and then keep update it in order to maximize the revenue.
@ -137,6 +142,21 @@ pub struct PayloadBuilderAttributes {
pub withdrawals: Vec<Withdrawal>,
/// Root of the parent beacon block
pub parent_beacon_block_root: Option<B256>,
/// Optimism Payload Builder Attributes
#[cfg(feature = "optimism")]
pub optimism_payload_attributes: OptimismPayloadBuilderAttributes,
}
/// Optimism Payload Builder Attributes
#[cfg(feature = "optimism")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OptimismPayloadBuilderAttributes {
/// NoTxPool option for the generated payload
pub no_tx_pool: bool,
/// Transactions for the generated payload
pub transactions: Vec<TransactionSigned>,
/// The gas limit for the generated payload
pub gas_limit: Option<u64>,
}
// === impl PayloadBuilderAttributes ===
@ -145,9 +165,23 @@ impl PayloadBuilderAttributes {
/// Creates a new payload builder for the given parent block and the attributes.
///
/// Derives the unique [PayloadId] for the given parent and attributes
pub fn new(parent: B256, attributes: PayloadAttributes) -> Self {
pub fn try_new(parent: B256, attributes: PayloadAttributes) -> Result<Self, DecodeError> {
#[cfg(not(feature = "optimism"))]
let id = payload_id(&parent, &attributes);
#[cfg(feature = "optimism")]
let (id, transactions) = {
let transactions = attributes
.optimism_payload_attributes
.transactions
.as_deref()
.unwrap_or(&[])
.iter()
.map(|tx| TransactionSigned::decode_enveloped(tx.clone()))
.collect::<Result<_, _>>()?;
(payload_id(&parent, &attributes, &transactions), transactions)
};
let withdraw = attributes.withdrawals.map(
|withdrawals: Vec<reth_rpc_types::engine::payload::Withdrawal>| {
withdrawals
@ -157,7 +191,7 @@ impl PayloadBuilderAttributes {
},
);
Self {
Ok(Self {
id,
parent,
timestamp: attributes.timestamp.to(),
@ -165,9 +199,14 @@ impl PayloadBuilderAttributes {
prev_randao: attributes.prev_randao,
withdrawals: withdraw.unwrap_or_default(),
parent_beacon_block_root: attributes.parent_beacon_block_root,
}
#[cfg(feature = "optimism")]
optimism_payload_attributes: OptimismPayloadBuilderAttributes {
no_tx_pool: attributes.optimism_payload_attributes.no_tx_pool.unwrap_or_default(),
transactions,
gas_limit: attributes.optimism_payload_attributes.gas_limit,
},
})
}
/// Returns the configured [CfgEnv] and [BlockEnv] for the targeted payload (that has the
/// `parent` as its parent).
///
@ -182,6 +221,11 @@ impl PayloadBuilderAttributes {
let mut cfg = CfgEnv::default();
cfg.chain_id = chain_spec.chain().id();
#[cfg(feature = "optimism")]
{
cfg.optimism = chain_spec.is_optimism();
}
// ensure we're not missing any timestamp based hardforks
cfg.spec_id = revm_spec_by_timestamp_after_merge(chain_spec, self.timestamp);
@ -229,7 +273,11 @@ impl PayloadBuilderAttributes {
/// Generates the payload id for the configured payload
///
/// Returns an 8-byte identifier by hashing the payload components with sha256 hash.
pub(crate) fn payload_id(parent: &B256, attributes: &PayloadAttributes) -> PayloadId {
pub(crate) fn payload_id(
parent: &B256,
attributes: &PayloadAttributes,
#[cfg(feature = "optimism")] txs: &Vec<TransactionSigned>,
) -> PayloadId {
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(parent.as_slice());
@ -241,9 +289,25 @@ pub(crate) fn payload_id(parent: &B256, attributes: &PayloadAttributes) -> Paylo
withdrawals.encode(&mut buf);
hasher.update(buf);
}
if let Some(parent_beacon_block) = attributes.parent_beacon_block_root {
hasher.update(parent_beacon_block);
}
#[cfg(feature = "optimism")]
{
let no_tx_pool = attributes.optimism_payload_attributes.no_tx_pool.unwrap_or_default();
if no_tx_pool || !txs.is_empty() {
hasher.update([no_tx_pool as u8]);
hasher.update(txs.len().to_be_bytes());
txs.iter().for_each(|tx| hasher.update(tx.hash()));
}
if let Some(gas_limit) = attributes.optimism_payload_attributes.gas_limit {
hasher.update(gas_limit.to_be_bytes());
}
}
let out = hasher.finalize();
PayloadId::new(out.as_slice()[..8].try_into().expect("sufficient length"))
}