Skip to content

Commit

Permalink
fix make lint
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Aug 28, 2023
1 parent 9631ad3 commit eb990c5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
41 changes: 25 additions & 16 deletions common/libzkp/impl/src/batch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::utils::{c_char_to_str, c_char_to_vec, string_to_c_char, vec_to_c_char, OUTPUT_DIR};
use crate::types::{CheckChunkProofsResponse, ProofResult};
use crate::{
types::{CheckChunkProofsResponse, ProofResult},
utils::{c_char_to_str, c_char_to_vec, string_to_c_char, vec_to_c_char, OUTPUT_DIR},
};
use libc::c_char;
use prover::{
aggregator::{Prover, Verifier},
Expand Down Expand Up @@ -59,16 +61,20 @@ pub unsafe extern "C" fn check_chunk_proofs(chunk_proofs: *const c_char) -> *con
let check_result: Result<bool, String> = panic::catch_unwind(|| {
let chunk_proofs = c_char_to_vec(chunk_proofs);
let chunk_proofs = serde_json::from_slice::<Vec<ChunkProof>>(&chunk_proofs)
.map_err(|e| format!("Failed to deserialize chunk proofs: {:?}", e))?;
.map_err(|e| format!("Failed to deserialize chunk proofs: {e:?}"))?;

if chunk_proofs.is_empty() {
return Err("Provided chunk proofs are empty.".to_string());
}

PROVER.get()
.map_err(|_| "Failed to get reference to PROVER.".to_string())?
.check_chunk_proofs(&chunk_proofs)
.map_err(|e| format!("Error checking chunk proofs: {:?}", e))
}).unwrap_or_else(|err| Err(format!("Unwind error: {:?}", err)));
let prover_ref = PROVER
.get()
.ok_or_else(|| "Failed to get reference to PROVER.".to_string())?;

let valid = prover_ref.check_chunk_proofs(&chunk_proofs);
Ok(valid)
})
.unwrap_or_else(|e| Err(format!("Unwind error: {e:?}")));

let r = match check_result {
Ok(valid) => CheckChunkProofsResponse {
Expand All @@ -95,25 +101,28 @@ pub unsafe extern "C" fn gen_batch_proof(
let chunk_proofs = c_char_to_vec(chunk_proofs);

let chunk_hashes = serde_json::from_slice::<Vec<ChunkHash>>(&chunk_hashes)
.map_err(|e| format!("Failed to deserialize chunk hashes: {:?}", e))?;
.map_err(|e| format!("Failed to deserialize chunk hashes: {e:?}"))?;
let chunk_proofs = serde_json::from_slice::<Vec<ChunkProof>>(&chunk_proofs)
.map_err(|e| format!("Failed to deserialize chunk proofs: {:?}", e))?;
.map_err(|e| format!("Failed to deserialize chunk proofs: {e:?}"))?;

if chunk_hashes.len() != chunk_proofs.len() {
return Err("Chunk hashes and chunk proofs lengths mismatch.".to_string());
}

let chunk_hashes_proofs = chunk_hashes.into_iter().zip(chunk_proofs.into_iter()).collect();
let chunk_hashes_proofs = chunk_hashes
.into_iter()
.zip(chunk_proofs.into_iter())
.collect();

let proof = PROVER
.get_mut()
.map_err(|_| "Failed to get mutable reference to PROVER.".to_string())?
.ok_or_else(|| "Failed to get mutable reference to PROVER.".to_string())?
.gen_agg_evm_proof(chunk_hashes_proofs, None, OUTPUT_DIR.as_deref())
.map_err(|e| format!("Proof generation failed: {:?}", e))?;
.map_err(|e| format!("Proof generation failed: {e:?}"))?;

serde_json::to_vec(&proof)
.map_err(|e| format!("Failed to serialize the proof: {:?}", e))
}).unwrap_or_else(|err| Err(format!("Unwind error: {:?}", err)));
serde_json::to_vec(&proof).map_err(|e| format!("Failed to serialize the proof: {e:?}"))
})
.unwrap_or_else(|e| Err(format!("Unwind error: {e:?}")));

let r = match proof_result {
Ok(proof_bytes) => ProofResult {
Expand Down
18 changes: 10 additions & 8 deletions common/libzkp/impl/src/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::utils::{c_char_to_str, c_char_to_vec, string_to_c_char, vec_to_c_char, OUTPUT_DIR};
use crate::types::ProofResult;
use crate::{
types::ProofResult,
utils::{c_char_to_str, c_char_to_vec, string_to_c_char, vec_to_c_char, OUTPUT_DIR},
};
use libc::c_char;
use prover::{
utils::init_env_and_log,
Expand Down Expand Up @@ -59,17 +61,17 @@ pub unsafe extern "C" fn gen_chunk_proof(block_traces: *const c_char) -> *const
let proof_result: Result<Vec<u8>, String> = panic::catch_unwind(|| {
let block_traces = c_char_to_vec(block_traces);
let block_traces = serde_json::from_slice::<Vec<BlockTrace>>(&block_traces)
.map_err(|e| format!("Failed to deserialize block traces: {:?}", e))?;
.map_err(|e| format!("Failed to deserialize block traces: {e:?}"))?;

let proof = PROVER
.get_mut()
.map_err(|_| "Failed to get mutable reference to PROVER.".to_string())?
.ok_or_else(|| "Failed to get mutable reference to PROVER.".to_string())?
.gen_chunk_proof(block_traces, None, OUTPUT_DIR.as_deref())
.map_err(|e| format!("Proof generation failed: {:?}", e))?;
.map_err(|e| format!("Proof generation failed: {e:?}"))?;

serde_json::to_vec(&proof)
.map_err(|e| format!("Failed to serialize the proof: {:?}", e))
}).unwrap_or_else(|err| Err(format!("Unwind error: {:?}", err)));
serde_json::to_vec(&proof).map_err(|e| format!("Failed to serialize the proof: {e:?}"))
})
.unwrap_or_else(|e| Err(format!("Unwind error: {e:?}")));

let r = match proof_result {
Ok(proof_bytes) => ProofResult {
Expand Down
2 changes: 1 addition & 1 deletion common/libzkp/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

mod batch;
mod chunk;
mod utils;
mod types;
mod utils;
8 changes: 4 additions & 4 deletions common/libzkp/impl/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use serde::{Deserialize, Serialize};
// `error` provides additional details in case the check failed.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CheckChunkProofsResponse {
ok: bool,
pub ok: bool,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<String>,
pub error: Option<String>,
}

// Encapsulates the result from generating a proof.
Expand All @@ -16,7 +16,7 @@ pub struct CheckChunkProofsResponse {
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ProofResult {
#[serde(skip_serializing_if = "Option::is_none")]
message: Option<Vec<u8>>,
pub message: Option<Vec<u8>>,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<String>,
pub error: Option<String>,
}
5 changes: 4 additions & 1 deletion prover/core/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ func (p *ProverCore) ProveChunk(taskID string, traces []*types.BlockTrace) (*mes
if err != nil {
return nil, err
}
proofByt := p.proveChunk(tracesByt)
proofByt, err := p.proveChunk(tracesByt)
if err != nil {
return nil, err
}

err = p.mayDumpProof(taskID, proofByt)
if err != nil {
Expand Down

0 comments on commit eb990c5

Please sign in to comment.