Skip to content

Commit

Permalink
fix: review and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
atanmarko committed Aug 22, 2024
1 parent f782c06 commit 9087c75
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
46 changes: 19 additions & 27 deletions zero_bin/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,14 @@ pub async fn prove(
let block_number = proof.b_height;

// Write latest generated proof to disk if proof_output_dir is provided
let return_proof: Option<GeneratedBlockProof> = if proof_output_dir.is_some() {
write_proof(proof_output_dir, &proof).await?;
None
} else {
Some(proof.clone())
};
// or alternatively return proof as function result.
let return_proof: Option<GeneratedBlockProof> =
if let Some(output_dir) = proof_output_dir {
write_proof_to_dir(output_dir, &proof).await?;
None
} else {
Some(proof.clone())
};

if tx.send(proof).is_err() {
anyhow::bail!("Failed to send proof");
Expand All @@ -183,28 +185,18 @@ pub async fn prove(
results.try_collect().await
}

/// Write the proof to the disk (if `output_dir` is provided) or stdout.
pub(crate) async fn write_proof(
output_dir: Option<PathBuf>,
proof: &GeneratedBlockProof,
) -> Result<()> {
/// Write the proof to the `output_dir` directory.
async fn write_proof_to_dir(output_dir: PathBuf, proof: &GeneratedBlockProof) -> Result<()> {
let proof_serialized = serde_json::to_vec(proof)?;
let block_proof_file_path =
output_dir.map(|path| generate_block_proof_file_name(&path.to_str(), proof.b_height));
match block_proof_file_path {
Some(p) => {
if let Some(parent) = p.parent() {
tokio::fs::create_dir_all(parent).await?;
}

let mut f = tokio::fs::File::create(p).await?;
f.write_all(&proof_serialized)
.await
.context("Failed to write proof to disk")
}
None => tokio::io::stdout()
.write_all(&proof_serialized)
.await
.context("Failed to write proof to stdout"),
generate_block_proof_file_name(&output_dir.to_str(), proof.b_height);

if let Some(parent) = block_proof_file_path.parent() {
tokio::fs::create_dir_all(parent).await?;
}

let mut f = tokio::fs::File::create(block_proof_file_path).await?;
f.write_all(&proof_serialized)
.await
.context("Failed to write proof to disk")
}
2 changes: 1 addition & 1 deletion zero_bin/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where
.chunks(2)
.into_iter()
.map(|mut chunk| {
// We convert to tuple of (current, Option previous block)
// We convert to tuple of (current block, optional previous block)
let first = chunk
.next()
.expect("must be valid according to itertools::Iterator::chunks definition");
Expand Down

0 comments on commit 9087c75

Please sign in to comment.