chore: unify empty payload building (#10807)

This commit is contained in:
Matthias Seitz
2024-09-10 14:21:49 +02:00
committed by GitHub
parent 72c53b88bc
commit e4b5325875
4 changed files with 83 additions and 310 deletions

View File

@ -12,13 +12,14 @@ use reth_evm::{system_calls::pre_block_beacon_root_contract_call, ConfigureEvm};
use reth_execution_types::ExecutionOutcome;
use reth_payload_builder::error::PayloadBuilderError;
use reth_primitives::{
constants::{BEACON_NONCE, EMPTY_RECEIPTS, EMPTY_TRANSACTIONS},
eip4844::calculate_excess_blob_gas,
proofs, Block, Header, IntoRecoveredTransaction, Receipt, TxType, EMPTY_OMMER_ROOT_HASH,
constants::BEACON_NONCE, eip4844::calculate_excess_blob_gas, proofs, Block, Header,
IntoRecoveredTransaction, Receipt, TxType, EMPTY_OMMER_ROOT_HASH,
};
use reth_provider::StateProviderFactory;
use reth_revm::database::StateProviderDatabase;
use reth_transaction_pool::{BestTransactionsAttributes, TransactionPool};
use reth_transaction_pool::{
noop::NoopTransactionPool, BestTransactionsAttributes, TransactionPool,
};
use reth_trie::HashedPostState;
use revm::{
db::states::bundle_state::BundleRetention,
@ -75,7 +76,7 @@ where
&self,
args: BuildArguments<Pool, Client, OptimismPayloadBuilderAttributes, OptimismBuiltPayload>,
) -> Result<BuildOutcome<OptimismBuiltPayload>, PayloadBuilderError> {
optimism_payload_builder(self.evm_config.clone(), args, self.compute_pending_block)
optimism_payload(self.evm_config.clone(), args, self.compute_pending_block)
}
fn on_missing_payload(
@ -87,137 +88,25 @@ where
MissingPayloadBehaviour::AwaitInProgress
}
// NOTE: this should only be used for testing purposes because this doesn't have access to L1
// system txs, hence on_missing_payload we return [MissingPayloadBehaviour::AwaitInProgress].
fn build_empty_payload(
&self,
client: &Client,
config: PayloadConfig<Self::Attributes>,
) -> Result<OptimismBuiltPayload, PayloadBuilderError> {
let extra_data = config.extra_data();
let PayloadConfig {
initialized_block_env,
parent_block,
attributes,
chain_spec,
initialized_cfg,
..
} = config;
debug!(target: "payload_builder", parent_hash = ?parent_block.hash(), parent_number = parent_block.number, "building empty payload");
let state = client.state_by_block_hash(parent_block.hash()).map_err(|err| {
warn!(target: "payload_builder", parent_hash=%parent_block.hash(), %err, "failed to get state for empty payload");
err
})?;
let mut db = State::builder()
.with_database(StateProviderDatabase::new(state))
.with_bundle_update()
.build();
let base_fee = initialized_block_env.basefee.to::<u64>();
let block_gas_limit: u64 =
initialized_block_env.gas_limit.try_into().unwrap_or(chain_spec.max_gas_limit);
// apply eip-4788 pre block contract call
pre_block_beacon_root_contract_call(
&mut db,
&self.evm_config,
&chain_spec,
&initialized_cfg,
&initialized_block_env,
attributes.payload_attributes.parent_beacon_block_root,
)
.map_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
%err,
"failed to apply beacon root contract call for empty payload"
);
PayloadBuilderError::Internal(err.into())
})?;
let WithdrawalsOutcome { withdrawals_root, withdrawals } = commit_withdrawals(
&mut db,
&chain_spec,
attributes.payload_attributes.timestamp,
attributes.payload_attributes.withdrawals.clone(),
)
.map_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
%err,
"failed to commit withdrawals for empty payload"
);
err
})?;
// merge all transitions into bundle state, this would apply the withdrawal balance
// changes and 4788 contract call
db.merge_transitions(BundleRetention::PlainState);
// calculate the state root
let bundle_state = db.take_bundle();
let hashed_state = HashedPostState::from_bundle_state(&bundle_state.state);
let state_root = db.database.state_root(hashed_state).map_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
%err,
"failed to calculate state root for empty payload"
);
err
})?;
let mut excess_blob_gas = None;
let mut blob_gas_used = None;
if chain_spec.is_cancun_active_at_timestamp(attributes.payload_attributes.timestamp) {
excess_blob_gas = if chain_spec.is_cancun_active_at_timestamp(parent_block.timestamp) {
let parent_excess_blob_gas = parent_block.excess_blob_gas.unwrap_or_default();
let parent_blob_gas_used = parent_block.blob_gas_used.unwrap_or_default();
Some(calculate_excess_blob_gas(parent_excess_blob_gas, parent_blob_gas_used))
} else {
// for the first post-fork block, both parent.blob_gas_used and
// parent.excess_blob_gas are evaluated as 0
Some(calculate_excess_blob_gas(0, 0))
};
blob_gas_used = Some(0);
}
let header = Header {
parent_hash: parent_block.hash(),
ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: initialized_block_env.coinbase,
state_root,
transactions_root: EMPTY_TRANSACTIONS,
withdrawals_root,
receipts_root: EMPTY_RECEIPTS,
logs_bloom: Default::default(),
timestamp: attributes.payload_attributes.timestamp,
mix_hash: attributes.payload_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: 0,
extra_data,
blob_gas_used,
excess_blob_gas,
parent_beacon_block_root: attributes.payload_attributes.parent_beacon_block_root,
requests_root: None,
let args = BuildArguments {
client,
config,
// we use defaults here because for the empty payload we don't need to execute anything
pool: NoopTransactionPool::default(),
cached_reads: Default::default(),
cancel: Default::default(),
best_payload: None,
};
let block = Block { header, body: vec![], ommers: vec![], withdrawals, requests: None };
let sealed_block = block.seal_slow();
Ok(OptimismBuiltPayload::new(
attributes.payload_attributes.payload_id(),
sealed_block,
U256::ZERO,
chain_spec,
attributes,
None,
))
optimism_payload(self.evm_config.clone(), args, false)?
.into_payload()
.ok_or_else(|| PayloadBuilderError::MissingPayload)
}
}
@ -230,7 +119,7 @@ where
/// 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<EvmConfig, Pool, Client>(
pub(crate) fn optimism_payload<EvmConfig, Pool, Client>(
evm_config: EvmConfig,
args: BuildArguments<Pool, Client, OptimismPayloadBuilderAttributes, OptimismBuiltPayload>,
_compute_pending_block: bool,