Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(torii): index model packing metadata #970

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions crates/torii/client/src/contract/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ use starknet_crypto::poseidon_hash_many;

use crate::contract::world::{ContractReaderError, WorldContractReader};

const WORLD_MODEL_SELECTOR_STR: &str = "model";
const SCHEMA_SELECTOR_STR: &str = "schema";
const LAYOUT_SELECTOR_STR: &str = "layout";
const PACKED_SIZE_SELECTOR_STR: &str = "packed_size";
const UNPACKED_SIZE_SELECTOR_STR: &str = "unpacked_size";

#[cfg(test)]
#[path = "model_test.rs"]
mod model_test;
Expand Down Expand Up @@ -56,7 +62,7 @@ impl<'a, P: Provider + Sync> ModelReader<'a, P> {
FunctionCall {
contract_address: world.address,
calldata: vec![name],
entry_point_selector: get_selector_from_name("model").unwrap(),
entry_point_selector: get_selector_from_name(WORLD_MODEL_SELECTOR_STR).unwrap(),
},
block_id,
)
Expand All @@ -71,7 +77,7 @@ impl<'a, P: Provider + Sync> ModelReader<'a, P> {
}

pub async fn schema(&self, block_id: BlockId) -> Result<Ty, ModelError<P::Error>> {
let entrypoint = get_selector_from_name("schema").unwrap();
let entrypoint = get_selector_from_name(SCHEMA_SELECTOR_STR).unwrap();

let res = self
.world
Expand All @@ -86,7 +92,7 @@ impl<'a, P: Provider + Sync> ModelReader<'a, P> {
&self,
block_id: BlockId,
) -> Result<FieldElement, ModelError<P::Error>> {
let entrypoint = get_selector_from_name("packed_size").unwrap();
let entrypoint = get_selector_from_name(PACKED_SIZE_SELECTOR_STR).unwrap();

let res = self
.world
Expand All @@ -97,8 +103,11 @@ impl<'a, P: Provider + Sync> ModelReader<'a, P> {
Ok(res[1])
}

pub async fn size(&self, block_id: BlockId) -> Result<FieldElement, ModelError<P::Error>> {
let entrypoint = get_selector_from_name("size").unwrap();
pub async fn unpacked_size(
&self,
block_id: BlockId,
) -> Result<FieldElement, ModelError<P::Error>> {
let entrypoint = get_selector_from_name(UNPACKED_SIZE_SELECTOR_STR).unwrap();

let res = self
.world
Expand All @@ -113,7 +122,7 @@ impl<'a, P: Provider + Sync> ModelReader<'a, P> {
&self,
block_id: BlockId,
) -> Result<Vec<FieldElement>, ModelError<P::Error>> {
let entrypoint = get_selector_from_name("layout").unwrap();
let entrypoint = get_selector_from_name(LAYOUT_SELECTOR_STR).unwrap();

let res = self
.world
Expand Down
10 changes: 9 additions & 1 deletion crates/torii/core/src/processors/register_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ impl<P: Provider + Sync + 'static> EventProcessor<P> for RegisterModelProcessor
event: &Event,
) -> Result<(), Error> {
let name = parse_cairo_short_string(&event.data[0])?;

// TODO: remove BlockId as argument
let model = world.model(&name, BlockId::Tag(BlockTag::Latest)).await?;
let schema = model.schema(BlockId::Tag(BlockTag::Latest)).await?;
let layout = model.layout(BlockId::Tag(BlockTag::Latest)).await?;

let unpacked_size: u8 =
model.unpacked_size(BlockId::Tag(BlockTag::Latest)).await?.try_into()?;
let packed_size: u8 =
model.packed_size(BlockId::Tag(BlockTag::Latest)).await?.try_into()?;

info!("Registered model: {}", name);

db.register_model(schema, layout, event.data[1]).await?;
db.register_model(schema, layout, event.data[1], packed_size, unpacked_size).await?;

Ok(())
}
Expand Down
15 changes: 8 additions & 7 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,17 @@ impl Sql {
model: Ty,
layout: Vec<FieldElement>,
class_hash: FieldElement,
packed_size: u8,
unpacked_size: u8,
) -> Result<()> {
let layout_blob = layout.iter().map(|x| (*x).try_into().unwrap()).collect::<Vec<u8>>();
self.query_queue.push(format!(
"INSERT INTO models (id, name, class_hash, layout) VALUES ('{}', '{}', '{:#x}', '{}') \
ON CONFLICT(id) DO UPDATE SET class_hash='{:#x}'",
model.name(),
model.name(),
class_hash,
hex::encode(&layout_blob),
class_hash
"INSERT INTO models (id, name, class_hash, layout, packed_size, unpacked_size) VALUES \
('{id}', '{name}', '{class_hash:#x}', '{layout}', '{packed_size}', \
'{unpacked_size}') ON CONFLICT(id) DO UPDATE SET class_hash='{class_hash:#x}'",
id = model.name(),
name = model.name(),
layout = hex::encode(&layout_blob)
));

let mut model_idx = 0_usize;
Expand Down
26 changes: 16 additions & 10 deletions crates/torii/core/src/sql_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,29 @@ async fn test_load_from_remote() {
let models = sqlx::query("SELECT * FROM models").fetch_all(&pool).await.unwrap();
assert_eq!(models.len(), 2);

let (id, name): (String, String) =
sqlx::query_as("SELECT id, name FROM models WHERE id = 'Position'")
.fetch_one(&pool)
.await
.unwrap();
let (id, name, packed_size, unpacked_size): (String, String, u8, u8) = sqlx::query_as(
"SELECT id, name, packed_size, unpacked_size FROM models WHERE id = 'Position'",
)
.fetch_one(&pool)
.await
.unwrap();

assert_eq!(id, "Position");
assert_eq!(name, "Position");
assert_eq!(packed_size, 1);
assert_eq!(unpacked_size, 2);

let (id, name): (String, String) =
sqlx::query_as("SELECT id, name FROM models WHERE id = 'Moves'")
.fetch_one(&pool)
.await
.unwrap();
let (id, name, packed_size, unpacked_size): (String, String, u8, u8) = sqlx::query_as(
"SELECT id, name, packed_size, unpacked_size FROM models WHERE id = 'Moves'",
)
.fetch_one(&pool)
.await
.unwrap();

assert_eq!(id, "Moves");
assert_eq!(name, "Moves");
assert_eq!(packed_size, 1);
assert_eq!(unpacked_size, 2);

db.store_event(
&Event {
Expand Down
4 changes: 4 additions & 0 deletions crates/torii/graphql/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub async fn entity_fixtures(db: &mut Sql) {
}),
vec![],
FieldElement::ONE,
0,
0,
)
.await
.unwrap();
Expand Down Expand Up @@ -145,6 +147,8 @@ pub async fn entity_fixtures(db: &mut Sql) {
}),
vec![],
FieldElement::TWO,
0,
0,
)
.await
.unwrap();
Expand Down
6 changes: 4 additions & 2 deletions crates/torii/migrations/20230316154230_setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ CREATE TABLE worlds (
CREATE TABLE models (
id TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
class_hash TEXT NOT NULL,
transaction_hash TEXT,
layout BLOB NOT NULL,
transaction_hash TEXT,
class_hash TEXT NOT NULL,
packed_size INTEGER NOT NULL,
unpacked_size INTEGER NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Expand Down
Loading