Skip to content

Commit

Permalink
Refactor error handling to remove unwrap() calls and prevent panics
Browse files Browse the repository at this point in the history
- Replaced unwrap() calls with ? operator for safer error propagation
- Updated functions to return Result for flexible error handling
- Improved main function to handle potential errors gracefully
- Ensured the program exits cleanly on error instead of panicking
  • Loading branch information
gssakash-SxT committed Oct 30, 2024
1 parent fd1588d commit 8894a39
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 51 deletions.
20 changes: 1 addition & 19 deletions .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,6 @@ jobs:
run: cargo run --example hello_world --no-default-features --features="test"
- name: Run space example
run: cargo run --example space
- name: Run dog breeds example
run: cargo run --example dog_breeds
- name: Run wood types example
run: cargo run --example wood_types
- name: Run dinosaurs example
run: cargo run --example dinosaurs
- name: Run books example
run: cargo run --example books
- name: Run brands example
run: cargo run --example brands
- name: Run avocado-prices example
run: cargo run --example avocado-prices
- name: Run plastics example
run: cargo run --example plastics
- name: Run sushi example
run: cargo run --example sushi
- name: Run tech_gadget_prices example
run: cargo run --example tech_gadget_prices
- name: Run posql_db example (With Blitzar)
run: bash crates/proof-of-sql/examples/posql_db/run_example.sh
- name: Run posql_db example (Without Blitzar)
Expand Down Expand Up @@ -245,4 +227,4 @@ jobs:
- name: Install solhint
run: npm install -g solhint
- name: Run tests
run: solhint -c 'crates/proof-of-sql/.solhint.json' 'crates/proof-of-sql/**/*.sol' -w 0
run: solhint -c 'crates/proof-of-sql/.solhint.json' 'crates/proof-of-sql/**/*.sol' -w 0
8 changes: 8 additions & 0 deletions crates/proof-of-sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ required-features = [ "arrow" ]
name = "vehicles"
required-features = [ "arrow" ]

[[example]]
name = "countries"
required-features = [ "arrow" ]

[[example]]
name = "rockets"
required-features = [ "arrow" ]

[[bench]]
name = "posql_benches"
harness = false
Expand Down
66 changes: 34 additions & 32 deletions crates/proof-of-sql/examples/tech_gadget_prices/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Example to use Proof of SQL with a tech gadget prices dataset.
//! To run, use `cargo run --example tech_gadget_prices`.
//! This is a non-interactive example of using Proof of SQL with a `tech_gadget_prices` dataset.
//! To run this, use cargo run --release --example `tech_gadget_prices`.
//!
//! NOTE: If this doesn't work because you do not have the appropriate GPU drivers installed,
//! you can run cargo run --release --example `tech_gadget_prices` --no-default-features --features="arrow cpu-perf" instead. It will be slower for proof generation.

use arrow::datatypes::SchemaRef;
use arrow_csv::{infer_schema_from_files, ReaderBuilder};
Expand All @@ -12,25 +15,26 @@ use proof_of_sql::{
sql::{parse::QueryExpr, proof::QueryProof},
};
use rand::{rngs::StdRng, SeedableRng};
use std::{fs::File, time::Instant};
use std::{error::Error, fs::File, time::Instant};

const DORY_SETUP_MAX_NU: usize = 8;
const DORY_SEED: [u8; 32] = *b"tech-gadget-prices-dataset-seed!";

fn prove_and_verify_query(
sql: &str,
accessor: &OwnedTableTestAccessor<DynamicDoryEvaluationProof>,
prover_setup: &ProverSetup,
verifier_setup: &VerifierSetup,
) {
) -> Result<(), Box<dyn Error>> {
println!("Parsing the query: {sql}...");
let now = Instant::now();
let query_plan = QueryExpr::<DynamicDoryCommitment>::try_new(
sql.parse().unwrap(),
"tech_gadget_prices".parse().unwrap(),
sql.parse()?,
"tech_gadget_prices".parse()?,
accessor,
)
.unwrap();
)?;
println!("Done in {} ms.", now.elapsed().as_secs_f64() * 1000.);

print!("Generating proof...");
let now = Instant::now();
let (proof, provable_result) = QueryProof::<DynamicDoryEvaluationProof>::new(
Expand All @@ -39,42 +43,39 @@ fn prove_and_verify_query(
&prover_setup,
);
println!("Done in {} ms.", now.elapsed().as_secs_f64() * 1000.);

print!("Verifying proof...");
let now = Instant::now();
let result = proof
.verify(
query_plan.proof_expr(),
accessor,
&provable_result,
&verifier_setup,
)
.unwrap();
let result = proof.verify(
query_plan.proof_expr(),
accessor,
&provable_result,
&verifier_setup,
)?;
println!("Verified in {} ms.", now.elapsed().as_secs_f64() * 1000.);

println!("Query Result:");
println!("{:?}", result.table);
Ok(())
}

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
let mut rng = StdRng::from_seed(DORY_SEED);
let public_parameters = PublicParameters::rand(DORY_SETUP_MAX_NU, &mut rng);
let prover_setup = ProverSetup::from(&public_parameters);
let verifier_setup = VerifierSetup::from(&public_parameters);

let filename = "./tech_gadget_prices/tech_gadget_prices.csv";
let data_batch = ReaderBuilder::new(SchemaRef::new(
infer_schema_from_files(&[filename.to_string()], b',', None, true).unwrap(),
))
.with_header(true)
.build(File::open(filename).unwrap())
.unwrap()
.next()
.unwrap()
.unwrap();
let schema = infer_schema_from_files(&[filename.to_string()], b',', None, true)?;
let data_batch = ReaderBuilder::new(SchemaRef::new(schema))
.with_header(true)
.build(File::open(filename)?)?
.next()
.ok_or("No data found in CSV file")??;

let accessor = OwnedTableTestAccessor::<DynamicDoryEvaluationProof>::new_from_table(
"tech_gadget_prices.prices".parse().unwrap(),
OwnedTable::try_from(data_batch).unwrap(),
"tech_gadget_prices.prices".parse()?,
OwnedTable::try_from(data_batch)?,
0,
&prover_setup,
);
Expand All @@ -84,23 +85,24 @@ fn main() {
&accessor,
&prover_setup,
&verifier_setup,
);
)?;
prove_and_verify_query(
"SELECT Brand, COUNT(*) AS total FROM prices GROUP BY Brand ORDER BY total",
&accessor,
&prover_setup,
&verifier_setup,
);
)?;
prove_and_verify_query(
"SELECT Name, Price FROM prices WHERE Category = 'Smartphone' ORDER BY Price DESC LIMIT 3",
&accessor,
&prover_setup,
&verifier_setup,
);
)?;
prove_and_verify_query(
"SELECT Name, ReleaseYear FROM prices WHERE Price > 500 ORDER BY ReleaseYear DESC",
&accessor,
&prover_setup,
&verifier_setup,
);
)?;
Ok(())
}

0 comments on commit 8894a39

Please sign in to comment.