From cff1e5f114dd51354143bc851cf5720c1bc35e1a Mon Sep 17 00:00:00 2001 From: Trevor Lovell Date: Sat, 26 Oct 2024 12:19:04 -0600 Subject: [PATCH] feat: move most functionality to a lib.rs function --- src/lib.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 93 ++--------------------------------------------------- 2 files changed, 96 insertions(+), 90 deletions(-) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..fc2e9b9 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,93 @@ +mod auth; +mod substrate; +mod sxt_chain_runtime; + +use std::collections::HashMap; +use std::path::Path; + +use dotenv::dotenv; +use proof_of_sql::base::database::{OwnedTable, TableRef}; +use proof_of_sql::proof_primitive::dory::{ + DoryScalar, + DynamicDoryCommitment, + DynamicDoryEvaluationProof, + VerifierSetup, +}; +use proof_of_sql::sql::parse::QueryExpr; +use proof_of_sql::sql::proof::VerifiableQueryResult; +use prover::{ProverContextRange, ProverQuery, ProverResponse}; +use reqwest::Client; + +mod prover { + tonic::include_proto!("sxt.core"); +} + +pub async fn query_and_verify( + sql: &str, + table_id: TableRef, +) -> Result, Box> { + dotenv().ok(); + let prover_root_url = std::env::var("PROVER_ROOT_URL")?; + let substrate_node_url = std::env::var("SUBSTRATE_NODE_URL")?; + //let verifier_setup = VerifierSetup::from(&public_parameters); + let verifier_setup = VerifierSetup::load_from_file(Path::new("verifier_setup.bin"))?; + // Accessor setup + let accessor = + substrate::query_commitments(&[table_id.resource_id()], &substrate_node_url).await?; + // Parse the SQL query + let query: QueryExpr = + QueryExpr::try_new(sql.parse()?, "ETHEREUM".parse()?, &accessor)?; + let proof_plan = query.proof_expr(); + let serialized_proof_plan = flexbuffers::to_vec(proof_plan)?; + // Send the query to the prover + let mut query_context = HashMap::new(); + let table_ref = TableRef::new("ETHEREUM.CONTRACT_EVT_APPROVALFORALL".parse()?); + let commitment_range = accessor[&table_ref].range(); + query_context.insert( + table_id.to_string().to_uppercase(), + ProverContextRange { + start: commitment_range.start as u64, + ends: vec![commitment_range.end as u64], + }, + ); + let prover_query = ProverQuery { + proof_plan: serialized_proof_plan, + query_context, + commitment_scheme: 1, + }; + let client = Client::new(); + let apikey = std::env::var("SXT_API_KEY")?; + // Usually it is the same as the prover root URL + let auth_root_url = std::env::var("AUTH_ROOT_URL")?; + let access_token = auth::get_access_token(&apikey, &auth_root_url).await?; + let response = client + .post(format!("https://{}/v1/prove", prover_root_url)) + .bearer_auth(&access_token) + .json(&prover_query) + .send() + .await? + .error_for_status()?; + let serialized_prover_response = response.text().await?; + let prover_response = serde_json::from_str::(&serialized_prover_response) + .map_err(|_e| { + format!( + "Failed to parse prover response: {}", + &serialized_prover_response + ) + })?; + let stringified_verifiable_result = prover_response.verifiable_result.clone(); + let verifiable_result: VerifiableQueryResult = + flexbuffers::from_slice(&stringified_verifiable_result)?; + // Verify the proof + let proof = verifiable_result.proof.unwrap(); + let serialized_result = verifiable_result.provable_result.unwrap(); + let owned_table_result = proof + .verify( + query.proof_expr(), + &accessor, + &serialized_result, + &&verifier_setup, + )? + .table; + Ok(owned_table_result) +} diff --git a/src/main.rs b/src/main.rs index a083d63..33248fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,96 +1,9 @@ -mod auth; -mod substrate; -mod sxt_chain_runtime; - -use dotenv::dotenv; -use proof_of_sql::{ - base::database::TableRef, - proof_primitive::dory::{ - DoryCommitment, DoryEvaluationProof, DoryVerifierPublicSetup, VerifierSetup, - }, - sql::{parse::QueryExpr, proof::VerifiableQueryResult}, -}; -use prover::{ProverContextRange, ProverQuery, ProverResponse}; -use reqwest::Client; -use std::{collections::HashMap, path::Path}; - -mod prover { - tonic::include_proto!("sxt.core"); -} - #[tokio::main] async fn main() -> Result<(), Box> { // Load environment variables - dotenv().ok(); - let sql = "SELECT * FROM ETHEREUM.CONTRACT_EVT_APPROVALFORALL;"; - let prover_root_url = std::env::var("PROVER_ROOT_URL")?; - let substrate_node_url = std::env::var("SUBSTRATE_NODE_URL")?; - // Dory setup - let sigma = 12; - //let verifier_setup = VerifierSetup::from(&public_parameters); - let verifier_setup = VerifierSetup::load_from_file(Path::new("verifier_setup.bin"))?; - let dory_verifier_setup = DoryVerifierPublicSetup::new(&verifier_setup, sigma); - // Accessor setup - let accessor = substrate::query_commitments( - &["ETHEREUM.CONTRACT_EVT_APPROVALFORALL".parse()?], - &substrate_node_url, - ) - .await?; - // Parse the SQL query - let query: QueryExpr = - QueryExpr::try_new(sql.parse()?, "ETHEREUM".parse()?, &accessor)?; - let proof_plan = query.proof_expr(); - let serialized_proof_plan = flexbuffers::to_vec(proof_plan)?; - // Send the query to the prover - let mut query_context = HashMap::new(); - let table_ref = TableRef::new("ETHEREUM.CONTRACT_EVT_APPROVALFORALL".parse()?); - let commitment_range = accessor[&table_ref].range(); - query_context.insert( - "ETHEREUM.CONTRACT_EVT_APPROVALFORALL".to_string(), - ProverContextRange { - start: commitment_range.start as u64, - ends: vec![commitment_range.end as u64], - }, - ); - let prover_query = ProverQuery { - proof_plan: serialized_proof_plan, - query_context, - commitment_scheme: 1, - }; - let client = Client::new(); - let apikey = std::env::var("SXT_API_KEY")?; - // Usually it is the same as the prover root URL - let auth_root_url = std::env::var("AUTH_ROOT_URL")?; - let access_token = auth::get_access_token(&apikey, &auth_root_url).await?; - let response = client - .post(format!("https://{}/v1/prove", prover_root_url)) - .bearer_auth(&access_token) - .json(&prover_query) - .send() - .await? - .error_for_status()?; - let serialized_prover_response = response.text().await?; - let prover_response = serde_json::from_str::(&serialized_prover_response) - .map_err(|_e| { - format!( - "Failed to parse prover response: {}", - &serialized_prover_response - ) - })?; - let stringified_verifiable_result = prover_response.verifiable_result.clone(); - let verifiable_result: VerifiableQueryResult = - flexbuffers::from_slice(&stringified_verifiable_result)?; - // Verify the proof - let proof = verifiable_result.proof.unwrap(); - let serialized_result = verifiable_result.provable_result.unwrap(); - let owned_table_result = proof - .verify( - query.proof_expr(), - &accessor, - &serialized_result, - &dory_verifier_setup, - )? - .table; + let table = "ETHEREUM.CONTRACT_EVT_APPROVALFORALL"; + let sql = format!("SELECT * FROM {table};"); + let owned_table_result = sxt_proof_of_sql_sdk::query_and_verify(&sql, table.parse()?).await?; println!("Query result: {:?}", owned_table_result); Ok(()) }