Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: integrate executed block into OP built payload #10162

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/optimism/payload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ reth-execution-types.workspace = true
reth-payload-builder.workspace = true
reth-payload-primitives.workspace = true
reth-basic-payload-builder.workspace = true
reth-trie.workspace = true
reth-chain-state.workspace = true

# ethereum
revm.workspace = true
Expand Down
34 changes: 30 additions & 4 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
payload::{OptimismBuiltPayload, OptimismPayloadBuilderAttributes},
};
use reth_basic_payload_builder::*;
use reth_chain_state::ExecutedBlock;
use reth_chainspec::{EthereumHardforks, OptimismHardfork};
use reth_evm::{system_calls::pre_block_beacon_root_contract_call, ConfigureEvm};
use reth_execution_types::ExecutionOutcome;
Expand All @@ -17,11 +18,13 @@ use reth_primitives::{
use reth_provider::StateProviderFactory;
use reth_revm::database::StateProviderDatabase;
use reth_transaction_pool::{BestTransactionsAttributes, TransactionPool};
use reth_trie::HashedPostState;
use revm::{
db::states::bundle_state::BundleRetention,
primitives::{EVMError, EnvWithHandlerCfg, InvalidTransaction, ResultAndState},
DatabaseCommit, State,
};
use std::sync::Arc;
use tracing::{debug, trace, warn};

/// Optimism's payload builder
Expand Down Expand Up @@ -214,6 +217,7 @@ where
U256::ZERO,
chain_spec,
attributes,
None,
))
}
}
Expand Down Expand Up @@ -262,6 +266,7 @@ where
let base_fee = initialized_block_env.basefee.to::<u64>();

let mut executed_txs = Vec::with_capacity(attributes.transactions.len());
let mut executed_senders = Vec::with_capacity(attributes.transactions.len());

let mut best_txs = pool.best_transactions_with_attributes(BestTransactionsAttributes::new(
base_fee,
Expand Down Expand Up @@ -402,7 +407,8 @@ where
.then_some(1),
}));

// append transaction to the list of executed transactions
// append sender and transaction to the respective lists
executed_senders.push(sequencer_tx.signer());
executed_txs.push(sequencer_tx.into_signed());
}

Expand Down Expand Up @@ -490,7 +496,8 @@ where
.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
// append sender and transaction to the respective lists
executed_senders.push(tx.signer());
executed_txs.push(tx.into_signed());
}
}
Expand Down Expand Up @@ -524,9 +531,18 @@ where
let logs_bloom = execution_outcome.block_logs_bloom(block_number).expect("Number is in range");

// calculate the state root
let state_root = {
let hashed_state = HashedPostState::from_bundle_state(&execution_outcome.state().state);
let (state_root, trie_output) = {
let state_provider = db.database.0.inner.borrow_mut();
state_provider.db.state_root(execution_outcome.state())?
state_provider.db.hashed_state_root_with_updates(hashed_state.clone()).inspect_err(
|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
%err,
"failed to calculate state root for empty payload"
);
},
)?
};

// create the block header
Expand Down Expand Up @@ -582,12 +598,22 @@ where
let sealed_block = block.seal_slow();
debug!(target: "payload_builder", ?sealed_block, "sealed built block");

// create the executed block data
let executed = ExecutedBlock {
block: Arc::new(sealed_block.clone()),
senders: Arc::new(executed_senders),
execution_output: Arc::new(execution_outcome),
hashed_state: Arc::new(hashed_state),
trie: Arc::new(trie_output),
};

let mut payload = OptimismBuiltPayload::new(
attributes.payload_attributes.id,
sealed_block,
total_fees,
chain_spec,
attributes,
Some(executed),
);

// extend the payload with the blob sidecars from the executed txs
Expand Down
19 changes: 15 additions & 4 deletions crates/optimism/payload/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Optimism builder support

use alloy_rlp::Encodable;
use reth_chain_state::ExecutedBlock;
use reth_chainspec::{ChainSpec, EthereumHardforks};
use reth_evm_optimism::revm_spec_by_timestamp_after_bedrock;
use reth_payload_builder::EthPayloadBuilderAttributes;
Expand All @@ -13,6 +14,8 @@ use reth_primitives::{
Address, BlobTransactionSidecar, Header, SealedBlock, TransactionSigned, Withdrawals, B256,
U256,
};
/// Re-export for use in downstream arguments.
pub use reth_rpc_types::engine::OptimismPayloadAttributes;
use reth_rpc_types::engine::{
ExecutionPayloadEnvelopeV2, ExecutionPayloadV1, OptimismExecutionPayloadEnvelopeV3,
OptimismExecutionPayloadEnvelopeV4, PayloadId,
Expand All @@ -24,9 +27,6 @@ use reth_rpc_types_compat::engine::payload::{
use revm::primitives::HandlerCfg;
use std::sync::Arc;

/// Re-export for use in downstream arguments.
pub use reth_rpc_types::engine::OptimismPayloadAttributes;

/// Optimism Payload Builder Attributes
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OptimismPayloadBuilderAttributes {
Expand Down Expand Up @@ -168,6 +168,8 @@ pub struct OptimismBuiltPayload {
pub(crate) id: PayloadId,
/// The built block
pub(crate) block: SealedBlock,
/// Block execution data for the payload, if any.
pub(crate) executed_block: Option<ExecutedBlock>,
/// The fees of the block
pub(crate) fees: U256,
/// The blobs, proofs, and commitments in the block. If the block is pre-cancun, this will be
Expand All @@ -189,8 +191,9 @@ impl OptimismBuiltPayload {
fees: U256,
chain_spec: Arc<ChainSpec>,
attributes: OptimismPayloadBuilderAttributes,
executed_block: Option<ExecutedBlock>,
) -> Self {
Self { id, block, fees, sidecars: Vec::new(), chain_spec, attributes }
Self { id, block, executed_block, fees, sidecars: Vec::new(), chain_spec, attributes }
}

/// Returns the identifier of the payload.
Expand Down Expand Up @@ -222,6 +225,10 @@ impl BuiltPayload for OptimismBuiltPayload {
fn fees(&self) -> U256 {
self.fees
}

fn executed_block(&self) -> Option<ExecutedBlock> {
self.executed_block.clone()
}
}

impl<'a> BuiltPayload for &'a OptimismBuiltPayload {
Expand All @@ -232,6 +239,10 @@ impl<'a> BuiltPayload for &'a OptimismBuiltPayload {
fn fees(&self) -> U256 {
(**self).fees()
}

fn executed_block(&self) -> Option<ExecutedBlock> {
self.executed_block.clone()
}
}

// V1 engine_getPayloadV1 response
Expand Down
Loading