feat: integrate executed block into OP built payload (#10162)

Co-authored-by: Federico Gimenez <fgimenez@users.noreply.github.com>
This commit is contained in:
Matthias Seitz
2024-08-07 16:13:50 +02:00
committed by GitHub
parent 7ec3b92d36
commit 0fb72762de
4 changed files with 49 additions and 8 deletions

View File

@ -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

View File

@ -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;
@ -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
@ -214,6 +217,7 @@ where
U256::ZERO,
chain_spec,
attributes,
None,
))
}
}
@ -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,
@ -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());
}
@ -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());
}
}
@ -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
@ -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

View File

@ -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;
@ -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,
@ -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 {
@ -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
@ -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.
@ -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 {
@ -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