Skip to content

Commit

Permalink
feat(builder): ethereum builder config (paradigmxyz#13315)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk authored Dec 11, 2024
1 parent 3d12a4e commit c553b1e
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 69 deletions.
3 changes: 2 additions & 1 deletion bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use reth_cli_commands::common::{AccessRights, CliNodeTypes, Environment, Environ
use reth_cli_runner::CliContext;
use reth_consensus::{Consensus, FullConsensus};
use reth_errors::RethResult;
use reth_ethereum_payload_builder::EthereumBuilderConfig;
use reth_evm::execute::{BlockExecutorProvider, Executor};
use reth_execution_types::ExecutionOutcome;
use reth_fs_util as fs;
Expand Down Expand Up @@ -228,7 +229,6 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
};
let payload_config = PayloadConfig::new(
Arc::new(SealedHeader::new(best_block.header().clone(), best_block.hash())),
Bytes::default(),
reth_payload_builder::EthPayloadBuilderAttributes::try_new(
best_block.hash(),
payload_attrs,
Expand All @@ -247,6 +247,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {

let payload_builder = reth_ethereum_payload_builder::EthereumPayloadBuilder::new(
EthEvmConfig::new(provider_factory.chain_spec()),
EthereumBuilderConfig::new(Default::default()),
);

match payload_builder.try_build(args)? {
Expand Down
2 changes: 2 additions & 0 deletions bin/reth/src/commands/debug_cmd/replay_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use reth_config::Config;
use reth_consensus::FullConsensus;
use reth_db::DatabaseEnv;
use reth_engine_util::engine_store::{EngineMessageStore, StoredEngineApiMessage};
use reth_ethereum_payload_builder::EthereumBuilderConfig;
use reth_fs_util as fs;
use reth_network::{BlockDownloaderProvider, NetworkHandle};
use reth_network_api::NetworkInfo;
Expand Down Expand Up @@ -123,6 +124,7 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
// Set up payload builder
let payload_builder = reth_ethereum_payload_builder::EthereumPayloadBuilder::new(
EthEvmConfig::new(provider_factory.chain_spec()),
EthereumBuilderConfig::new(Default::default()),
);

let payload_generator = BasicPayloadJobGenerator::with_builder(
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ reth-consensus.workspace = true
reth-beacon-consensus.workspace = true
reth-rpc.workspace = true
reth-node-api.workspace = true
reth-node-core.workspace = true
reth-chainspec.workspace = true
reth-primitives.workspace = true
reth-revm = { workspace = true, features = ["std"] }
Expand All @@ -43,7 +44,6 @@ reth-chainspec.workspace = true
reth-db.workspace = true
reth-exex.workspace = true
reth-node-api.workspace = true
reth-node-core.workspace = true
reth-payload-primitives.workspace = true
reth-e2e-test-utils.workspace = true
reth-rpc-eth-api.workspace = true
Expand Down
25 changes: 21 additions & 4 deletions crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use reth_chainspec::ChainSpec;
use reth_ethereum_engine_primitives::{
EthBuiltPayload, EthPayloadAttributes, EthPayloadBuilderAttributes,
};
use reth_ethereum_payload_builder::EthereumBuilderConfig;
use reth_evm::execute::BasicBlockExecutorProvider;
use reth_evm_ethereum::execute::EthExecutionStrategyFactory;
use reth_network::{EthNetworkPrimitives, NetworkHandle, PeersInfo};
Expand All @@ -23,6 +24,7 @@ use reth_node_builder::{
rpc::{EngineValidatorBuilder, RpcAddOns},
BuilderContext, Node, NodeAdapter, NodeComponentsBuilder, PayloadBuilderConfig, PayloadTypes,
};
use reth_node_core::version::default_extra_data_bytes;
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_primitives::{EthPrimitives, PooledTransactionsElement};
use reth_provider::{CanonStateSubscriptions, EthStorage};
Expand Down Expand Up @@ -228,9 +230,24 @@ where
}

/// A basic ethereum payload service.
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
pub struct EthereumPayloadBuilder;
#[derive(Clone, Debug)]
pub struct EthereumPayloadBuilder {
/// Payload builder configuration.
config: EthereumBuilderConfig,
}

impl Default for EthereumPayloadBuilder {
fn default() -> Self {
Self { config: EthereumBuilderConfig::new(default_extra_data_bytes()) }
}
}

impl EthereumPayloadBuilder {
/// Create new ethereum payload builder.
pub const fn new(config: EthereumBuilderConfig) -> Self {
Self { config }
}
}

impl EthereumPayloadBuilder {
/// A helper method initializing [`PayloadBuilderService`] with the given EVM config.
Expand All @@ -254,7 +271,7 @@ impl EthereumPayloadBuilder {
>,
{
let payload_builder =
reth_ethereum_payload_builder::EthereumPayloadBuilder::new(evm_config);
reth_ethereum_payload_builder::EthereumPayloadBuilder::new(evm_config, self.config);
let conf = ctx.payload_builder_config();

let payload_job_config = BasicPayloadJobGeneratorConfig::default()
Expand Down
22 changes: 22 additions & 0 deletions crates/ethereum/payload/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use alloy_primitives::Bytes;

/// Settings for the Ethereum builder.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct EthereumBuilderConfig {
/// Block extra data.
pub extra_data: Bytes,
}

impl EthereumBuilderConfig {
/// Create new payload builder config.
pub const fn new(extra_data: Bytes) -> Self {
Self { extra_data }
}
}

impl EthereumBuilderConfig {
/// Returns owned extra data bytes for the block.
pub fn extra_data(&self) -> Bytes {
self.extra_data.clone()
}
}
38 changes: 27 additions & 11 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,26 @@ use revm::{
use std::sync::Arc;
use tracing::{debug, trace, warn};

mod config;
pub use config::*;

type BestTransactionsIter<Pool> = Box<
dyn BestTransactions<Item = Arc<ValidPoolTransaction<<Pool as TransactionPool>::Transaction>>>,
>;

/// Ethereum payload builder
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EthereumPayloadBuilder<EvmConfig = EthEvmConfig> {
/// The type responsible for creating the evm.
evm_config: EvmConfig,
/// Payload builder configuration.
builder_config: EthereumBuilderConfig,
}

impl<EvmConfig> EthereumPayloadBuilder<EvmConfig> {
/// `EthereumPayloadBuilder` constructor.
pub const fn new(evm_config: EvmConfig) -> Self {
Self { evm_config }
pub const fn new(evm_config: EvmConfig, builder_config: EthereumBuilderConfig) -> Self {
Self { evm_config, builder_config }
}
}

Expand Down Expand Up @@ -107,9 +112,14 @@ where
.map_err(PayloadBuilderError::other)?;

let pool = args.pool.clone();
default_ethereum_payload(self.evm_config.clone(), args, cfg_env, block_env, |attributes| {
pool.best_transactions_with_attributes(attributes)
})
default_ethereum_payload(
self.evm_config.clone(),
self.builder_config.clone(),
args,
cfg_env,
block_env,
|attributes| pool.best_transactions_with_attributes(attributes),
)
}

fn build_empty_payload(
Expand All @@ -133,9 +143,14 @@ where

let pool = args.pool.clone();

default_ethereum_payload(self.evm_config.clone(), args, cfg_env, block_env, |attributes| {
pool.best_transactions_with_attributes(attributes)
})?
default_ethereum_payload(
self.evm_config.clone(),
self.builder_config.clone(),
args,
cfg_env,
block_env,
|attributes| pool.best_transactions_with_attributes(attributes),
)?
.into_payload()
.ok_or_else(|| PayloadBuilderError::MissingPayload)
}
Expand All @@ -149,6 +164,7 @@ where
#[inline]
pub fn default_ethereum_payload<EvmConfig, Pool, Client, F>(
evm_config: EvmConfig,
builder_config: EthereumBuilderConfig,
args: BuildArguments<Pool, Client, EthPayloadBuilderAttributes, EthBuiltPayload>,
initialized_cfg: CfgEnvWithHandlerCfg,
initialized_block_env: BlockEnv,
Expand All @@ -167,7 +183,7 @@ where
let state = StateProviderDatabase::new(state_provider);
let mut db =
State::builder().with_database(cached_reads.as_db_mut(state)).with_bundle_update().build();
let PayloadConfig { parent_header, extra_data, attributes } = config;
let PayloadConfig { parent_header, attributes } = config;

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;
Expand Down Expand Up @@ -470,7 +486,7 @@ where
gas_limit: block_gas_limit,
difficulty: U256::ZERO,
gas_used: cumulative_gas_used,
extra_data,
extra_data: builder_config.extra_data,
parent_beacon_block_root: attributes.parent_beacon_block_root,
blob_gas_used: blob_gas_used.map(Into::into),
excess_blob_gas: excess_blob_gas.map(Into::into),
Expand Down
8 changes: 4 additions & 4 deletions crates/node/core/src/args/payload_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{cli::config::PayloadBuilderConfig, version::default_extradata};
use crate::{cli::config::PayloadBuilderConfig, version::default_extra_data};
use alloy_consensus::constants::MAXIMUM_EXTRA_DATA_SIZE;
use alloy_eips::{eip1559::ETHEREUM_BLOCK_GAS_LIMIT, merge::SLOT_DURATION};
use clap::{
Expand All @@ -13,7 +13,7 @@ use std::{borrow::Cow, ffi::OsStr, time::Duration};
#[command(next_help_heading = "Builder")]
pub struct PayloadBuilderArgs {
/// Block extra data set by the payload builder.
#[arg(long = "builder.extradata", value_parser = ExtradataValueParser::default(), default_value_t = default_extradata())]
#[arg(long = "builder.extradata", value_parser = ExtradataValueParser::default(), default_value_t = default_extra_data())]
pub extradata: String,

/// Target gas ceiling for built blocks.
Expand All @@ -40,7 +40,7 @@ pub struct PayloadBuilderArgs {
impl Default for PayloadBuilderArgs {
fn default() -> Self {
Self {
extradata: default_extradata(),
extradata: default_extra_data(),
max_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT,
interval: Duration::from_secs(1),
deadline: SLOT_DURATION,
Expand Down Expand Up @@ -130,7 +130,7 @@ mod tests {

#[test]
fn test_default_extradata() {
let extradata = default_extradata();
let extradata = default_extra_data();
let args = CommandParser::<PayloadBuilderArgs>::parse_from([
"reth",
"--builder.extradata",
Expand Down
13 changes: 10 additions & 3 deletions crates/node/core/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Version information for reth.
use alloy_primitives::Bytes;
use alloy_rpc_types_engine::ClientCode;
use reth_db::ClientVersion;

Expand Down Expand Up @@ -114,7 +115,7 @@ pub(crate) const P2P_CLIENT_VERSION: &str = const_format::concatcp!(
env!("VERGEN_CARGO_TARGET_TRIPLE")
);

/// The default extradata used for payload building.
/// The default extra data used for payload building.
///
/// - The latest version from Cargo.toml
/// - The OS identifier
Expand All @@ -124,10 +125,16 @@ pub(crate) const P2P_CLIENT_VERSION: &str = const_format::concatcp!(
/// ```text
/// reth/v{major}.{minor}.{patch}/{OS}
/// ```
pub fn default_extradata() -> String {
pub fn default_extra_data() -> String {
format!("reth/v{}/{}", env!("CARGO_PKG_VERSION"), std::env::consts::OS)
}

/// The default extra data in bytes.
/// See [`default_extra_data`].
pub fn default_extra_data_bytes() -> Bytes {
Bytes::from(default_extra_data().as_bytes().to_vec())
}

/// The default client version accessing the database.
pub fn default_client_version() -> ClientVersion {
ClientVersion {
Expand All @@ -143,7 +150,7 @@ mod tests {

#[test]
fn assert_extradata_less_32bytes() {
let extradata = default_extradata();
let extradata = default_extra_data();
assert!(extradata.len() <= 32, "extradata must be less than 32 bytes: {extradata}")
}
}
10 changes: 3 additions & 7 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,7 @@ where
let (initialized_cfg, initialized_block_env) =
self.cfg_and_block_env(&attributes, &parent).map_err(PayloadBuilderError::other)?;

let config = PayloadConfig {
parent_header: Arc::new(parent),
attributes,
extra_data: Default::default(),
};
let config = PayloadConfig { parent_header: Arc::new(parent), attributes };
let ctx = OpPayloadBuilderCtx {
evm_config: self.evm_config.clone(),
chain_spec: client.chain_spec(),
Expand Down Expand Up @@ -621,7 +617,7 @@ impl<EvmConfig> OpPayloadBuilderCtx<EvmConfig> {

/// Returns the extra data for the block.
///
/// After holocene this extracts the extradata from the paylpad
/// After holocene this extracts the extra data from the payload
pub fn extra_data(&self) -> Result<Bytes, PayloadBuilderError> {
if self.is_holocene_active() {
self.attributes()
Expand All @@ -632,7 +628,7 @@ impl<EvmConfig> OpPayloadBuilderCtx<EvmConfig> {
)
.map_err(PayloadBuilderError::other)
} else {
Ok(self.config.extra_data.clone())
Ok(Default::default())
}
}

Expand Down
23 changes: 3 additions & 20 deletions crates/payload/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,7 @@ where
.ok_or_else(|| PayloadBuilderError::MissingParentHeader(attributes.parent()))?
};

let config = PayloadConfig::new(
Arc::new(parent_header.clone()),
self.config.extradata.clone(),
attributes,
);
let config = PayloadConfig::new(Arc::new(parent_header.clone()), attributes);

let until = self.job_deadline(config.attributes.timestamp());
let deadline = Box::pin(tokio::time::sleep_until(until));
Expand Down Expand Up @@ -713,30 +709,17 @@ impl Drop for Cancelled {
pub struct PayloadConfig<Attributes> {
/// The parent header.
pub parent_header: Arc<SealedHeader>,
/// Block extra data.
pub extra_data: Bytes,
/// Requested attributes for the payload.
pub attributes: Attributes,
}

impl<Attributes> PayloadConfig<Attributes> {
/// Returns an owned instance of the [`PayloadConfig`]'s `extra_data` bytes.
pub fn extra_data(&self) -> Bytes {
self.extra_data.clone()
}
}

impl<Attributes> PayloadConfig<Attributes>
where
Attributes: PayloadBuilderAttributes,
{
/// Create new payload config.
pub const fn new(
parent_header: Arc<SealedHeader>,
extra_data: Bytes,
attributes: Attributes,
) -> Self {
Self { parent_header, extra_data, attributes }
pub const fn new(parent_header: Arc<SealedHeader>, attributes: Attributes) -> Self {
Self { parent_header, attributes }
}

/// Returns the payload id.
Expand Down
Loading

0 comments on commit c553b1e

Please sign in to comment.