Skip to content

Commit

Permalink
refactor: change PayloadConfig to use parent header instead of pare…
Browse files Browse the repository at this point in the history
…nt block (paradigmxyz#12159)
  • Loading branch information
lean-apple authored Oct 29, 2024
1 parent b48fa68 commit 7880d4d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 39 deletions.
5 changes: 3 additions & 2 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use reth_node_ethereum::{EthEvmConfig, EthExecutorProvider};
use reth_payload_builder::database::CachedReads;
use reth_primitives::{
revm_primitives::KzgSettings, BlobTransaction, BlobTransactionSidecar,
PooledTransactionsElement, SealedBlock, SealedBlockWithSenders, Transaction, TransactionSigned,
PooledTransactionsElement, SealedBlock, SealedBlockWithSenders, SealedHeader, Transaction,
TransactionSigned,
};
use reth_provider::{
providers::BlockchainProvider, BlockHashReader, BlockReader, BlockWriter, ChainSpecProvider,
Expand Down Expand Up @@ -224,7 +225,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
withdrawals: None,
};
let payload_config = PayloadConfig::new(
Arc::clone(&best_block),
Arc::new(SealedHeader::new(best_block.header().clone(), best_block.hash())),
Bytes::default(),
reth_payload_builder::EthPayloadBuilderAttributes::try_new(
best_block.hash(),
Expand Down
28 changes: 14 additions & 14 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
&self,
args: BuildArguments<Pool, Client, EthPayloadBuilderAttributes, EthBuiltPayload>,
) -> 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();
default_ethereum_payload(self.evm_config.clone(), args, cfg_env, block_env, |attributes| {
Expand All @@ -120,7 +120,7 @@ where
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();

Expand Down Expand Up @@ -154,13 +154,13 @@ where
let BuildArguments { client, pool, mut cached_reads, config, cancel, best_payload } = args;

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 mut db =
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 sum_blob_gas_used = 0;
let block_gas_limit: u64 = initialized_block_env.gas_limit.to::<u64>();
Expand Down Expand Up @@ -189,7 +189,7 @@ where
)
.map_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
parent_hash=%parent_header.hash(),
%err,
"failed to apply beacon root contract call for payload"
);
Expand All @@ -201,10 +201,10 @@ where
&mut db,
&initialized_cfg,
&initialized_block_env,
parent_block.hash(),
parent_header.hash(),
)
.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())
})?;

Expand Down Expand Up @@ -371,7 +371,7 @@ where
let state_provider = db.database.0.inner.borrow_mut();
state_provider.db.state_root_with_updates(hashed_state.clone()).inspect_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
parent_hash=%parent_header.hash(),
%err,
"failed to calculate state root for payload"
);
Expand All @@ -393,9 +393,9 @@ where
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) {
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();
excess_blob_gas = if chain_spec.is_cancun_active_at_timestamp(parent_header.timestamp) {
let parent_excess_blob_gas = parent_header.excess_blob_gas.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))
} else {
// for the first post-fork block, both parent.blob_gas_used and
Expand All @@ -407,7 +407,7 @@ where
}

let header = Header {
parent_hash: parent_block.hash(),
parent_hash: parent_header.hash(),
ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: initialized_block_env.coinbase,
state_root,
Expand All @@ -419,7 +419,7 @@ where
mix_hash: attributes.prev_randao,
nonce: BEACON_NONCE.into(),
base_fee_per_gas: Some(base_fee),
number: parent_block.number + 1,
number: parent_header.number + 1,
gas_limit: block_gas_limit,
difficulty: U256::ZERO,
gas_used: cumulative_gas_used,
Expand Down
18 changes: 9 additions & 9 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ where
&self,
args: BuildArguments<Pool, Client, OptimismPayloadBuilderAttributes, OptimismBuiltPayload>,
) -> 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)
}

Expand Down Expand Up @@ -132,7 +132,7 @@ where
cancel: Default::default(),
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)?
.into_payload()
.ok_or_else(|| PayloadBuilderError::MissingPayload)
Expand Down Expand Up @@ -163,13 +163,13 @@ where
let BuildArguments { client, pool, mut cached_reads, config, cancel, best_payload } = args;

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 mut db =
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 block_gas_limit: u64 = attributes.gas_limit.unwrap_or_else(|| {
Expand Down Expand Up @@ -206,7 +206,7 @@ where
)
.map_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
parent_header=%parent_header.hash(),
%err,
"failed to apply beacon root contract call for payload"
);
Expand Down Expand Up @@ -449,7 +449,7 @@ where
let state_provider = db.database.0.inner.borrow_mut();
state_provider.db.state_root_with_updates(hashed_state.clone()).inspect_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
parent_header=%parent_header.hash(),
%err,
"failed to calculate state root for payload"
);
Expand All @@ -470,7 +470,7 @@ where
};

let header = Header {
parent_hash: parent_block.hash(),
parent_hash: parent_header.hash(),
ommers_hash: EMPTY_OMMER_ROOT_HASH,
beneficiary: initialized_block_env.coinbase,
state_root,
Expand All @@ -482,7 +482,7 @@ where
mix_hash: attributes.payload_attributes.prev_randao,
nonce: BEACON_NONCE.into(),
base_fee_per_gas: Some(base_fee),
number: parent_block.number + 1,
number: parent_header.number + 1,
gas_limit: block_gas_limit,
difficulty: U256::ZERO,
gas_used: cumulative_gas_used,
Expand Down
20 changes: 12 additions & 8 deletions crates/payload/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use reth_payload_primitives::{
BuiltPayload, PayloadBuilderAttributes, PayloadBuilderError, PayloadKind,
};
use reth_primitives::{
constants::RETH_CLIENT_VERSION, proofs, BlockNumberOrTag, SealedBlock, Withdrawals,
constants::RETH_CLIENT_VERSION, proofs, BlockNumberOrTag, SealedHeader, Withdrawals,
};
use reth_provider::{
BlockReaderIdExt, BlockSource, CanonStateNotification, ProviderError, StateProviderFactory,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<Client, Pool, Tasks, Builder> BasicPayloadJobGenerator<Client, Pool, Tasks,
&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.
fn maybe_pre_cached(&self, parent: B256) -> Option<CachedReads> {
self.pre_cached.as_ref().filter(|pc| pc.block == parent).map(|pc| pc.cached.clone())
Expand Down Expand Up @@ -163,13 +163,17 @@ where
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 =
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 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 {
config,
Expand Down Expand Up @@ -706,8 +710,8 @@ impl Drop for Cancelled {
/// Static config for how to build a payload.
#[derive(Clone, Debug)]
pub struct PayloadConfig<Attributes> {
/// The parent block.
pub parent_block: Arc<SealedBlock>,
/// The parent header.
pub parent_header: Arc<SealedHeader>,
/// Block extra data.
pub extra_data: Bytes,
/// Requested attributes for the payload.
Expand All @@ -727,11 +731,11 @@ where
{
/// Create new payload config.
pub const fn new(
parent_block: Arc<SealedBlock>,
parent_header: Arc<SealedHeader>,
extra_data: Bytes,
attributes: Attributes,
) -> Self {
Self { parent_block, extra_data, attributes }
Self { parent_header, extra_data, attributes }
}

/// Returns the payload id.
Expand Down
8 changes: 4 additions & 4 deletions examples/custom-engine-types/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ where
args: BuildArguments<Pool, Client, Self::Attributes, Self::BuiltPayload>,
) -> Result<BuildOutcome<Self::BuiltPayload>, PayloadBuilderError> {
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();

Expand All @@ -354,7 +354,7 @@ where
client,
pool,
cached_reads,
config: PayloadConfig { parent_block, extra_data, attributes: attributes.0 },
config: PayloadConfig { parent_header, extra_data, attributes: attributes.0 },
cancel,
best_payload,
})
Expand All @@ -365,10 +365,10 @@ where
client: &Client,
config: PayloadConfig<Self::Attributes>,
) -> 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();
<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})
}
}

Expand Down
7 changes: 5 additions & 2 deletions examples/custom-payload-builder/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use reth::{
use reth_basic_payload_builder::{BasicPayloadJobGeneratorConfig, PayloadBuilder, PayloadConfig};
use reth_node_api::PayloadBuilderAttributes;
use reth_payload_builder::{PayloadBuilderError, PayloadJobGenerator};
use reth_primitives::BlockNumberOrTag;
use reth_primitives::{BlockNumberOrTag, SealedHeader};
use std::sync::Arc;

/// The generator type that creates new jobs that builds empty blocks.
Expand Down Expand Up @@ -77,7 +77,10 @@ where
// we already know the hash, so we can seal it
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 {
client: self.client.clone(),
_pool: self.pool.clone(),
Expand Down

0 comments on commit 7880d4d

Please sign in to comment.