Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add formatter in CI #33

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ jobs:
- name: Build
run: cargo build --verbose

- name: Cargo fmt
- run: cargo fmt --check
HermanObst marked this conversation as resolved.
Show resolved Hide resolved

- name: Lint with Clippy
run: cargo clippy -- -D warnings

Expand Down
16 changes: 10 additions & 6 deletions integration-tests/evm-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ use stark_evm_adapter::{
annotation_parser::{split_fri_merkle_statements, SplitProofs},
ContractFunctionCall,
};
use std::{
convert::TryFrom, fs, path::PathBuf, str::FromStr, sync::Arc
};
use std::{convert::TryFrom, fs, path::PathBuf, str::FromStr, sync::Arc};

/// Verify a proof file against Ethereum SHARP contracts.
///
///
/// See lib.rs for more details
pub async fn verify_annotated_proof_with_l1(annotated_proof_file: &PathBuf, mainnet_rpc: String) -> Result<(), Box<dyn std::error::Error>> {
pub async fn verify_annotated_proof_with_l1(
annotated_proof_file: &PathBuf,
mainnet_rpc: String,
) -> Result<(), Box<dyn std::error::Error>> {
let proof_str = fs::read_to_string(annotated_proof_file)?;
let annotated_proof: AnnotatedProof = serde_json::from_str(proof_str.as_str())?;

Expand All @@ -28,7 +29,10 @@ pub async fn verify_annotated_proof_with_l1(annotated_proof_file: &PathBuf, main
verify_split_proofs_with_l1(&split_proofs, mainnet_rpc).await
}

pub async fn verify_split_proofs_with_l1(split_proofs: &SplitProofs, mainnet_rpc: String) -> Result<(), Box<dyn std::error::Error>> {
pub async fn verify_split_proofs_with_l1(
split_proofs: &SplitProofs,
mainnet_rpc: String,
) -> Result<(), Box<dyn std::error::Error>> {
let anvil = Some(Anvil::new().fork(mainnet_rpc).spawn());
let endpoint = anvil.as_ref().unwrap().endpoint();
let provider = Provider::<Http>::try_from(endpoint.as_str())?;
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/evm-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use std::path::PathBuf;

/// Binary borrowed from `stark-evm-adapter` used to test a split proof against in-production
/// SHARP provers on Ethereum.
///
///
/// Source: https://github.com/notlesh/stark-evm-adapter/blob/main/examples/verify_stone_proof.rs
///
///
/// Input file ("split proof") should be a proof JSON file generated from `cpu_air_prover` along
/// with an `annotations` field (array) and `extra_annotations` field (array) which come from,
/// respectively, `--annotations_file` and `--extra_output_file` from `cpu_air_verifier`.
///
///
/// This also requires `anvil` from `forge`
/// [to be installed](https://book.getfoundry.sh/getting-started/installation).
///
///
/// A suitable input file can be borrowed from
/// https://github.com/notlesh/stark-evm-adapter/blob/main/tests/fixtures/annotated_proof.json

Expand Down
5 changes: 3 additions & 2 deletions integration-tests/tests/integration/test_starknet_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ mod tests {
assert!(split_proofs.fri_merkle_statements.len() > 0);

let private_url = "<redacted>";
evm_adapter::verify_split_proofs_with_l1(&split_proofs, private_url.into()).await.unwrap();

evm_adapter::verify_split_proofs_with_l1(&split_proofs, private_url.into())
.await
.unwrap();
}

#[ignore = "this test takes ~5 minutes to run"]
Expand Down
34 changes: 20 additions & 14 deletions madara-prover-rpc-server/src/evm_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::path::Path;

use madara_prover_common::toolkit::read_json_from_file;
use stark_evm_adapter::{
annotation_parser::{split_fri_merkle_statements, SplitProofs},
annotated_proof::AnnotatedProof,
annotation_parser::{split_fri_merkle_statements, SplitProofs},
};
use std::io::BufRead;
use thiserror::Error;
Expand All @@ -13,31 +13,30 @@ pub enum SplitProverError {
#[error("I/O Error")]
Io(#[from] std::io::Error),
#[error("Error involving split proof")]
ProofParseError(#[from] stark_evm_adapter::errors::ParseError)
ProofParseError(#[from] stark_evm_adapter::errors::ParseError),
}

/// Uses stark-evm-adapter to split the proof.
/// Uses stark-evm-adapter to split the proof.
pub fn split_proof(
proof_file: &Path,
annotations_file: &Path,
extra_annotations_file: &Path
extra_annotations_file: &Path,
) -> Result<SplitProofs, SplitProverError> {
// 'proof_file' is not expected to have an annotations or an extra_annotations field.
// but this will cause an error if we try to parse it as an AnnotatedProof without these
// fields.
//
//
// since these values are given as separate files, we will with the proof as a JSON object
// and add the 'annotations' and 'extra_annotations' fields manually, as the `stark-evm-adapter`
// binary does.
let mut proof_json: serde_json::Value = read_json_from_file(proof_file)?;
proof_json["annotations"] = load_annotations_file(annotations_file)?.into();
proof_json["extra_annotations"] = load_annotations_file(extra_annotations_file)?.into();

let annotated_proof: AnnotatedProof = serde_json::from_value(proof_json)
.unwrap(); // TODO
let annotated_proof: AnnotatedProof = serde_json::from_value(proof_json).unwrap(); // TODO

let split_proofs: SplitProofs = split_fri_merkle_statements(annotated_proof)?;

Ok(split_proofs)
}

Expand All @@ -56,17 +55,24 @@ pub fn load_annotations_file(file: &Path) -> std::io::Result<Vec<String>> {
mod tests {
#[test]
fn split_proof_works_with_empty_bootloader_proof() {
let annotated_proof_file = test_cases::get_test_case_file_path("bootloader/empty_bootloader_proof/annotated_proof.json");
let annotations_file = test_cases::get_test_case_file_path("bootloader/empty_bootloader_proof/annotations.txt");
let extra_annotations_file = test_cases::get_test_case_file_path("bootloader/empty_bootloader_proof/extra_annotations.txt");
let annotated_proof_file = test_cases::get_test_case_file_path(
"bootloader/empty_bootloader_proof/annotated_proof.json",
);
let annotations_file = test_cases::get_test_case_file_path(
"bootloader/empty_bootloader_proof/annotations.txt",
);
let extra_annotations_file = test_cases::get_test_case_file_path(
"bootloader/empty_bootloader_proof/extra_annotations.txt",
);
let split_proofs = crate::evm_adapter::split_proof(
&annotated_proof_file,
&annotations_file,
&extra_annotations_file
).unwrap();
&extra_annotations_file,
)
.unwrap();

assert!(split_proofs.merkle_statements.len() > 0);
assert!(split_proofs.fri_merkle_statements.len() > 0);
assert!(split_proofs.main_proof.proof.len() > 0);
}
}
}
23 changes: 16 additions & 7 deletions madara-prover-rpc-server/src/services/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::cairo::ExecutionArtifacts;
use crate::evm_adapter;
use madara_prover_common::models::{Proof, ProofAnnotations, ProverConfig, ProverParameters, ProverWorkingDirectory};
use madara_prover_common::models::{
Proof, ProofAnnotations, ProverConfig, ProverParameters, ProverWorkingDirectory,
};
use stone_prover::error::{ProverError, VerifierError};
use stone_prover::fri::generate_prover_parameters;
use stone_prover::prover::{run_prover_async, run_verifier_async};
Expand Down Expand Up @@ -65,7 +67,7 @@ pub fn format_verifier_error(e: VerifierError) -> Status {
)),
VerifierError::IoError(io_error) => {
Status::internal(format!("Could not run the verifier: {}", io_error))
},
}
}
}

Expand All @@ -88,16 +90,23 @@ pub fn get_prover_parameters(
/// Calls `cpu_air_verifier` to verify the proof and produce annotations, then uses
/// `stark-evm-adapter` to split the proof. The given Proof will then be modified to contain
/// this additional split-proof.
pub async fn verify_and_annotate_proof(proof: &mut Proof, working_dir: &mut ProverWorkingDirectory) -> Result<(), Status> {
pub async fn verify_and_annotate_proof(
proof: &mut Proof,
working_dir: &mut ProverWorkingDirectory,
) -> Result<(), Status> {
let _ = // TODO: return type seems worthless here
call_verifier(working_dir)
.await
.map_err(format_verifier_error)?;

let proof_file_path = working_dir.proof_file.as_path();
let annotations_file_path = working_dir.annotations_file.clone()
let annotations_file_path = working_dir
.annotations_file
.clone()
.ok_or(Status::internal("Expected annotations_file_path"))?;
let extra_annotations_file_path = working_dir.extra_annotations_file.clone()
let extra_annotations_file_path = working_dir
.extra_annotations_file
.clone()
.ok_or(Status::internal("Expected extra_annotations_file_path"))?;

let split_proof = evm_adapter::split_proof(
Expand All @@ -106,8 +115,8 @@ pub async fn verify_and_annotate_proof(proof: &mut Proof, working_dir: &mut Prov
extra_annotations_file_path.as_path(),
)
.map_err(|_| Status::internal("Unable to generate split proof"))?;

proof.split_proofs = Some(split_proof);

Ok(())
}
}
8 changes: 5 additions & 3 deletions madara-prover-rpc-server/src/services/starknet_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use madara_prover_common::models::{Proof, ProverConfig, ProverWorkingDirectory};
use stone_prover::error::ProverError;

use crate::cairo::{extract_execution_artifacts, ExecutionArtifacts, ExecutionError};
use crate::services::common::{call_prover, format_prover_error, get_prover_parameters, verify_and_annotate_proof};
use crate::services::common::{
call_prover, format_prover_error, get_prover_parameters, verify_and_annotate_proof,
};
use crate::services::starknet_prover::starknet_prover_proto::starknet_prover_server::StarknetProver;
use crate::services::starknet_prover::starknet_prover_proto::{
StarknetExecutionRequest, StarknetProverResponse,
Expand Down Expand Up @@ -212,8 +214,8 @@ impl StarknetProver for StarknetProverService {

let (mut proof, mut working_dir) =
call_prover(&execution_artifacts, &prover_config, &prover_parameters)
.await
.map_err(format_prover_error)?;
.await
.map_err(format_prover_error)?;

// If split proof was requested, build it
if split_proof {
Expand Down
1 change: 0 additions & 1 deletion stone-prover/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ fn make_docker_build_command(repo_dir: &Path, image_name: &str) -> String {
docker_build_command.push_str(" --build-arg CMAKE_ARGS=-DNO_AVX=1");
}


// Check if a cache image exists. Used by the CI/CD pipeline.
if let Ok(cache_image) = std::env::var("STONE_PROVER_DOCKER_CACHE") {
docker_build_command.push_str(&format!(" --cache-from {cache_image}"));
Expand Down
2 changes: 1 addition & 1 deletion stone-prover/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ pub enum VerifierError {
IoError(#[from] std::io::Error),
#[error("verifier run failed")]
CommandError(std::process::Output),
}
}
2 changes: 1 addition & 1 deletion stone-prover/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod error;
pub mod prover;
pub mod fri;
pub mod prover;
12 changes: 4 additions & 8 deletions stone-prover/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::path::Path;

use tempfile::tempdir;

use madara_prover_common::models::{Proof, ProofAnnotations, ProverConfig, ProverParameters, ProverWorkingDirectory, PublicInput};
use madara_prover_common::models::{
Proof, ProofAnnotations, ProverConfig, ProverParameters, ProverWorkingDirectory, PublicInput,
};
use madara_prover_common::toolkit::{read_json_from_file, write_json_to_file};

use crate::error::{ProverError, VerifierError};
Expand Down Expand Up @@ -272,14 +274,8 @@ pub async fn run_verifier_async(
annotation_file: &Path,
extra_output_file: &Path,
) -> Result<ProofAnnotations, VerifierError> {

// Call the verifier
run_verifier_from_command_line_async(
in_file,
annotation_file,
extra_output_file,
)
.await?;
run_verifier_from_command_line_async(in_file, annotation_file, extra_output_file).await?;

let annotations = ProofAnnotations {
annotation_file: annotation_file.into(),
Expand Down