diff --git a/sdk/src/args.rs b/sdk/src/args.rs index be02423..1dac3d4 100644 --- a/sdk/src/args.rs +++ b/sdk/src/args.rs @@ -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. @@ -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 { @@ -90,5 +100,6 @@ impl From<&SdkArgs> for SxTClient { args.sxt_api_key.clone(), args.verifier_setup.clone(), ) + .with_postprocessing(args.postprocessing_level) } } diff --git a/sdk/src/client.rs b/sdk/src/client.rs index d29d626..3a8ef09 100644 --- a/sdk/src/client.rs +++ b/sdk/src/client.rs @@ -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; @@ -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 { @@ -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 { @@ -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. @@ -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 = - 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 = + 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()), } } } diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index e06f226..ffa2e7a 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -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;