From b4c9785e50e4818b3387f9afe273242db150dc6d Mon Sep 17 00:00:00 2001 From: Ian Joiner <14581281+iajoiner@users.noreply.github.com> Date: Mon, 30 Sep 2024 13:56:48 -0400 Subject: [PATCH] feat: switch to substrate --- Cargo.toml | 3 +++ src/main.rs | 28 +++++++++----------- src/substrate.rs | 56 ++++++++++++++++++++++++++++++++++++++++ src/sxt_chain_runtime.rs | 1 - 4 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 src/substrate.rs diff --git a/Cargo.toml b/Cargo.toml index 9acf53c..70c1292 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,11 @@ ark-std = { version = "0.4.0", features = [ "parallel" ] } curve25519-dalek = { version = "4", features = ["rand_core"] } flexbuffers = { version = "2.0.0" } proof-of-sql = { version = "0.12.3", default-features = false, features = ["blitzar", "test"] } +proof-of-sql-parser = { version = "0.12.3" } prost = "0.12" serde = { version = "1.0", features = ["serde_derive"] } +subxt = "0.37.0" +subxt-signer = "0.37.0" tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] } tonic = { version = "0.11", features = ["tls", "tls-roots"] } diff --git a/src/main.rs b/src/main.rs index 67a19d8..af4c7cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod substrate; mod sxt_chain_runtime; use ark_std::test_rng; @@ -6,7 +7,7 @@ use curve25519_dalek::RistrettoPoint; use flexbuffers; use proof_of_sql::{ base::{ - commitment::InnerProductProof, + commitment::{InnerProductProof, QueryCommitments}, database::{owned_table_utility::*, OwnedTableTestAccessor, TestAccessor}, }, proof_primitive::dory::{ @@ -15,7 +16,7 @@ use proof_of_sql::{ }, sql::{parse::QueryExpr, proof::VerifiableQueryResult}, }; -use prover::{prover_client::ProverClient, ProverQuery, ProverContextRange}; +use prover::{prover_client::ProverClient, ProverContextRange, ProverQuery}; use std::collections::HashMap; mod prover { @@ -33,19 +34,14 @@ async fn main() -> Result<(), Box> { // let dory_prover_setup = DoryProverPublicSetup::new(&prover_setup, 3); // let dory_verifier_setup = DoryVerifierPublicSetup::new(&verifier_setup, 3); // Accessor setup - let mut accessor = OwnedTableTestAccessor::::new_empty_with_setup(()); - let table = owned_table([ - smallint("smallint_minmax", [i16::MIN, -1, 0, 1, i16::MAX]), - int("int_minmax", [i32::MIN, -1, 0, 1, i32::MAX]), - bigint("bigint_minmax", [i64::MIN, -1, 0, 1, i64::MAX]), - boolean("boolean_minmax", [false, true, false, true, false]), - bigint("proof_order", [0_i64, 1, 2, 3, 4]), - ]); - accessor.add_table( - "proofs_smoke_tests.kyucqklhqcmlvuyc".parse()?, - table.clone(), - 0, - ); + let accessor = substrate::query_commitments( + &[ + "proofs_smoke_tests".parse()?, + "proofs_smoke_tests.kyucqklhqcmlvuyc".parse()?, + ], + "", + "dory".parse()?, + )?; // Parse the SQL query let query: QueryExpr = QueryExpr::try_new(sql.parse()?, "proofs_smoke_tests".parse()?, &accessor)?; @@ -58,7 +54,7 @@ async fn main() -> Result<(), Box> { ProverContextRange { start: 0, ends: vec![5], - } + }, ); let prover_query = ProverQuery { proof_plan: serialized_proof_plan, diff --git a/src/substrate.rs b/src/substrate.rs new file mode 100644 index 0000000..c248c00 --- /dev/null +++ b/src/substrate.rs @@ -0,0 +1,56 @@ +use proof_of_sql::base::{commitment::{QueryCommitments, TableCommitment}, database::TableRef}; +use proof_of_sql_parser::{Identifier, ResourceId}; +use proof_of_sql_commitment_map::CommitmentScheme; +use subxt::{ + client::OfflineClientT, + utils::{AccountId32, MultiAddress}, + OnlineClient, PolkadotConfig, +}; +use subxt_signer::sr25519::dev::{self}; +use crate::sxt_chain_runtime::api::runtime_types::sxt_core::{ByteString, tables::TableIdentifier}; + +/// Derive the runtime from the metadata +#[subxt::subxt(runtime_metadata_path = "sxt.scale")] +pub mod sxt_runtime {} +/// Use the standard PolkadotConfig +type SxtConfig = PolkadotConfig; + +/// Convert PoSQL `Identifier` to SxT Core `ByteString` +fn identifier_to_byte_string(identifier: &Identifier) -> ByteString { + let byte_string = ByteString::new(); + let name = identifier.as_str().as_bytes(); + // Unwrapping is allowed since both PoSQL and SxT Core identifiers have the same length restrictions + ByteString.try_extend(name).unwrap() +} + +/// Convert PoSQL resource IDs to SxT Core table identifiers +fn resource_id_to_table_id(resource_id: &ResourceId) -> TableIdentifier { + TableIdentifier { + name: identifier_to_byte_string(resource_id.object_name()), + namespace: identifier_to_byte_string(resource_id.schema()), + } +} + +/// Query the commitments pallet to find which commitments +pub fn query_commitments( + resource_ids: &[ResourceId], + url: &str, + commitment_scheme: CommitmentScheme, +) -> Result> { + let api = OnlineClient::::from_url(url).await?; + let mut accessor = QueryCommitments::new(); + resource_ids.iter().map(|id| -> Result<(TableRef, TableCommitment), Box>{ + let table_id = resource_id_to_table_id(id); + let commitments_query = sxt_runtime::storage() + .commitments() + .commitments(table_id, commitment_scheme); + let table_commitments: TableCommitment = api + .storage() + .at_latest() + .await? + .fetch(&commitments_query) + .await? + .unwrap(); + (TableRef::new(id), commitments) + }).collect::>>() +} diff --git a/src/sxt_chain_runtime.rs b/src/sxt_chain_runtime.rs index 306ebc3..85df253 100644 --- a/src/sxt_chain_runtime.rs +++ b/src/sxt_chain_runtime.rs @@ -11031,4 +11031,3 @@ pub mod api { } } } -