diff --git a/crates/block-producer/src/batch_builder/mod.rs b/crates/block-producer/src/batch_builder/mod.rs index 0e80a9b08..d32889ca1 100644 --- a/crates/block-producer/src/batch_builder/mod.rs +++ b/crates/block-producer/src/batch_builder/mod.rs @@ -2,12 +2,16 @@ use std::{num::NonZeroUsize, ops::Range, time::Duration}; use miden_node_proto::domain::batch::BatchInputs; use miden_node_utils::formatting::format_array; -use miden_objects::batch::{BatchId, ProposedBatch, ProvenBatch}; +use miden_objects::{ + batch::{BatchId, ProposedBatch, ProvenBatch}, + MIN_PROOF_SECURITY_LEVEL, +}; use miden_proving_service_client::proving_service::batch_prover::RemoteBatchProver; use miden_tx_batch_prover::LocalBatchProver; use rand::Rng; use tokio::{task::JoinSet, time}; use tracing::{debug, info, instrument, Span}; +use url::Url; use crate::{ domain::transaction::AuthenticatedTransaction, errors::BuildBatchError, mempool::SharedMempool, @@ -31,10 +35,22 @@ pub struct BatchBuilder { /// /// Note: this _must_ be sign positive and less than 1.0. pub failure_rate: f32, + /// The batch prover to use. + /// + /// If not provided, a local batch prover is used. + batch_prover: BatchProver, } -impl Default for BatchBuilder { - fn default() -> Self { +impl BatchBuilder { + /// Creates a new [`BatchBuilder`] with the given batch prover URL. + /// + /// If no URL is provided, a local batch prover is used. + pub fn new(batch_prover_url: Option) -> Self { + let batch_prover = match batch_prover_url { + Some(url) => BatchProver::new_remote(url), + None => BatchProver::new_local(MIN_PROOF_SECURITY_LEVEL), + }; + Self { batch_interval: SERVER_BUILD_BATCH_FREQUENCY, // SAFETY: 2 is non-zero so this always succeeds. @@ -42,17 +58,16 @@ impl Default for BatchBuilder { // Note: The range cannot be empty. simulated_proof_time: Duration::ZERO..Duration::from_millis(1), failure_rate: 0.0, + batch_prover, } } -} -impl BatchBuilder { /// Starts the [`BatchBuilder`], creating and proving batches at the configured interval. /// /// A pool of batch-proving workers is spawned, which are fed new batch jobs periodically. /// A batch is skipped if there are no available workers, or if there are no transactions /// available to batch. - pub async fn run(self, mempool: SharedMempool, store: StoreClient, batch_prover: BatchProver) { + pub async fn run(self, mempool: SharedMempool, store: StoreClient) { assert!( self.failure_rate < 1.0 && self.failure_rate.is_sign_positive(), "Failure rate must be a percentage" @@ -66,7 +81,7 @@ impl BatchBuilder { self.simulated_proof_time, self.failure_rate, store, - batch_prover, + self.batch_prover, ); loop { diff --git a/crates/block-producer/src/server.rs b/crates/block-producer/src/server.rs index 12d790ab0..8a97c0e56 100644 --- a/crates/block-producer/src/server.rs +++ b/crates/block-producer/src/server.rs @@ -11,7 +11,6 @@ use miden_node_utils::{ }; use miden_objects::{ block::BlockNumber, transaction::ProvenTransaction, utils::serde::Deserializable, - MIN_PROOF_SECURITY_LEVEL, }; use tokio::{net::TcpListener, sync::Mutex}; use tokio_stream::wrappers::TcpListenerStream; @@ -19,7 +18,7 @@ use tonic::Status; use tracing::{debug, info, instrument}; use crate::{ - batch_builder::{BatchBuilder, BatchProver}, + batch_builder::BatchBuilder, block_builder::BlockBuilder, config::BlockProducerConfig, domain::transaction::AuthenticatedTransaction, @@ -45,7 +44,6 @@ pub struct BlockProducer { rpc_listener: TcpListener, store: StoreClient, chain_tip: BlockNumber, - batch_prover: BatchProver, } impl BlockProducer { @@ -82,13 +80,8 @@ impl BlockProducer { info!(target: COMPONENT, "Server initialized"); - let batch_prover = match config.batch_prover_url { - Some(url) => BatchProver::new_remote(url), - None => BatchProver::new_local(MIN_PROOF_SECURITY_LEVEL), - }; - Ok(Self { - batch_builder: BatchBuilder::default(), + batch_builder: BatchBuilder::new(config.batch_prover_url), block_builder: BlockBuilder::new(store.clone()), batch_budget: BatchBudget::default(), block_budget: BlockBudget::default(), @@ -97,7 +90,6 @@ impl BlockProducer { store, rpc_listener, chain_tip, - batch_prover, }) } @@ -112,7 +104,6 @@ impl BlockProducer { store, chain_tip, expiration_slack, - batch_prover, } = self; let mempool = Mempool::shared( @@ -136,7 +127,7 @@ impl BlockProducer { let mempool = mempool.clone(); let store = store.clone(); async { - batch_builder.run(mempool, store, batch_prover).await; + batch_builder.run(mempool, store).await; Ok(()) } })