Skip to content

Commit

Permalink
feat: add PostprocessingLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
iajoiner committed Oct 29, 2024
1 parent c7d5054 commit c27df89
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
13 changes: 12 additions & 1 deletion sdk/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::SxTClient;
use crate::{PostprocessingLevel, SxTClient};
use clap::Parser;

/// Struct to define and parse command-line arguments for Proof of SQL Client.
Expand Down Expand Up @@ -79,6 +79,16 @@ pub struct SdkArgs {
default_value = "verifier_setup.bin"
)]
pub verifier_setup: String,

/// Level of postprocessing allowed. Default is `Cheap`.
#[arg(
long,
value_name = "POSTPROCESSING_LEVEL",
default_value = "PostprocessingLevel::Cheap",
value_enum,
help = "Level of postprocessing allowed, default is `Cheap`"
)]
pub postprocessing_level: PostprocessingLevel,
}

impl From<&SdkArgs> for SxTClient {
Expand All @@ -90,5 +100,6 @@ impl From<&SdkArgs> for SxTClient {
args.sxt_api_key.clone(),
args.verifier_setup.clone(),
)
.with_postprocessing(args.postprocessing_level)
}
}
48 changes: 40 additions & 8 deletions sdk/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use proof_of_sql::{
proof_primitive::dory::{
DoryScalar, DynamicDoryCommitment, DynamicDoryEvaluationProof, VerifierSetup,
},
sql::{parse::QueryExpr, postprocessing::apply_postprocessing_steps, proof::VerifiableQueryResult},
sql::{
parse::QueryExpr,
postprocessing::{apply_postprocessing_steps, OwnedTablePostprocessing},
proof::VerifiableQueryResult,
},
};
use prover::{ProverContextRange, ProverQuery, ProverResponse};
use reqwest::Client;
Expand All @@ -14,6 +18,19 @@ mod prover {
tonic::include_proto!("sxt.core");
}

/// Level of postprocessing allowed
///
/// Some postprocessing steps are expensive so we allow the user to control the level of postprocessing.
#[derive(Debug, Clone, Copy)]
pub enum PostprocessingLevel {
/// No postprocessing allowed
None,
/// Only cheap postprocessing allowed
Cheap,
/// All postprocessing allowed
All,
}

/// Space and Time (SxT) client
#[derive(Debug, Clone)]
pub struct SxTClient {
Expand All @@ -34,6 +51,9 @@ pub struct SxTClient {

/// Path to the verifier setup binary file
pub verifier_setup: String,

/// Level of postprocessing allowed. Default is [`PostprocessingLevel::Cheap`].
pub postprocessing_level: PostprocessingLevel,
}

impl SxTClient {
Expand All @@ -51,9 +71,16 @@ impl SxTClient {
substrate_node_url,
sxt_api_key,
verifier_setup,
postprocessing_level: PostprocessingLevel::Cheap,
}
}

/// Set the level of postprocessing allowed
pub fn with_postprocessing(&mut self, postprocessing_level: PostprocessingLevel) -> &mut Self {
self.postprocessing_level = postprocessing_level;
self
}

/// Query and verify a SQL query
///
/// Run a SQL query and verify the result using Dynamic Dory.
Expand Down Expand Up @@ -119,13 +146,18 @@ impl SxTClient {
.table;
// Apply postprocessing steps
let postprocessing = query_expr.postprocessing();
if postprocessing.is_empty() {
Ok(owned_table_result)
} else {
println!("Postprocessing required");
let transformed_result: OwnedTable<DoryScalar> =
apply_postprocessing_steps(owned_table_result, postprocessing).unwrap();
Ok(transformed_result)
let is_postprocessing_expensive = postprocessing.iter().any(|step| match step {
OwnedTablePostprocessing::Slice(_) | OwnedTablePostprocessing::GroupBy(_) => true,
_ => false,
});
match (self.postprocessing_level, postprocessing.len(), is_postprocessing_expensive) {
(_, 0, false) => Ok(owned_table_result),
(PostprocessingLevel::All, _, _) | (PostprocessingLevel::Cheap, _, false) => {
let transformed_result: OwnedTable<DoryScalar> =
apply_postprocessing_steps(owned_table_result, postprocessing)?;
Ok(transformed_result)
}
_ => Err("Required postprocessing is not allowed. Please examine your query or change `PostprocessingLevel` using `client.with_postprocessing`".into()),
}
}
}
2 changes: 1 addition & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub use args::SdkArgs;
mod auth;
pub use auth::get_access_token;
mod client;
pub use client::SxTClient;
pub use client::{PostprocessingLevel, SxTClient};
mod substrate;
pub use substrate::query_commitments;
mod sxt_chain_runtime;

0 comments on commit c27df89

Please sign in to comment.