Skip to content

Commit

Permalink
feat: add OpBuilder config (#13132)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 4, 2024
1 parent 27dab59 commit 874cf89
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
43 changes: 25 additions & 18 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
//! Optimism payload builder implementation.
use std::{fmt::Display, sync::Arc};

use crate::{
config::OpBuilderConfig,
error::OpPayloadBuilderError,
payload::{OpBuiltPayload, OpPayloadBuilderAttributes},
};
use alloy_consensus::{Header, Transaction, EMPTY_OMMER_ROOT_HASH};
use alloy_eips::{eip4895::Withdrawals, merge::BEACON_NONCE};
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_rpc_types_debug::ExecutionWitness;
use alloy_rpc_types_engine::PayloadId;
use op_alloy_consensus::DepositTransaction;
use op_alloy_rpc_types_engine::OpPayloadAttributes;
use reth_basic_payload_builder::*;
use reth_chain_state::ExecutedBlock;
use reth_chainspec::{ChainSpecProvider, EthereumHardforks};
Expand All @@ -26,9 +31,10 @@ use reth_provider::{
HashedPostStateProvider, ProviderError, StateProofProvider, StateProviderFactory,
StateRootProvider,
};
use reth_revm::database::StateProviderDatabase;
use reth_revm::{database::StateProviderDatabase, witness::ExecutionWitnessRecord};
use reth_transaction_pool::{
noop::NoopTransactionPool, BestTransactionsAttributes, PoolTransaction, TransactionPool,
noop::NoopTransactionPool, pool::BestPayloadTransactions, BestTransactionsAttributes,
PoolTransaction, TransactionPool,
};
use revm::{
db::{states::bundle_state::BundleRetention, State},
Expand All @@ -38,34 +44,35 @@ use revm::{
},
Database, DatabaseCommit,
};
use std::{fmt::Display, sync::Arc};
use tracing::{debug, trace, warn};

use crate::{
error::OpPayloadBuilderError,
payload::{OpBuiltPayload, OpPayloadBuilderAttributes},
};
use op_alloy_consensus::DepositTransaction;
use op_alloy_rpc_types_engine::OpPayloadAttributes;
use reth_revm::witness::ExecutionWitnessRecord;
use reth_transaction_pool::pool::BestPayloadTransactions;

/// Optimism's payload builder
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone)]
pub struct OpPayloadBuilder<EvmConfig, Txs = ()> {
/// The rollup's compute pending block configuration option.
// TODO(clabby): Implement this feature.
pub compute_pending_block: bool,
/// The type responsible for creating the evm.
pub evm_config: EvmConfig,
/// Settings for the builder, e.g. DA settings.
pub config: OpBuilderConfig,
/// The type responsible for yielding the best transactions for the payload if mempool
/// transactions are allowed.
pub best_transactions: Txs,
}

impl<EvmConfig> OpPayloadBuilder<EvmConfig> {
/// `OpPayloadBuilder` constructor.
pub const fn new(evm_config: EvmConfig) -> Self {
Self { compute_pending_block: true, evm_config, best_transactions: () }
///
/// Configures the builder with the default settings.
pub fn new(evm_config: EvmConfig) -> Self {
Self::with_builder_config(evm_config, Default::default())
}

/// Configures the builder with the given [`OpBuilderConfig`].
pub const fn with_builder_config(evm_config: EvmConfig, config: OpBuilderConfig) -> Self {
Self { compute_pending_block: true, evm_config, config, best_transactions: () }
}
}

Expand All @@ -82,8 +89,8 @@ impl<EvmConfig, Txs> OpPayloadBuilder<EvmConfig, Txs> {
self,
best_transactions: T,
) -> OpPayloadBuilder<EvmConfig, T> {
let Self { compute_pending_block, evm_config, .. } = self;
OpPayloadBuilder { compute_pending_block, evm_config, best_transactions }
let Self { compute_pending_block, evm_config, config, .. } = self;
OpPayloadBuilder { compute_pending_block, evm_config, best_transactions, config }
}

/// Enables the rollup's compute pending block configuration option.
Expand Down
38 changes: 38 additions & 0 deletions crates/optimism/payload/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,34 @@
use std::sync::{atomic::AtomicU64, Arc};

/// Settings for the OP builder.
#[derive(Debug, Clone, Default)]
pub struct OpBuilderConfig {
/// Data availability configuration for the OP builder.
pub da_config: OpDAConfig,
}

impl OpBuilderConfig {
/// Creates a new OP builder configuration with the given data availability configuration.
pub const fn new(da_config: OpDAConfig) -> Self {
Self { da_config }
}

/// Returns the Data Availability configuration for the OP builder, if it has configured
/// constraints.
pub fn constrained_da_config(&self) -> Option<&OpDAConfig> {
if self.da_config.is_empty() {
None
} else {
Some(&self.da_config)
}
}
}

/// Contains the Data Availability configuration for the OP builder.
///
/// This type is shareable and can be used to update the DA configuration for the OP payload
/// builder.
#[derive(Debug, Clone, Default)]
pub struct OpDAConfig {
inner: Arc<OpDAConfigInner>,
Expand All @@ -16,6 +43,11 @@ impl OpDAConfig {
this
}

/// Returns whether the configuration is empty.
pub fn is_empty(&self) -> bool {
self.max_da_tx_size().is_none() && self.max_da_block_size().is_none()
}

/// Returns the max allowed data availability size per transactions, if any.
pub fn max_da_tx_size(&self) -> Option<u64> {
let val = self.inner.max_da_tx_size.load(std::sync::atomic::Ordering::Relaxed);
Expand Down Expand Up @@ -84,4 +116,10 @@ mod tests {
assert_eq!(da.max_da_tx_size(), None);
assert_eq!(da.max_da_block_size(), None);
}

#[test]
fn test_da_constrained() {
let config = OpBuilderConfig::default();
assert!(config.constrained_da_config().is_none());
}
}

0 comments on commit 874cf89

Please sign in to comment.