-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move most functionality to a lib.rs function
- Loading branch information
1 parent
e21503b
commit cff1e5f
Showing
2 changed files
with
96 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OwnedTable<DoryScalar>, Box<dyn core::error::Error>> { | ||
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<DynamicDoryCommitment> = | ||
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::<ProverResponse>(&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<DynamicDoryEvaluationProof> = | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<dyn core::error::Error>> { | ||
// 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<DoryCommitment> = | ||
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::<ProverResponse>(&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<DoryEvaluationProof> = | ||
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(()) | ||
} |