Skip to content

Commit

Permalink
feat(builder): ethereum builder config
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk committed Dec 11, 2024
1 parent 8fd305a commit e4e8c21
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 42 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
32 changes: 28 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_extradata;
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_primitives::{EthPrimitives, PooledTransactionsElement};
use reth_provider::{CanonStateSubscriptions, EthStorage};
Expand Down Expand Up @@ -228,9 +230,29 @@ where
}

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

impl Default for EthereumPayloadBuilder {
fn default() -> Self {
Self {
builder_config: EthereumBuilderConfig::new(
default_extradata().as_bytes().to_vec().into(),
),
}
}
}

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

impl EthereumPayloadBuilder {
/// A helper method initializing [`PayloadBuilderService`] with the given EVM config.
Expand All @@ -253,8 +275,10 @@ impl EthereumPayloadBuilder {
PayloadBuilderAttributes = EthPayloadBuilderAttributes,
>,
{
let payload_builder =
reth_ethereum_payload_builder::EthereumPayloadBuilder::new(evm_config);
let payload_builder = reth_ethereum_payload_builder::EthereumPayloadBuilder::new(
evm_config,
self.builder_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 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
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
6 changes: 1 addition & 5 deletions crates/payload/basic/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ where
cached_reads: args.cached_reads.clone(),
config: PayloadConfig {
parent_header: args.config.parent_header.clone(),
extra_data: args.config.extra_data.clone(),
attributes: left_attr.clone(),
},
cancel: args.cancel.clone(),
Expand All @@ -226,7 +225,6 @@ where
cached_reads: args.cached_reads.clone(),
config: PayloadConfig {
parent_header: args.config.parent_header.clone(),
extra_data: args.config.extra_data.clone(),
attributes: right_attr.clone(),
},
cancel: args.cancel.clone(),
Expand All @@ -252,16 +250,14 @@ where
match config.attributes {
Either::Left(left_attr) => {
let left_config = PayloadConfig {
attributes: left_attr,
parent_header: config.parent_header.clone(),
extra_data: config.extra_data.clone(),
attributes: left_attr,
};
self.left.build_empty_payload(client, left_config).map(Either::Left)
}
Either::Right(right_attr) => {
let right_config = PayloadConfig {
parent_header: config.parent_header.clone(),
extra_data: config.extra_data.clone(),
attributes: right_attr,
};
self.right.build_empty_payload(client, right_config).map(Either::Right)
Expand Down

0 comments on commit e4e8c21

Please sign in to comment.