Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Start integrating op-revm into the payload builder + executor
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Oct 15, 2023
1 parent c2ea10f commit e173828
Show file tree
Hide file tree
Showing 9 changed files with 568 additions and 30 deletions.
4 changes: 2 additions & 2 deletions bin/reth/src/cli/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ pub trait RethNodeCommandConfig: fmt::Debug {
.extradata(conf.extradata_rlp_bytes())
.max_gas_limit(conf.max_gas_limit());

// TODO(clabby): Re-add conf option
#[cfg(feature = "optimism")]
let payload_job_config =
payload_job_config.compute_pending_block(conf.compute_pending_block());
let payload_job_config = payload_job_config.compute_pending_block(false);

// The default payload builder is implemented on the unit type.
#[cfg(not(feature = "optimism"))]
Expand Down
290 changes: 288 additions & 2 deletions crates/payload/basic/src/optimism.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Optimism's [PayloadBuilder] implementation.
use super::*;
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
Expand All @@ -12,13 +13,298 @@ use super::*;
/// 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>,
args: BuildArguments<Pool, Client>,
) -> Result<BuildOutcome, PayloadBuilderError>
where
Client: StateProviderFactory,
Pool: TransactionPool,
{
unimplemented!()
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 = 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.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::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(|| {
Ok::<_, PayloadBuilderError>(
db.load_cache_account(sequencer_tx.signer())
.map_err(|_| PayloadBuilderError::AccountLoadFailed(sequencer_tx.signer()))?
.clone()
.account
.ok_or(PayloadBuilderError::AccountLoadFailed(sequencer_tx.signer()))?,
)
})
.transpose()?;

// 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) => {
if sequencer_tx.is_deposit() {
if is_regolith || !sequencer_tx.is_system_transaction() {
cumulative_gas_used += sequencer_tx.gas_limit();
}
receipts.push(Some(Receipt {
tx_type: sequencer_tx.tx_type(),
success: false,
cumulative_gas_used,
logs: vec![],
#[cfg(feature = "optimism")]
deposit_nonce: None,
}));
executed_txs.push(sequencer_tx.into_signed());
continue
}

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, ?sequencer_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, ?sequencer_tx, "skipping invalid transaction and its descendants");
}

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.info.nonce),
}));

// append transaction to the list of executed transactions
executed_txs.push(sequencer_tx.into_signed());
}

if !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(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,
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
Expand Down
9 changes: 9 additions & 0 deletions crates/primitives/src/chain/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,15 @@ impl ChainSpecBuilder {
)
}

/// Enable Optimism L2 modifications
///
/// Warning: By itself, this does not enable the earliest Optimism hardfork, Bedrock.
#[cfg(feature = "optimism")]
pub fn optimism_activated(mut self) -> Self {
self.optimism = true;
self
}

/// Enable Frontier at genesis.
pub fn frontier_activated(mut self) -> Self {
self.hardforks.insert(Hardfork::Frontier, ForkCondition::Block(0));
Expand Down
Loading

0 comments on commit e173828

Please sign in to comment.