Skip to content

Commit

Permalink
feat: add remote block prover as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiagoPittella committed Feb 24, 2025
1 parent 0b5644b commit e28dcce
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 11 deletions.
6 changes: 6 additions & 0 deletions bin/node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct NormalizedBlockProducerConfig {
endpoint: Url,
verify_tx_proofs: bool,
batch_prover_url: Option<Url>,
block_prover_url: Option<Url>,
}

impl Default for NormalizedRpcConfig {
Expand All @@ -50,11 +51,13 @@ impl Default for NormalizedBlockProducerConfig {
store_url: _,
verify_tx_proofs,
batch_prover_url,
block_prover_url,
} = BlockProducerConfig::default();
Self {
endpoint,
verify_tx_proofs,
batch_prover_url,
block_prover_url,
}
}
}
Expand All @@ -68,6 +71,7 @@ impl NodeConfig {
store_url: store.endpoint.clone(),
verify_tx_proofs: block_producer.verify_tx_proofs,
batch_prover_url: block_producer.batch_prover_url,
block_prover_url: block_producer.block_prover_url,
};

let rpc = RpcConfig {
Expand Down Expand Up @@ -103,6 +107,7 @@ mod tests {
endpoint = "http://127.0.0.1:8080"
verify_tx_proofs = true
batch_prover_url = "http://127.0.0.1:8081"
block_prover_url = "http://127.0.0.1:8082"
[rpc]
endpoint = "http://127.0.0.1:8080"
Expand All @@ -124,6 +129,7 @@ mod tests {
endpoint: Url::parse("http://127.0.0.1:8080").unwrap(),
verify_tx_proofs: true,
batch_prover_url: Some(Url::parse("http://127.0.0.1:8081").unwrap()),
block_prover_url: Some(Url::parse("http://127.0.0.1:8082").unwrap()),
},
rpc: NormalizedRpcConfig {
endpoint: Url::parse("http://127.0.0.1:8080").unwrap(),
Expand Down
4 changes: 3 additions & 1 deletion config/miden-node.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ endpoint = "http://127.0.0.1:48046"
# transaction queue.
verify_tx_proofs = true
# address of the remote batch prover service
batch_prover_url = "http://127.0.0.1:8082/"
batch_prover_url = "http://127.0.0.1:8081/"
# address of the remote block prover service
block_prover_url = "http://127.0.0.1:8082/"

[rpc]
# port defined as: sum(ord(c)**p for (p, c) in enumerate('miden-rpc', 1)) % 2**16
Expand Down
1 change: 1 addition & 0 deletions crates/block-producer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ miden-objects = { workspace = true }
miden-processor = { workspace = true }
miden-proving-service-client = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "santiagopittella-clonable-batch-provers", features = [
"batch-prover",
"block-prover",
] }
miden-tx = { workspace = true }
miden-tx-batch-prover = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "santiagopittella-clonable-batch-provers" }
Expand Down
58 changes: 50 additions & 8 deletions crates/block-producer/src/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use miden_objects::{
note::NoteHeader,
MIN_PROOF_SECURITY_LEVEL,
};
use miden_proving_service_client::proving_service::block_prover::RemoteBlockProver;
use rand::Rng;
use tokio::time::Duration;
use tracing::{instrument, Span};
use tracing::{info, instrument, Span};
use url::Url;

use crate::{
errors::BuildBlockError, mempool::SharedMempool, store::StoreClient, COMPONENT,
Expand All @@ -34,17 +36,25 @@ pub struct BlockBuilder {
pub store: StoreClient,

/// The prover used to prove a proposed block into a proven block.
pub block_prover: LocalBlockProver,
pub block_prover: BlockProver,
}

impl BlockBuilder {
pub fn new(store: StoreClient) -> Self {
/// Creates a new [`BlockBuilder`] with the given [`StoreClient`] and optional block prover URL.
///
/// If the block prover URL is not set, the block builder will use the local block prover.
pub fn new(store: StoreClient, block_prover_url: Option<Url>) -> Self {
let block_prover = match block_prover_url {
Some(url) => BlockProver::new_remote(url),
None => BlockProver::new_local(MIN_PROOF_SECURITY_LEVEL),
};

Self {
block_interval: SERVER_BLOCK_FREQUENCY,
// Note: The range cannot be empty.
simulated_proof_time: Duration::ZERO..Duration::from_millis(1),
failure_rate: 0.0,
block_prover: LocalBlockProver::new(MIN_PROOF_SECURITY_LEVEL),
block_prover,
store,
}
}
Expand Down Expand Up @@ -190,10 +200,7 @@ impl BlockBuilder {
&self,
proposed_block: ProposedBlock,
) -> Result<ProvenBlock, BuildBlockError> {
let proven_block = self
.block_prover
.prove(proposed_block)
.map_err(BuildBlockError::ProveBlockFailed)?;
let proven_block = self.block_prover.prove(proposed_block).await?;

self.simulate_proving().await;

Expand Down Expand Up @@ -352,3 +359,38 @@ impl TelemetryInjectorExt for ProvenBlock {
span.set_attribute("block.commitments.transaction", header.tx_hash());
}
}

// BLOCK PROVER
// ================================================================================================

pub enum BlockProver {
Local(LocalBlockProver),
Remote(RemoteBlockProver),
}

impl BlockProver {
pub fn new_local(security_level: u32) -> Self {
info!(target: COMPONENT, "Using local block prover");
Self::Local(LocalBlockProver::new(security_level))
}

pub fn new_remote(endpoint: impl Into<String>) -> Self {
info!(target: COMPONENT, "Using remote block prover");
Self::Remote(RemoteBlockProver::new(endpoint))
}

#[instrument(target = COMPONENT, skip_all, err)]
pub async fn prove(
&self,
proposed_block: ProposedBlock,
) -> Result<ProvenBlock, BuildBlockError> {
match self {
Self::Local(prover) => {
prover.prove(proposed_block).map_err(BuildBlockError::ProveBlockFailed)
},
Self::Remote(prover) => {
prover.prove(proposed_block).await.map_err(BuildBlockError::RemoteProverError)
},
}
}
}
17 changes: 16 additions & 1 deletion crates/block-producer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ pub struct BlockProducerConfig {
pub verify_tx_proofs: bool,

/// URL of the remote batch prover.
///
/// If not set, the block producer will use the local batch prover.
pub batch_prover_url: Option<Url>,

/// URL of the remote block prover.
///
/// If not set, the block producer will use the local block prover.
pub block_prover_url: Option<Url>,
}

impl Display for BlockProducerConfig {
Expand All @@ -38,7 +45,14 @@ impl Display for BlockProducerConfig {
.as_ref()
.map_or_else(|| "None".to_string(), ToString::to_string);

write!(f, ", batch_prover_url: \"{batch_prover_url}\" }}")
write!(f, ", batch_prover_url: \"{batch_prover_url}\" }}")?;

let block_prover_url = self
.block_prover_url
.as_ref()
.map_or_else(|| "None".to_string(), ToString::to_string);

write!(f, ", block_prover_url: \"{block_prover_url}\" }}")
}
}

Expand All @@ -53,6 +67,7 @@ impl Default for BlockProducerConfig {
.unwrap(),
verify_tx_proofs: true,
batch_prover_url: None,
block_prover_url: None,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/block-producer/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ pub enum BuildBlockError {
/// responses.
#[error("nothing actually went wrong, failure was injected on purpose")]
InjectedFailure,
#[error("failed to prove block with remote prover")]
RemoteProverError(#[source] RemoteProverError),
}

// Store errors
Expand Down
2 changes: 1 addition & 1 deletion crates/block-producer/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl BlockProducer {

Ok(Self {
batch_builder: BatchBuilder::new(config.batch_prover_url),
block_builder: BlockBuilder::new(store.clone()),
block_builder: BlockBuilder::new(store.clone(), config.block_prover_url),
batch_budget: BatchBudget::default(),
block_budget: BlockBudget::default(),
state_retention: SERVER_MEMPOOL_STATE_RETENTION,
Expand Down

0 comments on commit e28dcce

Please sign in to comment.