use crate::{ chainspec::BerachainChainSpec, engine::payload::{ BerachainBuiltPayload, BerachainPayloadAttributes, BerachainPayloadBuilderAttributes, }, hardforks::BerachainHardforks, node::evm::config::{BerachainEvmConfig, BerachainNextBlockEnvAttributes}, primitives::{BerachainHeader, BerachainPrimitives}, transaction::BerachainTxEnvelope, }; use alloy_consensus::Transaction; use alloy_primitives::U256; use alloy_rlp::Encodable; use reth::{ api::{FullNodeTypes, NodeTypes, PayloadBuilderError, PayloadTypes, TxTy}, chainspec::EthereumHardforks, providers::StateProviderFactory, revm::{State, context::Block, database::StateProviderDatabase}, transaction_pool::{PoolTransaction, TransactionPool}, }; use reth_basic_payload_builder::{ BuildArguments, BuildOutcome, MissingPayloadBehaviour, PayloadBuilder, PayloadConfig, is_better_payload, }; use reth_chainspec::{ChainSpecProvider, EthChainSpec}; use reth_consensus_common::validation::MAX_RLP_BLOCK_SIZE; use reth_errors::ConsensusError; use reth_ethereum_engine_primitives::BlobSidecars; use reth_ethereum_payload_builder::EthereumBuilderConfig; use reth_evm::{ ConfigureEvm, Evm, block::{BlockExecutionError, BlockValidationError}, execute::{BlockBuilder, BlockBuilderOutcome}, }; use reth_node_builder::{BuilderContext, PayloadBuilderConfig, components::PayloadBuilderBuilder}; use reth_payload_primitives::PayloadBuilderAttributes; use reth_primitives_traits::transaction::error::InvalidTransactionError; use reth_transaction_pool::{ BestTransactions, BestTransactionsAttributes, ValidPoolTransaction, error::{Eip4844PoolTransactionError, InvalidPoolTransactionError}, }; use std::sync::Arc; use tracing::{debug, trace, warn}; type BestTransactionsIter = Box< dyn BestTransactions::Transaction>>>, >; /// Service builder for creating Berachain payload builders /// /// This component integrates with the Reth node builder system to provide /// a Berachain-specific payload service that handles the conversion between /// Berachain payload attributes and Ethereum payload building logic. #[derive(Clone, Default, Debug)] #[non_exhaustive] pub struct BerachainPayloadServiceBuilder; impl PayloadBuilderBuilder for BerachainPayloadServiceBuilder where Types: NodeTypes, Node: FullNodeTypes, Pool: TransactionPool>> + Unpin + 'static, Types::Payload: PayloadTypes< BuiltPayload = BerachainBuiltPayload, PayloadAttributes = BerachainPayloadAttributes, PayloadBuilderAttributes = BerachainPayloadBuilderAttributes, >, { type PayloadBuilder = BerachainPayloadBuilder; async fn build_payload_builder( self, ctx: &BuilderContext, pool: Pool, evm_config: BerachainEvmConfig, ) -> eyre::Result { let conf = ctx.payload_builder_config(); let chain = ctx.chain_spec().chain(); let gas_limit = conf.gas_limit_for(chain); Ok(BerachainPayloadBuilder::new( ctx.provider().clone(), pool, evm_config, EthereumBuilderConfig::new().with_gas_limit(gas_limit), )) } } /// Berachain-specific payload builder implementation /// /// This payload builder handles Berachain-specific payload attributes while /// delegating the actual payload building to the proven Ethereum implementation. /// It provides the necessary type conversions and maintains compatibility /// with Berachain's chain specification. #[derive(Debug, Clone)] pub struct BerachainPayloadBuilder { /// Client providing access to node state client: Client, /// Transaction pool pool: Pool, /// The type responsible for creating the evm evm_config: BerachainEvmConfig, /// Payload builder configuration builder_config: EthereumBuilderConfig, } impl BerachainPayloadBuilder { /// Create a new Berachain payload builder pub const fn new( client: Client, pool: Pool, evm_config: BerachainEvmConfig, builder_config: EthereumBuilderConfig, ) -> Self { Self { client, pool, evm_config, builder_config } } } impl PayloadBuilder for BerachainPayloadBuilder where Client: StateProviderFactory + ChainSpecProvider + Clone, Pool: TransactionPool>, { type Attributes = BerachainPayloadBuilderAttributes; type BuiltPayload = BerachainBuiltPayload; fn try_build( &self, args: BuildArguments, ) -> Result, PayloadBuilderError> { default_berachain_payload( self.evm_config.clone(), self.client.clone(), self.pool.clone(), self.builder_config.clone(), args, |attributes| self.pool.best_transactions_with_attributes(attributes), ) } fn on_missing_payload( &self, _args: BuildArguments, ) -> MissingPayloadBehaviour { if self.builder_config.await_payload_on_missing { MissingPayloadBehaviour::AwaitInProgress } else { MissingPayloadBehaviour::RaceEmptyPayload } } fn build_empty_payload( &self, config: PayloadConfig, ) -> Result { let args = BuildArguments::new(Default::default(), config, Default::default(), None); default_berachain_payload( self.evm_config.clone(), self.client.clone(), self.pool.clone(), self.builder_config.clone(), args, |attributes| self.pool.best_transactions_with_attributes(attributes), )? .into_payload() .ok_or_else(|| PayloadBuilderError::MissingPayload) } } /// Constructs an Ethereum transaction payload using the best transactions from the pool. /// /// Given build arguments including an Ethereum client, transaction pool, /// 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 fn default_berachain_payload( evm_config: BerachainEvmConfig, client: Client, pool: Pool, builder_config: EthereumBuilderConfig, args: BuildArguments, best_txs: F, ) -> Result, PayloadBuilderError> where Client: StateProviderFactory + ChainSpecProvider, Pool: TransactionPool>, F: FnOnce(BestTransactionsAttributes) -> BestTransactionsIter, { let BuildArguments { mut cached_reads, config, cancel, best_payload } = args; let PayloadConfig { parent_header, attributes } = config; let state_provider = client.state_by_block_hash(parent_header.hash())?; let state = StateProviderDatabase::new(&state_provider); let mut db = State::builder().with_database(cached_reads.as_db_mut(state)).with_bundle_update().build(); let mut builder = evm_config .builder_for_next_block( &mut db, &parent_header, BerachainNextBlockEnvAttributes { timestamp: attributes.timestamp(), suggested_fee_recipient: attributes.suggested_fee_recipient(), prev_randao: attributes.prev_randao(), gas_limit: builder_config.gas_limit(parent_header.gas_limit), parent_beacon_block_root: attributes.parent_beacon_block_root(), withdrawals: Some(attributes.withdrawals().clone()), prev_proposer_pubkey: attributes.prev_proposer_pubkey, extra_data: builder_config.extra_data.clone(), }, ) .map_err(PayloadBuilderError::other)?; let chain_spec = client.chain_spec(); 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 block_gas_limit: u64 = builder.evm_mut().block().gas_limit(); let base_fee = builder.evm_mut().block().basefee(); let mut best_txs = best_txs(BestTransactionsAttributes::new( base_fee, builder.evm_mut().block().blob_gasprice().map(|gasprice| gasprice as u64), )); let mut total_fees = U256::ZERO; builder.apply_pre_execution_changes().map_err(|err| { warn!(target: "payload_builder", %err, "failed to apply pre-execution changes"); PayloadBuilderError::Internal(err.into()) })?; // initialize empty blob sidecars at first. If cancun is active then this will be populated by // blob sidecars if any. let mut blob_sidecars = BlobSidecars::Empty; let mut block_blob_count = 0; let mut block_transactions_rlp_length = 0; let blob_params = chain_spec.blob_params_at_timestamp(attributes.timestamp); let protocol_max_blob_count = blob_params.as_ref().map(|params| params.max_blob_count).unwrap_or_default(); // Apply user-configured blob limit (EIP-7872) // Per EIP-7872: if the minimum is zero, set it to one let max_blob_count = builder_config .max_blobs_per_block .map(|user_limit| std::cmp::min(user_limit, protocol_max_blob_count).max(1)) .unwrap_or(protocol_max_blob_count); let is_osaka = chain_spec.is_osaka_active_at_timestamp(attributes.timestamp); let withdrawals_rlp_length = attributes.withdrawals().length(); let prague3_active = chain_spec.is_prague3_active_at_timestamp(attributes.timestamp()); if prague3_active { debug!(target: "payload_builder", "Prague3 is active, building empty block"); } while !prague3_active && let Some(pool_tx) = best_txs.next() { // ensure we still have capacity for this transaction if cumulative_gas_used + pool_tx.gas_limit() > block_gas_limit { // we can't fit this transaction into the block, so we need to mark it as invalid // which also removes all dependent transaction from the iterator before we can // continue best_txs.mark_invalid( &pool_tx, &InvalidPoolTransactionError::ExceedsGasLimit(pool_tx.gas_limit(), block_gas_limit), ); continue; } // check if the job was cancelled, if so we can exit early if cancel.is_cancelled() { return Ok(BuildOutcome::Cancelled); } // convert tx to a signed transaction let tx = pool_tx.to_consensus(); let tx_rlp_len = tx.inner().length(); let estimated_block_size_with_tx = block_transactions_rlp_length + tx_rlp_len + withdrawals_rlp_length + 1024; if is_osaka && estimated_block_size_with_tx > MAX_RLP_BLOCK_SIZE { best_txs.mark_invalid( &pool_tx, &InvalidPoolTransactionError::OversizedData { size: estimated_block_size_with_tx, limit: MAX_RLP_BLOCK_SIZE, }, ); continue; } // There's only limited amount of blob space available per block, so we need to check if // the EIP-4844 can still fit in the block let mut blob_tx_sidecar = None; if let Some(blob_tx) = tx.as_eip4844() { let tx_blob_count = blob_tx.tx().blob_versioned_hashes.len() as u64; if block_blob_count + tx_blob_count > max_blob_count { // we can't fit this _blob_ transaction into the block, so we mark it as // invalid, which removes its dependent transactions from // the iterator. This is similar to the gas limit condition // for regular transactions above. trace!(target: "payload_builder", tx=?tx.hash(), ?block_blob_count, "skipping blob transaction because it would exceed the max blob count per block"); best_txs.mark_invalid( &pool_tx, &InvalidPoolTransactionError::Eip4844( Eip4844PoolTransactionError::TooManyEip4844Blobs { have: block_blob_count + tx_blob_count, permitted: max_blob_count, }, ), ); continue; } // BRIP-0010 Osaka does not adopt EIP-7594 (PeerDAS); EIP-4844 sidecars remain the // sole accepted format across forks. let blob_sidecar_result = 'sidecar: { let Some(sidecar) = pool.get_blob(*tx.hash()).map_err(PayloadBuilderError::other)? else { break 'sidecar Err(Eip4844PoolTransactionError::MissingEip4844BlobSidecar); }; if sidecar.is_eip4844() { Ok(sidecar) } else { Err(Eip4844PoolTransactionError::Eip7594SidecarDisallowed) } }; blob_tx_sidecar = match blob_sidecar_result { Ok(sidecar) => Some(sidecar), Err(error) => { best_txs.mark_invalid(&pool_tx, &InvalidPoolTransactionError::Eip4844(error)); continue; } }; } // Execute the transaction let gas_used = match builder.execute_transaction(tx.clone()) { Ok(gas_used) => gas_used, Err(BlockExecutionError::Validation(BlockValidationError::InvalidTx { error, .. })) => { if error.is_nonce_too_low() { // if the nonce is too low, we can skip this transaction trace!(target: "payload_builder", %error, ?tx, "skipping nonce too low transaction"); } else { // if the transaction is invalid, we can skip it and all of its // descendants trace!(target: "payload_builder", %error, ?tx, "skipping invalid transaction and its descendants"); best_txs.mark_invalid( &pool_tx, &InvalidPoolTransactionError::Consensus( InvalidTransactionError::TxTypeNotSupported, ), ); } continue; } // this is an error that we should treat as fatal for this attempt Err(err) => return Err(PayloadBuilderError::evm(err)), }; // add to the total blob gas used if the transaction successfully executed if let Some(blob_tx) = tx.as_eip4844() { block_blob_count += blob_tx.tx().blob_versioned_hashes.len() as u64; // if we've reached the max blob count, we can skip blob txs entirely if block_blob_count == max_blob_count { best_txs.skip_blobs(); } } block_transactions_rlp_length += tx_rlp_len; // update and add to total fees let miner_fee = tx.effective_tip_per_gas(base_fee).expect("fee is always valid; execution succeeded"); total_fees += U256::from(miner_fee) * U256::from(gas_used); cumulative_gas_used += gas_used; // Add blob tx sidecar to the payload. if let Some(sidecar) = blob_tx_sidecar { blob_sidecars.push_sidecar_variant(sidecar.as_ref().clone()); } } // check if we have a better block if !is_better_payload(best_payload.as_ref(), total_fees) { // Release db drop(builder); // can skip building the block return Ok(BuildOutcome::Aborted { fees: total_fees, cached_reads }); } let BlockBuilderOutcome { execution_result, block, .. } = builder.finish(&state_provider)?; let requests = chain_spec .is_prague_active_at_timestamp(attributes.timestamp) .then_some(execution_result.requests); let sealed_block = Arc::new(block.sealed_block().clone()); debug!(target: "payload_builder", id=%attributes.id, sealed_block_header = ?sealed_block.sealed_header(), "sealed built block"); if is_osaka && sealed_block.rlp_length() > MAX_RLP_BLOCK_SIZE { return Err(PayloadBuilderError::other(ConsensusError::BlockTooLarge { rlp_length: sealed_block.rlp_length(), max_rlp_length: MAX_RLP_BLOCK_SIZE, })); } let payload = BerachainBuiltPayload::new(attributes.id, sealed_block, total_fees, requests) // add blob sidecars from the executed txs .with_sidecars(blob_sidecars); Ok(BuildOutcome::Better { payload, cached_reads }) } #[cfg(test)] mod tests { use crate::test_utils::bepolia_chainspec; use reth_chainspec::EthChainSpec; use reth_node_core::{args::PayloadBuilderArgs, cli::config::PayloadBuilderConfig}; #[test] fn test_berachain_uses_36m_gas_limit() { let chain_spec = bepolia_chainspec(); let config = PayloadBuilderArgs::default(); let gas_limit = config.gas_limit_for(chain_spec.chain()); assert_eq!( gas_limit, 36_000_000, "Berachain expects 36M gas limit from upstream Reth configuration" ); } }