refactor: change PayloadConfig to use parent header instead of parent block (#12159)

This commit is contained in:
Léa Narzis
2024-10-29 16:52:00 +07:00
committed by GitHub
parent b48fa68f65
commit 7880d4ddb0
6 changed files with 47 additions and 39 deletions

View File

@ -29,7 +29,8 @@ use reth_node_ethereum::{EthEvmConfig, EthExecutorProvider};
use reth_payload_builder::database::CachedReads; use reth_payload_builder::database::CachedReads;
use reth_primitives::{ use reth_primitives::{
revm_primitives::KzgSettings, BlobTransaction, BlobTransactionSidecar, revm_primitives::KzgSettings, BlobTransaction, BlobTransactionSidecar,
PooledTransactionsElement, SealedBlock, SealedBlockWithSenders, Transaction, TransactionSigned, PooledTransactionsElement, SealedBlock, SealedBlockWithSenders, SealedHeader, Transaction,
TransactionSigned,
}; };
use reth_provider::{ use reth_provider::{
providers::BlockchainProvider, BlockHashReader, BlockReader, BlockWriter, ChainSpecProvider, providers::BlockchainProvider, BlockHashReader, BlockReader, BlockWriter, ChainSpecProvider,
@ -224,7 +225,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
withdrawals: None, withdrawals: None,
}; };
let payload_config = PayloadConfig::new( let payload_config = PayloadConfig::new(
Arc::clone(&best_block), Arc::new(SealedHeader::new(best_block.header().clone(), best_block.hash())),
Bytes::default(), Bytes::default(),
reth_payload_builder::EthPayloadBuilderAttributes::try_new( reth_payload_builder::EthPayloadBuilderAttributes::try_new(
best_block.hash(), best_block.hash(),

View File

@ -97,7 +97,7 @@ where
&self, &self,
args: BuildArguments<Pool, Client, EthPayloadBuilderAttributes, EthBuiltPayload>, args: BuildArguments<Pool, Client, EthPayloadBuilderAttributes, EthBuiltPayload>,
) -> Result<BuildOutcome<EthBuiltPayload>, PayloadBuilderError> { ) -> Result<BuildOutcome<EthBuiltPayload>, PayloadBuilderError> {
let (cfg_env, block_env) = self.cfg_and_block_env(&args.config, &args.config.parent_block); let (cfg_env, block_env) = self.cfg_and_block_env(&args.config, &args.config.parent_header);
let pool = args.pool.clone(); let pool = args.pool.clone();
default_ethereum_payload(self.evm_config.clone(), args, cfg_env, block_env, |attributes| { default_ethereum_payload(self.evm_config.clone(), args, cfg_env, block_env, |attributes| {
@ -120,7 +120,7 @@ where
None, None,
); );
let (cfg_env, block_env) = self.cfg_and_block_env(&args.config, &args.config.parent_block); let (cfg_env, block_env) = self.cfg_and_block_env(&args.config, &args.config.parent_header);
let pool = args.pool.clone(); let pool = args.pool.clone();
@ -154,13 +154,13 @@ where
let BuildArguments { client, pool, mut cached_reads, config, cancel, best_payload } = args; let BuildArguments { client, pool, mut cached_reads, config, cancel, best_payload } = args;
let chain_spec = client.chain_spec(); let chain_spec = client.chain_spec();
let state_provider = client.state_by_block_hash(config.parent_block.hash())?; let state_provider = client.state_by_block_hash(config.parent_header.hash())?;
let state = StateProviderDatabase::new(state_provider); let state = StateProviderDatabase::new(state_provider);
let mut db = let mut db =
State::builder().with_database_ref(cached_reads.as_db(state)).with_bundle_update().build(); State::builder().with_database_ref(cached_reads.as_db(state)).with_bundle_update().build();
let PayloadConfig { parent_block, extra_data, attributes } = config; let PayloadConfig { parent_header, extra_data, attributes } = config;
debug!(target: "payload_builder", id=%attributes.id, parent_hash = ?parent_block.hash(), parent_number = parent_block.number, "building new payload"); debug!(target: "payload_builder", id=%attributes.id, parent_header = ?parent_header.hash(), parent_number = parent_header.number, "building new payload");
let mut cumulative_gas_used = 0; let mut cumulative_gas_used = 0;
let mut sum_blob_gas_used = 0; let mut sum_blob_gas_used = 0;
let block_gas_limit: u64 = initialized_block_env.gas_limit.to::<u64>(); let block_gas_limit: u64 = initialized_block_env.gas_limit.to::<u64>();
@ -189,7 +189,7 @@ where
) )
.map_err(|err| { .map_err(|err| {
warn!(target: "payload_builder", warn!(target: "payload_builder",
parent_hash=%parent_block.hash(), parent_hash=%parent_header.hash(),
%err, %err,
"failed to apply beacon root contract call for payload" "failed to apply beacon root contract call for payload"
); );
@ -201,10 +201,10 @@ where
&mut db, &mut db,
&initialized_cfg, &initialized_cfg,
&initialized_block_env, &initialized_block_env,
parent_block.hash(), parent_header.hash(),
) )
.map_err(|err| { .map_err(|err| {
warn!(target: "payload_builder", parent_hash=%parent_block.hash(), %err, "failed to update blockhashes for payload"); warn!(target: "payload_builder", parent_hash=%parent_header.hash(), %err, "failed to update parent header blockhashes for payload");
PayloadBuilderError::Internal(err.into()) PayloadBuilderError::Internal(err.into())
})?; })?;
@ -371,7 +371,7 @@ where
let state_provider = db.database.0.inner.borrow_mut(); let state_provider = db.database.0.inner.borrow_mut();
state_provider.db.state_root_with_updates(hashed_state.clone()).inspect_err(|err| { state_provider.db.state_root_with_updates(hashed_state.clone()).inspect_err(|err| {
warn!(target: "payload_builder", warn!(target: "payload_builder",
parent_hash=%parent_block.hash(), parent_hash=%parent_header.hash(),
%err, %err,
"failed to calculate state root for payload" "failed to calculate state root for payload"
); );
@ -393,9 +393,9 @@ where
executed_txs.iter().filter(|tx| tx.is_eip4844()).map(|tx| tx.hash).collect(), executed_txs.iter().filter(|tx| tx.is_eip4844()).map(|tx| tx.hash).collect(),
)?; )?;
excess_blob_gas = if chain_spec.is_cancun_active_at_timestamp(parent_block.timestamp) { excess_blob_gas = if chain_spec.is_cancun_active_at_timestamp(parent_header.timestamp) {
let parent_excess_blob_gas = parent_block.excess_blob_gas.unwrap_or_default(); let parent_excess_blob_gas = parent_header.excess_blob_gas.unwrap_or_default();
let parent_blob_gas_used = parent_block.blob_gas_used.unwrap_or_default(); let parent_blob_gas_used = parent_header.blob_gas_used.unwrap_or_default();
Some(calc_excess_blob_gas(parent_excess_blob_gas, parent_blob_gas_used)) Some(calc_excess_blob_gas(parent_excess_blob_gas, parent_blob_gas_used))
} else { } else {
// for the first post-fork block, both parent.blob_gas_used and // for the first post-fork block, both parent.blob_gas_used and
@ -407,7 +407,7 @@ where
} }
let header = Header { let header = Header {
parent_hash: parent_block.hash(), parent_hash: parent_header.hash(),
ommers_hash: EMPTY_OMMER_ROOT_HASH, ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: initialized_block_env.coinbase, beneficiary: initialized_block_env.coinbase,
state_root, state_root,
@ -419,7 +419,7 @@ where
mix_hash: attributes.prev_randao, mix_hash: attributes.prev_randao,
nonce: BEACON_NONCE.into(), nonce: BEACON_NONCE.into(),
base_fee_per_gas: Some(base_fee), base_fee_per_gas: Some(base_fee),
number: parent_block.number + 1, number: parent_header.number + 1,
gas_limit: block_gas_limit, gas_limit: block_gas_limit,
difficulty: U256::ZERO, difficulty: U256::ZERO,
gas_used: cumulative_gas_used, gas_used: cumulative_gas_used,

View File

@ -103,7 +103,7 @@ where
&self, &self,
args: BuildArguments<Pool, Client, OptimismPayloadBuilderAttributes, OptimismBuiltPayload>, args: BuildArguments<Pool, Client, OptimismPayloadBuilderAttributes, OptimismBuiltPayload>,
) -> Result<BuildOutcome<OptimismBuiltPayload>, PayloadBuilderError> { ) -> Result<BuildOutcome<OptimismBuiltPayload>, PayloadBuilderError> {
let (cfg_env, block_env) = self.cfg_and_block_env(&args.config, &args.config.parent_block); let (cfg_env, block_env) = self.cfg_and_block_env(&args.config, &args.config.parent_header);
optimism_payload(&self.evm_config, args, cfg_env, block_env, self.compute_pending_block) optimism_payload(&self.evm_config, args, cfg_env, block_env, self.compute_pending_block)
} }
@ -132,7 +132,7 @@ where
cancel: Default::default(), cancel: Default::default(),
best_payload: None, best_payload: None,
}; };
let (cfg_env, block_env) = self.cfg_and_block_env(&args.config, &args.config.parent_block); let (cfg_env, block_env) = self.cfg_and_block_env(&args.config, &args.config.parent_header);
optimism_payload(&self.evm_config, args, cfg_env, block_env, false)? optimism_payload(&self.evm_config, args, cfg_env, block_env, false)?
.into_payload() .into_payload()
.ok_or_else(|| PayloadBuilderError::MissingPayload) .ok_or_else(|| PayloadBuilderError::MissingPayload)
@ -163,13 +163,13 @@ where
let BuildArguments { client, pool, mut cached_reads, config, cancel, best_payload } = args; let BuildArguments { client, pool, mut cached_reads, config, cancel, best_payload } = args;
let chain_spec = client.chain_spec(); let chain_spec = client.chain_spec();
let state_provider = client.state_by_block_hash(config.parent_block.hash())?; let state_provider = client.state_by_block_hash(config.parent_header.hash())?;
let state = StateProviderDatabase::new(state_provider); let state = StateProviderDatabase::new(state_provider);
let mut db = let mut db =
State::builder().with_database_ref(cached_reads.as_db(state)).with_bundle_update().build(); State::builder().with_database_ref(cached_reads.as_db(state)).with_bundle_update().build();
let PayloadConfig { parent_block, attributes, extra_data } = config; let PayloadConfig { parent_header, attributes, extra_data } = config;
debug!(target: "payload_builder", id=%attributes.payload_attributes.payload_id(), parent_hash = ?parent_block.hash(), parent_number = parent_block.number, "building new payload"); debug!(target: "payload_builder", id=%attributes.payload_attributes.payload_id(), parent_header = ?parent_header.hash(), parent_number = parent_header.number, "building new payload");
let mut cumulative_gas_used = 0; let mut cumulative_gas_used = 0;
let block_gas_limit: u64 = attributes.gas_limit.unwrap_or_else(|| { let block_gas_limit: u64 = attributes.gas_limit.unwrap_or_else(|| {
@ -206,7 +206,7 @@ where
) )
.map_err(|err| { .map_err(|err| {
warn!(target: "payload_builder", warn!(target: "payload_builder",
parent_hash=%parent_block.hash(), parent_header=%parent_header.hash(),
%err, %err,
"failed to apply beacon root contract call for payload" "failed to apply beacon root contract call for payload"
); );
@ -449,7 +449,7 @@ where
let state_provider = db.database.0.inner.borrow_mut(); let state_provider = db.database.0.inner.borrow_mut();
state_provider.db.state_root_with_updates(hashed_state.clone()).inspect_err(|err| { state_provider.db.state_root_with_updates(hashed_state.clone()).inspect_err(|err| {
warn!(target: "payload_builder", warn!(target: "payload_builder",
parent_hash=%parent_block.hash(), parent_header=%parent_header.hash(),
%err, %err,
"failed to calculate state root for payload" "failed to calculate state root for payload"
); );
@ -470,7 +470,7 @@ where
}; };
let header = Header { let header = Header {
parent_hash: parent_block.hash(), parent_hash: parent_header.hash(),
ommers_hash: EMPTY_OMMER_ROOT_HASH, ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: initialized_block_env.coinbase, beneficiary: initialized_block_env.coinbase,
state_root, state_root,
@ -482,7 +482,7 @@ where
mix_hash: attributes.payload_attributes.prev_randao, mix_hash: attributes.payload_attributes.prev_randao,
nonce: BEACON_NONCE.into(), nonce: BEACON_NONCE.into(),
base_fee_per_gas: Some(base_fee), base_fee_per_gas: Some(base_fee),
number: parent_block.number + 1, number: parent_header.number + 1,
gas_limit: block_gas_limit, gas_limit: block_gas_limit,
difficulty: U256::ZERO, difficulty: U256::ZERO,
gas_used: cumulative_gas_used, gas_used: cumulative_gas_used,

View File

@ -23,7 +23,7 @@ use reth_payload_primitives::{
BuiltPayload, PayloadBuilderAttributes, PayloadBuilderError, PayloadKind, BuiltPayload, PayloadBuilderAttributes, PayloadBuilderError, PayloadKind,
}; };
use reth_primitives::{ use reth_primitives::{
constants::RETH_CLIENT_VERSION, proofs, BlockNumberOrTag, SealedBlock, Withdrawals, constants::RETH_CLIENT_VERSION, proofs, BlockNumberOrTag, SealedHeader, Withdrawals,
}; };
use reth_provider::{ use reth_provider::{
BlockReaderIdExt, BlockSource, CanonStateNotification, ProviderError, StateProviderFactory, BlockReaderIdExt, BlockSource, CanonStateNotification, ProviderError, StateProviderFactory,
@ -122,7 +122,7 @@ impl<Client, Pool, Tasks, Builder> BasicPayloadJobGenerator<Client, Pool, Tasks,
&self.executor &self.executor
} }
/// Returns the pre-cached reads for the given parent block if it matches the cached state's /// Returns the pre-cached reads for the given parent header if it matches the cached state's
/// block. /// block.
fn maybe_pre_cached(&self, parent: B256) -> Option<CachedReads> { fn maybe_pre_cached(&self, parent: B256) -> Option<CachedReads> {
self.pre_cached.as_ref().filter(|pc| pc.block == parent).map(|pc| pc.cached.clone()) self.pre_cached.as_ref().filter(|pc| pc.block == parent).map(|pc| pc.cached.clone())
@ -163,13 +163,17 @@ where
block.seal(attributes.parent()) block.seal(attributes.parent())
}; };
let hash = parent_block.hash();
let parent_header = parent_block.header();
let header = SealedHeader::new(parent_header.clone(), hash);
let config = let config =
PayloadConfig::new(Arc::new(parent_block), self.config.extradata.clone(), attributes); PayloadConfig::new(Arc::new(header), self.config.extradata.clone(), attributes);
let until = self.job_deadline(config.attributes.timestamp()); let until = self.job_deadline(config.attributes.timestamp());
let deadline = Box::pin(tokio::time::sleep_until(until)); let deadline = Box::pin(tokio::time::sleep_until(until));
let cached_reads = self.maybe_pre_cached(config.parent_block.hash()); let cached_reads = self.maybe_pre_cached(hash);
let mut job = BasicPayloadJob { let mut job = BasicPayloadJob {
config, config,
@ -706,8 +710,8 @@ impl Drop for Cancelled {
/// Static config for how to build a payload. /// Static config for how to build a payload.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct PayloadConfig<Attributes> { pub struct PayloadConfig<Attributes> {
/// The parent block. /// The parent header.
pub parent_block: Arc<SealedBlock>, pub parent_header: Arc<SealedHeader>,
/// Block extra data. /// Block extra data.
pub extra_data: Bytes, pub extra_data: Bytes,
/// Requested attributes for the payload. /// Requested attributes for the payload.
@ -727,11 +731,11 @@ where
{ {
/// Create new payload config. /// Create new payload config.
pub const fn new( pub const fn new(
parent_block: Arc<SealedBlock>, parent_header: Arc<SealedHeader>,
extra_data: Bytes, extra_data: Bytes,
attributes: Attributes, attributes: Attributes,
) -> Self { ) -> Self {
Self { parent_block, extra_data, attributes } Self { parent_header, extra_data, attributes }
} }
/// Returns the payload id. /// Returns the payload id.

View File

@ -341,7 +341,7 @@ where
args: BuildArguments<Pool, Client, Self::Attributes, Self::BuiltPayload>, args: BuildArguments<Pool, Client, Self::Attributes, Self::BuiltPayload>,
) -> Result<BuildOutcome<Self::BuiltPayload>, PayloadBuilderError> { ) -> Result<BuildOutcome<Self::BuiltPayload>, PayloadBuilderError> {
let BuildArguments { client, pool, cached_reads, config, cancel, best_payload } = args; let BuildArguments { client, pool, cached_reads, config, cancel, best_payload } = args;
let PayloadConfig { parent_block, extra_data, attributes } = config; let PayloadConfig { parent_header, extra_data, attributes } = config;
let chain_spec = client.chain_spec(); let chain_spec = client.chain_spec();
@ -354,7 +354,7 @@ where
client, client,
pool, pool,
cached_reads, cached_reads,
config: PayloadConfig { parent_block, extra_data, attributes: attributes.0 }, config: PayloadConfig { parent_header, extra_data, attributes: attributes.0 },
cancel, cancel,
best_payload, best_payload,
}) })
@ -365,10 +365,10 @@ where
client: &Client, client: &Client,
config: PayloadConfig<Self::Attributes>, config: PayloadConfig<Self::Attributes>,
) -> Result<Self::BuiltPayload, PayloadBuilderError> { ) -> Result<Self::BuiltPayload, PayloadBuilderError> {
let PayloadConfig { parent_block, extra_data, attributes } = config; let PayloadConfig { parent_header, extra_data, attributes } = config;
let chain_spec = client.chain_spec(); let chain_spec = client.chain_spec();
<reth_ethereum_payload_builder::EthereumPayloadBuilder as PayloadBuilder<Pool, Client>>::build_empty_payload(&reth_ethereum_payload_builder::EthereumPayloadBuilder::new(EthEvmConfig::new(chain_spec.clone())),client, <reth_ethereum_payload_builder::EthereumPayloadBuilder as PayloadBuilder<Pool, Client>>::build_empty_payload(&reth_ethereum_payload_builder::EthereumPayloadBuilder::new(EthEvmConfig::new(chain_spec.clone())),client,
PayloadConfig { parent_block, extra_data, attributes: attributes.0}) PayloadConfig { parent_header, extra_data, attributes: attributes.0})
} }
} }

View File

@ -8,7 +8,7 @@ use reth::{
use reth_basic_payload_builder::{BasicPayloadJobGeneratorConfig, PayloadBuilder, PayloadConfig}; use reth_basic_payload_builder::{BasicPayloadJobGeneratorConfig, PayloadBuilder, PayloadConfig};
use reth_node_api::PayloadBuilderAttributes; use reth_node_api::PayloadBuilderAttributes;
use reth_payload_builder::{PayloadBuilderError, PayloadJobGenerator}; use reth_payload_builder::{PayloadBuilderError, PayloadJobGenerator};
use reth_primitives::BlockNumberOrTag; use reth_primitives::{BlockNumberOrTag, SealedHeader};
use std::sync::Arc; use std::sync::Arc;
/// The generator type that creates new jobs that builds empty blocks. /// The generator type that creates new jobs that builds empty blocks.
@ -77,7 +77,10 @@ where
// we already know the hash, so we can seal it // we already know the hash, so we can seal it
block.seal(attributes.parent()) block.seal(attributes.parent())
}; };
let config = PayloadConfig::new(Arc::new(parent_block), Bytes::default(), attributes); let hash = parent_block.hash();
let header = SealedHeader::new(parent_block.header().clone(), hash);
let config = PayloadConfig::new(Arc::new(header), Bytes::default(), attributes);
Ok(EmptyBlockPayloadJob { Ok(EmptyBlockPayloadJob {
client: self.client.clone(), client: self.client.clone(),
_pool: self.pool.clone(), _pool: self.pool.clone(),