Skip to content

Commit

Permalink
Implement SQL model reader
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Nov 1, 2023
1 parent b3bb279 commit 289f83e
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ serde = { version = "1.0.156", features = [ "derive" ] }
serde_json = "1.0"
serde_with = "2.3.1"
smol_str = { version = "0.2.0", features = [ "serde" ] }
sqlx = { version = "0.6.2", features = [ "chrono", "macros", "offline", "runtime-actix-rustls", "sqlite", "uuid" ] }
starknet = "0.6.0"
starknet-crypto = "0.6.0"
starknet_api = { git = "https://github.com/starkware-libs/starknet-api", rev = "ecc9b6946ef13003da202838e4124a9ad2efabb0" }
Expand Down
6 changes: 3 additions & 3 deletions crates/torii/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version.workspace = true
[dependencies]
anyhow.workspace = true
async-trait.workspace = true
base64.workspace = true
chrono.workspace = true
dojo-types = { path = "../../dojo-types" }
dojo-world = { path = "../../dojo-world", features = [ "contracts", "manifest" ] }
Expand All @@ -20,20 +21,19 @@ hex.workspace = true
lazy_static.workspace = true
log = "0.4.17"
once_cell.workspace = true
reqwest = { version = "0.11.22", features = [ "blocking" ]}
reqwest = { version = "0.11.22", features = [ "blocking" ] }
scarb-ui.workspace = true
serde.workspace = true
serde_json.workspace = true
slab = "0.4.2"
sqlx = { version = "0.6.2", features = [ "chrono", "macros", "offline", "runtime-actix-rustls", "sqlite", "uuid" ] }
sqlx.workspace = true
starknet-crypto.workspace = true
starknet.workspace = true
thiserror.workspace = true
tokio = { version = "1.32.0", features = [ "sync" ], default-features = true }
tokio-stream = "0.1.11"
tokio-util = "0.7.7"
tracing.workspace = true
base64.workspace = true

[dev-dependencies]
camino.workspace = true
Expand Down
18 changes: 18 additions & 0 deletions crates/torii/core/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use starknet::core::types::FromStrError;
use starknet::core::utils::CairoShortStringToFeltError;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("parsing error: {0}")]
Parse(#[from] ParseError),
#[error(transparent)]
Sql(#[from] sqlx::Error),
}

#[derive(Debug, thiserror::Error)]
pub enum ParseError {
#[error(transparent)]
FromStr(#[from] FromStrError),
#[error(transparent)]
CairoShortStringToFelt(#[from] CairoShortStringToFeltError),
}
2 changes: 2 additions & 0 deletions crates/torii/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use sqlx::FromRow;
use crate::types::SQLFieldElement;

pub mod engine;
pub mod error;
pub mod model;
pub mod processors;
pub mod simple_broker;
pub mod sql;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,80 @@
use async_trait::async_trait;
use dojo_types::schema::{Enum, EnumOption, Member, Struct, Ty};
use dojo_world::contracts::model::ModelReader;
use sqlx::{Pool, Sqlite};
use starknet::core::types::FieldElement;

use super::error::{self, Error};

pub struct ModelSQLReader {
/// The name of the model
name: String,
/// The class hash of the model
class_hash: FieldElement,
pool: Pool<Sqlite>,
packed_size: FieldElement,
unpacked_size: FieldElement,
layout: Vec<FieldElement>,
}

impl ModelSQLReader {
pub async fn new(name: &str, pool: Pool<Sqlite>) -> Result<Self, Error> {
let (name, class_hash, packed_size, unpacked_size, layout): (
String,
String,
u32,
u32,
String,
) = sqlx::query_as(
"SELECT name, class_hash, packed_size, unpacked_size, layout FROM models WHERE id = ?",
)
.bind(name)
.fetch_one(&pool)
.await?;

let class_hash =
FieldElement::from_hex_be(&class_hash).map_err(error::ParseError::FromStr)?;
let packed_size = FieldElement::from(packed_size);
let unpacked_size = FieldElement::from(unpacked_size);

let layout = hex::decode(layout).unwrap();
let layout = layout.iter().map(|e| FieldElement::from(*e)).collect();

Ok(Self { name: name.clone(), class_hash, pool, packed_size, unpacked_size, layout })
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl ModelReader<Error> for ModelSQLReader {
fn class_hash(&self) -> FieldElement {
self.class_hash
}

async fn schema(&self) -> Result<Ty, Error> {
let model_members: Vec<SqlModelMember> = sqlx::query_as(
"SELECT id, model_idx, member_idx, name, type, type_enum, enum_options, key FROM \
model_members WHERE model_id = ? ORDER BY model_idx ASC, member_idx ASC",
)
.bind(self.name.clone())
.fetch_all(&self.pool)
.await?;

Ok(parse_sql_model_members(&self.name, &model_members))
}

async fn packed_size(&self) -> Result<FieldElement, Error> {
Ok(self.packed_size)
}

async fn unpacked_size(&self) -> Result<FieldElement, Error> {
Ok(self.unpacked_size)
}

async fn layout(&self) -> Result<Vec<FieldElement>, Error> {
Ok(self.layout.clone())
}
}

#[allow(unused)]
#[derive(Debug, sqlx::FromRow)]
Expand Down Expand Up @@ -73,7 +149,7 @@ mod tests {
use dojo_types::schema::{Enum, EnumOption, Member, Struct, Ty};

use super::SqlModelMember;
use crate::server::utils::parse_sql_model_members;
use crate::model::parse_sql_model_members;

#[test]
fn parse_simple_model_members_to_ty() {
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ lazy_static.workspace = true
scarb-ui.workspace = true
serde.workspace = true
serde_json.workspace = true
sqlx = { version = "0.6.2", features = [ "chrono", "macros", "offline", "runtime-actix-rustls", "sqlite", "uuid" ] }
sqlx.workspace = true
strum.workspace = true
strum_macros.workspace = true
thiserror.workspace = true
Expand Down
5 changes: 3 additions & 2 deletions crates/torii/grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version.workspace = true
[dependencies]
bytes = "1.0"
dojo-types = { path = "../../dojo-types" }
torii-core = { path = "../core" }
futures-util = "0.3.28"
futures.workspace = true
parking_lot.workspace = true
Expand All @@ -22,10 +23,10 @@ strum_macros.workspace = true
# server
hex.workspace = true
hyper = "0.14.27"
rand = "0.8.5"
serde_json.workspace = true
tower = "0.4.13"
tracing.workspace = true
rand = "0.8.5"

[target.'cfg(target_arch = "wasm32")'.dependencies]
tonic-web-wasm-client.workspace = true
Expand All @@ -34,7 +35,7 @@ wasm-tonic.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
prost.workspace = true
sqlx = { version = "0.6.2", features = [ "chrono", "macros", "offline", "runtime-actix-rustls", "sqlite", "uuid" ] }
sqlx.workspace = true
tokio-stream = "0.1.14"
tokio.workspace = true
tonic.workspace = true
Expand Down
21 changes: 2 additions & 19 deletions crates/torii/grpc/src/server/error.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
use starknet::core::types::FromStrError;
use starknet::core::utils::CairoShortStringToFeltError;
use starknet::providers::{Provider, ProviderError};

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("parsing error: {0}")]
Parse(#[from] ParseError),
#[error(transparent)]
Sql(#[from] sqlx::Error),
}

#[derive(Debug, thiserror::Error)]
pub enum ParseError {
#[error(transparent)]
FromStr(#[from] FromStrError),
#[error(transparent)]
CairoShortStringToFelt(#[from] CairoShortStringToFeltError),
}
use torii_core::error::ParseError;

#[derive(Debug, thiserror::Error)]
pub enum SubscriptionError<P: Provider> {
#[error(transparent)]
Parse(#[from] super::error::ParseError),
Parse(#[from] ParseError),
#[error(transparent)]
Provider(ProviderError<<P as Provider>::Error>),
}
5 changes: 2 additions & 3 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod error;
pub mod logger;
pub mod subscription;
pub mod utils;

use std::pin::Pin;
use std::str::FromStr;
Expand All @@ -20,10 +19,10 @@ use starknet_crypto::FieldElement;
use tokio::sync::mpsc::Receiver;
use tokio_stream::wrappers::ReceiverStream;
use tonic::{Request, Response, Status};
use torii_core::error::{Error, ParseError};
use torii_core::model::{parse_sql_model_members, SqlModelMember};

use self::error::{Error, ParseError};
use self::subscription::SubscribeRequest;
use self::utils::{parse_sql_model_members, SqlModelMember};
use crate::protos::{self};

#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ indexmap = "1.9.3"
scarb.workspace = true
serde.workspace = true
serde_json.workspace = true
sqlx = { version = "0.6.2", features = [ "chrono", "macros", "offline", "runtime-actix-rustls", "uuid" ] }
sqlx.workspace = true
starknet-crypto.workspace = true
starknet.workspace = true
tokio-stream = "0.1.11"
Expand Down

0 comments on commit 289f83e

Please sign in to comment.