Skip to content

Commit

Permalink
review: move the batch_prover field from BatchProducer to BatchBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiagoPittella committed Feb 24, 2025
1 parent 19ae2c4 commit d03caff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
29 changes: 22 additions & 7 deletions crates/block-producer/src/batch_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,28 +35,39 @@ 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<Url>) -> 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.
workers: 2.try_into().unwrap(),
// 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"
Expand All @@ -66,7 +81,7 @@ impl BatchBuilder {
self.simulated_proof_time,
self.failure_rate,
store,
batch_prover,
self.batch_prover,
);

loop {
Expand Down
15 changes: 3 additions & 12 deletions crates/block-producer/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ 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;
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,
Expand All @@ -45,7 +44,6 @@ pub struct BlockProducer {
rpc_listener: TcpListener,
store: StoreClient,
chain_tip: BlockNumber,
batch_prover: BatchProver,
}

impl BlockProducer {
Expand Down Expand Up @@ -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(),
Expand All @@ -97,7 +90,6 @@ impl BlockProducer {
store,
rpc_listener,
chain_tip,
batch_prover,
})
}

Expand All @@ -112,7 +104,6 @@ impl BlockProducer {
store,
chain_tip,
expiration_slack,
batch_prover,
} = self;

let mempool = Mempool::shared(
Expand All @@ -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(())
}
})
Expand Down

0 comments on commit d03caff

Please sign in to comment.