diff --git a/crates/torii/client/src/contract/model.rs b/crates/torii/client/src/contract/model.rs index 8ed5d426a3..0f47118280 100644 --- a/crates/torii/client/src/contract/model.rs +++ b/crates/torii/client/src/contract/model.rs @@ -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; @@ -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, ) @@ -71,7 +77,7 @@ impl<'a, P: Provider + Sync> ModelReader<'a, P> { } pub async fn schema(&self, block_id: BlockId) -> Result> { - let entrypoint = get_selector_from_name("schema").unwrap(); + let entrypoint = get_selector_from_name(SCHEMA_SELECTOR_STR).unwrap(); let res = self .world @@ -86,7 +92,7 @@ impl<'a, P: Provider + Sync> ModelReader<'a, P> { &self, block_id: BlockId, ) -> Result> { - let entrypoint = get_selector_from_name("packed_size").unwrap(); + let entrypoint = get_selector_from_name(PACKED_SIZE_SELECTOR_STR).unwrap(); let res = self .world @@ -97,8 +103,11 @@ impl<'a, P: Provider + Sync> ModelReader<'a, P> { Ok(res[1]) } - pub async fn size(&self, block_id: BlockId) -> Result> { - let entrypoint = get_selector_from_name("size").unwrap(); + pub async fn unpacked_size( + &self, + block_id: BlockId, + ) -> Result> { + let entrypoint = get_selector_from_name(UNPACKED_SIZE_SELECTOR_STR).unwrap(); let res = self .world @@ -113,7 +122,7 @@ impl<'a, P: Provider + Sync> ModelReader<'a, P> { &self, block_id: BlockId, ) -> Result, ModelError> { - let entrypoint = get_selector_from_name("layout").unwrap(); + let entrypoint = get_selector_from_name(LAYOUT_SELECTOR_STR).unwrap(); let res = self .world diff --git a/crates/torii/core/src/processors/register_model.rs b/crates/torii/core/src/processors/register_model.rs index 196e9aafed..b9aca39fa9 100644 --- a/crates/torii/core/src/processors/register_model.rs +++ b/crates/torii/core/src/processors/register_model.rs @@ -28,12 +28,20 @@ impl EventProcessor

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(()) } diff --git a/crates/torii/core/src/sql.rs b/crates/torii/core/src/sql.rs index 34bca747a3..9ad17c2b93 100644 --- a/crates/torii/core/src/sql.rs +++ b/crates/torii/core/src/sql.rs @@ -81,16 +81,17 @@ impl Sql { model: Ty, layout: Vec, class_hash: FieldElement, + packed_size: u8, + unpacked_size: u8, ) -> Result<()> { let layout_blob = layout.iter().map(|x| (*x).try_into().unwrap()).collect::>(); 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; diff --git a/crates/torii/core/src/sql_test.rs b/crates/torii/core/src/sql_test.rs index 7ba279366e..9b349213fc 100644 --- a/crates/torii/core/src/sql_test.rs +++ b/crates/torii/core/src/sql_test.rs @@ -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 { diff --git a/crates/torii/graphql/src/tests/mod.rs b/crates/torii/graphql/src/tests/mod.rs index 7eb3c1fd7a..05d1b07cbc 100644 --- a/crates/torii/graphql/src/tests/mod.rs +++ b/crates/torii/graphql/src/tests/mod.rs @@ -109,6 +109,8 @@ pub async fn entity_fixtures(db: &mut Sql) { }), vec![], FieldElement::ONE, + 0, + 0, ) .await .unwrap(); @@ -145,6 +147,8 @@ pub async fn entity_fixtures(db: &mut Sql) { }), vec![], FieldElement::TWO, + 0, + 0, ) .await .unwrap(); diff --git a/crates/torii/migrations/20230316154230_setup.sql b/crates/torii/migrations/20230316154230_setup.sql index 729724dfe3..f59199c315 100644 --- a/crates/torii/migrations/20230316154230_setup.sql +++ b/crates/torii/migrations/20230316154230_setup.sql @@ -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 );