//! Optimism Node types config. use std::sync::Arc; use alloy_consensus::Header; use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig}; use reth_chainspec::{EthChainSpec, Hardforks}; use reth_db::transaction::{DbTx, DbTxMut}; use reth_evm::{execute::BasicBlockExecutorProvider, ConfigureEvm}; use reth_network::{NetworkConfig, NetworkHandle, NetworkManager, PeersInfo}; use reth_node_api::{ AddOnsContext, EngineValidator, FullNodeComponents, NodeAddOns, PayloadBuilder, }; use reth_node_builder::{ components::{ ComponentsBuilder, ConsensusBuilder, ExecutorBuilder, NetworkBuilder, PayloadServiceBuilder, PoolBuilder, PoolBuilderConfigOverrides, }, node::{FullNodeTypes, NodeTypes, NodeTypesWithEngine}, rpc::{EngineValidatorBuilder, RethRpcAddOns, RpcAddOns, RpcHandle}, BuilderContext, Node, NodeAdapter, NodeComponentsBuilder, PayloadBuilderConfig, }; use reth_optimism_chainspec::OpChainSpec; use reth_optimism_consensus::OpBeaconConsensus; use reth_optimism_evm::{OpEvmConfig, OpExecutionStrategyFactory}; use reth_optimism_payload_builder::builder::OpPayloadTransactions; use reth_optimism_primitives::OpPrimitives; use reth_optimism_rpc::{ witness::{DebugExecutionWitnessApiServer, OpDebugWitnessApi}, OpEthApi, }; use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService}; use reth_primitives::BlockBody; use reth_provider::{ providers::ChainStorage, BlockBodyWriter, CanonStateSubscriptions, DBProvider, EthStorage, ProviderResult, }; use reth_rpc_server_types::RethRpcModule; use reth_tracing::tracing::{debug, info}; use reth_transaction_pool::{ blobstore::DiskFileBlobStore, CoinbaseTipOrdering, TransactionPool, TransactionValidationTaskExecutor, }; use reth_trie_db::MerklePatriciaTrie; use crate::{ args::RollupArgs, engine::OpEngineValidator, txpool::{OpTransactionPool, OpTransactionValidator}, OpEngineTypes, }; /// Storage implementation for Optimism. #[derive(Debug, Default, Clone)] pub struct OpStorage(EthStorage); impl> BlockBodyWriter for OpStorage { fn write_block_bodies( &self, provider: &Provider, bodies: Vec<(u64, Option)>, ) -> ProviderResult<()> { self.0.write_block_bodies(provider, bodies) } fn remove_block_bodies_above( &self, provider: &Provider, block: alloy_primitives::BlockNumber, ) -> ProviderResult<()> { self.0.remove_block_bodies_above(provider, block) } } impl ChainStorage for OpStorage { fn writer( &self, ) -> impl reth_provider::ChainStorageWriter, OpPrimitives> where TX: DbTxMut + DbTx + 'static, Types: NodeTypes, { self } } /// Type configuration for a regular Optimism node. #[derive(Debug, Default, Clone)] #[non_exhaustive] pub struct OpNode { /// Additional Optimism args pub args: RollupArgs, } impl OpNode { /// Creates a new instance of the Optimism node type. pub const fn new(args: RollupArgs) -> Self { Self { args } } /// Returns the components for the given [`RollupArgs`]. pub fn components( args: RollupArgs, ) -> ComponentsBuilder< Node, OpPoolBuilder, OpPayloadBuilder, OpNetworkBuilder, OpExecutorBuilder, OpConsensusBuilder, > where Node: FullNodeTypes< Types: NodeTypesWithEngine, >, { let RollupArgs { disable_txpool_gossip, compute_pending_block, discovery_v4, .. } = args; ComponentsBuilder::default() .node_types::() .pool(OpPoolBuilder::default()) .payload(OpPayloadBuilder::new(compute_pending_block)) .network(OpNetworkBuilder { disable_txpool_gossip, disable_discovery_v4: !discovery_v4, }) .executor(OpExecutorBuilder::default()) .consensus(OpConsensusBuilder::default()) } } impl Node for OpNode where N: FullNodeTypes< Types: NodeTypesWithEngine< Engine = OpEngineTypes, ChainSpec = OpChainSpec, Primitives = OpPrimitives, Storage = OpStorage, >, >, { type ComponentsBuilder = ComponentsBuilder< N, OpPoolBuilder, OpPayloadBuilder, OpNetworkBuilder, OpExecutorBuilder, OpConsensusBuilder, >; type AddOns = OpAddOns>::Components>>; fn components_builder(&self) -> Self::ComponentsBuilder { let Self { args } = self; Self::components(args.clone()) } fn add_ons(&self) -> Self::AddOns { OpAddOns::new(self.args.sequencer_http.clone()) } } impl NodeTypes for OpNode { type Primitives = OpPrimitives; type ChainSpec = OpChainSpec; type StateCommitment = MerklePatriciaTrie; type Storage = OpStorage; } impl NodeTypesWithEngine for OpNode { type Engine = OpEngineTypes; } /// Add-ons w.r.t. optimism. #[derive(Debug)] pub struct OpAddOns(pub RpcAddOns, OpEngineValidatorBuilder>); impl Default for OpAddOns { fn default() -> Self { Self::new(None) } } impl OpAddOns { /// Create a new instance with the given `sequencer_http` URL. pub fn new(sequencer_http: Option) -> Self { Self(RpcAddOns::new(move |ctx| OpEthApi::new(ctx, sequencer_http), Default::default())) } } impl NodeAddOns for OpAddOns where N: FullNodeComponents< Types: NodeTypes, PayloadBuilder: PayloadBuilder::Engine>, >, OpEngineValidator: EngineValidator<::Engine>, { type Handle = RpcHandle>; async fn launch_add_ons( self, ctx: reth_node_api::AddOnsContext<'_, N>, ) -> eyre::Result { // install additional OP specific rpc methods let debug_ext = OpDebugWitnessApi::new(ctx.node.provider().clone(), ctx.node.evm_config().clone()); self.0 .launch_add_ons_with(ctx, move |modules| { debug!(target: "reth::cli", "Installing debug payload witness rpc endpoint"); modules.merge_if_module_configured(RethRpcModule::Debug, debug_ext.into_rpc())?; Ok(()) }) .await } } impl RethRpcAddOns for OpAddOns where N: FullNodeComponents< Types: NodeTypes, PayloadBuilder: PayloadBuilder::Engine>, >, OpEngineValidator: EngineValidator<::Engine>, { type EthApi = OpEthApi; fn hooks_mut(&mut self) -> &mut reth_node_builder::rpc::RpcHooks { self.0.hooks_mut() } } /// A regular optimism evm and executor builder. #[derive(Debug, Default, Clone, Copy)] #[non_exhaustive] pub struct OpExecutorBuilder; impl ExecutorBuilder for OpExecutorBuilder where Node: FullNodeTypes>, { type EVM = OpEvmConfig; type Executor = BasicBlockExecutorProvider; async fn build_evm( self, ctx: &BuilderContext, ) -> eyre::Result<(Self::EVM, Self::Executor)> { let evm_config = OpEvmConfig::new(ctx.chain_spec()); let strategy_factory = OpExecutionStrategyFactory::new(ctx.chain_spec(), evm_config.clone()); let executor = BasicBlockExecutorProvider::new(strategy_factory); Ok((evm_config, executor)) } } /// A basic optimism transaction pool. /// /// This contains various settings that can be configured and take precedence over the node's /// config. #[derive(Debug, Default, Clone)] pub struct OpPoolBuilder { /// Enforced overrides that are applied to the pool config. pub pool_config_overrides: PoolBuilderConfigOverrides, } impl PoolBuilder for OpPoolBuilder where Node: FullNodeTypes>, { type Pool = OpTransactionPool; async fn build_pool(self, ctx: &BuilderContext) -> eyre::Result { let Self { pool_config_overrides } = self; let data_dir = ctx.config().datadir(); let blob_store = DiskFileBlobStore::open(data_dir.blobstore(), Default::default())?; let validator = TransactionValidationTaskExecutor::eth_builder(Arc::new( ctx.chain_spec().inner.clone(), )) .no_eip4844() .with_head_timestamp(ctx.head().timestamp) .kzg_settings(ctx.kzg_settings()?) .with_additional_tasks( pool_config_overrides .additional_validation_tasks .unwrap_or_else(|| ctx.config().txpool.additional_validation_tasks), ) .build_with_tasks(ctx.provider().clone(), ctx.task_executor().clone(), blob_store.clone()) .map(|validator| { OpTransactionValidator::new(validator) // In --dev mode we can't require gas fees because we're unable to decode // the L1 block info .require_l1_data_gas_fee(!ctx.config().dev.dev) }); let transaction_pool = reth_transaction_pool::Pool::new( validator, CoinbaseTipOrdering::default(), blob_store, pool_config_overrides.apply(ctx.pool_config()), ); info!(target: "reth::cli", "Transaction pool initialized"); let transactions_path = data_dir.txpool_transactions(); // spawn txpool maintenance task { let pool = transaction_pool.clone(); let chain_events = ctx.provider().canonical_state_stream(); let client = ctx.provider().clone(); let transactions_backup_config = reth_transaction_pool::maintain::LocalTransactionBackupConfig::with_local_txs_backup(transactions_path); ctx.task_executor().spawn_critical_with_graceful_shutdown_signal( "local transactions backup task", |shutdown| { reth_transaction_pool::maintain::backup_local_transactions_task( shutdown, pool.clone(), transactions_backup_config, ) }, ); // spawn the maintenance task ctx.task_executor().spawn_critical( "txpool maintenance task", reth_transaction_pool::maintain::maintain_transaction_pool_future( client, pool, chain_events, ctx.task_executor().clone(), Default::default(), ), ); debug!(target: "reth::cli", "Spawned txpool maintenance task"); } Ok(transaction_pool) } } /// A basic optimism payload service builder #[derive(Debug, Default, Clone)] pub struct OpPayloadBuilder { /// By default the pending block equals the latest block /// to save resources and not leak txs from the tx-pool, /// this flag enables computing of the pending block /// from the tx-pool instead. /// /// If `compute_pending_block` is not enabled, the payload builder /// will use the payload attributes from the latest block. Note /// that this flag is not yet functional. pub compute_pending_block: bool, /// The type responsible for yielding the best transactions for the payload if mempool /// transactions are allowed. pub best_transactions: Txs, } impl OpPayloadBuilder { /// Create a new instance with the given `compute_pending_block` flag. pub const fn new(compute_pending_block: bool) -> Self { Self { compute_pending_block, best_transactions: () } } } impl OpPayloadBuilder where Txs: OpPayloadTransactions, { /// Configures the type responsible for yielding the transactions that should be included in the /// payload. pub fn with_transactions( self, best_transactions: T, ) -> OpPayloadBuilder { let Self { compute_pending_block, .. } = self; OpPayloadBuilder { compute_pending_block, best_transactions } } /// A helper method to initialize [`PayloadBuilderService`] with the given EVM config. pub fn spawn( self, evm_config: Evm, ctx: &BuilderContext, pool: Pool, ) -> eyre::Result> where Node: FullNodeTypes< Types: NodeTypesWithEngine, >, Pool: TransactionPool + Unpin + 'static, Evm: ConfigureEvm
, { let payload_builder = reth_optimism_payload_builder::OpPayloadBuilder::new(evm_config) .with_transactions(self.best_transactions) .set_compute_pending_block(self.compute_pending_block); let conf = ctx.payload_builder_config(); let payload_job_config = BasicPayloadJobGeneratorConfig::default() .interval(conf.interval()) .deadline(conf.deadline()) .max_payload_tasks(conf.max_payload_tasks()) // no extradata for OP .extradata(Default::default()); let payload_generator = BasicPayloadJobGenerator::with_builder( ctx.provider().clone(), pool, ctx.task_executor().clone(), payload_job_config, payload_builder, ); let (payload_service, payload_builder) = PayloadBuilderService::new(payload_generator, ctx.provider().canonical_state_stream()); ctx.task_executor().spawn_critical("payload builder service", Box::pin(payload_service)); Ok(payload_builder) } } impl PayloadServiceBuilder for OpPayloadBuilder where Node: FullNodeTypes>, Pool: TransactionPool + Unpin + 'static, Txs: OpPayloadTransactions, { async fn spawn_payload_service( self, ctx: &BuilderContext, pool: Pool, ) -> eyre::Result> { self.spawn(OpEvmConfig::new(ctx.chain_spec()), ctx, pool) } } /// A basic optimism network builder. #[derive(Debug, Default, Clone)] pub struct OpNetworkBuilder { /// Disable transaction pool gossip pub disable_txpool_gossip: bool, /// Disable discovery v4 pub disable_discovery_v4: bool, } impl OpNetworkBuilder { /// Returns the [`NetworkConfig`] that contains the settings to launch the p2p network. /// /// This applies the configured [`OpNetworkBuilder`] settings. pub fn network_config( &self, ctx: &BuilderContext, ) -> eyre::Result::Provider>> where Node: FullNodeTypes>, { let Self { disable_txpool_gossip, disable_discovery_v4 } = self.clone(); let args = &ctx.config().network; let network_builder = ctx .network_config_builder()? // apply discovery settings .apply(|mut builder| { let rlpx_socket = (args.addr, args.port).into(); if disable_discovery_v4 || args.discovery.disable_discovery { builder = builder.disable_discv4_discovery(); } if !args.discovery.disable_discovery { builder = builder.discovery_v5( args.discovery.discovery_v5_builder( rlpx_socket, ctx.config() .network .resolved_bootnodes() .or_else(|| ctx.chain_spec().bootnodes()) .unwrap_or_default(), ), ); } builder }); let mut network_config = ctx.build_network_config(network_builder); // When `sequencer_endpoint` is configured, the node will forward all transactions to a // Sequencer node for execution and inclusion on L1, and disable its own txpool // gossip to prevent other parties in the network from learning about them. network_config.tx_gossip_disabled = disable_txpool_gossip; Ok(network_config) } } impl NetworkBuilder for OpNetworkBuilder where Node: FullNodeTypes>, Pool: TransactionPool + Unpin + 'static, { async fn build_network( self, ctx: &BuilderContext, pool: Pool, ) -> eyre::Result { let network_config = self.network_config(ctx)?; let network = NetworkManager::builder(network_config).await?; let handle = ctx.start_network(network, pool); info!(target: "reth::cli", enode=%handle.local_node_record(), "P2P networking initialized"); Ok(handle) } } /// A basic optimism consensus builder. #[derive(Debug, Default, Clone)] #[non_exhaustive] pub struct OpConsensusBuilder; impl ConsensusBuilder for OpConsensusBuilder where Node: FullNodeTypes>, { type Consensus = Arc; async fn build_consensus(self, ctx: &BuilderContext) -> eyre::Result { Ok(Arc::new(OpBeaconConsensus::new(ctx.chain_spec()))) } } /// Builder for [`OpEngineValidator`]. #[derive(Debug, Default, Clone)] #[non_exhaustive] pub struct OpEngineValidatorBuilder; impl EngineValidatorBuilder for OpEngineValidatorBuilder where Types: NodeTypesWithEngine, Node: FullNodeComponents, OpEngineValidator: EngineValidator, { type Validator = OpEngineValidator; async fn build(self, ctx: &AddOnsContext<'_, Node>) -> eyre::Result { Ok(OpEngineValidator::new(ctx.config.chain.clone())) } }